1 /**************************************************************************
2 *
3 * Copyright 2007-2015 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * Wrapper for math.h which makes sure we have definitions of all the c99
30 * functions.
31 */
32
33
34 #ifndef _C99_MATH_H_
35 #define _C99_MATH_H_
36
37 #include <math.h>
38 #include "c99_compat.h"
39
40
41 /* This is to ensure that we get M_PI, etc. definitions */
42 #if defined(_MSC_VER) && !defined(_USE_MATH_DEFINES)
43 #error _USE_MATH_DEFINES define required when building with MSVC
44 #endif
45
46
47 #if !defined(_MSC_VER) && \
48 __STDC_VERSION__ < 199901L && \
49 (!defined(_XOPEN_SOURCE) || _XOPEN_SOURCE < 600) && \
50 !defined(__cplusplus)
51
52 static inline long int
lrint(double d)53 lrint(double d)
54 {
55 long int rounded = (long int)(d + 0.5);
56
57 if (d - floor(d) == 0.5) {
58 if (rounded % 2 != 0)
59 rounded += (d > 0) ? -1 : 1;
60 }
61
62 return rounded;
63 }
64
65 static inline long int
lrintf(float f)66 lrintf(float f)
67 {
68 long int rounded = (long int)(f + 0.5f);
69
70 if (f - floorf(f) == 0.5f) {
71 if (rounded % 2 != 0)
72 rounded += (f > 0) ? -1 : 1;
73 }
74
75 return rounded;
76 }
77
78 static inline long long int
llrint(double d)79 llrint(double d)
80 {
81 long long int rounded = (long long int)(d + 0.5);
82
83 if (d - floor(d) == 0.5) {
84 if (rounded % 2 != 0)
85 rounded += (d > 0) ? -1 : 1;
86 }
87
88 return rounded;
89 }
90
91 static inline long long int
llrintf(float f)92 llrintf(float f)
93 {
94 long long int rounded = (long long int)(f + 0.5f);
95
96 if (f - floorf(f) == 0.5f) {
97 if (rounded % 2 != 0)
98 rounded += (f > 0) ? -1 : 1;
99 }
100
101 return rounded;
102 }
103
104 static inline float
exp2f(float f)105 exp2f(float f)
106 {
107 return powf(2.0f, f);
108 }
109
110 static inline double
exp2(double d)111 exp2(double d)
112 {
113 return pow(2.0, d);
114 }
115
116 #endif /* C99 */
117
118
119 #ifndef M_PI
120 #define M_PI (3.14159265358979323846)
121 #endif
122
123 #ifndef M_E
124 #define M_E (2.7182818284590452354)
125 #endif
126
127 #ifndef M_LOG2E
128 #define M_LOG2E (1.4426950408889634074)
129 #endif
130
131 #ifndef FLT_MAX_EXP
132 #define FLT_MAX_EXP 128
133 #endif
134
135
136 #if defined(fpclassify)
137 /* ISO C99 says that fpclassify is a macro. Assume that any implementation
138 * of fpclassify, whether it's in a C99 compiler or not, will be a macro.
139 */
140 #elif defined(__cplusplus)
141 /* For C++, fpclassify() should be defined in <cmath> */
142 #elif defined(_MSC_VER)
143 /* Not required on VS2013 and above. Oddly, the fpclassify() function
144 * doesn't exist in such a form on MSVC. This is an implementation using
145 * slightly different lower-level Windows functions.
146 */
147 #include <float.h>
148
149 static inline enum {FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, FP_NORMAL}
fpclassify(double x)150 fpclassify(double x)
151 {
152 switch(_fpclass(x)) {
153 case _FPCLASS_SNAN: /* signaling NaN */
154 case _FPCLASS_QNAN: /* quiet NaN */
155 return FP_NAN;
156 case _FPCLASS_NINF: /* negative infinity */
157 case _FPCLASS_PINF: /* positive infinity */
158 return FP_INFINITE;
159 case _FPCLASS_NN: /* negative normal */
160 case _FPCLASS_PN: /* positive normal */
161 return FP_NORMAL;
162 case _FPCLASS_ND: /* negative denormalized */
163 case _FPCLASS_PD: /* positive denormalized */
164 return FP_SUBNORMAL;
165 case _FPCLASS_NZ: /* negative zero */
166 case _FPCLASS_PZ: /* positive zero */
167 return FP_ZERO;
168 default:
169 /* Should never get here; but if we do, this will guarantee
170 * that the pattern is not treated like a number.
171 */
172 return FP_NAN;
173 }
174 }
175 #else
176 #error "Need to include or define an fpclassify function"
177 #endif
178
179
180 /* Since C++11, the following functions are part of the std namespace. Their C
181 * counteparts should still exist in the global namespace, however cmath
182 * undefines those functions, which in glibc 2.23, are defined as macros rather
183 * than functions as in glibc 2.22.
184 */
185 #if __cplusplus >= 201103L && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 23))
186 #include <cmath>
187
188 using std::fpclassify;
189 using std::isfinite;
190 using std::isinf;
191 using std::isnan;
192 using std::isnormal;
193 using std::signbit;
194 using std::isgreater;
195 using std::isgreaterequal;
196 using std::isless;
197 using std::islessequal;
198 using std::islessgreater;
199 using std::isunordered;
200 #endif
201
202
203 #endif /* #define _C99_MATH_H_ */
204