This repository was archived by the owner on Feb 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsili.h
More file actions
12539 lines (10180 loc) · 366 KB
/
Copy pathsili.h
File metadata and controls
12539 lines (10180 loc) · 366 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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
sili.h - v0.3.0 - a general-purpose programming library to replace the C/C++ standard libraries.
===========================================================================
- YOU MUST DEFINE 'SI_IMPLEMENTATION' in EXACTLY _one_ C file that includes
this header, BEFORE the include like this:
#define SI_IMPLEMENTATION
#include "sili.h"
- All other files should just include the library without the #define macro.
- A C99 or C++20 plus environment is expected. If your compiler isn't able
to support either target, do not expect sili and its libraries to function.
- If you want to disable certain features, you can do:
- #define SI_NO_MEMORY
- #define SI_NO_ALLOCATOR
- #define SI_NO_ARRAY
- #define SI_NO_STRING
- #define SI_NO_OPTIONAL
- #define SI_NO_UNICODE
- #define SI_NO_CHAR
- #define SI_NO_HASHING
- #define SI_NO_MAP
- #define SI_NO_TIME
- #define SI_NO_BIT
- #define SI_NO_PRINT
- #define SI_NO_MATH
- #define SI_NO_SYSTEM
- #define SI_NO_VIRTUAL_MEMORY
- #define SI_NO_IO
- #define SI_NO_THREAD
- #define SI_NO_CPU
- #define SI_NO_DLL
- #define SI_NO_BENCHMARK
before the SI_IMPLEMENTATION macro, as well as before any other include of
`sili.h`.
- NOTE: Disabling certain features may or may not cause compiler errors for
certain spots that are dependent on them. Use this feature at your own risk.
===========================================================================
DOCUMENTATION
- All functions, constant variables and macros contain a comment with a
description of what they do above them, as well what they return (if anything).
Macros that act like functions specifically use a specific declaration format
because of their lack of typing.
That being:
/ argumentName - TYPE | otherArgumentName - KEYWORD | ...VALUES - TYPE*
description of the macro. /
#define smth(argumentName, otherArgumentName, .../ VALUES/)
- More often than not a macro's argument will not be a specific type and
instead some sort of keyword. In practice they're declared as fully capitalized
words (eg. UINT). These keywords denote that the required argument for the
macro can be anything as long as it fufills the original word's connotation.
- General list of the macro keywords, their meanings and examples of them:
- TYPE - any type name (siString, usize, void*).
- TYPE* - any type name's pointer type (siString*, usize*, void**).
- INT - any signed integer (50, -250LL, ISIZE_MAX).
- UINT - any unsigned integer (50, 250ULL, USIZE_MAX).
- FUNCTION - any function name that's visibly-exposed to the compiler.
- EXPRESSION - any legal C expression (60, "hello", SI_RGB(255, 255, 255)).
- VARIADIC - an indefinite amount of legal C expressions ("firstValue", 230, "third")
- NAME - any regular text with no enquotes (test, var, len).
- VAR - any variable that's visible-exposed to the compiler.
- ANYTHING - anything that compiles without error.
===========================================================================
MACROS
- For any of the macros to work, you must _always_ define it before including
the library. Example:
```c
#define SI_IMPLEMENTATION
#define SI_RELEASE_MODE
#include "sili.h"
```
- SI_RELEASE_MODE - disables certain functionality that decrease performance
(such as logging, error reporting, assertions, etc). Defining "NDEBUG" does
the same thing.
- SI_NO_ASSERTIONS - all 'SI_ASSERT' functions get disabled entirely. 'SI_PANIC'
functions still function.
- SI_NO_ERROR_STRUCT - strips the 'siError' structure's members down to just
the error code member. This gets turned on automatically via 'SI_RELEASE_MODE'.
- SI_NO_ERROR_LOGS - disables sili's automatic logging for when a 'SI_ERROR'
is declared. This gets turned on automatically via 'SI_RELEASE_MODE'.
- SI_NO_WINDOWS_H - disables the inclusion of the win32 API inside the header.
===========================================================================
CREDITS
- Ginger Bill's 'gb.h' (https://github.com//gingerBill/gb) - inspired me to
make the Sili Toolchain, as well as certain features were taken from the
library and implemented here.
LICENSE
- This software is licensed under the zlib license (see the LICENSE at the
bottom of the file).
WARNING
- Sili and its supplementary libraries are designed to be fast, modern, but
also experimental. As a result some unwarranted results may occur during use,
such as:
1) Features not working as expected;
2) Functions having no or incompleted documentation;
3) API breaking changes between releases (especially before v1.0.0 release);
4) Little to no security checks for malicious code that attempt to exploit
parts of the code.
*/
#if (_MSC_VER >= 1020) || ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
#pragma once
#endif
#ifndef SI_INCLUDE_SI_H
#define SI_INCLUDE_SI_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef SI_VERSION_MAJOR
#define SI_VERSION_MAJOR 0
#define SI_VERSION_MINOR 3
#define SI_VERSION_PATCH 0
#endif
#define SI_VERSION_CURRENT SI_VERSION(SI_VERSION_MAJOR, SI_VERSION_MINOR, SI_VERSION_PATCH)
/* major - UINT | minor - UINT | patch - UINT
* Combines major, minor, and patch version numbers into a single integer. */
#define SI_VERSION(major, minor, patch) (major * 10000 + minor * 100 + patch)
#ifndef SI_EXTERN
#ifdef __cplusplus
#define SI_EXTERN extern "C"
#else
#define SI_EXTERN extern
#endif
#endif
#if defined(__EMSCRIPTEN__) || defined(__wasm) || defined(__wasm32) || defined(__wasm32__) \
|| defined(__wasm__) || defined(__WASM__) || defined(__wasm64__)
#ifdef __EMSCRIPTEN__
#define SI_SYSTEM_EMSCRIPTEN 1
#else
#define SI_SYSTEM_WASI 1
#define SI_NO_CRT
#endif
#define SI_SYSTEM_STR "WebAssembly"
#define SI_SYSTEM_IS_WASM 1
#elif defined(_WIN32) || defined(_WIN64) || (defined(__CYGWIN__) && !defined(_WIN32))
#define SI_SYSTEM_WINDOWS 1
#define SI_SYSTEM_STR "Windows"
#define SI_SYSTEM_IS_WINDOWS 1
#elif defined(__APPLE__) && defined(__MACH__)
#include <TargetConditionals.h>
#define SI_SYSTEM_IS_APPLE 1
#define SI_SYSTEM_IS_BSD 1
#if TARGET_OS_SIMULATOR == 1 || TARGET_OS_IPHONE == 1
#define SI_SYSTEM_IOS 1
#define SI_SYSTEM_STR "iOS"
#elif TARGET_OS_MAC == 1
#define SI_SYSTEM_OSX 1
#define SI_SYSTEM_STR "MacOS"
#endif
#elif defined(__ANDROID__)
#define SI_SYSTEM_ANDROID 1
#define SI_SYSTEM_STR "Android"
#define SI_SYSTEM_IS_UNIX 1
#elif defined(__linux__)
#define SI_SYSTEM_LINUX 1
#define SI_SYSTEM_STR "Linux"
#define SI_SYSTEM_IS_UNIX 1
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
#define SI_SYSTEM_FREEBSD
#define SI_SYSTEM_STR "FreeBSD"
#define SI_SYSTEM_IS_UNIX 1
#define SI_SYSTEM_IS_BSD 1
#elif defined(__OpenBSD__)
#define SI_SYSTEM_OPENBSD
#define SI_SYSTEM_STR "OpenBSD"
#define SI_SYSTEM_IS_UNIX 1
#define SI_SYSTEM_IS_BSD 1
#elif defined(unix) || defined(__unix__) || defined(__unix)
#define SI_SYSTEM_UNIX_OTHER 1
#define SI_SYSTEM_STR "Unix"
#define SI_SYSTEM_IS_UNIX 1
#else
#define SI_SYSTEM_UNKNOWN 1
#define SI_SYSTEM_STR "Unknown"
#endif
#if defined(__GNUC__) && !defined(__clang__)
#define SI_COMPILER_GCC 1
#define SI_COMPILER_STR "GCC"
#if defined(__GNUC_PATCHLEVEL__)
#define SI_COMPILER_VERSION SI_VERSION(__GNUC__, __GNUC_MINOR__, __GNUC__PATCHLEVEL__)
#else
#define SI_COMPILER_VERSION SI_VERSION(__GNUC__, __GNUC_MINOR__, 0)
#endif
#elif defined(_MSC_VER)
#define SI_COMPILER_MSVC 1
#define SI_COMPILER_STR "MSVC"
#define SI_COMPILER_VERSION SI_VERSION(_MSC_VER / 100, _MSC_VER % 100, 0)
#elif defined(__clang__)
#define SI_COMPILER_CLANG 1
#define SI_COMPILER_STR "Clang"
#define SI_COMPILER_VERSION SI_VERSION(__clang_major__, __clang_minor__, __clang_patchlevel__)
#elif defined(__TINYC__)
#define SI_COMPILER_TCC 1
#define SI_COMPILER_STR "Tiny C Compiler"
#define SI_COMPILER_VERSION SI_VERSION(0, __TINYC__ / 100, __TINYC__ % 100)
#else
#define SI_COMPILER_UNKNOWN 1
#define SI_COMPILER_STR "Unknown"
#define SI_COMPILER_VERSION SI_VERSION(0, 0, 0)
#endif
/* compiler - NAME | major - UINT | minor - UINT | patch - UINT
* A pre-processor condition to check if the current compiler version passes
* the minimum version. */
#define SI_COMPILER_CHECK_MIN(compiler, major, minor, patch) \
(SI_COMPILER_ ## compiler && SI_COMPILER_VERSION >= SI_VERSION(major, minor, patch))
/* compiler - NAME | major - UINT | minor - UINT | patch - UINT
* A pre-processor condition to check if the current compiler version is less than
* or is equal to the maximum version. */
#define SI_COMPILER_CHECK_MAX(compiler, major, minor, patch) \
(SI_COMPILER_ ## compiler && SI_COMPILER_VERSION <= SI_VERSION(major, minor, patch))
/* Checks if the currently-used compiler supports '#pragma once' functinoality. */
#define SI_COMPILER_HAS_PRAGMA_ONCE (SI_COMPILER_CLANG || SI_COMPILER_MSVC || SI_COMPILER_CHECK_MIN(GCC, 3, 4, 0))
#ifdef __cplusplus
#ifdef __OBJC__
#define SI_LANGUAGE_OBJ_CPP 1
#define SI_LANGUAGE_STR "Objective-C++"
#else
#define SI_LANGUAGE_CPP 1
#define SI_LANGUAGE_STR "C++"
#endif
#define SI_LANGUAGE_IS_CPP 1
#else
#ifdef __OBJC__
#define SI_LANGUAGE_OBJ_C 1
#define SI_LANGUAGE_STR "Objective-C"
#elif defined(__cplus__)
#define SI_LANGUAGE_CPLUS 1
#define SI_LANGUAGE_STR "C-Plus"
#else
#define SI_LANGUAGE_C 1
#define SI_LANGUAGE_STR "C"
#endif
#define SI_LANGUAGE_IS_C 1
#endif
#if SI_LANGUAGE_IS_C
#ifndef SI_STANDARD_VERSION
#if defined(SI_COMPILER_GCC) && __STDC_VERSION__ == 202200L
#define SI_STANDARD_VERSION SI_STANDARD_C23
#elif defined(__STDC_VERSION__)
#define SI_STANDARD_VERSION __STDC_VERSION__
#else
#define SI_STANDARD_VERSION 198900L
#endif
#endif
#define SI_STANDARD_C99 199901l
#define SI_STANDARD_C11 201112L
#define SI_STANDARD_C17 201710L
#define SI_STANDARD_C23 202311L
#else
#ifndef SI_STANDARD_VERSION
#define SI_STANDARD_VERSION __cplusplus
#endif
#define SI_STANDARD_CPP20 202002L
#define SI_STANDARD_CPP23 202302L
#endif
/* language - NAME | standard - NAME
* A pre-processor condition to check if the current language's standard version
* passes the minimum version. */
#define SI_STANDARD_CHECK_MIN(language, standard) \
(SI_LANGUAGE_IS_ ## language && SI_STANDARD_VERSION >= SI_STANDARD_ ## standard)
/* language - NAME | standard - NAME
* A pre-processor condition to check if the current language's standard version
* is less than or is equal to the maximum version. */
#define SI_STANDARD_CHECK_MAX(language, standard) \
(SI_LANGUAGE_IS_ ## language && SI_STANDARD_VERSION <= SI_STANDARD_ ## standard)
#ifndef SI_ARCH_STR
#if defined(__i386__) || defined(__i386) || defined(i386) || defined(__IA32__) \
|| defined(__X86__) || defined(_M_IX86) || defined(_M_I386) || defined(_X86_) \
|| defined(__THW_INTEL__) || defined(__I86__) || defined(__INTEL__) || defined(__386)
#define SI_ARCH_I386 1
#define SI_ARCH_STR "i386"
#define SI_ARCH_IS_X86 1
#define SI_ARCH_IS_32BIT 1
#elif defined(_M_X64) || defined(_M_AMD64) || defined(__x86_64__) || defined(__amd64__) \
|| defined(__amd64) || defined(__x86_64)
#define SI_ARCH_AMD64 1
#define SI_ARCH_STR "amd64"
#define SI_ARCH_IS_X86 1
#define SI_ARCH_IS_64BIT 1
#elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || defined(_ARCH_PPC64)
#define SI_ARCH_PPC64 1
#define SI_ARCH_STR "PowerPC 64-bit"
#define SI_ARCH_IS_PPC 1
#define SI_ARCH_IS_64BIT 1
#elif defined(__powerpc__) || defined(__powerpc) || defined(__POWERPC__) || defined(__ppc__) \
|| defined(__PPC__) || defined(_M_PPC) || defined(_ARCH_PPC) || defined(__PPCGECKO__) \
|| defined(__PPCBROADWAY__) || defined(_XENON) || defined(__ppc)
#define SI_ARCH_PPC32 1
#define SI_ARCH_STR "PowerPC 32-bit"
#define SI_ARCH_IS_PPC 1
#define SI_ARCH_IS_32BIT 1
#elif defined(__aarch64__) || defined(_M_ARM64)
#define SI_ARCH_ARM64 1
#define SI_ARCH_STR "ARM64"
#define SI_ARCH_IS_ARM 1
#define SI_ARCH_IS_64BIT 1
#elif defined(__arm__) || defined(__thumb__) || defined(__TARGET_ARCH_ARM) \
|| defined(__TARGET_ARCH_THUMB) || defined(_ARM) || defined(_M_ARM) || defined(_M_ARMT) \
|| defined(__arm)
#define SI_ARCH_ARM32 1
#define SI_ARCH_STR "ARM32"
#define SI_ARCH_IS_ARM 1
#define SI_ARCH_IS_32BIT 1
#elif defined(__riscv)
#if __riscv_xlen == 32
#define SI_ARCH_RISC_V32 1
#define SI_ARCH_STR "RISC-V 32-bit"
#define SI_ARCH_IS_32BIT 1
#elif __riscv_xlen == 64
#define SI_ARCH_RISC_V64 1
#define SI_ARCH_STR "RISC-V 64-bit"
#define SI_ARCH_IS_64BIT 1
#endif
#define SI_ARCH_IS_RISC 1
#elif SI_SYSTEM_IS_WASM
#if defined(__wasm32__) || defined(__EMSCRIPTEN_32BIT__)
#define SI_ARCH_WASM32 1
#define SI_ARCH_STR "WASM 32-bit"
#define SI_ARCH_IS_32BIT 1
#elif defined(__wasm64__) || defined(__EMSCRIPTEN_64BIT__)
#define SI_ARCH_WASM64 1
#define SI_ARCH_STR "WASM 64-bit"
#define SI_ARCH_IS_64BIT 1
#endif
#define SI_ARCH_IS_WASM 1
#else
#define SI_ARCH_UNKNOWN 1
#define SI_ARCH_STR "Unknown"
#define SI_ARCH_IS_32BIT 1 /* Assume that stuff is 32-bit for the sake of it. */
#warning "Expect errors."
#endif
#endif /* #ifndef SI_ARCH_STR */
#if !defined(SI_ENDIAN_STR) && (!defined(SI_ENDIAN_IS_LITTLE) || !defined(SI_ENDIAN_IS_BIG))
#if SI_COMPILER_GCC || SI_COMPILER_CLANG || SI_COMPILER_TCC
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define SI_ENDIAN_IS_LITTLE 1
#define SI_ENDIAN_STR "Little-endian"
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define SI_ENDIAN_IS_BIG 1
#define SI_ENDIAN_STR "Big-endian"
#else
#define SI_ENDIAN_UNKNOWN 1
#define SI_ENDIAN_STR "Unknown"
#warning "Expect undefined behaviour"
#endif
#else
/* NOTE(EimaMei): A "portable"(?) way of checking endianness at compile
* time.
*
* Rationale: In the C89 specification, it states that multi-chars are
* legal but left up for implementation. This results in most compilers
* just appending the characters since that's cheap and fast, but at the
* cost of it it introduces endianness issues, which is why (I presume)
* GCC complains about it.
*
* Due to this, it's more or less somewhat safe to assume that if a
* multi-char is created in that way, then you can also assume the endian
* at compile time too! */
#if 'ABCD' == 0x41424344UL
#define SI_ENDIAN_IS_LITTLE 1
#define SI_ENDIAN_STR "Little-endian"
#elif 'ABCD' == 0x44434241UL
#define SI_ENDIAN_IS_BIG 1
#define SI_ENDIAN_STR "Big-endian"
#else
#define SI_ENDIAN_UNKNOWN 1
#define SI_ENDIAN_STR "Unknown"
#warning "Expect undefined behaviour"
#endif
#endif
#endif
#if defined(SI_EXPORT) || defined(SI_IMPORT)
#if SI_SYSTEM_IS_WINDOWS
#if SI_COMPILER_TCC
#define __declspec(x) __attribute__((x))
#endif
#if defined(SI_EXPORT)
#define SIDEF __declspec(dllexport)
#else
#define SIDEF __declspec(dllimport)
#endif
#elif defined(SI_EXPORT)
#define SIDEF __attribute__((visibility("default")))
#endif
#endif
#ifndef SIDEF
#define SIDEF extern
#endif
#ifndef SI_STATIC_ASSERT_MSG
#if SI_STANDARD_CHECK_MIN(C, C11)
/* condition - EXPRESSIONG | msg - cstring
* Stops the program from being compiled if the condition isn't met with
* an explanation as to why. */
#define SI_STATIC_ASSERT_MSG(condition, msg) _Static_assert(condition, msg)
#elif SI_LANGUAGE_IS_CPP
/* condition - EXPRESSIONG | msg - cstring
* Stops the program from being compiled if the condition isn't met with
* an explanation as to why. */
#define SI_STATIC_ASSERT_MSG(condition, msg) static_assert(condition, msg)
#else
/* condition - EXPRESSIONG | msg - cstring
* Stops the program from being compiled if the condition isn't met with
* an explanation as to why. */
#define SI_STATIC_ASSERT_MSG(condition, msg) SI_STATIC_ASSERT_MSG1(condition, msg, __LINE__)
#define SI_STATIC_ASSERT_MSG1(condition, msg, line) SI_STATIC_ASSERT_MSG2(condition, msg, line)
#define SI_STATIC_ASSERT_MSG2(condition, msg, line) \
typedef char __si_error_if_negative_##line[(condition) ? 1 : -1]
#endif
#endif
/* condition - EXPRESSIONG
* Stops the program from being compiled if the condition isn't met. */
#define SI_STATIC_ASSERT(condition) SI_STATIC_ASSERT_MSG(condition, "")
/* == SUPPORT CHECKS == */
#if SI_LANGUAGE_IS_C
SI_STATIC_ASSERT_MSG(SI_STANDARD_VERSION >= SI_STANDARD_C99, "Sili does not support C targets lower than C99.");
#elif SI_LANGUAGE_IS_CPP
SI_STATIC_ASSERT_MSG(SI_STANDARD_VERSION >= SI_STANDARD_CPP20, "Sili does not support C++ targets lower than C++20.");
#endif
#if SI_SYSTEM_IS_UNIX || SI_SYSTEM_IS_APPLE || SI_SYSTEM_EMSCRIPTEN
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <stdlib.h>
#include <unistd.h>
#if !defined(SI_NO_MEMORY) && !defined(SI_NO_CRT)
#include <memory.h>
#endif
#ifndef SI_NO_VIRTUAL_MEMORY
#include <sys/mman.h>
#endif
#ifndef SI_NO_SYSTEM
#include <errno.h>
#endif
#ifndef SI_NO_IO
#include <fcntl.h>
#include <sys/stat.h>
#include <dirent.h>
#endif
#ifndef SI_NO_THREAD
#include <pthread.h>
#endif
#ifndef SI_NO_PRINT
#include <stdarg.h>
#endif
#ifndef SI_NO_DLL
#include <dlfcn.h>
#endif
#elif SI_SYSTEM_IS_WINDOWS
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef SI_NO_WINDOWS_H
#define NOMINMAX 1
#define WIN32_LEAN_AND_MEAN 1
#define WIN32_MEAN_AND_LEAN 1
#define VC_EXTRALEAN 1
#include <windows.h>
#include <aclapi.h>
#include <shellapi.h>
#undef NOMINMAX
#undef WIN32_LEAN_AND_MEAN
#undef WIN32_MEAN_AND_LEAN
#undef VC_EXTRALEAN
#endif
#if SI_COMPILER_MSVC
#include <malloc.h>
#include <intrin.h>
#endif
#endif
#if SI_SYSTEM_IS_APPLE
#include <sys/socket.h>
#include <sys/sysctl.h>
#elif SI_SYSTEM_IS_UNIX
#include <sys/sendfile.h>
#elif SI_SYSTEM_EMSCRIPTEN
#include <emscripten/emscripten.h>
#include <wasi/api.h>
#elif SI_SYSTEM_WASI
#ifndef SI_NO_PRINT
#include <stdarg.h>
#endif
#include <wasi/api.h>
#endif
#if defined(SI_RELEASE_MODE) || defined(NDEBUG)
#undef SI_NO_ASSERTIONS
#undef SI_NO_ERROR_LOGS
#undef SI_NO_ERROR_STRUCT
#define SI_NO_ASSERTIONS
#define SI_NO_ALLOC_DEBUG_INFO
#define SI_NO_ERROR_LOGS
#define SI_NO_ERROR_STRUCT
#endif
#ifndef SI_NO_TYPE_DEFS
#include <stdint.h>
#include <stddef.h>
typedef uint8_t u8;
typedef int8_t i8;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint32_t u32;
typedef int32_t i32;
typedef uint64_t u64;
typedef int64_t i64;
typedef size_t usize;
typedef ptrdiff_t isize;
typedef float f32;
typedef double f64;
typedef u8 b8;
typedef u16 b16;
typedef u32 b32;
typedef u64 b64;
#if !defined(bool) && SI_STANDARD_CHECK_MAX(C, C17)
typedef b8 bool;
#endif
#endif
SI_STATIC_ASSERT(sizeof(u8) == sizeof(i8));
SI_STATIC_ASSERT(sizeof(u16) == sizeof(i16));
SI_STATIC_ASSERT(sizeof(u32) == sizeof(i32));
SI_STATIC_ASSERT(sizeof(u64) == sizeof(i64));
SI_STATIC_ASSERT(sizeof(usize) == sizeof(isize));
SI_STATIC_ASSERT(sizeof(bool) == 1);
SI_STATIC_ASSERT(sizeof(u8) == 1);
SI_STATIC_ASSERT(sizeof(u16) == 2);
SI_STATIC_ASSERT(sizeof(u32) == 4);
SI_STATIC_ASSERT(sizeof(u64) == 8);
#if SI_ARCH_IS_32BIT
SI_STATIC_ASSERT(sizeof(usize) == 4);
#else
SI_STATIC_ASSERT(sizeof(usize) == 8);
#endif
SI_STATIC_ASSERT(sizeof(f32) == 4);
SI_STATIC_ASSERT(sizeof(f64) == 8);
#if SI_ARCH_IS_64BIT
#ifndef USIZE_MAX
#define USIZE_MAX (usize)(UINT64_MAX)
#endif
#ifndef ISIZE_MAX
#define ISIZE_MAX (isize)(INT64_MAX)
#endif
#ifndef ISIZE_MIN
#define ISIZE_MIN (isize)(INT64_MIN)
#endif
#else
#ifndef USIZE_MAX
#define USIZE_MAX (usize)(UINT32_MAX)
#endif
#ifndef ISIZE_MAX
#define ISIZE_MAX (isize)(INT32_MAX)
#endif
#ifndef ISIZE_MIN
#define ISIZE_MIN (isize)(INT32_MIN)
#endif
#endif
#ifndef FLOAT32_MIN
#define FLOAT32_MIN 1.17549435e-38f
#endif
#ifndef FLOAT32_MAX
#define FLOAT32_MAX 3.40282347e+38f
#endif
#ifndef FLOAT64_MIN
#define FLOAT64_MIN 2.2250738585072014e-308
#endif
#ifndef FLOAT64_MAX
#define FLOAT64_MAX 1.7976931348623157e+308
#endif
#if SI_STANDARD_CHECK_MAX(C, C17)
#ifndef true
#define true (1)
#endif
#ifndef false
#define false (0)
#endif
#endif
SI_STATIC_ASSERT(sizeof(bool) == 1);
SI_STATIC_ASSERT(sizeof(b8) == 1);
SI_STATIC_ASSERT(sizeof(b16) == 2);
SI_STATIC_ASSERT(sizeof(b32) == 4);
SI_STATIC_ASSERT(sizeof(b64) == 8);
SI_STATIC_ASSERT(true == 1);
SI_STATIC_ASSERT(false == 0);
#ifndef force_inline
#if SI_COMPILER_MSVC
#define force_inline __forceinline
#elif SI_COMPILER_CHECK_MIN(GCC, 3, 1, 0) || SI_COMPILER_CHECK_MIN(CLANG, 2, 6, 0)
#define force_inline static inline __attribute__((always_inline))
#else
#define force_inline static inline
#endif
#endif
#ifndef siIntern
#define siIntern static
#endif
#if SI_STANDARD_CHECK_MIN(C, C23) || SI_LANGUAGE_IS_CPP
/* Specifies a fallthrough for the compiler. */
#define siFallthrough [[fallthrough]]
#elif SI_COMPILER_CHECK_MIN(GCC, 7, 0, 0) || SI_COMPILER_CHECK_MIN(CLANG, 3, 5, 0)
/* Specifies a fallthrough for the compiler. */
#define siFallthrough __attribute__((fallthrough))
#else
/* Specifies a fallthrough for the compiler. */
#define siFallthrough do {} while (0)
#endif
#ifndef restrict
#if SI_LANGUAGE_IS_CPP
#if SI_COMPILER_CLANG || SI_COMPILER_GCC
#define restrict __restrict
#elif SI_COMPILER_CHECK_MIN(MSVC, 14, 0, 0)
#define restrict __restrict
#else
#define restrict
#endif
#endif
#endif
#ifndef siNoreturn
#if SI_STANDARD_CHECK_MIN(C, C23) || SI_LANGUAGE_IS_CPP
/* Specifies that the function will not return anything. */
#define siNoreturn [[noreturn]]
#elif SI_STANDARD_CHECK_MIN(C, C11)
/* Specifies that the function will not return anything. */
#define siNoreturn _Noreturn
#else
#if defined(SI_COMPILER_CLANG) || SI_COMPILER_CHECK_MIN(GCC, 2, 5, 0)
/* Specifies that the function will not return anything. */
#define siNoreturn __attribute__((noreturn))
#elif defined(SI_COMPILER_MSVC)
/* Specifies that the function will not return anything. */
#define siNoreturn __declspec(noreturn)
#else
/* Specifies that the function will not return anything. */
#define siNoreturn
#endif
#endif
#endif
#if SI_STANDARD_CHECK_MIN(C, C11)
/* memberName - NAME | unionMembers - TYPE NAME;<...>
* Defines an anonymous union, whose members can be accessed either through
* '<var>.<memberName>.<member>' or '<var>.<member>'. The later only compiles
* on C11 and onwards. */
#define union_anonymous(memberName, .../*unionMembers*/) \
union { \
__VA_ARGS__ \
union { __VA_ARGS__ } memberName; \
}
#else
/* memberName - NAME | unionMembers - TYPE NAME;<...>
* Defines an anonymous union, whose members can be accessed either through
* '<var>.<memberName>.<member>' or '<var>.<member>'. The later only compiles
* on C11 and onwards. */
#define union_anonymous(memberName, .../*unionMembers*/) \
union { \
__VA_ARGS__ \
} memberName
#endif
#ifndef cstring
/* An immutable NULL-terminated C-string type. */
typedef const char* cstring;
#endif
/*
========================
| Constants |
========================
*/
/* x - INT.
* Converts kilobytes into bytes (JEDEC). */
#define SI_KILO(x) ((x) * 1024)
/* x - INT.
* Converts megabytes into bytes (JEDEC). */
#define SI_MEGA(x) (SI_KILO(x) * 1024)
/* x - INT.
* Converts gigabytes into bytes (JEDEC). */
#define SI_GIGA(x) (SI_MEGA(x) * 1024)
/* x - INT.
* Converts terabytes into bytes (JEDEC). */
#define SI_TERA(x) (SI_GIGA(x) * 1024LL)
/* x - UINT
* Shortcut for (1ull << x). */
#define SI_BIT(x) (1ULL << (x))
#ifndef nil
#if SI_LANGUAGE_IS_CPP
/* NULL value. */
#define nil nullptr
#else
/* NULL value. */
#define nil ((void*)0)
#endif
#endif
SI_STATIC_ASSERT(sizeof(nil) == sizeof(void*));
/*
========================
| Declaration macros |
========================
*/
/* type - TYPE | name - NAME
* Defines the enum with the given integer type. */
#define SI_ENUM(type, name) typedef type name; enum
/* ...x - ANYTHING
* Silences the 'unused identifier' warning for the specified expression. */
#define SI_UNUSED(.../* x */) (void)(__VA_ARGS__)
/* ...x - ANYTHING
* Discards the function's return and silences the 'unused return value' warning. */
#define SI_DISCARD(.../* x */) (void)!(__VA_ARGS__)
/*
========================
| Operators |
========================
*/
#ifndef si_sizeof
/* ...expr - EXPRESSION
* Returns the size of the expression (in isize). */
#define si_sizeof(.../* expr */) ((isize)sizeof(__VA_ARGS__))
#endif
#ifndef countof
/* ...value - ARRAY
* Gets the static length of the given value (must be an array). */
#define countof(.../*value*/) (isize)(sizeof(__VA_ARGS__) / sizeof((__VA_ARGS__)[0]))
#endif
#ifndef countof_str
/* ...value - ARRAY
* Gets the length of a static NULL-terminated C-string.*/
#define countof_str(.../* value */) (countof(__VA_ARGS__) - 1)
#endif
SI_STATIC_ASSERT(countof("abcd") == 5);
SI_STATIC_ASSERT(countof_str("abcd") == 4);
#ifndef offsetof
#if SI_COMPILER_CHECK_MIN(GCC, 3, 4, 0) || SI_COMPILER_CHECK_MIN(CLANG, 2, 6, 0)
/* type - STRUCT TYPE | element - TYPE's member
* Returns the offset of the specified member. */
#define offsetof(type, element) __builtin_offsetof(type, element)
#else
/* type - STRUCT TYPE | element - TYPE's member
* Returns the offset of the specified member. */
#define offsetof(type, element) ((isize)&(((type*)nil)->element))
#endif
#endif
#if !defined(alignof) && SI_LANGUAGE_IS_C
#if SI_STANDARD_CHECK_MIN(C, C11)
/* type - TYPE
* Gets the alignment of a type. */
#define alignof(type) _Alignof(type)
#elif SI_COMPILER_GCC || SI_COMPILER_CLANG
/* type - TYPE
* Gets the alignment of a type. */
#define alignof(type) __alignof__(type)
#else
/* type - TYPE
* Gets the alignment of a type. */
#define alignof(type) offsetof(struct { char c; type member; }, member)
#endif
#endif
/*
========================
| Casting |
========================
*/
#if SI_LANGUAGE_IS_C
/* type - TYPE | value - EXPRESSION | valueType - TYPE
* Type prunes a value with the specified type. */
#define si_transmute(type, value, valueType) ((union { valueType in; type out; }){value}.out)
#else
/* type - TYPE | value - EXPRESSION | valueType - TYPE
* Type prunes a value with the specified type. */
#define si_transmute(type, value, valueType) si_cppTransmute<type, valueType>(value)
extern "C++" {
template<typename type, typename valueType>
type si_cppTransmute(valueType value) {
union { valueType in; type out; } res = {value};
return res.out;
}
}
#endif
/* ptr - void*
* Converts the pointer's value into an u8. */
#define SI_TO_U8(ptr) (*si_transmute(u8*, (const void*)(ptr), const void*))
/* ptr - void*
* Converts the pointer's value into an u16. */
#define SI_TO_U16(ptr) (*si_transmute(u16*, (const void*)(ptr), const void*))
/* ptr - void*
* Converts the pointer's value into an u32. */
#define SI_TO_U32(ptr) (*si_transmute(u32*, (const void*)(ptr), const void*))
/* ptr - void*
* Converts the pointer's value into an u64. */
#define SI_TO_U64(ptr) (*si_transmute(u64*, (const void*)(ptr), const void*))
/*
========================
| General macros |
========================
*/
#ifndef si_swapEx
#ifndef SI_COMPILER_MSVC
#define si_swapEx(a, b, typeSize) do { \
SI_ASSERT(si_sizeof(a) == si_sizeof(b)); \
SI_ASSERT(si_sizeof(a) == typeSize); \
u8 tmp[typeSize]; \
si_memcopy(tmp, &(a), typeSize); \
si_memcopy(&(a), &(b), typeSize); \
si_memcopy(&(b), tmp, typeSize); \
} while (0)
#else /* NOTE(EimaMei): MSVC is dumb, for some reason the 2nd variable does
* not get swapped if 'memcpy' isn't used for /O2. */
#define si_swapEx(a, b, typeSize) do { \
SI_ASSERT(si_sizeof(a) == si_sizeof(b)); \
SI_ASSERT(si_sizeof(a) == typeSize); \
u8 tmp[typeSize]; \
memcpy(tmp, &(a), typeSize); \
memcpy(&(a), &(b), typeSize); \
memcpy(&(b), tmp, typeSize); \
} while (0)
#endif
#endif
/* a - VARIABLE | b - VARIABLE
* Swaps the value of 'a' with 'b'; 'b' with 'a'. */
#define si_swap(a, b) si_swapEx(a, b, si_sizeof(a))
#ifndef for_range
/* countvar - NAME | start - INT | end - INT
* A loop with that goes through 'end' - 'start' cycles whilst incrementing 'countVar'
* each time. */
#define for_range(countVar, start, end) for_rangeEx(isize, countVar, start, end)
#endif