-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdjl_os.hxx
More file actions
491 lines (413 loc) · 14.2 KB
/
Copy pathdjl_os.hxx
File metadata and controls
491 lines (413 loc) · 14.2 KB
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
These are some utilities and abstractions for building on Windows, Linux, and macOS
*/
#pragma once
#include <stdint.h>
#include <stdio.h>
#include <time.h>
#ifdef _WIN32
#ifndef UNICODE
#define UNICODE
#endif
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <conio.h>
#include <direct.h>
#include <intrin.h>
#include <io.h>
#define not_inlined __declspec(noinline)
#define force_inlined __forceinline
inline void sleep_ms( uint64_t ms ) { SleepEx( (DWORD) ms, FALSE ); }
inline bool file_exists( char const * pfile )
{
uint32_t attr = GetFileAttributesA( pfile );
return ( ( INVALID_FILE_ATTRIBUTES != attr ) && ( ! ( FILE_ATTRIBUTE_DIRECTORY & attr ) ) );
} //file_exists
inline void bump_thread_priority() { SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_HIGHEST ); }
inline void set_process_affinity( uint64_t processAffinityMask )
{
SetProcessAffinityMask( (HANDLE) -1, processAffinityMask );
}
#elif defined( WATCOMDOS ) || defined( WATCOMLINUX )
#include <io.h>
#define MAX_PATH 255
#define not_inlined
#define force_inlined __inline
inline void sleep_ms( uint64_t ms ) {}
inline bool file_exists( char const * pfile )
{
FILE * fp = fopen( pfile, "r" );
bool exists = false;
if ( fp )
{
fclose( fp );
exists = true;
}
return exists;
} //file_exists
inline void bump_thread_priority() {}
inline void set_process_affinity( uint64_t processAffinityMask ) {}
inline int getpid() { return 0; }
#define _countof( X ) ( sizeof( X ) / sizeof( X[0] ) )
inline void swap( uint8_t & a, uint8_t & b ) { uint8_t c = a; a = b; b = c; }
#else // Linux, MacOS, etc.
#include <termios.h>
#ifdef __mc68000__
#define truncl trunc
extern "C" int nanosleep( const struct timespec * duration, struct timespec * rem );
#endif
#include <thread>
#include <sched.h>
#include <unistd.h>
#include <ctype.h>
#define not_inlined __attribute__ ((noinline))
#define force_inlined inline
inline void bump_thread_priority() {}
inline void set_process_affinity( uint64_t processAffinityMask )
{
#if !defined(__APPLE__) && !defined( __mc68000__ )
cpu_set_t mask;
CPU_ZERO( &mask );
for ( long l = 0; l < 32; l++ )
{
int b = ( 1 << l );
if ( 0 != ( b & processAffinityMask ) )
CPU_SET( l, &mask );
}
// this does nothing on WSL 1 or 2 except make you believe it might work until you actually check
int status = sched_setaffinity( 0, sizeof( mask ), &mask );
#endif
} //set_process_affinity
template < typename T, size_t N > size_t _countof( T ( & arr )[ N ] ) { return std::extent< T[ N ] >::value; }
#define _stricmp strcasecmp
#define MAX_PATH 1024
extern "C" inline char * strupr( char * s )
{
for ( char * t = s; *t; t++ )
*t = toupper( *t );
return s;
} //strupr
inline char * _strupr( char * s ) { return strupr( s ); }
extern "C" inline char * strlwr( char * s )
{
for ( char * t = s; *t; t++ )
*t = tolower( *t );
return s;
} //strlwr
inline char * _strlwr( char * s ) { return strlwr( s ); }
inline uint64_t _abs64( int64_t x ) { return ( x > 0 ) ? x : -x; }
inline void sleep_ms( uint64_t ms )
{
uint64_t total_ns = ms * 1000000;
long ns = (long) ( total_ns % 1000000000 );
long sec = (long) ( total_ns / 1000000000 );
struct timespec ts = { sec, ns };
nanosleep( &ts, 0 );
} //sleep_ms
inline bool file_exists( char const * pfile )
{
FILE * fp = fopen( pfile, "r" );
bool exists = false;
if ( fp )
{
fclose( fp );
exists = true;
}
return exists;
} //file_exists
#endif
template <class T> inline T get_max( T a, T b )
{
if ( a > b )
return a;
return b;
} //get_max
template <class T> inline T get_min( T a, T b )
{
if ( a < b )
return a;
return b;
} //get_min
template <class T> inline T round_up( T x, T multiple )
{
if ( 0 == multiple )
return x;
T remainder = x % multiple;
if ( 0 == remainder )
return x;
return x + multiple - remainder;
} //round_up
inline const char * target_platform()
{
#if defined( __riscv ) // g++ on linux
return "riscv";
#elif defined( __amd64__ ) // g++ on linux
return "amd64";
#elif defined( __i386__ ) // g++ on linux
return "i386";
#elif defined( __aarch64__ ) // g++ on linux
return "arm64";
#elif defined( _M_AMD64 ) // msft on Windows
return "amd64";
#elif defined( _M_ARM64 ) // msft on Windows
return "arm64";
#elif defined( WATCOMDOS ) // WATCOM for 8086
return "8086";
#elif defined( WATCOMLINUX ) // WATCOM for i386
return "i386";
#elif defined( _M_IX86 ) // msft on Windows 32-bit
return "x86";
#elif defined( __ARM_32BIT_STATE ) // ARM32 on Raspberry PI (and more)
return "arm32";
#elif defined( __mc68000__ )
return "m68000";
#elif defined( sparc )
return "sparc";
#else
return "(other)";
#endif
} //target_platform
inline const char * build_type()
{
#ifdef NDEBUG
return "release";
#else
return "debug";
#endif
} //build_type
inline const char * compiler_used()
{
static char acver[ 100 ];
#if defined( __GNUC__ )
return "g++";
#elif defined( _MSC_VER )
snprintf( acver, sizeof( acver ), "msft C++ ver %u", _MSC_VER );
return acver;
#elif defined( __clang__ )
return "clang";
#elif defined( WATCOMDOS )
return "watcom 8086";
#elif defined( WATCOMLINUX )
return "watcom i386";
#else
return "unknown";
#endif
} //compiler_used
inline const char * build_platform()
{
#if defined( __APPLE__ )
return "apple";
#elif defined( __linux )
return "linux";
#elif defined( _WIN32 )
return "windows";
#elif defined( __LINUX__ ) // watcom does this
return "linux";
#elif defined( __UNIX__ ) // watcom does this
return "unix";
#elif defined( __WINDOWS__ ) || defined( __NT__ ) // watcom does this
return "windows";
#else
return "unknown";
#endif
} //build_platform
inline const char * build_string()
{
static char bs[ 320 ];
snprintf( bs, sizeof( bs ), "Built for %s %s on %c%c %c%c%c %s %s by %s on %s\n",
target_platform(), build_type(), __DATE__[4], __DATE__[5],
__DATE__[0], __DATE__[1], __DATE__[2], &__DATE__[7], __TIME__, compiler_used(), build_platform() );
return bs;
} //build_string
#if defined( __GNUC__ ) || defined( __clang__ )
#define assume_false return( 0 ) // clearly terrible, but this code will never execute. ever.
#define assume_false_return return // clearly terrible, but this code will never execute. ever.
#elif defined( WATCOMDOS ) || defined( WATCOMLINUX )
#define assume_false return( 0 ) // clearly terrible, but this code will never execute. ever.
#define __assume( x )
#else
#define assume_false __assume( false )
#define assume_false_return __assume( false )
#endif
inline long portable_filelen( int descriptor )
{
#ifdef _WIN32
long current = _lseek( descriptor, 0, SEEK_CUR );
long len = _lseek( descriptor, 0, SEEK_END );
_lseek( descriptor, current, SEEK_SET );
#else
long current = lseek( descriptor, 0, SEEK_CUR );
long len = lseek( descriptor, 0, SEEK_END );
lseek( descriptor, current, SEEK_SET );
#endif
return len;
} //portable_filelen
inline long portable_filelen( FILE * fp )
{
long current = ftell( fp );
fseek( fp, 0, SEEK_END );
long len = ftell( fp );
fseek( fp, current, SEEK_SET );
return len;
} //portable_filelen
inline long portable_filelen( const char * p )
{
FILE * fp = fopen( p, "r" );
if ( 0 != fp )
{
long len = portable_filelen( fp );
fclose( fp );
return len;
}
return 0;
} //portable_filelen
class CFile
{
private:
FILE * fp;
public:
CFile( FILE * file ) : fp( file ) {}
~CFile() { close(); }
FILE * get() { return fp; }
void close()
{
if ( NULL != fp )
{
fclose( fp );
fp = NULL;
}
}
};
inline char printable( uint8_t x )
{
if ( x < ' ' || x >= 127 )
return ' ';
return x;
} //printable
#if ( ( defined( __clang__ ) || defined( __GNUC__ ) ) )
inline uint64_t flip_endian64( uint64_t x ) { return __builtin_bswap64( x ); }
inline uint32_t flip_endian32( uint32_t x ) { return __builtin_bswap32( x ); }
inline uint16_t flip_endian16( uint16_t x ) { return __builtin_bswap16( x ); }
#elif defined( _MSC_VER )
inline uint64_t flip_endian64( uint64_t x ) { return _byteswap_uint64( x ); }
inline uint32_t flip_endian32( uint32_t x ) { return _byteswap_ulong( x ); }
inline uint16_t flip_endian16( uint16_t x ) { return _byteswap_ushort( x ); }
#else
inline uint64_t flip_endian64( uint64_t x )
{
return ( ( x & 0xffull ) << 56 ) | ( ( x & 0xff00ull ) << 40 ) | ( ( x & 0xff0000ull ) << 24 ) | ( ( x & 0xff000000ull ) << 8 ) |
( ( x & 0xff00000000ull ) >> 8 ) | ( ( x & 0xff0000000000ull ) >> 24 ) | ( ( x & 0xff000000000000ull ) >> 40 ) | ( x >> 56 );
} //flip_endian64
inline uint32_t flip_endian32( uint32_t x )
{
return ( ( x & 0xff ) << 24 ) | ( ( x & 0xff00) << 8 ) | ( ( x & 0xff0000) >> 8 ) | ( x >> 24 );
} //flip_endian32
inline uint16_t flip_endian16( uint16_t x )
{
return ( ( x >> 8 ) | ( ( x & 0xff ) << 8 ) );
} //flip_endian16
#endif
// le16_t/le32_t: storage types for multi-byte fields that live in guest memory or in a fixed
// little-endian file/wire format (e.g. an MZ EXE header) -- places where the bytes are
// always little-endian regardless of host, and are also visible to code that doesn't go
// through a swap-aware accessor (e.g. a struct overlaid directly on emulated RAM). On a
// little-endian host these are plain integers with zero overhead; on a big-endian host
// every load/store transparently byte-swaps.
#ifdef TARGET_BIG_ENDIAN
class le16_t
{
uint16_t v;
public:
le16_t() : v( 0 ) {}
le16_t( uint16_t x ) : v( flip_endian16( x ) ) {}
operator uint16_t() const { return flip_endian16( v ); }
le16_t & operator=( uint16_t x ) { v = flip_endian16( x ); return *this; }
le16_t & operator++() { *this = (uint16_t) ( (uint16_t) *this + 1 ); return *this; }
le16_t operator++( int ) { le16_t tmp = *this; ++(*this); return tmp; }
le16_t & operator+=( uint16_t x ) { *this = (uint16_t) ( (uint16_t) *this + x ); return *this; }
le16_t & operator-=( uint16_t x ) { *this = (uint16_t) ( (uint16_t) *this - x ); return *this; }
}; //le16_t
class le32_t
{
uint32_t v;
public:
le32_t() : v( 0 ) {}
le32_t( uint32_t x ) : v( flip_endian32( x ) ) {}
operator uint32_t() const { return flip_endian32( v ); }
le32_t & operator=( uint32_t x ) { v = flip_endian32( x ); return *this; }
le32_t & operator++() { *this = (uint32_t) ( (uint32_t) *this + 1 ); return *this; }
le32_t operator++( int ) { le32_t tmp = *this; ++(*this); return tmp; }
le32_t & operator+=( uint32_t x ) { *this = (uint32_t) ( (uint32_t) *this + x ); return *this; }
le32_t & operator-=( uint32_t x ) { *this = (uint32_t) ( (uint32_t) *this - x ); return *this; }
}; //le32_t
#else
typedef uint16_t le16_t;
typedef uint32_t le32_t;
#endif
#ifdef sparc // If using buildroot and wide strings weren't included...
inline size_t wcstombs( char * dst, const wchar_t * src, size_t len ) // simplistic ascii-only version
{
if ( 0 == dst || 0 == src )
return -1;
size_t i = 0;
while ( ( i < ( len - 1 ) ) && src[i] )
{
dst[ i ] = (char) src[ i ];
i++;
}
dst[ i ] = 0;
return i;
} //wcstombs
inline size_t wcslen(const wchar_t *str)
{
size_t len = 0;
while ( *str )
{
len++;
str++;
}
return len;
} //wcslen
#endif
inline uint8_t bit_count( uint64_t x )
{
// use popcnt if possible. It's not available on the Q9650 and other older Intel CPUs. use fallback code below instead if needed.
#if defined( __GNUC__ ) || defined( __clang__ )
return (uint8_t) __builtin_popcountll( x );
#elif defined( _MSC_VER )
return (uint8_t) __popcnt64( x );
#elif defined( __aarch64__ )
return (uint8_t) std::bitset<64>( x ).count();
#else
uint8_t count = 0;
while ( 0 != x )
{
x &= ( x - 1 ); // Deletes the rightmost 1-bit
count++;
}
return count;
#endif
} //bit_count64
inline uint8_t bit_count8( uint8_t x )
{
// use popcnt if possible. It's not available on the Q9650 and other older Intel CPUs. use fallback code below instead if needed.
#if defined( __GNUC__ ) || defined( __clang__ )
return (uint8_t) __builtin_popcount( x );
#elif defined( _MSC_VER )
return (uint8_t) __popcnt16( x );
#elif defined( __aarch64__ )
return (uint8_t) std::bitset<8>( x ).count();
#else
uint8_t count = 0;
while ( 0 != x )
{
x &= ( x - 1 ); // Deletes the rightmost 1-bit
count++;
}
return count;
#endif
} //bit_count8
inline bool is_parity_even8( uint8_t x )
{
return ( ! ( bit_count8( x ) & 1 ) );
} //is_parity_even8