mesa: replace LOGF, EXPF with logf, expf
[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 static inline float logf(float x)
96 {
97 return (float) log(x);
98 }
99
100 static inline float expf(float x)
101 {
102 return (float) exp(x);
103 }
104
105
106 #else
107 /* Work-around an extra semi-colon in VS 2005 logf definition */
108 #ifdef logf
109 #undef logf
110 #define logf(x) ((float)log((double)(x)))
111 #endif /* logf */
112
113 #if _MSC_VER < 1800
114 #define isfinite(x) _finite((double)(x))
115 #define isnan(x) _isnan((double)(x))
116 #endif /* _MSC_VER < 1800 */
117 #endif /* _MSC_VER < 1400 && !defined(__cplusplus) */
118
119 #if _MSC_VER < 1800
120 static inline double log2( double x )
121 {
122 const double invln2 = 1.442695041;
123 return log( x ) * invln2;
124 }
125
126 static inline double
127 round(double x)
128 {
129 return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
130 }
131
132 static inline float
133 roundf(float x)
134 {
135 return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
136 }
137 #endif
138
139 #ifndef INFINITY
140 #define INFINITY (DBL_MAX + DBL_MAX)
141 #endif
142
143 #ifndef NAN
144 #define NAN (INFINITY - INFINITY)
145 #endif
146
147 #endif /* _MSC_VER */
148
149
150 #if __STDC_VERSION__ < 199901L && (!defined(__cplusplus) || defined(_MSC_VER))
151 static inline long int
152 lrint(double d)
153 {
154 long int rounded = (long int)(d + 0.5);
155
156 if (d - floor(d) == 0.5) {
157 if (rounded % 2 != 0)
158 rounded += (d > 0) ? -1 : 1;
159 }
160
161 return rounded;
162 }
163
164 static inline long int
165 lrintf(float f)
166 {
167 long int rounded = (long int)(f + 0.5f);
168
169 if (f - floorf(f) == 0.5f) {
170 if (rounded % 2 != 0)
171 rounded += (f > 0) ? -1 : 1;
172 }
173
174 return rounded;
175 }
176
177 static inline long long int
178 llrint(double d)
179 {
180 long long int rounded = (long long int)(d + 0.5);
181
182 if (d - floor(d) == 0.5) {
183 if (rounded % 2 != 0)
184 rounded += (d > 0) ? -1 : 1;
185 }
186
187 return rounded;
188 }
189
190 static inline long long int
191 llrintf(float f)
192 {
193 long long int rounded = (long long int)(f + 0.5f);
194
195 if (f - floorf(f) == 0.5f) {
196 if (rounded % 2 != 0)
197 rounded += (f > 0) ? -1 : 1;
198 }
199
200 return rounded;
201 }
202 #endif /* C99 */
203
204
205 #endif /* #define _C99_MATH_H_ */