/
u_misc.cpp
329 lines (292 loc) · 8.86 KB
/
u_misc.cpp
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
#include <SDL_cpuinfo.h>
#include <SDL_endian.h>
#include <time.h> // time_t, time
#include <float.h>
#include <limits.h>
#include "u_misc.h"
#include "u_memory.h" // unique_ptr
namespace u {
union CheckEndian {
constexpr CheckEndian(uint32_t value)
: asUint32(value)
{
}
uint32_t asUint32;
uint8_t asBytes[sizeof asUint32];
};
static constexpr CheckEndian kCheckEndian(0x01020304);
bool isLilEndian() {
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
return true;
#endif
return kCheckEndian.asBytes[0] == 0x04;
}
bool isBigEndian() {
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
return true;
#endif
return kCheckEndian.asBytes[0] == 0x01;
}
void *moveMemory(void *dest, const void *src, size_t n) {
unsigned char *d = (unsigned char *)dest;
const unsigned char *s = (const unsigned char *)src;
if (d == s)
return d;
if (s+n <= d || d+n <= s)
return memcpy(d, s, n);
if (d < s) {
if ((uintptr_t)s % sizeof(size_t) == (uintptr_t)d % sizeof(size_t)) {
while ((uintptr_t)d % sizeof(size_t)) {
if (!n--)
return dest;
*d++ = *s++;
}
for (; n >= sizeof(size_t); n -= sizeof(size_t), d += sizeof(size_t), s += sizeof(size_t))
*(size_t*)d = *(size_t*)s;
}
for (; n; n--)
*d++ = *s++;
} else {
if ((uintptr_t)s % sizeof(size_t) == (uintptr_t)d % sizeof(size_t)) {
while ((uintptr_t)(d+n) % sizeof(size_t)) {
if (!n--)
return dest;
d[n] = s[n];
}
while (n >= sizeof(size_t)) {
n -= sizeof(size_t);
*(size_t*)(d+n) = *(size_t*)(s+n);
}
}
while (n) {
n--;
d[n] = s[n];
}
}
return dest;
}
static inline uint32_t m32(uint32_t x) {
return 0x80000000 & x;
}
static inline uint32_t l31(uint32_t x) {
return 0x7FFFFFFF & x;
}
static inline bool odd(uint32_t x) {
return x & 1; // Check if number is odd
}
// State for Mersenne Twister
static struct mtState {
static constexpr size_t kSize = 624;
static constexpr size_t kPeriod = 397;
static constexpr size_t kDiff = kSize - kPeriod;
static constexpr uint32_t kMatrix[2] = { 0, 0x9908B0DF };
mtState();
uint32_t randu();
float randf();
protected:
void generate();
private:
uint32_t m_mt[kSize];
size_t m_index;
} gRandomState;
constexpr uint32_t mtState::kMatrix[2];
inline mtState::mtState() :
m_index(0)
{
union {
time_t t;
uint32_t u;
} seed = { time(nullptr) };
m_mt[0] = seed.u;
for (size_t i = 1; i < kSize; ++i)
m_mt[i] = 0x6C078965u * (m_mt[i-1] ^ m_mt[i-1] >> 30) + i;
}
inline void mtState::generate() {
size_t y;
size_t i;
// i = [0 ... 226]
for (i = 0; i < kDiff; ++i) {
y = m32(m_mt[i]) | l31(m_mt[i+1]);
m_mt[i] = m_mt[i+kPeriod] ^ (y>>1) ^ kMatrix[odd(y)];
}
// i = [227 ... 622]
for (i = kDiff; i < kSize-1; ) {
for (size_t j = 0; j < 11; j++, ++i) {
y = m32(m_mt[i]) | l31(m_mt[i+1]);
m_mt[i] = m_mt[i-kDiff] ^ (y>>1) ^ kMatrix[odd(y)];
}
}
// i = [623]
y = m32(m_mt[kSize-1]) | l31(m_mt[kSize-1]);
m_mt[kSize-1] = m_mt[kPeriod-1] ^ (y>>1) ^ kMatrix[odd(y)];
}
inline uint32_t mtState::randu() {
if (m_index == 0)
generate();
uint32_t y = m_mt[m_index];
// Tempering
y ^= y >> 11;
y ^= y << 7 & 0x9D2C5680;
y ^= y << 15 & 0xEFC60000;
y ^= y >> 18;
if (++m_index == kSize)
m_index = 0;
return y;
}
inline float mtState::randf() {
return float(randu()) / UINT32_MAX;
}
uint32_t randu() {
return gRandomState.randu();
}
float randf() {
return gRandomState.randf();
}
unsigned char log2(uint32_t v) {
// 32-byte table
static const unsigned char kMultiplyDeBruijnBitPosition[32] = {
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
return kMultiplyDeBruijnBitPosition[(uint32_t)(v * 0x077CB531U) >> 27];
}
#if defined(__GNUC__)
# include <cpuid.h>
# define CPUID(FUNC, A, B, C, D) \
__get_cpuid((FUNC), &A, &B, &C, &D)
#elif defined(_MSC_VER)
# define CPUID(FUNC, A, B, C, D) \
do { \
int info[4]; \
__cpuid(info, (FUNC)); \
A = info[0]; \
B = info[1]; \
C = info[2]; \
D = info[3]; \
} while (0)
#else
# define CPUID(FUNC, A, B, C, D) \
([]() { static_assert(0, "no suitable implementation of CPUID found"); })()
#endif
const char *CPUDesc() {
static char desc[1024];
if (desc[0])
return desc;
int i = 0;
unsigned int a = 0, b = 0, c = 0, d = 0;
CPUID(0x80000000, a, b, c, d);
if (a >= 0x80000004) {
for (int k = 0; k < 3; k++) {
CPUID(0x80000002u + k, a, b, c, d);
for (int j = 0; j < 4; j++) { desc[i++] = (char)(a & 0xFF); a >>= 8; }
for (int j = 0; j < 4; j++) { desc[i++] = (char)(b & 0xFF); b >>= 8; }
for (int j = 0; j < 4; j++) { desc[i++] = (char)(c & 0xFF); c >>= 8; }
for (int j = 0; j < 4; j++) { desc[i++] = (char)(d & 0xFF); d >>= 8; }
}
}
if (desc[0]) {
// trim leading whitespace
char *trim = desc;
while (u::isspace(*trim)) trim++;
if (trim != desc && *trim)
u::moveMemory(desc, trim, strlen(trim) + 1);
// trim trailing whitespace
const size_t size = strlen(desc);
char *end = desc + size - 1;
while (end >= desc && u::isspace(*end))
end--;
*(end + 1) = '\0';
// replace instances of two or more spaces with one
char *dest = desc;
char *chop = desc;
while (*chop) {
while (*chop == ' ' && *(chop + 1) == ' ')
chop++;
*dest++ = *chop++;
}
*dest ='\0';
char format[1024];
int count = SDL_GetCPUCount();
int wrote = snprintf(format, sizeof format, "%s (%d %s)",
desc, count, count > 1 ? "cores" : "core");
if (wrote == -1)
return desc;
memcpy(desc, format, wrote + 1);
return desc;
}
snprintf(desc, sizeof desc, "Unknown");
return desc;
}
const char *RAMDesc() {
static char desc[128];
if (desc[0])
return desc;
// SDL reports MB, sizeMetric expects bytes
snprintf(desc, sizeof desc, "%s", u::sizeMetric(size_t(SDL_GetSystemRAM()) * (1u << 20)).c_str());
return desc;
}
static struct crc32Table {
crc32Table() {
// Build the initial CRC32 table
for (size_t i = 0; i < 256; i++) {
uint32_t c = i;
for (size_t j = 0; j < 8; j++)
c = (c & 1) ? (c >> 1) ^ 0xEDB88320 : (c >> 1);
m_table[0][i] = c;
}
// Build the others for slicing-by-8
for (size_t i = 0; i < 256; i++) {
uint32_t c = m_table[0][i];
for (size_t j = 1; j < 8; j++) {
c = m_table[0][c & 0xFF] ^ (c >> 8);
m_table[j][i] = c;
}
}
}
uint32_t operator()(size_t i, size_t j) const {
return m_table[i][j];
}
private:
uint32_t m_table[8][256];
} gCRC32;
// based on the slicing-by-8 algorithm as described in
// "High Octane CRC Generation with the Intel Slicing-by-8 Algorithm"
uint32_t crc32(const unsigned char *buffer, size_t length) {
// Align to 8 for better performance
uint32_t crc = ~0u;
for (; length && ((uintptr_t)buffer & 7); length--, buffer++)
crc = gCRC32(0, (crc ^ *buffer) & 0xFF) ^ (crc >> 8);
for (; length >= 8; length -= 8, buffer += 8) {
uint32_t word;
memcpy(&word, buffer, sizeof word);
u::endianSwap(&word, 1);
crc ^= word;
crc = gCRC32(7, crc & 0xFF) ^
gCRC32(6, (crc >> 8) & 0xFF) ^
gCRC32(5, (crc >> 16) & 0xFF) ^
gCRC32(4, (crc >> 24) & 0xFF) ^
gCRC32(3, buffer[4]) ^
gCRC32(2, buffer[5]) ^
gCRC32(1, buffer[6]) ^
gCRC32(0, buffer[7]);
}
for (; length; length--, buffer++)
crc = gCRC32(0, (crc ^ *buffer) & 0xFF) ^ (crc >> 8);
return ~crc;
}
}
#if defined(_MSC_VER)
// Visual Studio 2015+ adds telemetry calls into the resulting binary which
// can only be disabled by linking an object or adding some empty stubs to
// the code.
//
// The optimizer should eliminate these calls.
//
// https://www.reddit.com/r/cpp/comments/4ibauu/visual_studio_adding_telemetry_function_calls_to
extern "C" {
void _cdecl __vcrt_initialize_telemetry_provider() { }
void _cdecl __telemetry_main_invoke_trigger() { }
void _cdecl __telemetry_main_return_trigger() { }
void _cdecl __vcrt_uninitialize_telemetry_provider() { }
};
#endif