aboutsummaryrefslogtreecommitdiff
path: root/test_common/harness/compat.h
blob: a42f29172dce564a057508f905f532846d7cfef8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//
// Copyright (c) 2017 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef COMPAT_H_
#define COMPAT_H_

#if defined(_WIN32) && defined(_MSC_VER)
#include <Windows.h>
#else
#ifdef __cplusplus
#define EXTERN_C extern "C"
#else
#define EXTERN_C
#endif
#endif


//
// stdlib.h
//

#include <stdlib.h> // On Windows, _MAX_PATH defined there.

// llabs appeared in MS C v16 (VS 10/2010).
#if defined(_MSC_VER) && _MSC_VER <= 1500
EXTERN_C inline long long llabs(long long __x) { return __x >= 0 ? __x : -__x; }
#endif


//
// stdbool.h
//

// stdbool.h appeared in MS C v18 (VS 12/2013).
#if defined(_MSC_VER) && MSC_VER <= 1700
#if !defined(__cplusplus)
typedef char bool;
#define true 1
#define false 0
#endif
#else
#include <stdbool.h>
#endif // defined(_MSC_VER) && MSC_VER <= 1700


//
// stdint.h
//

// stdint.h appeared in MS C v16 (VS 10/2010) and Intel C v12.
#if defined(_MSC_VER)                                                          \
    && (!defined(__INTEL_COMPILER) && _MSC_VER <= 1500                         \
        || defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1200)
typedef unsigned char uint8_t;
typedef char int8_t;
typedef unsigned short uint16_t;
typedef short int16_t;
typedef unsigned int uint32_t;
typedef int int32_t;
typedef unsigned long long uint64_t;
typedef long long int64_t;
#else
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS
#endif
#include <stdint.h>
#endif


//
// float.h
//

#include <float.h>


//
// fenv.h
//

// fenv.h appeared in MS C v18 (VS 12/2013).
#if defined(_MSC_VER) && _MSC_VER <= 1700 && !defined(__INTEL_COMPILER)
// reimplement fenv.h because windows doesn't have it
#define FE_INEXACT 0x0020
#define FE_UNDERFLOW 0x0010
#define FE_OVERFLOW 0x0008
#define FE_DIVBYZERO 0x0004
#define FE_INVALID 0x0001
#define FE_ALL_EXCEPT 0x003D
int fetestexcept(int excepts);
int feclearexcept(int excepts);
#else
#include <fenv.h>
#endif


//
// math.h
//

#if defined(__INTEL_COMPILER)
#include <mathimf.h>
#else
#include <math.h>
#endif

#ifndef M_PI
#define M_PI 3.14159265358979323846264338327950288
#endif

#if defined(_MSC_VER)

#ifdef __cplusplus
extern "C" {
#endif

#ifndef NAN
#define NAN (INFINITY - INFINITY)
#endif

#ifndef HUGE_VALF
#define HUGE_VALF (float)HUGE_VAL
#endif

#ifndef INFINITY
#define INFINITY (FLT_MAX + FLT_MAX)
#endif

#ifndef isfinite
#define isfinite(x) _finite(x)
#endif

#ifndef isnan
#define isnan(x) ((x) != (x))
#endif

#ifndef isinf
#define isinf(_x) ((_x) == INFINITY || (_x) == -INFINITY)
#endif

#if _MSC_VER < 1900 && !defined(__INTEL_COMPILER)

double rint(double x);
float rintf(float x);
long double rintl(long double x);

float cbrtf(float);
double cbrt(double);

int ilogb(double x);
int ilogbf(float x);
int ilogbl(long double x);

double fmax(double x, double y);
double fmin(double x, double y);
float fmaxf(float x, float y);
float fminf(float x, float y);

double log2(double x);
long double log2l(long double x);

double exp2(double x);
long double exp2l(long double x);

double fdim(double x, double y);
float fdimf(float x, float y);
long double fdiml(long double x, long double y);

double remquo(double x, double y, int* quo);
float remquof(float x, float y, int* quo);
long double remquol(long double x, long double y, int* quo);

long double scalblnl(long double x, long n);

float hypotf(float x, float y);
long double hypotl(long double x, long double y);
double lgamma(double x);
float lgammaf(float x);

double trunc(double x);
float truncf(float x);

double log1p(double x);
float log1pf(float x);
long double log1pl(long double x);

double copysign(double x, double y);
float copysignf(float x, float y);
long double copysignl(long double x, long double y);

long lround(double x);
long lroundf(float x);
// long lroundl(long double x)

double round(double x);
float roundf(float x);
long double roundl(long double x);

int cf_signbit(double x);
int cf_signbitf(float x);

// Added in _MSC_VER == 1800 (Visual Studio 2013)
#if _MSC_VER < 1800
static int signbit(double x) { return cf_signbit(x); }
#endif
static int signbitf(float x) { return cf_signbitf(x); }

long int lrint(double flt);
long int lrintf(float flt);

float int2float(int32_t ix);
int32_t float2int(float fx);

#endif // _MSC_VER < 1900 && ! defined( __INTEL_COMPILER )

#if _MSC_VER < 1900 && (!defined(__INTEL_COMPILER) || __INTEL_COMPILER < 1300)
// These functions appeared in Intel C v13 and Visual Studio 2015
float nanf(const char* str);
double nan(const char* str);
long double nanl(const char* str);
#endif

#ifdef __cplusplus
}
#endif

#endif // defined(_MSC_VER)

#if defined(__ANDROID__)
#define log2(X) (log(X) / log(2))
#endif


//
// stdio.h
//

#if defined(_MSC_VER)
// snprintf added in _MSC_VER == 1900 (Visual Studio 2015)
#if _MSC_VER < 1900
#define snprintf sprintf_s
#endif
#endif // defined(_MSC_VER)


//
// string.h
//

#if defined(_MSC_VER)
#define strtok_r strtok_s
#endif


//
// unistd.h
//

#if defined(_MSC_VER)
EXTERN_C unsigned int sleep(unsigned int sec);
EXTERN_C int usleep(int usec);
#endif


//
// syscall.h
//

#if defined(__ANDROID__)
// Android bionic's isn't providing SYS_sysctl wrappers.
#define SYS__sysctl __NR__sysctl
#endif


// Some tests use _malloca which defined in malloc.h.
#if !defined(__APPLE__)
#include <malloc.h>
#endif


//
// ???
//

#if defined(_MSC_VER)

#define MAXPATHLEN _MAX_PATH

EXTERN_C uint64_t ReadTime(void);
EXTERN_C double SubtractTime(uint64_t endTime, uint64_t startTime);

/** Returns the number of leading 0-bits in x,
    starting at the most significant bit position.
    If x is 0, the result is undefined.
*/
EXTERN_C int __builtin_clz(unsigned int pattern);

#endif


/*-----------------------------------------------------------------------------
   WARNING: DO NOT USE THESE MACROS:
        MAKE_HEX_FLOAT, MAKE_HEX_DOUBLE, MAKE_HEX_LONG.

   This is a typical usage of the macros:

     double yhi = MAKE_HEX_DOUBLE(0x1.5555555555555p-2,0x15555555555555LL,-2);

   (taken from math_brute_force/reference_math.c). There are two problems:

     1. There is an error here. On Windows in will produce incorrect result
        `0x1.5555555555555p+50'.
        To have a correct result it should be written as:
           MAKE_HEX_DOUBLE(0x1.5555555555555p-2, 0x15555555555555LL, -54)
        A proper value of the third argument is not obvious -- sometimes it
        should be the same as exponent of the first argument, but sometimes
        not.

     2. Information is duplicated. It is easy to make a mistake.

   Use HEX_FLT, HEX_DBL, HEX_LDBL macros instead
   (see them in the bottom of the file).
-----------------------------------------------------------------------------*/
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)

#define MAKE_HEX_FLOAT(x, y, z) ((float)ldexp((float)(y), z))
#define MAKE_HEX_DOUBLE(x, y, z) ldexp((double)(y), z)
#define MAKE_HEX_LONG(x, y, z) ((long double)ldexp((long double)(y), z))

#else

// Do not use these macros in new code, use HEX_FLT, HEX_DBL, HEX_LDBL instead.
#define MAKE_HEX_FLOAT(x, y, z) x
#define MAKE_HEX_DOUBLE(x, y, z) x
#define MAKE_HEX_LONG(x, y, z) x

#endif


/*-----------------------------------------------------------------------------
   HEX_FLT, HEXT_DBL, HEX_LDBL -- Create hex floating point literal of type
   float, double, long double respectively. Arguments:

      sm    -- sign of number,
      int   -- integer part of mantissa (without `0x' prefix),
      fract -- fractional part of mantissa (without decimal point and `L' or
            `LL' suffixes),
      se    -- sign of exponent,
      exp   -- absolute value of (binary) exponent.

   Example:

      double yhi = HEX_DBL(+, 1, 5555555555555, -, 2); // 0x1.5555555555555p-2

   Note:

      We have to pass signs as separate arguments because gcc pass negative
   integer values (e. g. `-2') into a macro as two separate tokens, so
   `HEX_FLT(1, 0, -2)' produces result `0x1.0p- 2' (note a space between minus
   and two) which is not a correct floating point literal.
-----------------------------------------------------------------------------*/
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
// If compiler does not support hex floating point literals:
#define HEX_FLT(sm, int, fract, se, exp)                                       \
    sm ldexpf((float)(0x##int##fract##UL),                                     \
              se exp + ilogbf((float)0x##int)                                  \
                  - ilogbf((float)(0x##int##fract##UL)))
#define HEX_DBL(sm, int, fract, se, exp)                                       \
    sm ldexp((double)(0x##int##fract##ULL),                                    \
             se exp + ilogb((double)0x##int)                                   \
                 - ilogb((double)(0x##int##fract##ULL)))
#define HEX_LDBL(sm, int, fract, se, exp)                                      \
    sm ldexpl((long double)(0x##int##fract##ULL),                              \
              se exp + ilogbl((long double)0x##int)                            \
                  - ilogbl((long double)(0x##int##fract##ULL)))
#else
// If compiler supports hex floating point literals: just concatenate all the
// parts into a literal.
#define HEX_FLT(sm, int, fract, se, exp) sm 0x##int##.##fract##p##se##exp##F
#define HEX_DBL(sm, int, fract, se, exp) sm 0x##int##.##fract##p##se##exp
#define HEX_LDBL(sm, int, fract, se, exp) sm 0x##int##.##fract##p##se##exp##L
#endif

#if defined(__MINGW32__)
#include <Windows.h>
#define sleep(sec) Sleep((sec)*1000)
#endif

#endif // COMPAT_H_