-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGLTFParser.cpp
More file actions
3675 lines (3214 loc) · 116 KB
/
GLTFParser.cpp
File metadata and controls
3675 lines (3214 loc) · 116 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
/*****************************************************************
* *
* Purpose: *
* Simple and efficient parser for GLTF format *
* allows you to import 3d mesh, material and scene *
* Author: *
* Anilcan Gulkaya 2023 anilcangulkaya7@gmail.com *
* Restrictions: *
* No extension support. *
* License: *
* No License whatsoever do Whatever you want. *
* *
*****************************************************************/
#ifndef AX_MALLOC
#define AX_MALLOC(size) ((void*)new char[size])
#define AX_CALLOC(size) ((void*)new char[size]{})
#define AX_FREE(ptr) (delete[] (char*)(ptr) )
#endif
#include "GLTFParser.h"
#ifndef __cplusplus
extern "C" {
#endif
/*****************************************************************
* ASTL COMMON *
*****************************************************************/
#ifndef ASTL_COMMON
#define ASTL_COMMON
#define __STDC_LIMIT_MACROS
#include <stdint.h>
#include <float.h>
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;
typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
typedef uint8_t uchar;
typedef uint16_t ushort;
typedef uint32_t uint;
#ifdef AX_EXPORT
#define AX_API __declspec(dllexport)
#else
#define AX_API __declspec(dllimport)
#endif
#ifdef _MSC_VER
# // do nothing it already has __forceinline
#elif __CLANG__
# define __forceinline [[clang::always_inline]]
#elif __GNUC__
#ifndef __forceinline
# define __forceinline inline __attribute__((always_inline))
#endif
#endif
#ifdef _MSC_VER
# include <intrin.h>
# define VECTORCALL __vectorcall
#elif __CLANG__
# define VECTORCALL [[clang::vectorcall]]
#elif __GNUC__
# define VECTORCALL
#endif
#if defined(__GNUC__)
# define AX_PACK(decl) decl __attribute__((__packed__))
#elif defined(_MSC_VER)
# define AX_PACK(decl) __pragma(pack(push, 1)) decl __pragma(pack(pop))
#else
#error you should define pack function
#endif
#if defined(__GNUC__) || defined(__MINGW32__)
#define RESTRICT __restrict__
#elif defined(_MSC_VER)
#define RESTRICT __restrict
#else
#define RESTRICT
#endif
#ifndef AXGLOBALCONST
# if _MSC_VER
# define AXGLOBALCONST extern const __declspec(selectany)
# elif defined(__GNUC__) && !defined(__MINGW32__)
# define AXGLOBALCONST extern const __attribute__((weak))
# else
# define AXGLOBALCONST extern const
# endif
#endif
#if defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__clang__)
# define AX_LIKELY(x) __builtin_expect(x, 1)
# define AX_UNLIKELY(x) __builtin_expect(x, 0)
#else
# define AX_LIKELY(x) (x)
# define AX_UNLIKELY(x) (x)
#endif
// https://nullprogram.com/blog/2022/06/26/
#if defined(_DEBUG) || defined(Debug)
# if __GNUC__
# define ASSERT(c) if (!(c)) __builtin_trap()
# elif _MSC_VER
# define ASSERT(c) if (!(c)) __debugbreak()
# else
# define ASSERT(c) if (!(c)) *(volatile int *)0 = 0
# endif
#else
# define ASSERT(c)
#endif
#if defined(__has_builtin)
# define AX_COMPILER_HAS_BUILTIN(x) __has_builtin(x)
#else
# define AX_COMPILER_HAS_BUILTIN(x) 0
#endif
#if AX_COMPILER_HAS_BUILTIN(__builtin_assume)
# define AX_ASSUME(x) __builtin_assume(x)
#elif defined(_MSC_VER)
# define AX_ASSUME(x) __assume(x)
#else
# define AX_ASSUME(x) (void)(x)
#endif
#if AX_COMPILER_HAS_BUILTIN(__builtin_unreachable)
# define AX_UNREACHABLE() __builtin_unreachable()
#elif _MSC_VER
# define AX_UNREACHABLE() __assume(0)
#else
# define AX_UNREACHABLE()
#endif
#if AX_COMPILER_HAS_BUILTIN(__builtin_prefetch)
# define AX_PREFETCH(x) __builtin_prefetch(x)
#elif defined(_MSC_VER)
# define AX_PREFETCH(x) _mm_prefetch(x, _MM_HINT_T0)
#else
# define AX_PREFETCH(x)
#endif
// https://gist.github.com/boxmein/7d8e5fae7febafc5851e
// https://en.wikipedia.org/wiki/CPUID
// example usage:
// void get_cpu_model(char *cpu_model) { // return example: "AMD Ryzen R5 1600"
// int* cpumdl = (int*)cpu_model;
// AX_CPUID(0x80000002, cpumdl); cpumdl += 4;
// AX_CPUID(0x80000003, cpumdl); cpumdl += 4;
// AX_CPUID(0x80000004, cpumdl);
// }
// int arr[4];
// AX_CPUID(1, arr);
// int numCores = (arr[1] >> 16) & 0xff; // virtual cores included
#if defined(__clang__) || defined(__GNUC__)
//# include <cpuid.h>
//# define AX_CPUID(num, regs) __cpuid(num, regs[0], regs[1], regs[2], regs[3])
//# define AX_CPUID2(num, sub, regs) __cpuid_count(num, sub, regs[0], regs[1], regs[2], regs[3])
# define AX_CPUID(num, regs)
#else
# define AX_CPUID(num, regs) __cpuid(regs, num)
#endif
/* Architecture Detection */
// detection code from mini audio
// you can define AX_NO_SSE2 or AX_NO_AVX2 in order to disable this extensions
#if defined(__x86_64__) || defined(_M_X64)
# define AX_X64
#elif defined(__i386) || defined(_M_IX86)
# define AX_X86
#elif defined(_M_ARM) || defined(_M_ARM64) || defined(_M_HYBRID_X86_ARM64) || defined(_M_ARM64EC) || __arm__ || __aarch64__
#define AX_ARM
#endif
#if defined(AX_ARM)
#if defined(_MSC_VER) && !defined(__clang__) && (defined(_M_ARM64) || defined(_M_HYBRID_X86_ARM64) || defined(_M_ARM64EC) || defined(__aarch64__))
#include <arm64_neon.h>
#else
#include <arm_neon.h>
#endif
#endif
// write AX_NO_SSE2 or AX_NO_AVX2 to disable vector instructions
/* Intrinsics Support */
#if (defined(AX_X64) || defined(AX_X86)) && !defined(AX_ARM)
#if defined(_MSC_VER) && !defined(__clang__)
#if _MSC_VER >= 1400 && !defined(AX_NO_SSE2) /* 2005 */
#define AX_SUPPORT_SSE
#endif
#if _MSC_VER >= 1700 && !defined(AX_NO_AVX2) /* 2012 */
#define AX_SUPPORT_AVX2
#endif
#else
#if defined(__SSE2__) && !defined(AX_NO_SSE2)
#define AX_SUPPORT_SSE
#endif
#if defined(__AVX2__) && !defined(AX_NO_AVX2)
#define AX_SUPPORT_AVX2
#endif
#endif
/* If at this point we still haven't determined compiler support for the intrinsics just fall back to __has_include. */
#if !defined(__GNUC__) && !defined(__clang__) && defined(__has_include)
#if !defined(AX_SUPPORT_SSE) && !defined(AX_NO_SSE2) && __has_include(<emmintrin.h>)
#define AX_SUPPORT_SSE
#endif
#if !defined(AX_SUPPORT_AVX2) && !defined(AX_NO_AVX2) && __has_include(<immintrin.h>)
#define AX_SUPPORT_AVX2
#endif
#endif
#if defined(AX_SUPPORT_AVX2) || defined(AX_SUPPORT_AVX)
#include <immintrin.h>
#elif defined(AX_SUPPORT_SSE)
#include <emmintrin.h>
#endif
#endif
#ifndef AX_NO_UNROLL
#if defined(__clang__)
# define AX_NO_UNROLL _Pragma("clang loop unroll(disable)") _Pragma("clang loop vectorize(disable)")
#elif defined(__GNUC__) >= 8
# define AX_NO_UNROLL _Pragma("GCC unroll 0")
#elif defined(_MSC_VER)
# define AX_NO_UNROLL __pragma(loop(no_vector))
#else
# define AX_NO_UNROLL
#endif
#endif
#if defined(__clang__) || defined(__GNUC__)
#define AX_CPP_VERSION __cplusplus
#define AX_CPP14 201402L
#define AX_CPP17 201703L
#define AX_CPP20 202002L
#elif defined(_MSC_VER)
#define AX_CPP_VERSION _MSC_VER
#define AX_CPP14 1900
#define AX_CPP17 1910
#define AX_CPP20 1920
#endif
#if AX_CPP_VERSION < AX_CPP14
// below c++ 14 does not support constexpr functions
#define __constexpr
#else
#define __constexpr constexpr
#endif
#if AX_CPP_VERSION >= AX_CPP17
# define if_constexpr if constexpr
#else
# define if_constexpr if
#endif
#if AX_CPP_VERSION < AX_CPP14
// below c++ 14 does not support constexpr
#define __const const
#else
#define __const constexpr
#endif
#ifdef _MSC_VER
#define SmallMemCpy(dst, src, size) __movsb((unsigned char*)(dst), (unsigned char*)(src), size);
#else
#define SmallMemCpy(dst, src, size) __builtin_memcpy(dst, src, size);
#endif
#ifdef _MSC_VER
#define SmallMemSet(dst, val, size) __stosb((unsigned char*)(dst), val, size);
#else
#define SmallMemSet(dst, val, size) __builtin_memset(dst, val, size);
#endif
#define MemsetZero(dst, size) SmallMemSet(dst, 0, size)
#if defined(_MSC_VER) && !defined(__clang__)
#if defined(_M_IX86) //< The __unaligned modifier isn't valid for the x86 platform.
#define UnalignedLoad64(ptr) *((uint64_t*)(ptr))
#else
#define UnalignedLoad64(ptr) *((__unaligned uint64_t*)(ptr))
#endif
#else
__forceinline uint64_t UnalignedLoad64(void const* ptr) {
__attribute__((aligned(1))) uint64_t const *result = (uint64_t const *)ptr;
return *result;
}
#endif
#if defined(_MSC_VER) && !defined(__clang__)
#if defined(_M_IX86) //< The __unaligned modifier isn't valid for the x86 platform.
#define UnalignedLoad32(ptr) *((uint32_t*)(ptr))
#else
#define UnalignedLoad32(ptr) *((__unaligned uint32_t*)(ptr))
#endif
#else
__forceinline uint64_t UnalignedLoad32(void const* ptr) {
__attribute__((aligned(1))) uint32_t const *result = (uint32_t const *)ptr;
return *result;
}
#endif
#define UnalignedLoadWord(x) (sizeof(unsigned long) == 8 ? UnalignedLoad64(x) : UnalignedLoad32(x))
// #define AX_USE_NAMESPACE
#ifdef AX_USE_NAMESPACE
# define AX_NAMESPACE namespace ax {
# define AX_END_NAMESPACE }
#else
# define AX_NAMESPACE
# define AX_END_NAMESPACE
#endif
AX_NAMESPACE
// change it as is mobile maybe?
inline constexpr bool IsAndroid()
{
#ifdef __ANDROID__
return true;
#else
return false;
#endif
}
template<typename T, int N>
__constexpr int ArraySize(const T (&)[N]) { return N; }
#if defined(_MSC_VER) /* Visual Studio */
#define AX_BSWAP32(x) _byteswap_ulong(x)
#elif (defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) \
|| (defined(__clang__) && __has_builtin(__builtin_bswap32))
#define AX_BSWAP32(x) __builtin_bswap32(x)
#else
inline uint32_t AX_BSWAP32(uint32_t x) {
return ((in << 24) & 0xff000000 ) |
((in << 8) & 0x00ff0000 ) |
((in >> 8) & 0x0000ff00 ) |
((in >> 24) & 0x000000ff );
}
#endif
#if defined(_MSC_VER)
#define ByteSwap32(x) _byteswap_uint64(x)
#elif (defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) \
|| (defined(__clang__) && __has_builtin(__builtin_bswap32))
#define ByteSwap64(x) __builtin_bswap64(x)
#else
inline uint64_t ByteSwap(uint64_t x) {
return ((x << 56) & 0xff00000000000000ULL) |
((x << 40) & 0x00ff000000000000ULL) |
((x << 24) & 0x0000ff0000000000ULL) |
((x << 8) & 0x000000ff00000000ULL) |
((x >> 8) & 0x00000000ff000000ULL) |
((x >> 24) & 0x0000000000ff0000ULL) |
((x >> 40) & 0x000000000000ff00ULL) |
((x >> 56) & 0x00000000000000ffULL);
}
#endif
#define ByteSwapWord(x) (sizeof(ulong) == 8 ? ByteSwap64(x) : ByteSwap32(x))
// according to intel intrinsic, popcnt instruction is 3 cycle (equal to mulps, addps)
// throughput is even double of mulps and addps which is 1.0 (%100)
// https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html
#if defined(__ARM_NEON__)
#define PopCount32(x) vcnt_u8((int8x8_t)x)
#elif defined(AX_SUPPORT_SSE)
#define PopCount32(x) _mm_popcnt_u32(x)
#define PopCount64(x) _mm_popcnt_u64(x)
#elif defined(__GNUC__) || !defined(__MINGW32__)
#define PopCount32(x) __builtin_popcount(x)
#define PopCount64(x) __builtin_popcountl(x)
#else
inline uint32_t PopCount32(uint32_t x) {
x = x - ((x >> 1) & 0x55555555); // add pairs of bits
x = (x & 0x33333333) + ((x >> 2) & 0x33333333); // quads
x = (x + (x >> 4)) & 0x0F0F0F0F; // groups of 8
return (x * 0x01010101) >> 24; // horizontal sum of bytes
}
// standard popcount; from wikipedia
inline uint64_t PopCount64(uint64_t x) {
x -= ((x >> 1) & 0x5555555555555555ull);
x = (x & 0x3333333333333333ull) + (x >> 2 & 0x3333333333333333ull);
return ((x + (x >> 4)) & 0xf0f0f0f0f0f0f0full) * 0x101010101010101ull >> 56;
}
#endif
#ifdef _MSC_VER
#define TrailingZeroCount32(x) _tzcnt_u32(x)
#define TrailingZeroCount64(x) _tzcnt_u64(x)
#elif defined(__GNUC__) || !defined(__MINGW32__)
#define TrailingZeroCount32(x) __builtin_ctz(x)
#define TrailingZeroCount64(x) __builtin_ctzll(x)
#else
#define TrailingZeroCount32(x) PopCount64((x & -x) - 1u)
#define TrailingZeroCount64(x) PopCount64((x & -x) - 1ull)
#endif
#define TrailingZeroCountWord(x) (sizeof(ulong) == 8 ? TrailingZeroCount64(x) : TrailingZeroCount32(x))
#ifdef _MSC_VER
#define LeadingZeroCount32(x) _lzcnt_u32(x)
#define LeadingZeroCount64(x) _lzcnt_u64(x)
#elif defined(__GNUC__) || !defined(__MINGW32__)
#define LeadingZeroCount32(x) __builtin_clz(x)
#define LeadingZeroCount64(x) __builtin_clzll(x)
#else
template<typename T> inline T LeadingZeroCount64(T x)
{
x |= (x >> 1);
x |= (x >> 2);
x |= (x >> 4);
x |= (x >> 8);
x |= (x >> 16);
if (sizeof(T) == 8) x |= (x >> 32);
return (sizeof(T) * 8) - PopCount(x);
}
#endif
#define LeadingZeroCountWord(x) (sizeof(ulong) == 8 ? LeadingZeroCount64(x) : LeadingZeroCount32(x))
template<typename T>
__forceinline __constexpr T NextSetBit(T* bits)
{
*bits &= ~T(1);
T tz = sizeof(T) == 8 ? (T)TrailingZeroCount64(*bits) : (T)TrailingZeroCount32(*bits);
*bits >>= tz;
return tz;
}
#define EnumHasBit(_enum, bit) !!(_enum & bit)
template<typename To, typename From>
__forceinline __constexpr To BitCast(const From& _Val)
{
#if AX_CPP_VERSION < AX_CPP17
return *reinterpret_cast<const To*>(&_Val);
#else
return __builtin_bit_cast(To, _Val);
#endif
}
#ifndef MIN
#if AX_CPP_VERSION >= AX_CPP17
template<typename T> __forceinline __constexpr T MIN(T a, T b) { return a < b ? a : b; }
template<typename T> __forceinline __constexpr T MAX(T a, T b) { return a > b ? a : b; }
#else
// using macro if less than 17 because we want this to be constexpr
# ifndef MIN
# define MIN(a, b) ((a) < (b) ? (a) : (b))
# define MAX(a, b) ((a) > (b) ? (a) : (b))
# endif
#endif
#endif
template<typename T> __forceinline __constexpr T Clamp(T x, T a, T b) { return MAX(a, MIN(b, x)); }
__forceinline __constexpr int64_t Abs(int64_t x)
{
return x & ~(1ull << 63ull);
}
__forceinline __constexpr int Abs(int x)
{
return x & ~(1 << 31);
}
__forceinline __constexpr float Abs(float x)
{
int ix = BitCast<int>(x) & 0x7FFFFFFF; // every bit except sign mask
return BitCast<float>(ix);
}
__forceinline __constexpr double Abs(double x)
{
uint64_t ix = BitCast<uint64_t >(x) & (~(1ull << 63ull));// every bit except sign mask
return BitCast<double>(ix);
}
template<typename T> __forceinline __constexpr
bool IsPowerOfTwo(T x) { return (x != 0) && ((x & (x - 1)) == 0); }
__forceinline __constexpr int NextPowerOf2(int x)
{
x--;
x |= x >> 1; x |= x >> 2; x |= x >> 4;
x |= x >> 8; x |= x >> 16;
return ++x;
}
__forceinline __constexpr int64_t NextPowerOf2(int64_t x)
{
x--;
x |= x >> 1; x |= x >> 2; x |= x >> 4;
x |= x >> 8; x |= x >> 16; x |= x >> 32;
return ++x;
}
// maybe we should move this to Algorithms.hpp
template<typename T>
inline uint PointerDistance(const T* begin, const T* end)
{
return uint((char*)end - (char*)begin) / sizeof(T);
}
inline __constexpr int CalculateArrayGrowth(int _size)
{
const int addition = _size >> 1;
if (AX_UNLIKELY(_size > (INT32_MAX - addition))) {
return INT32_MAX; // growth would overflow
}
return _size + addition; // growth is sufficient
}
#if (defined(__GNUC__) || defined(__clang__)) || \
(defined(_MSC_VER) && (AX_CPP_VERSION >= AX_CPP17))
#define StringLength(s) (int)__builtin_strlen(s)
#elif
typedef unsigned long long unsignedLongLong;
// http://www.lrdev.com/lr/c/strlen.c
inline int StringLength(char const* s)
{
char const* p = s;
const unsignedLongLong m = 0x7efefefefefefeffull;
const unsignedLongLong n = ~m;
for (; (unsignedLongLong)p & (sizeof(unsignedLongLong) - 1); p++)
if (!*p)
return (int)(unsignedLongLong)(p - s);
for (;;)
{
// memory is aligned from now on
unsignedLongLong i = *(const unsignedLongLong*)p;
if (!(((i + m) ^ ~i) & n)) {
p += sizeof(unsignedLongLong);
}
else
{
for (i = sizeof(unsignedLongLong); i; p++, i--)
if (!*p) return (int)(unsignedLongLong)(p - s);
}
}
}
#endif
#endif // ASTL_COMMON
/*****************************************************************
* IO *
*****************************************************************/
#include <stdio.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <io.h>
#include <direct.h>
#define F_OK 0
#define access _access
#else
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#define _mkdir mkdir
#define _fileno fileno
#define _filelengthi64 filelength
#endif
#if defined __WIN32__ || defined _WIN32 || defined _Windows
#if !defined S_ISDIR
#define S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR)
#endif
#endif
#ifdef __ANDROID__
#include <android/asset_manager.h>
#include <game-activity/native_app_glue/android_native_app_glue.h>
extern android_app* g_android_app;
#endif
struct ScopedFILE {
FILE* file;
ScopedFILE(FILE* _file) : file(_file) {}
~ScopedFILE() {
fclose(file);
}
};
// these functions works fine with file and folders
inline bool FileExist(const char* file) {
#ifdef __ANDROID__
AAsset* asset = AAssetManager_open(g_android_app->activity->assetManager, file, 0);
AAsset_close(asset);
return asset != nullptr;
#else
return access(file, F_OK) == 0;
#endif
}
inline uint64_t FileSize(const char* file) {
#ifdef __ANDROID__
AAsset* asset = AAssetManager_open(g_android_app->activity->assetManager, file, 0);
if (asset != nullptr) {
off64_t sz = AAsset_getLength64(asset);
AAsset_close(asset);
return sz;
}
return 0;
#elif defined(_WIN32) || defined(__linux__)
struct stat sb;
if (stat(file, &sb) == 0) return 0;
return sb.st_size;
#else
ScopedFILE f = fopen(file, "rb");
return _filelengthi64(fileno(f.file));
#endif
}
inline bool RenameFile(const char* oldFile, const char* newFile) {
return rename(oldFile, newFile) != 0;
}
inline bool CreateFolder(const char* folderName) {
#ifdef __ANDROID__
__builtin_trap();
return false;
#else
return _mkdir(folderName
#ifndef _WIN32
, 0777
#endif
) == 0;
#endif
}
inline bool IsDirectory(const char* path) {
struct stat file_info;
return stat(path, &file_info) == 0 && (S_ISDIR(file_info.st_mode));
}
enum AOpenFlag_ {
AOpenFlag_Read,
AOpenFlag_Write
};
typedef int AOpenFlag;
#ifdef __ANDROID__
struct AFile {
AAsset* asset;
};
inline AFile AFileOpen(const char* fileName, AOpenFlag flag) {
return { AAssetManager_open(g_android_app->activity->assetManager, fileName, 0) };
}
inline void AFileRead(void* dst, uint64_t size, AFile file) {
AAsset_read(file.asset, dst, size);
}
inline void AFileSeekBegin(long size, AFile file) {
AAsset_seek(file.asset, 0, SEEK_SET);
}
inline void AFileSeek(long offset, AFile file) {
AAsset_seek(file.asset, offset, SEEK_CUR);
}
inline void AFileWrite(const void* src, uint64_t size, AFile file)
{ }
inline void AFileClose(AFile file) {
AAsset_close(file.asset);
}
inline bool AFileExist(AFile file) {
return file.asset != nullptr;
}
inline uint64_t AFileSize(AFile file) {
return AAsset_getLength(file.asset);
}
#else
struct AFile {
FILE* file;
};
inline AFile AFileOpen(const char* fileName, AOpenFlag flag) {
FILE* file;
const char* modes[2] = { "rb", "wb" };
#ifdef _MSC_VER
fopen_s(&file, fileName, modes[flag]);
#else
file = fopen(fileName, modes[flag]);
#endif
AFile afile;
afile.file = file;
return afile;
}
inline void AFileRead(void* dst, uint64_t size, AFile file) {
fread(dst, 1, size, file.file);
}
inline void AFileWrite(const void* src, uint64_t size, AFile file) {
fwrite(src, 1, size, file.file);
}
inline void AFileSeekBegin(AFile file) {
fseek(file.file, 0, SEEK_SET);
}
inline void AFileSeek(long offset, AFile file) {
fseek(file.file, offset, SEEK_CUR);
}
inline void AFileClose(AFile file) {
fclose(file.file);
}
inline bool AFileExist(AFile file) {
return file.file != nullptr;
}
inline uint64_t AFileSize(AFile file) {
#if defined(_WIN32)
return _filelengthi64(_fileno(file.file));
#elif defined(__ANDROID__)
return AAsset_getLength(file.asset);
#else
if (fseek(file.file, 0, SEEK_END) != 0)
return 0; // Or handle the error as appropriate
long fileSize = ftell(file.file);
if (fileSize == -1)
return 0; // Or handle the error as appropriate
return (uint64_t)fileSize;
#endif
}
#endif
inline char* ReadAllFile(const char* fileName, char* buffer = 0) {
AFile file = AFileOpen(fileName, AOpenFlag_Read);
uint64_t fileSize = AFileSize(file);
if (buffer == nullptr)
buffer = (char*)AX_CALLOC(fileSize); // +1 for null terminator
AFileRead(buffer, fileSize, file);
AFileClose(file);
return buffer;
}
inline void FreeAllText(char* text) {
AX_FREE(text);
}
inline constexpr bool IsNumber(char a) { return a <= '9' && a >= '0'; };
inline constexpr bool IsLower(char a) { return a >= 'a' && a <= 'z'; };
inline constexpr bool IsUpper(char a) { return a >= 'A' && a <= 'Z'; };
inline constexpr bool ToLower(char a) { return a < 'a' ? a + ('A' - 'a') : a; }
inline constexpr bool ToUpper(char a) { return a > 'Z' ? a - 'a' + 'A' : a; }
// is alphabetical character?
inline constexpr bool IsChar(char a) { return IsUpper(a) || IsLower(a); };
inline constexpr bool IsWhitespace(char c) { return c <= ' '; }
template<typename T>
inline constexpr void Swap(T& a, T& b)
{
T temp = (T&&)a;
a = (T&&)b;
b = (T&&)temp;
}
// String to number functions
inline int ParseNumber(const char*& ptr)
{
const char* curr = ptr;
while (*curr && (*curr != '-' && !IsNumber(*curr)))
curr++; // skip whitespace
int val = 0l;
bool negative = false;
if (*curr == '-')
curr++, negative = true;
while (*curr > '\n' && IsNumber(*curr))
val = val * 10 + (*curr++ - '0');
ptr = curr;
return negative ? -val : val;
}
inline int ParsePositiveNumber(const char*& ptr)
{
const char* curr = ptr;
while (*curr && !IsNumber(*curr))
curr++; // skip whitespace
int val = 0;
while (*curr > '\n' && IsNumber(*curr))
val = val * 10 + (*curr++ - '0');
ptr = curr;
return val;
}
inline bool IsParsable(const char* curr)
{ // additional checks
if (*curr == 0 || *curr == '\n') return false;
if (!IsNumber(*curr) || *curr != '-') return false;
return true;
}
inline float ParseFloat(const char*& text)
{
const int MAX_POWER = 20;
const double POWER_10_POS[MAX_POWER] =
{
1.0e0, 1.0e1, 1.0e2, 1.0e3, 1.0e4, 1.0e5, 1.0e6, 1.0e7, 1.0e8, 1.0e9,
1.0e10, 1.0e11, 1.0e12, 1.0e13, 1.0e14, 1.0e15, 1.0e16, 1.0e17, 1.0e18, 1.0e19,
};
const double POWER_10_NEG[MAX_POWER] =
{
1.0e0, 1.0e-1, 1.0e-2, 1.0e-3, 1.0e-4, 1.0e-5, 1.0e-6, 1.0e-7, 1.0e-8, 1.0e-9,
1.0e-10, 1.0e-11, 1.0e-12, 1.0e-13, 1.0e-14, 1.0e-15, 1.0e-16, 1.0e-17, 1.0e-18, 1.0e-19,
};
const char* ptr = text;
while (!IsNumber(*ptr) && *ptr != '-') ptr++;
double sign = 1.0;
if(*ptr == '-')
sign = -1.0, ptr++;
double num = 0.0;
while (IsNumber(*ptr))
num = 10.0 * num + (double)(*ptr++ - '0');
if (*ptr == '.') ptr++;
double fra = 0.0, div = 1.0;
while (IsNumber(*ptr) && div < 1.0e9) // 1e8 is 1 and 8 zero 1000000000
fra = 10.0f * fra + (double)(*ptr++ - '0'), div *= 10.0f;
num += fra / div;
while (IsNumber(*ptr)) ptr++;
if (*ptr == 'e' || *ptr == 'E') // exponent
{
ptr++;
const double* powers;
switch (*ptr)
{
case '+': powers = POWER_10_POS; ptr++; break;
case '-': powers = POWER_10_NEG; ptr++; break;
default: powers = POWER_10_POS; break;
}
int eval = 0;
while (IsNumber(*ptr))
eval = 10 * eval + (*ptr++ - '0');
num *= (eval >= MAX_POWER) ? 0.0 : powers[eval];
}
text = ptr;
return (float)(sign * num);
}
#ifndef AX_NO_UNROLL
#if defined(__clang__)
# define AX_NO_UNROLL _Pragma("clang loop unroll(disable)") _Pragma("clang loop vectorize(disable)")
#elif defined(__GNUC__) >= 8
# define AX_NO_UNROLL _Pragma("GCC unroll 0")
#elif defined(_MSC_VER)
# define AX_NO_UNROLL __pragma(loop(no_vector))
#else
# define AX_NO_UNROLL
#endif
#endif
inline bool StartsWith(const char*& curr, const char* str)
{
const char* currStart = curr;
while (IsWhitespace(*curr)) curr++;
if (*curr != *str) return false;
while (*str && *curr++ == *str++);
bool isEqual = *str == 0;
if (!isEqual) curr = currStart;
return isEqual;
}
template<typename T> inline void Fill(T* begin, T* end, const T& val)
{
while (begin < end) *begin++ = val;
}
template<typename T> inline void FillN(T* arr, T val, int n)
{
for (int i = 0; i < n; ++i) arr[i] = val;
}
template<typename T> inline bool Contains(const T* arr, const T& val, int n)
{
for (int i = 0; i < n; ++i)
if (arr[i] == val) return true;
return false;
}
// returns the index if found, otherwise returns -1
template<typename T> inline int IndexOf(const T* arr, const T& val, int n)
{
for (int i = 0; i < n; ++i)
if (arr[i] == val) return i;
return -1;
}
// returns number of val contained in arr
template<typename T> inline int CountIf(const T* arr, const T& val, int n)
{
int count = 0;
for (int i = 0; i < n; ++i)
count += arr[i] == val;
return count;
}
template<typename T> inline void Copy(T* dst, const T* src, int n)
{
for (int i = 0; i < n; ++i)
dst[i] = src[i];
}
template<typename T> inline void MoveArray(T* dst, T* src, int n)
{
for (int i = 0; i < n; ++i)
dst[i] = (T&&)src[i];
}
template<typename T> inline void ClearN(T* src, int n)
{
for (int i = 0; i < n; ++i)
src[i].~T();
}
template<typename T> inline void ConstructN(T* src, int n)
{
T def{};
for (int i = 0; i < n; ++i)
src[i] = def;
}
/****************************************************************
* Memory *
*****************************************************************/
template<typename T> struct RemoveRef { typedef T Type; };
template<typename T> struct RemoveRef<T&> { typedef T Type; };
template<typename T> struct RemoveRef<T&&> { typedef T Type; };
template<typename T> struct RemovePtr { typedef T Type; };
template<typename T> struct RemovePtr<T*> { typedef T Type; };
template<typename T>
__forceinline typename RemoveRef<T>::Type&& Move(T&& obj)
{
typedef typename RemoveRef<T>::Type CastType;
return (CastType&&)obj;
}
template<typename T>
__forceinline T&& Forward(typename RemoveRef<T>::Type& obj) { return (T&&)obj; }
template<typename T>
__forceinline T&& Forward(typename RemoveRef<T>::Type&& obj) { return (T&&)obj; }