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