-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathlibcpp.hpp
More file actions
648 lines (600 loc) · 17.1 KB
/
libcpp.hpp
File metadata and controls
648 lines (600 loc) · 17.1 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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
//==========================================
// LIBCTINY - Matt Pietrek 2001
// MSDN Magazine, January 2001
//==========================================
#ifdef _MSC_VER
#pragma once
#endif
#ifndef __LIBC_HPP_HEADERGUARD_H__
#define __LIBC_HPP_HEADERGUARD_H__
#if defined(_WIN32)
//#pragma comment(lib, "../../shared/sys_msvcrt.lib")
#pragma comment(linker, "/defaultlib:user32.lib")
#pragma comment(linker, "/defaultlib:kernel32.lib")
//extern "C" void __cdecl operator delete(void *,unsigned __int64){}
#ifndef UNICODE
#define UNICODE
#endif
#define _NO_CRT_STDIO_INLINE
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
//extern "C" void __cdecl __std_terminate(){}
extern "C" __declspec(noreturn) void __cdecl __std_terminate()
{
//terminate();
}
extern "C" void __cdecl __CxxFrameHandler3(){}
extern "C" void __cdecl __chkstk(){}
extern "C" __declspec(noreturn) void __cdecl _invalid_parameter_noinfo_noreturn(void){}
/*_CRTIMP2_PURE*/ extern "C" long long __cdecl _Xtime_get_ticks(void){return 0;}
namespace std
{
[[noreturn]] void __cdecl _Xbad_alloc(void){}
[[noreturn]] void __cdecl _Xlength_error(char const *){}
}
extern "C" void _fltused() {} // symbol needs to be defined for some reason
// musl log2f
#include <math.h>
#include <stdint.h>
static const float
ivln2hi = 1.4428710938e+00, /* 0x3fb8b000 */
ivln2lo = -1.7605285393e-04, /* 0xb9389ad4 */
/* |(log(1+s)-log(1-s))/s - Lg(s)| < 2**-34.24 (~[-4.95e-11, 4.97e-11]). */
//Lg1 = 0xaaaaaa.0p-24, /* 0.66666662693 */
//Lg2 = 0xccce13.0p-25, /* 0.40000972152 */
//Lg3 = 0x91e9ee.0p-25, /* 0.28498786688 */
//Lg4 = 0xf89e26.0p-26; /* 0.24279078841 */
Lg1 = 0.66666662693,
Lg2 = 0.40000972152,
Lg3 = 0.28498786688,
Lg4 = 0.24279078841;
float log2f(float x)
{
union {float f; uint32_t i;} u = {x};
float_t hfsq,f,s,z,R,w,t1,t2,hi,lo;
uint32_t ix;
int k;
ix = u.i;
k = 0;
if (ix < 0x00800000 || ix>>31) { /* x < 2**-126 */
if (ix<<1 == 0)
return -1/(x*x); /* log(+-0)=-inf */
if (ix>>31)
return (x-x)/0.0f; /* log(-#) = NaN */
/* subnormal number, scale up x */
k -= 25;
x *= 0x1p25f;
u.f = x;
ix = u.i;
} else if (ix >= 0x7f800000) {
return x;
} else if (ix == 0x3f800000)
return 0;
/* reduce x into [sqrt(2)/2, sqrt(2)] */
ix += 0x3f800000 - 0x3f3504f3;
k += (int)(ix>>23) - 0x7f;
ix = (ix&0x007fffff) + 0x3f3504f3;
u.i = ix;
x = u.f;
f = x - 1.0f;
s = f/(2.0f + f);
z = s*s;
w = z*z;
t1= w*(Lg2+w*Lg4);
t2= z*(Lg1+w*Lg3);
R = t2 + t1;
hfsq = 0.5f*f*f;
hi = f - hfsq;
u.f = hi;
u.i &= 0xfffff000;
hi = u.f;
lo = f - hi - hfsq + s*(hfsq+R);
return (lo+hi)*ivln2lo + lo*ivln2hi + hi*ivln2hi + k;
} // musl log2f
//extern char * _ppszArgv[];
//int __cdecl _ConvertCommandLineToArgcArgv(void);
//typedef void(__cdecl *_PVFV)(void);
//extern _PVFV __xc_a[], __xc_z[]; /* C++ initializers */
//void __cdecl _initterm(_PVFV * pfbegin, _PVFV * pfend);
//void __cdecl _atexit_init(void);
//void __cdecl _DoExit(void);
//#pragma comment(linker, "/defaultlib:kernel32.lib")
//#if defined(need_to_make_subsystem_windows_switch)
//extern "C" int __cdecl main(int, char **, char **);
//extern "C" int __cdecl main(int, char**);
//extern "C" void __cdecl mainCRTStartup(void)
//{
// int mainret, argc;
// argc = _ConvertCommandLineToArgcArgv();
// _atexit_init(); // set up our minimal cheezy atexit table
// _initterm(__xc_a, __xc_z); // Call C++ constructors
// //mainret = main(argc, _ppszArgv, 0);
// mainret = main(argc, _ppszArgv);
// _DoExit();
// ExitProcess(mainret);
//}
//#endif
//#include <cstdint>
//#include <cstring>
//#define _MAX_CMD_LINE_ARGS 128
//char* _ppszArgv[_MAX_CMD_LINE_ARGS + 1];
//extern "C" void __cdecl WinMainCRTStartup(void)
//{
// int mainret;
// char *lpszCommandLine;
// STARTUPINFO StartupInfo;
//
// lpszCommandLine = GetCommandLine();
//
// // Skip past program name (first token in command line).
// if (*lpszCommandLine == '"') // Check for and handle quoted program name
// {
// lpszCommandLine++; // Get past the first quote
//
// // Now, scan, and skip over, subsequent characters until another
// // double-quote or a null is encountered
// while (*lpszCommandLine && (*lpszCommandLine != '"'))
// lpszCommandLine++;
//
// if (*lpszCommandLine == '"') // If we stopped on a double-quote (usual case), skip over it.
// lpszCommandLine++;
// }
// else { // First token wasn't a quote
// while (*lpszCommandLine > ' ')
// lpszCommandLine++;
// }
//
// // Skip past any white space preceeding the second token.
// while (*lpszCommandLine && (*lpszCommandLine <= ' '))
// lpszCommandLine++;
//
// StartupInfo.dwFlags = 0;
// GetStartupInfo(&StartupInfo);
//
// _atexit_init(); // set up our minimal cheezy atexit table
// _initterm(__xc_a, __xc_z); // Call C++ constructors
// mainret = WinMain(GetModuleHandle(NULL),
// NULL,
// lpszCommandLine,
// StartupInfo.dwFlags & STARTF_USESHOWWINDOW
// ? StartupInfo.wShowWindow : SW_SHOWDEFAULT);
//
// _DoExit();
// ExitProcess(mainret);
//}
//extern "C" int __cdecl printf(const char * format, ...);
//extern "C" int __cdecl printf(const char * format, ...)
//{
// char szBuff[1024];
// int retValue;
// DWORD cbWritten;
// va_list argptr;
//
// va_start(argptr, format);
// retValue = wvsprintf(szBuff, format, argptr);
// va_end(argptr);
//
// WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), szBuff, retValue,
// &cbWritten, 0);
//
// return retValue;
//}
// Force the linker to include USER32.LIB
//#pragma comment(linker, "/defaultlib:user32.lib")
//extern "C" int __cdecl Printf(const char * format, ...)
//#include <strsafe.h>
////#include <stdio.h>
////#include <stdarg.h>
//#define PRINTF_BUF_SZ 4096
//inline int __cdecl Printf(const char * format, ...)
//{
// char szBuf[PRINTF_BUF_SZ];
// int retValue;
// DWORD cbWritten;
// va_list argptr;
//
// va_start( argptr, format );
// //retValue = wvsprintf( szBuff, format, argptr );
// retValue = StringCchVPrintf(szBuf, PRINTF_BUF_SZ, format, argptr );
// va_end( argptr );
//
// auto len=strlen(szBuf);
//
// OutputDebugString(szBuf);
// //WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), szBuf, (DWORD)len, &cbWritten, 0 );
//
// return retValue;
//}
//void Print(const char* s)
//{
// AllocConsole();
// auto hndl = GetStdHandle(STD_OUTPUT_HANDLE);
// DWORD cbWritten;
// WriteFile(hndl, s, strlen(s), &cbWritten, 0);
// //OutputDebugString(s);
//}
//
//
//// command line arguments to fill argc and argv
//int __cdecl _ConvertCommandLineToArgcArgv(void)
//{
// int cbCmdLine;
// int argc;
// PSTR pszSysCmdLine, pszCmdLine;
//
// // Set to no argv elements, in case we have to bail out
// _ppszArgv[0] = 0;
//
// // First get a pointer to the system's version of the command line, and figure out how long it is.
// pszSysCmdLine = GetCommandLine();
// cbCmdLine = lstrlen(pszSysCmdLine);
//
// // Allocate memory to store a copy of the command line. We'll modify
// // this copy, rather than the original command line. Yes, this memory
// // currently doesn't explicitly get freed, but it goes away when the
// // process terminates.
// pszCmdLine = (PSTR)HeapAlloc(GetProcessHeap(), 0, cbCmdLine + 1);
// if (!pszCmdLine)
// return 0;
//
// // Copy the system version of the command line into our copy
// lstrcpy(pszCmdLine, pszSysCmdLine);
//
// if ('"' == *pszCmdLine) // If command line starts with a quote ("),
// { // it's a quoted filename. Skip to next quote.
// pszCmdLine++;
//
// _ppszArgv[0] = pszCmdLine; // argv[0] == executable name
//
// while (*pszCmdLine && (*pszCmdLine != '"'))
// pszCmdLine++;
//
// if (*pszCmdLine) // Did we see a non-NULL ending?
// *pszCmdLine++ = 0; // Null terminate and advance to next char
// else
// return 0; // Oops! We didn't see the end quote
// }
// else // A regular (non-quoted) filename
// {
// _ppszArgv[0] = pszCmdLine; // argv[0] == executable name
//
// while (*pszCmdLine && (' ' != *pszCmdLine) && ('\t' != *pszCmdLine))
// pszCmdLine++;
//
// if (*pszCmdLine)
// *pszCmdLine++ = 0; // Null terminate and advance to next char
// }
//
// // Done processing argv[0] (i.e., the executable name). Now do th
// // actual arguments
//
// argc = 1;
//
// while (1)
// {
// // Skip over any whitespace
// while (*pszCmdLine && (' ' == *pszCmdLine) || ('\t' == *pszCmdLine))
// pszCmdLine++;
//
// if (0 == *pszCmdLine) // End of command line???
// return argc;
//
// if ('"' == *pszCmdLine) // Argument starting with a quote???
// {
// pszCmdLine++; // Advance past quote character
//
// _ppszArgv[argc++] = pszCmdLine;
// _ppszArgv[argc] = 0;
//
// // Scan to end quote, or NULL terminator
// while (*pszCmdLine && (*pszCmdLine != '"'))
// pszCmdLine++;
//
// if (0 == *pszCmdLine)
// return argc;
//
// if (*pszCmdLine)
// *pszCmdLine++ = 0; // Null terminate and advance to next char
// }
// else // Non-quoted argument
// {
// _ppszArgv[argc++] = pszCmdLine;
// _ppszArgv[argc] = 0;
//
// // Skip till whitespace or NULL terminator
// while (*pszCmdLine && (' ' != *pszCmdLine) && ('\t' != *pszCmdLine))
// pszCmdLine++;
//
// if (0 == *pszCmdLine)
// return argc;
//
// if (*pszCmdLine)
// *pszCmdLine++ = 0; // Null terminate and advance to next char
// }
//
// if (argc >= (_MAX_CMD_LINE_ARGS))
// return argc;
// }
//}
//
//extern "C" void* __cdecl malloc(size_t bytes)
//{
// //return (void*)HeapAlloc(_heapHndl, 0, bytes);
// return HeapAlloc(GetProcessHeap(), 0, bytes);
//}
//extern "C" void* __cdecl calloc(size_t nitems, size_t size)
//{
// //return HeapAlloc( _heapHndl, HEAP_ZERO_MEMORY, nitems * size );
// return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nitems * size);
//}
//extern "C" void* __cdecl realloc(void * p, size_t size)
//{
// if (p)
// return HeapReAlloc(GetProcessHeap(), 0, p, size);
// else // 'p' is 0, and HeapReAlloc doesn't act like realloc() here
// return HeapAlloc(GetProcessHeap(), 0, size);
//}
//extern "C" void __cdecl free(_Pre_maybenull_ _Post_invalid_ void* mem)
//{
// //HeapFree(_heapHndl, 0, mem);
// HeapFree(GetProcessHeap(), 0, mem);
//}
//void* __cdecl operator new(size_t s)
//{
// return HeapAlloc(GetProcessHeap(), 0, s);
//}
//void __cdecl operator delete(void* p)
//{
// HeapFree(GetProcessHeap(), 0, p);
//}
//
//extern "C" double _hypot(double x, double y) { return 0; }
//extern "C" void _fltused() {} // symbol needs to be defined for some reason
////#endif // end hypot function from musl (needed by std::vector for some reason)
//
//// begin various windows entry points
////void entry(int argc, char** argv);
////int main(int argc, char** argv) { entry(argc, argv); }
////int WINAPI WinMain (
//// _In_ HINSTANCE hInstance,
//// _In_opt_ HINSTANCE hPrevInstance,
//// _In_ LPSTR lpCmdLine,
//// _In_ int nShowCmd
//// )
////{
//// auto argc = _ConvertCommandLineToArgcArgv();
//// entry(argc, _ppszArgv);
////}
////void DllMain() {}
//// end various windows entry points
//
//extern "C" int __cdecl isspace(int c){ return c == ' ' || (unsigned)c - '\t' < 5; }
//extern "C" int __cdecl isdigit(int c){ return (unsigned)c - '0' < 10; }
//extern "C" long __cdecl atol(const char * pstr)
//{
// int cCurr; // The current character.
// long lTotal; // The running total.
// int iIsNeg; // Holds the '-' sign.
//
// while(isspace(*pstr)){ ++pstr; } // Slide past any whitespace.
//
// // Get the current character.
// cCurr = *pstr++;
// // Save the negative sign.
// iIsNeg = cCurr;
// if (('-' == cCurr) || ('+' == cCurr))
// {
// // We have a sign, so skip it.
// cCurr = *pstr++;
// }
//
// // Initialize the total.
// lTotal = 0;
//
// // While we have digits, addem up.
// while (isdigit(cCurr))
// {
// // Add this digit to the total.
// lTotal = 10 * lTotal + (cCurr - '0');
// // Do the next character.
// cCurr = *pstr++;
// }
//
// // If we have a negative sign, convert the value.
// if ('-' == iIsNeg)
// {
// return (-lTotal);
// }
// else
// {
// return (lTotal);
// }
//}
//extern "C" int __cdecl atoi(const char * pstr)
//{
// return ((int)atol(pstr));
//}
//extern "C" char* __cdecl _strupr(char *s)
//{
// CharUpperBuff(s, lstrlen(s));
// return s;
//}
//extern "C" char* __cdecl _strlwr(char *s)
//{
// CharLowerBuff(s, lstrlen(s));
// return s;
//}
//extern "C" int __cdecl _strcmpi(const char *s1, const char *s2)
//{
// return lstrcmpi(s1, s2);
//}
//extern "C" int __cdecl _stricmp(const char *s1, const char *s2)
//{
// return lstrcmpi(s1, s2);
//}
//#define strupr _strupr
//#define strlwr _strlwr
#endif // end win32
#endif // end header guard
//#if defined(LIBC_HPP_IMPL)
// #pragma data_seg(".CRT$XCA")
// _PVFV __xc_a[] = { NULL };
//
// #pragma data_seg(".CRT$XCZ")
// _PVFV __xc_z[] = { NULL };
//
// #pragma data_seg() /* reset */
//
// #pragma comment(linker, "/merge:.CRT=.data")
//
// typedef void(__cdecl *_PVFV)(void);
//
// void __cdecl _initterm(
// _PVFV * pfbegin,
// _PVFV * pfend
// )
// {
// /*
// * walk the table of function pointers from the bottom up, until
// * the end is encountered. Do not skip the first entry. The initial
// * value of pfbegin points to the first valid entry. Do not try to
// * execute what pfend points to. Only entries before pfend are valid.
// */
// while (pfbegin < pfend)
// {
// // if current table entry is non-NULL, call thru it.
// if (*pfbegin != NULL)
// (**pfbegin)();
// ++pfbegin;
// }
// }
//
// static _PVFV * pf_atexitlist = 0;
// static unsigned max_atexitlist_entries = 0;
// static unsigned cur_atexitlist_entries = 0;
//
// void __cdecl _atexit_init(void)
// {
// max_atexitlist_entries = 32;
// pf_atexitlist = (_PVFV *)calloc(max_atexitlist_entries,
// sizeof(_PVFV*));
// }
//
// int __cdecl atexit(_PVFV func)
// {
// if (cur_atexitlist_entries < max_atexitlist_entries)
// {
// pf_atexitlist[cur_atexitlist_entries++] = func;
// return 0;
// }
//
// return -1;
// }
//
// void __cdecl _DoExit(void)
// {
// if (cur_atexitlist_entries)
// {
// _initterm(pf_atexitlist,
// // Use ptr math to find the end of the array
// pf_atexitlist + cur_atexitlist_entries);
// }
// }
//
// //namespace std
// //{
// // //_STD terminate();
// // [[noreturn]] _STD void terminate() noexcept {}
// //}
//
// extern "C" void __cdecl __std_terminate() {}
// extern "C" void __cdecl __CxxFrameHandler3() {}
//
// void __cdecl terminate() {}
//
// //namespace std
// //{
// //[[noreturn]] void ::std::terminate() noexcept {}
// //void __cdecl _std_terminate() noexcept {}
// // void __cdecl terminate() {}
// //}
//#endif
//int __cdecl printf(const wchar_t * format, ...)
//{
// wchar_t szBuff[4096];
// int retValue;
// DWORD cbWritten;
// va_list argptr;
//
// va_start( argptr, format );
// retValue = wvsprintf( szBuff, format, argptr );
// va_end( argptr );
//
// WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), szBuff, retValue,
// &cbWritten, 0 );
//
// return retValue;
//}
//// windows specific headers
//#define WIN32_LEAN_AND_MEAN
//#define NOMINMAX
//#include <windows.h>
//#include <exception>
//
//[[noreturn]] void terminate(){}
//
//namespace std
//{
//[[noreturn]] void terminate() noexcept;
//[[noreturn]] void terminate() ;
//}
//#pragma comment(linker, "/defaultlib:user32.lib")
//#pragma comment(linker, "/defaultlib:kernel32.lib")
//
// Modified version of the Visual C++ startup code. Simplified to
// make it easier to read. Only supports ANSI programs.
//
//void* memcpy(void* dest, const void* src, size_t n)
//{
// unsigned char* d = (unsigned char*)dest;
// const unsigned char* s = (const unsigned char*)src;
//
// for (; n; n--) *d++ = *s++;
// return dest;
//}
//#pragma warning(disable:4146) // hides msvc 'negative op on unsigned' warning
//void* memset(void *dest, int c, size_t n)
//{
// unsigned char* s = (unsigned char*)dest;
// size_t k;
//
// /* Fill head and tail with minimal branching. Each
// * conditional ensures that all the subsequently used
// * offsets are well-defined and in the dest region. */
// if (!n) return dest;
// s[0] = s[n - 1] = c;
// if (n <= 2) return dest;
// s[1] = s[n - 2] = c;
// s[2] = s[n - 3] = c;
// if (n <= 6) return dest;
// s[3] = s[n - 4] = c;
// if (n <= 8) return dest;
//
// /* Advance pointer to align it at a 4-byte boundary,
// * and truncate n to a multiple of 4. The previous code
// * already took care of any head/tail that get cut off
// * by the alignment. */
// k = -(uintptr_t)s & 3;
//
// //k = (-((uintptr_t)s)) & 3;
// s += k;
// n -= k;
// n &= -4;
//
// /* Pure C fallback with no aliasing violations. */
// for (; n; n--, s++) *s = c;
//
// return dest;
//}
//#pragma warning(default:4146)