-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform.h
176 lines (170 loc) · 4.67 KB
/
platform.h
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
//==============================================================================
// MIPS(R) Architecture For Programmers Volume II-A: The MIPS64(R) Instruction Set Reference Manual
//
// Document Number: MD00087
// Revision 6.06
// December 15, 2016
//
// Copyright(C) Wave Computing, Inc. All rights reserved.
//==============================================================================
#pragma once
#include <stdlib.h>
#if defined(_MSC_VER) && !defined(__llvm__)
# if (_MSC_VER < 1800)
typedef signed char int8_t;
typedef short int16_t;
typedef int int32_t;
typedef long long int64_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
# else
# include <stdint.h>
# endif
# include <crtdefs.h>
# include <malloc.h>
# include <intrin.h>
# define UNUSED
inline int __builtin_bswap32(int x)
{
return _byteswap_ulong(x);
}
inline long long __builtin_bswap64(long long x)
{
return _byteswap_uint64(x);
}
inline void __builtin_trap()
{
__debugbreak();
}
inline intptr_t __builtin_clzl(intptr_t bits)
{
unsigned long r = 0;
_BitScanReverse(&r, (long)bits);
return r ^ 31;
}
# if defined(_M_AMD64)
inline intptr_t __builtin_clzl(intptr_t bits)
{
unsigned long r = 0;
_BitScanReverse64(&r, (__int64)bits);
return r ^ 63;
}
# endif
# if (_MSC_VER < 1800)
# include <math.h>
# include <float.h>
inline float roundf(float fValue)
{
return (value < 0.0f) ? ceilf(value - 0.5f) : floorf(value + 0.5f);
}
# ifndef round
inline double round(double value)
{
return (value < 0.0) ? ceil(value - 0.5) : floor(value + 0.5);
}
# endif
inline float truncf(float value)
{
return (value >= 0.0f) ? floorf(value) : ceilf(value);
}
inline double trunc(double value)
{
return (value >= 0.0) ? floor(value) : ceil(value);
}
inline float rintf(float value)
{
return roundf(value);
}
inline double rint(double value)
{
return round(value);
}
inline float fminf(float X, float Y)
{
return X < Y ? X : Y;
}
inline double fmin(double X, double Y)
{
return X < Y ? X : Y;
}
inline float fmaxf(float X, float Y)
{
return X > Y ? X : Y;
}
inline double fmax(double X, double Y)
{
return X > Y ? X : Y;
}
# ifndef isnan
inline bool isnan(double value)
{
return _isnan(value) != 0;
}
# endif
inline int fpclassify(float value)
{
return 0; // TODO
}
inline int fpclassify(double value)
{
return 0; // TODO
}
# endif
#else
# include <stddef.h>
# include <stdint.h>
# define UNUSED __attribute__((unused))
# if defined(__APPLE__)
inline void* _aligned_malloc(size_t size, size_t alignment)
{
void* ptr = NULL;
posix_memalign(&ptr, size, alignment);
return ptr;
}
inline void _aligned_free(void* ptr)
{
free(ptr);
}
# elif defined(__linux__)
inline void* _aligned_malloc(size_t size, size_t alignment)
{
return memalign(alignment, size);
}
inline void _aligned_free(void* ptr)
{
free(ptr);
}
# endif
# if defined(__LP64__)
typedef __int128 int128_t;
typedef __uint128_t uint128_t;
inline int64_t _mul128(int64_t a, int64_t b, int64_t* hi)
{
int128_t hilo = (int128_t)a * b;
(*hi) = (int64_t)(hilo >> 64);
return (int64_t)hilo;
}
inline uint64_t _umul128(uint64_t a, uint64_t b, uint64_t* hi)
{
uint128_t hilo = (uint128_t)a * b;
(*hi) = (int64_t)(hilo >> 64);
return (int64_t)hilo;
}
# elif defined(_MSC_VER)
# include <intrin.h>
# endif
#endif
inline int32_t _mul64(int32_t a, int32_t b, int32_t* hi)
{
int64_t hilo = (int64_t)a * b;
(*hi) = (int32_t)(hilo >> 32);
return (int32_t)hilo;
}
inline uint32_t _umul64(uint32_t a, uint32_t b, uint32_t* hi)
{
uint64_t hilo = (uint64_t)a * b;
(*hi) = (uint32_t)(hilo >> 32);
return (uint32_t)hilo;
}