include,auxiliary: Remove support for MSVC older then 2008.
[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 < 1800
44 #define isfinite(x) _finite((double)(x))
45 #define isnan(x) _isnan((double)(x))
46 #endif /* _MSC_VER < 1800 */
47
48 #if _MSC_VER < 1800
49 static inline double log2( double x )
50 {
51 const double invln2 = 1.442695041;
52 return log( x ) * invln2;
53 }
54
55 static inline double
56 round(double x)
57 {
58 return x >= 0.0 ? floor(x + 0.5) : ceil(x - 0.5);
59 }
60
61 static inline float
62 roundf(float x)
63 {
64 return x >= 0.0f ? floorf(x + 0.5f) : ceilf(x - 0.5f);
65 }
66 #endif
67
68 #ifndef INFINITY
69 #define INFINITY (DBL_MAX + DBL_MAX)
70 #endif
71
72 #ifndef NAN
73 #define NAN (INFINITY - INFINITY)
74 #endif
75
76 #endif /* _MSC_VER */
77
78
79 #if __STDC_VERSION__ < 199901L && (!defined(__cplusplus) || defined(_MSC_VER))
80 static inline long int
81 lrint(double d)
82 {
83 long int rounded = (long int)(d + 0.5);
84
85 if (d - floor(d) == 0.5) {
86 if (rounded % 2 != 0)
87 rounded += (d > 0) ? -1 : 1;
88 }
89
90 return rounded;
91 }
92
93 static inline long int
94 lrintf(float f)
95 {
96 long int rounded = (long int)(f + 0.5f);
97
98 if (f - floorf(f) == 0.5f) {
99 if (rounded % 2 != 0)
100 rounded += (f > 0) ? -1 : 1;
101 }
102
103 return rounded;
104 }
105
106 static inline long long int
107 llrint(double d)
108 {
109 long long int rounded = (long long int)(d + 0.5);
110
111 if (d - floor(d) == 0.5) {
112 if (rounded % 2 != 0)
113 rounded += (d > 0) ? -1 : 1;
114 }
115
116 return rounded;
117 }
118
119 static inline long long int
120 llrintf(float f)
121 {
122 long long int rounded = (long long int)(f + 0.5f);
123
124 if (f - floorf(f) == 0.5f) {
125 if (rounded % 2 != 0)
126 rounded += (f > 0) ? -1 : 1;
127 }
128
129 return rounded;
130 }
131 #endif /* C99 */
132
133
134 /*
135 * signbit() is a macro on Linux. Not available on Windows.
136 */
137 #ifndef signbit
138 #define signbit(x) ((x) < 0.0f)
139 #endif
140
141
142 #endif /* #define _C99_MATH_H_ */