mesa: replace FREXPF, LDEXPF with frexpf, ldexpf
[mesa.git] / include / c99_math.h
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 #if defined(_MSC_VER)
42
43 #if _MSC_VER < 1400 && !defined(__cplusplus)
44
45 static inline float cosf( float f )
46 {
47 return (float) cos( (double) f );
48 }
49
50 static inline float sinf( float f )
51 {
52 return (float) sin( (double) f );
53 }
54
55 static inline float ceilf( float f )
56 {
57 return (float) ceil( (double) f );
58 }
59
60 static inline float floorf( float f )
61 {
62 return (float) floor( (double) f );
63 }
64
65 static inline float powf( float f, float g )
66 {
67 return (float) pow( (double) f, (double) g );
68 }
69
70 static inline float sqrtf( float f )
71 {
72 return (float) sqrt( (double) f );
73 }
74
75 static inline float fabsf( float f )
76 {
77 return (float) fabs( (double) f );
78 }
79
80 static inline float logf( float f )
81 {
82 return (float) log( (double) f );
83 }
84
85 static inline float frexpf(float x, int *exp)
86 {
87 return (float) frexp(x, exp);
88 }
89
90 static inline float ldexpf(float x, int exp)
91 {
92 return (float) ldexp(x, exp);
93 }
94
95
96 #else
97 /* Work-around an extra semi-colon in VS 2005 logf definition */
98 #ifdef logf
99 #undef logf
100 #define logf(x) ((float)log((double)(x)))
101 #endif /* logf */
102
103 #if _MSC_VER < 1800
104 #define isfinite(x) _finite((double)(x))
105 #define isnan(x) _isnan((double)(x))
106 #endif /* _MSC_VER < 1800 */
107 #endif /* _MSC_VER < 1400 && !defined(__cplusplus) */
108
109 #if _MSC_VER < 1800
110 static inline double log2( double x )
111 {
112 const double invln2 = 1.442695041;
113 return log( x ) * invln2;
114 }
115
116 static inline double
117 round(double x)
118 {
119 return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
120 }
121
122 static inline float
123 roundf(float x)
124 {
125 return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
126 }
127 #endif
128
129 #ifndef INFINITY
130 #define INFINITY (DBL_MAX + DBL_MAX)
131 #endif
132
133 #ifndef NAN
134 #define NAN (INFINITY - INFINITY)
135 #endif
136
137 #endif /* _MSC_VER */
138
139
140 #if __STDC_VERSION__ < 199901L && (!defined(__cplusplus) || defined(_MSC_VER))
141 static inline long int
142 lrint(double d)
143 {
144 long int rounded = (long int)(d + 0.5);
145
146 if (d - floor(d) == 0.5) {
147 if (rounded % 2 != 0)
148 rounded += (d > 0) ? -1 : 1;
149 }
150
151 return rounded;
152 }
153
154 static inline long int
155 lrintf(float f)
156 {
157 long int rounded = (long int)(f + 0.5f);
158
159 if (f - floorf(f) == 0.5f) {
160 if (rounded % 2 != 0)
161 rounded += (f > 0) ? -1 : 1;
162 }
163
164 return rounded;
165 }
166
167 static inline long long int
168 llrint(double d)
169 {
170 long long int rounded = (long long int)(d + 0.5);
171
172 if (d - floor(d) == 0.5) {
173 if (rounded % 2 != 0)
174 rounded += (d > 0) ? -1 : 1;
175 }
176
177 return rounded;
178 }
179
180 static inline long long int
181 llrintf(float f)
182 {
183 long long int rounded = (long long int)(f + 0.5f);
184
185 if (f - floorf(f) == 0.5f) {
186 if (rounded % 2 != 0)
187 rounded += (f > 0) ? -1 : 1;
188 }
189
190 return rounded;
191 }
192 #endif /* C99 */
193
194
195 #endif /* #define _C99_MATH_H_ */