1 /**************************************************************************
3 * Copyright 2007-2015 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 * Wrapper for math.h which makes sure we have definitions of all the c99
38 #include "c99_compat.h"
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
47 #if !defined(_MSC_VER) && \
48 __STDC_VERSION__ < 199901L && \
49 (!defined(_XOPEN_SOURCE) || _XOPEN_SOURCE < 600) && \
52 static inline long int
55 long int rounded
= (long int)(d
+ 0.5);
57 if (d
- floor(d
) == 0.5) {
59 rounded
+= (d
> 0) ? -1 : 1;
65 static inline long int
68 long int rounded
= (long int)(f
+ 0.5f
);
70 if (f
- floorf(f
) == 0.5f
) {
72 rounded
+= (f
> 0) ? -1 : 1;
78 static inline long long int
81 long long int rounded
= (long long int)(d
+ 0.5);
83 if (d
- floor(d
) == 0.5) {
85 rounded
+= (d
> 0) ? -1 : 1;
91 static inline long long int
94 long long int rounded
= (long long int)(f
+ 0.5f
);
96 if (f
- floorf(f
) == 0.5f
) {
98 rounded
+= (f
> 0) ? -1 : 1;
107 return powf(2.0f
, f
);
120 * signbit() is a macro on Linux. Not available on Windows.
123 #define signbit(x) ((x) < 0.0f)
128 #define M_PI (3.14159265358979323846)
132 #define M_E (2.7182818284590452354)
136 #define M_LOG2E (1.4426950408889634074)
140 #define FLT_MAX_EXP 128
144 #if defined(fpclassify)
145 /* ISO C99 says that fpclassify is a macro. Assume that any implementation
146 * of fpclassify, whether it's in a C99 compiler or not, will be a macro.
148 #elif defined(__cplusplus)
149 /* For C++, fpclassify() should be defined in <cmath> */
150 #elif defined(_MSC_VER)
151 /* Not required on VS2013 and above. Oddly, the fpclassify() function
152 * doesn't exist in such a form on MSVC. This is an implementation using
153 * slightly different lower-level Windows functions.
157 static inline enum {FP_NAN
, FP_INFINITE
, FP_ZERO
, FP_SUBNORMAL
, FP_NORMAL
}
160 switch(_fpclass(x
)) {
161 case _FPCLASS_SNAN
: /* signaling NaN */
162 case _FPCLASS_QNAN
: /* quiet NaN */
164 case _FPCLASS_NINF
: /* negative infinity */
165 case _FPCLASS_PINF
: /* positive infinity */
167 case _FPCLASS_NN
: /* negative normal */
168 case _FPCLASS_PN
: /* positive normal */
170 case _FPCLASS_ND
: /* negative denormalized */
171 case _FPCLASS_PD
: /* positive denormalized */
173 case _FPCLASS_NZ
: /* negative zero */
174 case _FPCLASS_PZ
: /* positive zero */
177 /* Should never get here; but if we do, this will guarantee
178 * that the pattern is not treated like a number.
184 #error "Need to include or define an fpclassify function"
188 #endif /* #define _C99_MATH_H_ */