• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //    http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #ifndef _COMPAT_H_
17 #define _COMPAT_H_
18 
19 #if defined(_WIN32) && defined(_MSC_VER)
20 #include <Windows.h>
21 #endif
22 
23 #ifdef __cplusplus
24 #define EXTERN_C extern "C"
25 #else
26 #define EXTERN_C
27 #endif
28 
29 
30 //
31 // stdlib.h
32 //
33 
34 #include <stdlib.h> // On Windows, _MAX_PATH defined there.
35 
36 // llabs appeared in MS C v16 (VS 10/2010).
37 #if defined(_MSC_VER) && _MSC_VER <= 1500
llabs(long long __x)38 EXTERN_C inline long long llabs(long long __x) { return __x >= 0 ? __x : -__x; }
39 #endif
40 
41 
42 //
43 // stdbool.h
44 //
45 
46 // stdbool.h appeared in MS C v18 (VS 12/2013).
47 #if defined(_MSC_VER) && MSC_VER <= 1700
48 #if !defined(__cplusplus)
49 typedef char bool;
50 #define true 1
51 #define false 0
52 #endif
53 #else
54 #include <stdbool.h>
55 #endif // defined(_MSC_VER) && MSC_VER <= 1700
56 
57 
58 //
59 // stdint.h
60 //
61 
62 // stdint.h appeared in MS C v16 (VS 10/2010) and Intel C v12.
63 #if defined(_MSC_VER)                                                          \
64     && (!defined(__INTEL_COMPILER) && _MSC_VER <= 1500                         \
65         || defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1200)
66 typedef unsigned char uint8_t;
67 typedef char int8_t;
68 typedef unsigned short uint16_t;
69 typedef short int16_t;
70 typedef unsigned int uint32_t;
71 typedef int int32_t;
72 typedef unsigned long long uint64_t;
73 typedef long long int64_t;
74 #else
75 #ifndef __STDC_LIMIT_MACROS
76 #define __STDC_LIMIT_MACROS
77 #endif
78 #include <stdint.h>
79 #endif
80 
81 
82 //
83 // float.h
84 //
85 
86 #include <float.h>
87 
88 
89 //
90 // fenv.h
91 //
92 
93 // fenv.h appeared in MS C v18 (VS 12/2013).
94 #if defined(_MSC_VER) && _MSC_VER <= 1700 && !defined(__INTEL_COMPILER)
95 // reimplement fenv.h because windows doesn't have it
96 #define FE_INEXACT 0x0020
97 #define FE_UNDERFLOW 0x0010
98 #define FE_OVERFLOW 0x0008
99 #define FE_DIVBYZERO 0x0004
100 #define FE_INVALID 0x0001
101 #define FE_ALL_EXCEPT 0x003D
102 int fetestexcept(int excepts);
103 int feclearexcept(int excepts);
104 #else
105 #include <fenv.h>
106 #endif
107 
108 
109 //
110 // math.h
111 //
112 
113 #if defined(__INTEL_COMPILER)
114 #include <mathimf.h>
115 #else
116 #include <math.h>
117 #endif
118 
119 #ifndef M_PI
120 #define M_PI 3.14159265358979323846264338327950288
121 #endif
122 
123 #if defined(_MSC_VER)
124 
125 #ifdef __cplusplus
126 extern "C" {
127 #endif
128 
129 #ifndef NAN
130 #define NAN (INFINITY - INFINITY)
131 #endif
132 
133 #ifndef HUGE_VALF
134 #define HUGE_VALF (float)HUGE_VAL
135 #endif
136 
137 #ifndef INFINITY
138 #define INFINITY (FLT_MAX + FLT_MAX)
139 #endif
140 
141 #ifndef isfinite
142 #define isfinite(x) _finite(x)
143 #endif
144 
145 #ifndef isnan
146 #define isnan(x) ((x) != (x))
147 #endif
148 
149 #ifndef isinf
150 #define isinf(_x) ((_x) == INFINITY || (_x) == -INFINITY)
151 #endif
152 
153 #if _MSC_VER < 1900 && !defined(__INTEL_COMPILER)
154 
155 double rint(double x);
156 float rintf(float x);
157 long double rintl(long double x);
158 
159 float cbrtf(float);
160 double cbrt(double);
161 
162 int ilogb(double x);
163 int ilogbf(float x);
164 int ilogbl(long double x);
165 
166 double fmax(double x, double y);
167 double fmin(double x, double y);
168 float fmaxf(float x, float y);
169 float fminf(float x, float y);
170 
171 double log2(double x);
172 long double log2l(long double x);
173 
174 double exp2(double x);
175 long double exp2l(long double x);
176 
177 double fdim(double x, double y);
178 float fdimf(float x, float y);
179 long double fdiml(long double x, long double y);
180 
181 double remquo(double x, double y, int* quo);
182 float remquof(float x, float y, int* quo);
183 long double remquol(long double x, long double y, int* quo);
184 
185 long double scalblnl(long double x, long n);
186 
187 float hypotf(float x, float y);
188 long double hypotl(long double x, long double y);
189 double lgamma(double x);
190 float lgammaf(float x);
191 
192 double trunc(double x);
193 float truncf(float x);
194 
195 double log1p(double x);
196 float log1pf(float x);
197 long double log1pl(long double x);
198 
199 double copysign(double x, double y);
200 float copysignf(float x, float y);
201 long double copysignl(long double x, long double y);
202 
203 long lround(double x);
204 long lroundf(float x);
205 // long lroundl(long double x)
206 
207 double round(double x);
208 float roundf(float x);
209 long double roundl(long double x);
210 
211 int cf_signbit(double x);
212 int cf_signbitf(float x);
213 
214 // Added in _MSC_VER == 1800 (Visual Studio 2013)
215 #if _MSC_VER < 1800
signbit(double x)216 static int signbit(double x) { return cf_signbit(x); }
217 #endif
signbitf(float x)218 static int signbitf(float x) { return cf_signbitf(x); }
219 
220 long int lrint(double flt);
221 long int lrintf(float flt);
222 
223 float int2float(int32_t ix);
224 int32_t float2int(float fx);
225 
226 #endif // _MSC_VER < 1900 && ! defined( __INTEL_COMPILER )
227 
228 #if _MSC_VER < 1900 && (!defined(__INTEL_COMPILER) || __INTEL_COMPILER < 1300)
229 // These functions appeared in Intel C v13 and Visual Studio 2015
230 float nanf(const char* str);
231 double nan(const char* str);
232 long double nanl(const char* str);
233 #endif
234 
235 #ifdef __cplusplus
236 }
237 #endif
238 
239 #endif // defined(_MSC_VER)
240 
241 #if defined(__ANDROID__)
242 #define log2(X) (log(X) / log(2))
243 #endif
244 
245 
246 //
247 // stdio.h
248 //
249 
250 #if defined(_MSC_VER)
251 // snprintf added in _MSC_VER == 1900 (Visual Studio 2015)
252 #if _MSC_VER < 1900
253 #define snprintf sprintf_s
254 #endif
255 #endif // defined(_MSC_VER)
256 
257 
258 //
259 // string.h
260 //
261 
262 #if defined(_MSC_VER)
263 #define strtok_r strtok_s
264 #endif
265 
266 
267 //
268 // unistd.h
269 //
270 
271 #if defined(_MSC_VER)
272 EXTERN_C unsigned int sleep(unsigned int sec);
273 EXTERN_C int usleep(int usec);
274 #endif
275 
276 
277 //
278 // syscall.h
279 //
280 
281 #if defined(__ANDROID__)
282 // Android bionic's isn't providing SYS_sysctl wrappers.
283 #define SYS__sysctl __NR__sysctl
284 #endif
285 
286 
287 // Some tests use _malloca which defined in malloc.h.
288 #if !defined(__APPLE__)
289 #include <malloc.h>
290 #endif
291 
292 
293 //
294 // ???
295 //
296 
297 #if defined(_MSC_VER)
298 
299 #define MAXPATHLEN _MAX_PATH
300 
301 EXTERN_C uint64_t ReadTime(void);
302 EXTERN_C double SubtractTime(uint64_t endTime, uint64_t startTime);
303 
304 /** Returns the number of leading 0-bits in x,
305     starting at the most significant bit position.
306     If x is 0, the result is undefined.
307 */
308 EXTERN_C int __builtin_clz(unsigned int pattern);
309 
310 #endif
311 
312 #ifndef MIN
313 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
314 #endif
315 #ifndef MAX
316 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
317 #endif
318 
319 
320 /*-----------------------------------------------------------------------------
321    WARNING: DO NOT USE THESE MACROS:
322         MAKE_HEX_FLOAT, MAKE_HEX_DOUBLE, MAKE_HEX_LONG.
323 
324    This is a typical usage of the macros:
325 
326      double yhi = MAKE_HEX_DOUBLE(0x1.5555555555555p-2,0x15555555555555LL,-2);
327 
328    (taken from math_brute_force/reference_math.c). There are two problems:
329 
330      1. There is an error here. On Windows in will produce incorrect result
331         `0x1.5555555555555p+50'.
332         To have a correct result it should be written as:
333            MAKE_HEX_DOUBLE(0x1.5555555555555p-2, 0x15555555555555LL, -54)
334         A proper value of the third argument is not obvious -- sometimes it
335         should be the same as exponent of the first argument, but sometimes
336         not.
337 
338      2. Information is duplicated. It is easy to make a mistake.
339 
340    Use HEX_FLT, HEX_DBL, HEX_LDBL macros instead
341    (see them in the bottom of the file).
342 -----------------------------------------------------------------------------*/
343 #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
344 
345 #define MAKE_HEX_FLOAT(x, y, z) ((float)ldexp((float)(y), z))
346 #define MAKE_HEX_DOUBLE(x, y, z) ldexp((double)(y), z)
347 #define MAKE_HEX_LONG(x, y, z) ((long double)ldexp((long double)(y), z))
348 
349 #else
350 
351 // Do not use these macros in new code, use HEX_FLT, HEX_DBL, HEX_LDBL instead.
352 #define MAKE_HEX_FLOAT(x, y, z) x
353 #define MAKE_HEX_DOUBLE(x, y, z) x
354 #define MAKE_HEX_LONG(x, y, z) x
355 
356 #endif
357 
358 
359 /*-----------------------------------------------------------------------------
360    HEX_FLT, HEXT_DBL, HEX_LDBL -- Create hex floating point literal of type
361    float, double, long double respectively. Arguments:
362 
363       sm    -- sign of number,
364       int   -- integer part of mantissa (without `0x' prefix),
365       fract -- fractional part of mantissa (without decimal point and `L' or
366             `LL' suffixes),
367       se    -- sign of exponent,
368       exp   -- absolute value of (binary) exponent.
369 
370    Example:
371 
372       double yhi = HEX_DBL(+, 1, 5555555555555, -, 2); // 0x1.5555555555555p-2
373 
374    Note:
375 
376       We have to pass signs as separate arguments because gcc pass negative
377    integer values (e. g. `-2') into a macro as two separate tokens, so
378    `HEX_FLT(1, 0, -2)' produces result `0x1.0p- 2' (note a space between minus
379    and two) which is not a correct floating point literal.
380 -----------------------------------------------------------------------------*/
381 #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
382 // If compiler does not support hex floating point literals:
383 #define HEX_FLT(sm, int, fract, se, exp)                                       \
384     sm ldexpf((float)(0x##int##fract##UL),                                     \
385               se exp + ilogbf((float)0x##int)                                  \
386                   - ilogbf((float)(0x##int##fract##UL)))
387 #define HEX_DBL(sm, int, fract, se, exp)                                       \
388     sm ldexp((double)(0x##int##fract##ULL),                                    \
389              se exp + ilogb((double)0x##int)                                   \
390                  - ilogb((double)(0x##int##fract##ULL)))
391 #define HEX_LDBL(sm, int, fract, se, exp)                                      \
392     sm ldexpl((long double)(0x##int##fract##ULL),                              \
393               se exp + ilogbl((long double)0x##int)                            \
394                   - ilogbl((long double)(0x##int##fract##ULL)))
395 #else
396 // If compiler supports hex floating point literals: just concatenate all the
397 // parts into a literal.
398 #define HEX_FLT(sm, int, fract, se, exp) sm 0x##int##.##fract##p##se##exp##F
399 #define HEX_DBL(sm, int, fract, se, exp) sm 0x##int##.##fract##p##se##exp
400 #define HEX_LDBL(sm, int, fract, se, exp) sm 0x##int##.##fract##p##se##exp##L
401 #endif
402 
403 #if defined(__MINGW32__)
404 #include <Windows.h>
405 #define sleep(sec) Sleep((sec)*1000)
406 #endif
407 
408 #endif // _COMPAT_H_
409