-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc.h
More file actions
4537 lines (3707 loc) · 131 KB
/
Copy pathlc.h
File metadata and controls
4537 lines (3707 loc) · 131 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
#ifndef LC_INCLUDE_H
#define LC_INCLUDE_H
// vim: set ft=c:
/// Table of content
///
/// @lc_toc_begin
/// - Preprocessor ..................................................8. [LC_XPP]
/// - Feature detection ...........................................313. [LC_XFD]
/// - Types .......................................................100. [LC_XTY]
/// - Platform code ................................................19. [LC_XPL]
/// - Utility macro ................................................95. [LC_XUM]
/// - Math .........................................................34. [LC_XMT]
/// - Relative pointer .............................................48. [LC_XRP]
/// - Ordering ....................................................143. [LC_XOD]
/// - Assert .......................................................71. [LC_XAS]
/// - Slice .......................................................108. [LC_XSC]
/// - Source location ..............................................40. [LC_XSL]
/// - Allocator ...................................................448. [LC_XAC]
/// - ANSI Escape Code .............................................54. [LC_XAE]
/// - Time ........................................................191. [LC_XCH]
/// - String view .................................................272. [LC_XSV]
/// - Logger ......................................................351. [LC_XLG]
/// - Context ......................................................28. [LC_XCT]
/// - Input/Output ................................................274. [LC_XIO]
/// - Input/Output implementation ................................1068. [LC_XIH]
/// - Vector .......................................................77. [LC_XVC]
/// - Strings .....................................................118. [LC_XSS]
/// - Path ........................................................188. [LC_XPH]
/// - Dynamic library ..............................................34. [LC_XDL]
/// - Bit stream ..................................................236. [LC_XBS]
/// - Endian .......................................................91. [LC_XED]
/// - Compression ..................................................11. [LC_XCM]
/// - Base64 .......................................................78. [LC_XBC]
/// @lc_toc_end
////////////////////////////////////////////////////////////////////////////////
/// Preprocessor [LC_XPP] ///
////////////////////////////////////////////////////////////////////////////////
#define LC_PP_STR(x) #x
#define LC_PP_CONCAT(x, y) x##y
#define LC_PP_DEFER(PP, ...) PP(__VA_ARGS__)
////////////////////////////////////////////////////////////////////////////////
/// Feature detection [LC_XFD] ///
////////////////////////////////////////////////////////////////////////////////
#ifdef __AVX__
#ifdef LC_ENABLE_AVX
#include <immintrin.h>
#define LC_CPU_AVX
#endif
#endif
// TODO: Detect architecture
#if __amd64__
#define LC_ARCH_AMD64
#endif
#ifdef __cplusplus
#define LC_STD_CXX
#endif
#define LC_STDC_C95 199409L
#define LC_STDC_C99 199901L
#define LC_STDC_C11 201112L
#define LC_STDC_C17 201710L
#if __clang_major__ <= 17 // C23 on clang 17
#define LC_STDC_C23 202000L
#else
#define LC_STDC_C23 202311L
#endif
#if defined(__GNUC__)
#define LC_CC_GCC 1
#else
#define LC_CC_GCC 0
#endif
#if defined(__clang__)
#define LC_CC_LLVM 1
#else
#define LC_CC_LLVM 0
#endif
#define LC_CC_GNU (LC_CC_GCC | LC_CC_LLVM)
#ifndef LC_STD_CXX
#if __STDC_VERSION__ < LC_STDC_C11
#if LC_CC_GNU
#define alignas _Alignas
#define alignof __alignof__
#else
#define alignas(x)
#define alignof(x)
#endif
#elif __STDC_VERSION__ < LC_STDC_C23
#define alignas _Alignas
#define alignof _Alignof
#endif
#endif
#if __STDC_VERSION__ < LC_STDC_C23
#include <stdbool.h>
#endif
#if LC_CC_GNU
#define LC_PACKED __attribute__((__packed__))
#define LC_HAVE_PACKED
#endif
#if __STDC_VERSION__ >= LC_STDC_C23
#define LC_HAVE_TYPEOF
#elif LC_CC_GNU
#define typeof __typeof__
#define LC_HAVE_TYPEOF
#endif
#ifndef LC_STD_CXX
#if __STDC_VERSION__ < LC_STDC_C11
#if LC_CC_GNU
#define static_assert _Static_assert
#else
#define static_assert(...) //
#endif
#elif __STDC_VERSION__ < LC_STDC_C23
#define static_assert _Static_assert
#endif
#endif
#ifdef LC_STD_CXX
#define LC_DEPRECATED(msg) [[deprecated(msg)]]
#else
#if __STDC_VERSION__ < LC_STDC_C23
#if LC_CC_GNU
#define LC_DEPRECATED(msg) __attribute__((deprecated(msg)))
#else
#define LC_DEPRECATED(msg)
#endif
#else
#define LC_DEPRECATED(msg) [[deprecated(msg)]]
#endif
#endif
#ifdef LC_STD_CXX
#define LC_FALLTHROUGH [[fallthrough]]
#else
#if __STDC_VERSION__ < LC_STDC_C23
#if LC_CC_GNU
#define LC_FALLTHROUGH __attribute__((fallthrough))
#else
#define LC_FALLTHROUGH
#endif
#else
#define LC_FALLTHROUGH [[fallthrough]]
#endif
#endif
#ifdef LC_STD_CXX
#define restrict
#endif
#if LC_CC_GNU
#define LC_NORETURN __attribute__((noreturn))
#else
#define LC_NORETURN
#endif
#if LC_CC_GNU
#define LC_EXPECT(expr, cond) __builtin_expect(expr, cond)
#else
#define LC_EXPECT(expr, cond) expr
#endif
#if __STDC_HOSTED__
#ifdef __unix__
#define LC_PLATFORM_UNIX
#endif
#ifdef _DEFAULT_SOURCE
#define LC_PLATFORM_POSIX
#endif
#ifdef _GNU_SOURCE
#define LC_PLATFORM_POSIX
#endif
#ifdef _POSIX_C_SOURCE
#define LC_PLATFORM_POSIX
#endif
#ifdef __linux__
#define LC_PLATFORM_LINUX
#endif
#ifdef __APPLE__
#define LC_PLATFORM_APPLE
#endif
#ifdef _WIN32
#define LC_PLATFORM_WIN
#endif
#ifdef __ANDROID__
#define LC_PLATFORM_ANDROID
#endif
#else
#define LC_PLATFORM_FREESTANDING
#endif
#ifndef LC_FEATURE_LIST
#define LC_ENABLE_ANSI_ESC
#define LC_ENABLE_DLOPEN
#define LC_ENABLE_IO
#endif
#ifndef LC_STD_CXX
#if __STDC_VERSION__ < LC_STDC_C11
#if LC_CC_GNU
#define thread_local __thread
#else
#define thread_local
#endif
#elif __STDC_VERSION__ < LC_STDC_C23
#define thread_local _Thread_local
#else
#endif
#endif
#if __STDC_VERSION__ < LC_STDC_C23
#define nullptr NULL
#endif
#if __STDC_VERSION__ < LC_STDC_C23 && !defined(LC_STD_CXX)
#define LC_NODISCARD
#else
#define LC_NODISCARD [[nodiscard]]
#endif
#if __STDC_VERSION__ < LC_STDC_C23 && !defined(LC_STD_CXX)
#if LC_CC_GNU
#define LC_UNUSED __attribute__((__unused__))
#else
#define LC_UNUSED
#endif
#else
#define LC_UNUSED [[maybe_unused]]
#endif
#ifdef __FAST_MATH__
#define LC_FAST_MATH
#endif
#if LC_CC_GCC
#define LC_PRINTF_LIKE(fmt_arg_pos, arg_pos) __attribute__((__format__(printf, fmt_arg_pos, arg_pos)))
#else
#define LC_PRINTF_LIKE(fmt_arg_pos, arg_pos)
#endif
#if LC_CC_GNU
#define LC_FILE_NAME __BASE_FILE__
#else
#define LC_FILE_NAME __FILE__
#endif
#if LC_CC_GNU
#define LC_ALWAYS_INLINE inline __attribute__((__always_inline__))
#else
#define LC_ALWAYS_INLINE inline
#endif
// TODO: Clean this up when I get a newer version of GCC/clang
// GCC has backported unsequenced to older version of the language, so let's
// wrap arround that instead of using `gnu::pure` and `gnu::const`.
#if LC_STDC_C23 <= __STDC_VERSION__
#if __has_c_attribute(reproducible)
#define LC_REPRODUCIBLE [[reproducible]]
#else
#define LC_REPRODUCIBLE
#endif
#if __has_c_attribute(unsequenced)
#define LC_UNSEQUENCED [[unsequenced]]
#elif LC_CC_GNU
#define LC_UNSEQUENCED LC_REPRODUCIBLE
#else
#define LC_UNSEQUENCED LC_REPRODUCIBLE
#endif
#if LC_CC_GNU
#define LC_CONST [[gnu::const]]
#else
#define LC_CONST LC_UNSEQUENCED
#endif
#elif LC_CC_GNU
#if __has_c_attribute(unsequenced)
#define LC_UNSEQUENCED __attribute__((unsequenced))
#else
#define LC_UNSEQUENCED
#endif
#define LC_CONST __attribute__((const))
#define LC_REPRODUCIBLE
#else
#define LC_UNSEQUENCED
#define LC_REPRODUCIBLE
#define LC_CONST
#endif
// Because floating point operations can affect floating point enviornment,
// unsequenced is unsuitable for standard compliant code.
// When fast math optimazations is enabled, we can ignore the problem.
#ifdef LC_FAST_MATH
#define LC_UNSEQUENCED_FP LC_UNSEQUENCED
#else
#define LC_UNSEQUENCED_FP
#endif
#ifdef LC_HAVE_TYPEOF
#define LC_UNSEQUENCED_FUNC(func) typeof (func) LC_UNSEQUENCED func
#define LC_UNSEQUENCED_FP_FUNC(func) typeof (func) LC_UNSEQUENCED_FP func
#else
#define LC_UNSEQUENCED_FUNC(func) static_assert(1)
#define LC_UNSEQUENCED_FP_FUNC(func) static_assert(1)
#endif
#define LC_PRAGMA(...) _Pragma(LC_PP_STR(__VA_ARGS__))
#if LC_CC_GCC
#define LC_WARN_PUSH _Pragma("GCC diagnostic push")
#define LC_WARN_POP _Pragma("GCC diagnostic pop")
#define LC_WARN_IGNORE(warn) LC_PRAGMA(GCC diagnostic ignored warn)
#else
#define LC_WARN_PUSH
#define LC_WARN_POP
#define LC_WARN_IGNORE(warn)
#endif
#if LC_CC_GCC
#define LC_FLAG_ENUM __attribute__((flag_enum))
#else
#define LC_FLAG_ENUM
#endif
#if LC_CC_GCC
#define LC_DESIGNATED_INIT __attribute__((designated_init))
#else
#define LC_DESIGNATED_INIT
#endif
#ifdef LC_STD_CXX
LC_WARN_PUSH
LC_WARN_IGNORE("-Wc99-extensions")
LC_WARN_IGNORE("-Wvla-cxx-extension")
LC_WARN_IGNORE("-Wmissing-field-initializers")
#endif
#ifdef LC_STD_CXX
extern "C" {
#endif
////////////////////////////////////////////////////////////////////////////////
/// Types [LC_XTY] ///
////////////////////////////////////////////////////////////////////////////////
#include <inttypes.h>
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
static_assert(CHAR_BIT == 8, "We assume that a byte is 8 bits");
typedef unsigned long long ullong;
typedef unsigned long ulong;
typedef unsigned int uint;
typedef unsigned short ushort;
typedef unsigned char uchar;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t i8, s8;
typedef int16_t i16, s16;
typedef int32_t i32, s32;
typedef int64_t i64, s64;
typedef u64 size64;
typedef u32 size32;
typedef u16 size16;
typedef u8 size8;
typedef i64 isize64;
typedef i32 isize32;
typedef i16 isize16;
typedef i8 isize8;
typedef volatile u8 vu8;
typedef volatile u16 vu16;
typedef volatile u32 vu32;
typedef volatile u64 vu64;
typedef volatile i8 vi8, vs8;
typedef volatile i16 vi16, vs16;
typedef volatile i32 vi32, vs32;
typedef volatile i64 vi64, vs64;
LC_DEPRECATED("ABI unstable") typedef intmax_t intmax_t;
LC_DEPRECATED("ABI unstable") typedef uintmax_t uintmax_t;
typedef float f32;
typedef double f64;
typedef volatile f32 vf32;
typedef volatile f64 vf64;
static_assert(sizeof(f32) == 4, " float should match IEEE-754 binary32 format");
static_assert(sizeof(f64) == 8, "double should match IEEE-754 binary64 format");
typedef ptrdiff_t ssize;
#ifndef SSIZE_MAX
#define SSIZE_MAX (ssize)(SIZE_MAX >> 1)
#endif
#define SSIZE_MIN ~(SSIZE_MAX)
// Constant literal macro
#define u8_c(x) UINT8_C(x)
#define u16_c(x) UINT16_C(x)
#define u32_c(x) UINT32_C(x)
#define u64_c(x) UINT64_C(x)
#define i8_c(x) INT8_C(x)
#define i16_c(x) INT16_C(x)
#define i32_c(x) INT32_C(x)
#define i64_c(x) INT64_C(x)
#define s8_c(x) INT8_C(x)
#define s16_c(x) INT16_C(x)
#define s32_c(x) INT32_C(x)
#define s64_c(x) INT64_C(x)
#define f32_c(x) x##f
#define f64_c(x) x
#if __STDC_VERSION__ < LC_STDC_C23
#define INT16_WIDTH 16
#define INT32_WIDTH 32
#define INT64_WIDTH 64
#define INT8_WIDTH 8
#define UINT16_WIDTH 16
#define UINT32_WIDTH 32
#define UINT64_WIDTH 64
#define UINT8_WIDTH 8
#endif
////////////////////////////////////////////////////////////////////////////////
/// Platform code [LC_XPL] ///
////////////////////////////////////////////////////////////////////////////////
#ifndef lc_memset
extern void *memset(void *, int, size_t);
#define lc_memset memset
#endif
#ifndef lc_memcpy
extern void *memcpy(void *restrict, const void *restrict, size_t);
#define lc_memcpy memcpy
#endif
#ifndef lc_memcmp
extern int memcmp(const void *restrict, const void *restrict, size_t);
#define lc_memcmp memcmp
#endif
////////////////////////////////////////////////////////////////////////////////
/// Utility macro [LC_XUM] ///
////////////////////////////////////////////////////////////////////////////////
#ifndef LEN
#define LEN(X) (sizeof(X) / sizeof(X)[0])
#endif
#ifdef LC_HAVE_TYPEOF
#define typeof_member(type, member) typeof(((type *)0)->member)
#endif
#define sizeof_member(type, member) (sizeof(((type *)0)->member))
#define struct_size_t(type, member, count) \
(offsetof(type, member) + (sizeof_member(type, member[0]) * (count)))
#ifdef LC_HAVE_TYPEOF
#define struct_size(ptr, member, count) struct_size_t(typeof(*ptr), member, count)
#endif
#define KiB (UINTMAX_C(0x01) << 10)
#define MiB (UINTMAX_C(0x01) << 20)
#define GiB (UINTMAX_C(0x01) << 30)
#define TiB (UINTMAX_C(0x01) << 40)
#define PiB (UINTMAX_C(0x01) << 50)
#define EiB (UINTMAX_C(0x01) << 60)
#if LC_CC_GCC
#define LC_ELVIS(X, Y) __extension__((X) ?: (Y))
#else
#define LC_ELVIS(X, Y) ((X) ? (X) : (Y))
#endif
#if LC_STDC_C11 <= __STDC_VERSION__
#define type_equal(expr, type) _Generic(expr, type: true, default: false)
#else
#define type_equal(expr, type) true
#endif
#define type_assert(expr, type) static_assert(type_equal(expr, type), #expr " == " #type);
#if LC_STDC_C11 <= __STDC_VERSION__
#define type_check(expr, type) _Generic(expr, type: expr)
#else
#define type_check(expr, type) expr
#endif
#define container_of(type, member, ptr) \
((type *)((u8 *)(ptr) - offsetof(type, member)))
#define container_of_c(type, member, ptr) \
((type *)(((u8 *)type_check(ptr, typeof_member(type, member) *)) - offsetof(type, member)))
#if LC_CC_LLVM && __has_builtin(__builtin_debugtrap)
#define LC_DEBUGBREAK() __builtin_debugtrap()
#elif LC_CC_GCC
#ifdef LC_ARCH_AMD64
#define LC_DEBUGBREAK() __asm__ volatile("int3")
#else
#define LC_DEBUGBREAK() __builtin_trap()
#endif
#else
#define LC_DEBUGBREAK()
#endif
#if __STDC_VERSION__ < LC_STDC_C23
#if LC_CC_GNU
#define lc_unreachable __builtin_unreachable
#else
#define lc_unreachable() *((void *volatile *volatile)NULL)
#endif
#else
#define lc_unreachable unreachable
#endif
#define lc_unimplemented(msg) LC_DEBUGBREAK()
#define lc_todo(msg) LC_DEBUGBREAK()
#define lc_assume(cond) \
do { \
if (!(cond)) \
lc_unreachable(); \
} while (false)
#define static_assert_size(type, size) \
static_assert(sizeof (type) == (size), "Unintended size of type " #type ", expected: " #size)
////////////////////////////////////////////////////////////////////////////////
/// Math [LC_XMT] ///
////////////////////////////////////////////////////////////////////////////////
#define MAX(X, Y) ((X) < (Y) ? (Y) : (X))
#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
#define IN_RANGE(X, LO, HI) (((LO) <= (X)) & ((X) <= (HI)))
#ifdef LC_HAVE_TYPEOF
#define ABS(X) (typeof(X))llabs(X)
#else
#define ABS(X) llabs(X)
#endif
#ifndef LC_STD_CXX
LC_NODISCARD
static inline f32
lerp(f32 a, f32 b, f32 t)
{
return a + t * (b - a);
}
LC_UNSEQUENCED_FP_FUNC(lerp);
#endif
LC_NODISCARD
static inline f32
clamp(f32 x, f32 lo, f32 hi)
{
return x < lo ? lo : (hi < x ? hi : x);
}
LC_UNSEQUENCED_FP_FUNC(clamp);
////////////////////////////////////////////////////////////////////////////////
/// Relative pointer [LC_XRP] ///
////////////////////////////////////////////////////////////////////////////////
#if !defined(LC_STD_CXX) && defined(LC_PACKED) && defined(LC_HAVE_TYPEOF)
#define rel_ptr(type, rel_type) \
struct LC_PACKED /* Anonymous tag */ { \
/* Relative offset to the thing pointing to */ \
rel_type _offset; \
/* zero-sized field to carry type infomation of the thing pointing to */ \
__extension__ type _[0]; \
}
#define rel_is_null(p) (0 == (p)._offset)
#define rel_to_abs(p) (typeof((p)._[0]))((intptr_t) & (p) + (p)._offset)
#define abs_to_rel(base, abs) ((typeof(base)){._offset = ((intptr_t)(abs) - (intptr_t) & (base))})
static_assert(sizeof(rel_ptr(const char *, i32)) == sizeof(i32), "Ensure the struct is packed");
static_assert(sizeof(rel_ptr(const void *, i8)) == sizeof(i8), "Ensure the struct is packed");
#else
#define rel_ptr(type, rel_type) rel_type
#define rel_is_null(p) (0 == (p))
#define rel_to_abs(p) ((intptr_t) & (p) + (p))
#define abs_to_rel(base, abs) ((intptr_t)(abs) - (intptr_t) & (base))
#endif
#define rel_ptr_i8(type) rel_ptr(type, i8)
#define rel_ptr_u8(type) rel_ptr(type, u8)
#define rel_ptr_i16(type) rel_ptr(type, i16)
#define rel_ptr_u16(type) rel_ptr(type, u16)
#define rel_ptr_i32(type) rel_ptr(type, i32)
#define rel_ptr_u32(type) rel_ptr(type, u32)
#define rel_ptr_i64(type) rel_ptr(type, i64)
#define rel_ptr_u64(type) rel_ptr(type, u64)
////////////////////////////////////////////////////////////////////////////////
/// Ordering [LC_XOD] ///
////////////////////////////////////////////////////////////////////////////////
// Does not make sense in cxx...
#ifndef LC_STD_CXX
#define LC_HAVE_ORDER
/**
* @breif A strict total order.
*
* Used as a result of a three-way comparison.
*
* This enum is compatible with existing C API that use comparison agains zero
* (eg. qsort). Any existing C API that returns an integer can be converted to
* this enum by using \p lc_order_from_int.
*
* A strict total order satisfy the following for all \a a, \a b and \a c
*
* -# a < a == false (irreflextive).
* -# if a < b == true then b < a == false (asymmetric).
* -# if a < b and b < c then a < c (transitive).
* -# if a != b then a < b or b < a (connected).
*
*/
enum LC_NODISCARD lc_order
{
LC_ORDER_LESS = -1,
LC_ORDER_EQUAL = +0,
LC_ORDER_GREATER = +1,
};
static inline enum lc_order lc_order_cmp_size_t(size_t lhs, size_t rhs);
static inline enum lc_order lc_order_cmp_u32(u32 lhs, u32 rhs);
static inline enum lc_order lc_order_cmp_i32(i32 lhs, i32 rhs);
static inline enum lc_order lc_order_from_i64(i64 order);
static inline enum lc_order lc_order_from_i32(i32 order);
static inline enum lc_order lc_order_from_i32(i32 order);
static inline enum lc_order lc_order_from_i16(i16 order);
static inline enum lc_order lc_order_from_i8(i8 order);
LC_UNSEQUENCED_FUNC(lc_order_cmp_size_t);
LC_UNSEQUENCED_FUNC(lc_order_cmp_u32);
LC_UNSEQUENCED_FUNC(lc_order_cmp_i32);
LC_UNSEQUENCED_FUNC(lc_order_from_i64);
LC_UNSEQUENCED_FUNC(lc_order_from_i32);
LC_UNSEQUENCED_FUNC(lc_order_from_i16);
LC_UNSEQUENCED_FUNC(lc_order_from_i8);
static inline enum lc_order
lc_order_cmp_size_t(size_t lhs, size_t rhs)
{
return (lhs > rhs) - (lhs < rhs);
}
static inline enum lc_order
lc_order_cmp_u32(u32 lhs, u32 rhs)
{
return (lhs > rhs) - (lhs < rhs);
}
static inline enum lc_order
lc_order_cmp_i32(i32 lhs, i32 rhs)
{
return (lhs > rhs) - (lhs < rhs);
}
/**
* @breif Converts an integer representing an order to an enumeration
* representing the order.
*
* @param order The integer to convert.
*
* @returns LC_ORDER_LESS if \a order is below zero,
* LC_ORDER_EQUAL if \a order is zero,
* LC_ORDER_GREATER if \a order is above zero.
*/
static inline enum lc_order
lc_order_from_i64(i64 order)
{
return (order > 0) - (order < 0);
}
/**
* @breif Converts an integer representing an order to an enumeration
* representing the order.
*
* @param order The integer to convert.
*
* @returns LC_ORDER_LESS if \a order is below zero,
* LC_ORDER_EQUAL if \a order is zero,
* LC_ORDER_GREATER if \a order is above zero.
*/
static inline enum lc_order
lc_order_from_i32(i32 order)
{
return (order > 0) - (order < 0);
}
/**
* @breif Converts an integer representing an order to an enumeration
* representing the order.
*
* @param order The integer to convert.
*
* @returns LC_ORDER_LESS if \a order is below zero,
* LC_ORDER_EQUAL if \a order is zero,
* LC_ORDER_GREATER if \a order is above zero.
*/
static inline enum lc_order
lc_order_from_i16(i16 order)
{
return (order > 0) - (order < 0);
}
/**
* @breif Converts an integer representing an order to an enumeration
* representing the order.
*
* @param order The integer to convert.
*
* @returns LC_ORDER_LESS if \a order is below zero,
* LC_ORDER_EQUAL if \a order is zero,
* LC_ORDER_GREATER if \a order is above zero.
*/
static inline enum lc_order
lc_order_from_i8(i8 order)
{
return (order > 0) - (order < 0);
}
#if __STDC_VERSION__ < LC_STDC_C11
#define lc_order_from_int lc_order_from_i32
#else
#define _lc__oo(type) type: lc_order_from_##type
#define lc_order_from_int(order) _Generic(order, _lc__oo(i8), _lc__oo(i16), _lc__oo(i32), _lc__oo(i64))(order)
#endif
#endif
////////////////////////////////////////////////////////////////////////////////
/// Assert [LC_XAS] ///
////////////////////////////////////////////////////////////////////////////////
#define LC_ASSERT_(expr, msg) \
do { \
extern int puts(const char *); \
\
if (LC_EXPECT(!(expr), false)) { \
puts(msg); \
LC_DEBUGBREAK(); \
lc_unreachable(); \
} \
} while (false);
#ifdef LC_STD_CXX
#define LC_ASSERT__(expr, msg, src_loc) \
do { \
if (LC_EXPECT(!(expr), false)) { \
_lc_log(ctx->logger, \
LC_LOG_LEVEL_FATAL, \
src_loc, \
"Assert failed on expression: %s message: %s", \
#expr, \
msg); \
\
/* We HAVE to save src_loc into a local variable to be compatible with C++ */ \
size_t i = 0; \
for (const struct lc_source_loc s = src_loc, *src = &s; src->prev; src = src->prev) \
_lc_log(ctx->logger, \
LC_LOG_LEVEL_FATAL, \
src_loc, \
"%02zu: %s:%d %s()", \
++i, \
src->filename, \
src->line, \
src->func); \
LC_DEBUGBREAK(); \
lc_unreachable(); \
} \
} while (false)
#else
#define LC_ASSERT__(expr, msg, src_loc) \
do { \
if (LC_EXPECT(!(expr), false)) { \
_lc_log(ctx->logger, \
LC_LOG_LEVEL_FATAL, \
src_loc, \
"Assert failed on expression: %s message: %s", \
#expr, \
msg); \
\
/* We HAVE to save src_loc into a local variable to be compatible with C++ */ \
size_t i = 0; \
for (const struct lc_source_loc s = src_loc, *src = &s; src->prev; src = src->prev) \
_lc_log(ctx->logger, \
LC_LOG_LEVEL_FATAL, \
src_loc, \
"%02zu: %s:%d %s()", \
++i, \
src->data->filename, \
src->data->line, \
src->data->func); \
LC_DEBUGBREAK(); \
lc_unreachable(); \
} \
} while (false)
#endif
#define LC_ASSERT(expr, msg) LC_ASSERT__(expr, msg, lc_src_loc(&src_loc))
////////////////////////////////////////////////////////////////////////////////
/// Slice [LC_XSC] ///
////////////////////////////////////////////////////////////////////////////////
typedef u8 lc_byte;
// Const byte slice
struct lc_bslice {
size_t size;
const lc_byte *data;
};
// Mutable byte slice
struct lc_mut_bslice {
size_t size;
lc_byte *data;
};
#define slice(n, ptr) ((struct lc_bslice){.size = n, .data = (const lc_byte *)ptr})
#define mut_slice(n, ptr) ((struct lc_mut_bslice){.size = n, .data = (lc_byte *)ptr})
#define slice_c(arr) slice(sizeof arr, arr)
#define mut_slice_c(arr) mut_slice(sizeof arr, arr)
static inline struct lc_bslice
lc_bslice_from_mut(struct lc_mut_bslice mut_slice)
{
return slice(mut_slice.size, mut_slice.data);
}
static inline struct lc_bslice
lc_bslice_subslice(struct lc_bslice slice, size_t idx, size_t size)
{
/// Case 4. Length zero
///
/// idx: does not matter
/// size: 0
///
/// Returns empty string
if (0 == size)
return slice(0, 0);
/// Case 2. Index out of bound
///
/// idx: (sv.length; ]
/// size: does not matter
///
/// Returns empty string
if (slice.size < idx)
return slice(0, 0);
/// Case 3. Substring length truncate
///
/// idx: [0; sv.length]
/// size: (sv.length - idx; ]
///
/// Trucate substring
if (slice.size < idx + size)
size -= (idx + size) - slice.size;
/// Case 1. Normal
///
/// idx: [0; sv.length]
/// size: [0; sv.length - idx]
///
/// Returns substring as expected
return slice(size, slice.data + idx);
}
static inline struct lc_mut_bslice
lc_bslice_mut_subslice(struct lc_mut_bslice slice, size_t idx, size_t size)
{
/// Case 4. Length zero
///
/// idx: does not matter
/// size: 0
///
/// Returns empty string
if (0 == size)
return mut_slice(0, 0);
/// Case 2. Index out of bound
///
/// idx: (sv.length; ]
/// size: does not matter
///
/// Returns empty string
if (slice.size < idx)
return mut_slice(0, 0);
/// Case 3. Substring length truncate
///
/// idx: [0; sv.length]
/// size: (sv.length - idx; ]
///
/// Trucate substring
if (slice.size < idx + size)
size -= (idx + size) - slice.size;
/// Case 1. Normal
///
/// idx: [0; sv.length]
/// size: [0; sv.length - idx]
///
/// Returns substring as expected
return mut_slice(size, slice.data + idx);
}
////////////////////////////////////////////////////////////////////////////////
/// Source location [LC_XSL] ///
////////////////////////////////////////////////////////////////////////////////
#ifdef LC_STD_CXX
struct lc_source_loc {
const char *restrict filename;
const char *restrict func;
int line;
const struct lc_source_loc *prev;
};
#define lc_src_loc(prev) \
LC_WARN_PUSH \
LC_WARN_IGNORE("-Wc99-extensions") \
((struct lc_source_loc){LC_FILE_NAME, __func__, __LINE__, prev}) LC_WARN_POP
#else
struct lc_source_loc_data {
const char *restrict filename;
const char *restrict func;
int line;