/
aot.h
3100 lines (2820 loc) · 124 KB
/
aot.h
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
#pragma once
#include "daScript/misc/callable.h"
#include "daScript/simulate/runtime_profile.h"
#include "daScript/simulate/debug_print.h"
#include "daScript/simulate/sim_policy.h"
#include "daScript/simulate/aot_builtin.h"
#include "daScript/simulate/aot_builtin_matrix.h"
#include "daScript/simulate/aot_builtin_time.h"
#include "daScript/simulate/bin_serializer.h"
#include "daScript/simulate/runtime_table.h"
#include "daScript/simulate/interop.h"
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4946) // reinterpret_cast used between related classes
#endif
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#if __GNUC__>=9
#pragma GCC diagnostic ignored "-Wclass-memaccess"
#endif
#pragma GCC diagnostic ignored "-Wsizeof-pointer-memaccess"
#endif
namespace das {
#define DAS_SETBOOLOR(a,b) (([&]()->bool{ bool & A=((a)); A=A||((b)); return A; })())
#define DAS_SETBOOLAND(a,b) (([&]()->bool{ bool & A=((a)); A=A&&((b)); return A; })())
#define DAS_SETBOOLXOR(a,b) (([&]()->bool{ bool & A=((a)); A=A^((b)); return A; })())
#define DAS_MAKE_ANNOTATION(name) ((TypeAnnotation*)(intptr_t(name)|1))
#define DAS_COMMENT(...)
#define DAS_STRINGIFY(x) #x
#define DAS_TOSTRING(x) DAS_STRINGIFY(x)
#define DAS_FILE_LINE_NAME(a,b) a " at line " DAS_TOSTRING(b)
#define DAS_FILE_LINE DAS_FILE_LINE_NAME(__FILE__,__LINE__)
void das_debug ( Context * context, TypeInfo * typeInfo, const char * FILE, int LINE, vec4f res, const char * message = nullptr );
template <typename TT>
struct das_auto_cast {
template <typename QQ>
__forceinline static TT cast ( QQ && expr ) {
return (TT) expr;
}
};
template <typename TT>
struct das_auto_cast_ref {
template <typename QQ>
__forceinline static TT & cast ( QQ & expr ) {
return expr;
}
template <typename QQ>
__forceinline static const TT & cast ( const QQ & expr ) {
return expr;
}
};
template <typename TT>
struct das_auto_cast_move {
template <typename QQ>
__forceinline static TT cast ( QQ && expr ) {
TT res = expr;
memset(&expr, 0, sizeof(QQ));
return res;
}
};
template <typename TT>
__forceinline void das_zero ( TT & a ) {
using TTNC = typename remove_const<TT>::type;
memset(const_cast<TTNC *>(&a), 0, sizeof(TT));
}
template <typename TT, typename QQ>
__forceinline void das_move ( TT & a, const QQ & b ) {
using TTNC = typename remove_const<TT>::type;
static_assert(sizeof(TT)<=sizeof(QQ),"can't move from smaller type");
if ( ((void*)&a) != ((void *)&b) ) {
memcpy(const_cast<TTNC *>(&a), &b, sizeof(TT));
memset((TTNC *)&b, 0, sizeof(TT));
}
}
template <typename TT, typename QQ>
__forceinline void das_copy ( TT & a, const QQ b ) {
using TTNC = typename remove_const<TT>::type;
static_assert(sizeof(TT)<=sizeof(QQ),"can't copy from smaller type");
memcpy(const_cast<TTNC *>(&a), &b, sizeof(TT));
}
template <typename TT>
__forceinline bool das_equ ( const TT & a, const TT & b ) {
return a == b;
}
template <typename TT>
__forceinline bool das_nequ ( const TT & a, const TT & b ) {
return a != b;
}
template <typename TT, typename QQ>
__forceinline bool das_equ_val ( TT a, QQ b ) {
return a == b;
}
template <typename TT, typename QQ>
__forceinline bool das_nequ_val ( TT a, QQ b ) {
return a != b;
}
template <typename TT, uint64_t mnh>
__forceinline TT & das_global ( Context * __context__ ) {
uint32_t offset = __context__->globalOffsetByMangledName(mnh);
return *(TT *)(__context__->globals + offset);
}
template <typename TT, uint64_t mnh>
__forceinline void das_global_zero ( Context * __context__ ) {
uint32_t offset = __context__->globalOffsetByMangledName(mnh);
memset(__context__->globals + offset, 0, sizeof(TT));
}
template <typename TT, uint32_t offset>
__forceinline TT & das_global_solid ( Context * __context__ ) {
return *(TT *)(__context__->globals + offset);
}
template <typename TT, uint32_t offset>
__forceinline void das_global_zero_solid ( Context * __context__ ) {
memset(__context__->globals + offset, 0, sizeof(TT));
}
template <typename TT, uint64_t mnh>
__forceinline TT & das_shared ( Context * __context__ ) {
uint32_t offset = __context__->globalOffsetByMangledName(mnh);
return *(TT *)(__context__->shared + offset);
}
template <typename TT, uint64_t mnh>
__forceinline void das_shared_zero ( Context * __context__ ) {
uint32_t offset = __context__->globalOffsetByMangledName(mnh);
memset(__context__->shared + offset, 0, sizeof(TT));
}
template <typename TT, uint32_t offset>
__forceinline TT & das_shared_solid ( Context * __context__ ) {
return *(TT *)(__context__->shared + offset);
}
template <typename TT, uint32_t offset>
__forceinline void das_shared_zero_solid ( Context * __context__ ) {
memset(__context__->shared + offset, 0, sizeof(TT));
}
__forceinline int das_memcmp ( void * left, void * right, int size ) {
return memcmp(left, right, size);
}
__forceinline void das_memcpy ( void * left, void * right, int size ) {
memcpy(left, right, size);
}
__forceinline void das_memset8 ( void * left, uint8_t value, int size ) {
memset(left, value, size);
}
__forceinline void das_memset128u ( void * left, vec4f value, int size ) {
vec4f * ptr = (vec4f *) left;
if ( size&1 ) { v_stu(ptr, value); ptr ++; size --; }
if ( size&3 ) { v_stu(ptr+0, value); v_stu(ptr+1, value); ptr += 2; size -= 2; }
if ( size&7 ) { v_stu(ptr+0, value); v_stu(ptr+1, value); v_stu(ptr+2, value); v_stu(ptr+3, value); ptr += 4; size -= 4; }
while ( size ) {
v_stu(ptr+0, value); v_stu(ptr+1, value); v_stu(ptr+2, value); v_stu(ptr+3, value);
v_stu(ptr+4, value); v_stu(ptr+5, value); v_stu(ptr+6, value); v_stu(ptr+7, value);
ptr += 8; size -= 8;
}
}
__forceinline void das_memset64 ( void * left, uint64_t value, int size ) {
uint64_t * ptr = (uint64_t *) left;
if ( size&1 ) { ptr[0] = value; ptr ++; size --; }
uint64_t tv[2] = { value, value };
das_memset128u(ptr, v_ldu((const float *)tv), size>>1);
}
__forceinline void das_memset32 ( void * left, uint32_t value, int size ) {
uint32_t * ptr = (uint32_t *) left;
if ( size&1 ) { ptr[0] = value; ptr ++; size --; }
das_memset64(ptr, value | (uint64_t(value)<<32), size>>1);
}
__forceinline void das_memset16 ( void * left, uint16_t value, int size ) {
uint16_t * ptr = (uint16_t *) left;
if ( size&1 ) { ptr[0] = value; ptr ++; size --; }
das_memset32(ptr, value | (uint32_t(value)<<16), size>>1);
}
template <typename TT>
__forceinline void das_ptr_inc ( TT * & ptr, int ) {
++ ptr;
}
template <typename TT>
__forceinline void das_ptr_dec ( TT * & ptr, int ) {
-- ptr;
}
template <typename TT>
__forceinline TT * das_ptr_add ( TT * ptr, int value, int ) {
return ptr + value;
}
template <typename TT>
__forceinline TT * das_ptr_sub ( TT * & ptr, int value, int ) {
return ptr - value;
}
template <typename TT>
__forceinline void das_ptr_set_add ( TT * & ptr, int value, int ) {
ptr += value;
}
template <typename TT>
__forceinline void das_ptr_set_sub ( TT * & ptr, int value, int ) {
ptr -= value;
}
__forceinline int64_t i_das_ptr_diff ( void * a, void *b, int stride ) {
return int64_t((char *)a - (char *)b) / int64_t(stride);
}
__forceinline char * das_string_cast ( const char * any ) {
return (char *) ( any ? any : "" );
}
template <typename TT>
struct das_arg {
static __forceinline TT & pass ( TT && a ) {
return *((TT *)&a);
}
static __forceinline TT & pass ( TT & a ) {
return a;
}
static __forceinline const TT & pass ( const TT && a ) {
return *((const TT *)&a);
}
static __forceinline const TT & pass ( const TT & a ) {
return a;
}
};
template <typename TT>
struct das_reinterpret {
template <typename QQ>
static __forceinline TT & pass ( QQ && a ) {
return *((TT *)&a);
}
template <typename QQ>
static __forceinline TT & pass ( QQ & a ) {
return *((TT *)&a);
}
template <typename QQ>
static __forceinline const TT & pass ( const QQ & a ) {
return *((const TT *)&a);
}
};
template <typename TT>
struct das_reinterpret<const TT> : das_reinterpret<TT> {};
template <>
struct das_reinterpret<vec4f>;
template <>
struct das_reinterpret<const vec4f>;
template <typename TT>
struct das_typedecl_value {
TT operator () () const {
return TT();
};
};
template <typename ResT,typename VecT, int index>
struct das_swizzle_ref {
static __forceinline ResT & swizzle ( VecT & val ) {
return *((ResT *)((&val.x) + index));
}
static __forceinline ResT & swizzle ( vec4f & valV ) {
VecT & val = *((VecT *)&valV);
return *((ResT *)((&val.x) + index));
}
};
template <typename ResT,typename VecT, int index>
struct das_swizzle_seq {
static __forceinline const ResT & swizzle ( const VecT & val ) {
return *((const ResT *)((&val.x) + index));
}
};
template <typename ResT,typename VecT, int f0, int f1, int f2 = 0, int f3 = 0>
struct das_swizzle {
static __forceinline ResT swizzle ( const VecT & val ) {
ResT res;
res.x = *((&val.x) + f0);
res.y = *((&val.x) + f1);
res.z = *((&val.x) + f2);
res.w = *((&val.x) + f3);
return res;
}
static __forceinline ResT swizzle ( vec4f valT ) {
return swizzle(cast<VecT>::to(valT));
}
};
template <typename ResT,typename VecT, int f0, int f1, int f2>
struct das_swizzle<ResT,VecT,f0,f1,f2,0> {
static __forceinline ResT swizzle ( const VecT & val ) {
ResT res;
res.x = *((&val.x) + f0);
res.y = *((&val.x) + f1);
res.z = *((&val.x) + f2);
return res;
}
static __forceinline ResT swizzle ( vec4f valT ) {
return swizzle(cast<VecT>::to(valT));
}
};
template <typename ResT,typename VecT, int f0, int f1>
struct das_swizzle<ResT,VecT,f0,f1,0,0> {
static __forceinline ResT swizzle ( const VecT & val ) {
ResT res;
res.x = *((&val.x) + f0);
res.y = *((&val.x) + f1);
return res;
}
static __forceinline ResT swizzle ( vec4f valT ) {
return swizzle(cast<VecT>::to(valT));
}
};
__forceinline bool das_get_bitfield ( uint32_t value, uint32_t mask ) {
return (value & mask) == mask;
}
template <typename TT>
struct cast_vec_ref {
static __forceinline TT & to(vec4f & x) {
return *(TT *)(&x);
}
static __forceinline TT & to(TT & x) {
return x;
}
static __forceinline const TT & to(const vec4f & x) {
return *(TT *)(&x);
}
static __forceinline const TT & to(const TT & x) {
return x;
}
};
template <typename TT>
struct das_cast {
template <typename QQ>
static __forceinline TT & cast ( const QQ & expr ) {
return reinterpret_cast<TT&>(const_cast<QQ &>(expr));
}
};
template <typename TT>
struct das_cast<TT *> {
template <typename QQ>
static __forceinline TT * cast ( QQ * expr ) {
return reinterpret_cast<TT *>(expr);
}
static __forceinline TT * cast ( nullptr_t ) {
return nullptr;
}
template <typename QQ>
static __forceinline TT * cast ( const QQ * expr ) {
return const_cast<TT *>(reinterpret_cast<const TT *>(expr));
}
template <typename QQ>
static __forceinline TT * cast ( const smart_ptr_raw<QQ> & expr ) {
return reinterpret_cast<TT *>(expr.get());
}
template <typename QQ>
static __forceinline TT * cast ( const smart_ptr<QQ> & expr ) {
return reinterpret_cast<TT *>(expr.get());
}
template <typename QQ>
static __forceinline TT * cast ( const QQ & expr ) {
return const_cast<TT *>(reinterpret_cast<const TT *>(&expr));
}
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4312) // reinterpret_cast used between related classes
#endif
static __forceinline TT * cast ( int32_t value ) { return (TT *) value; }
static __forceinline TT * cast ( uint32_t value ) { return (TT *) value; }
static __forceinline TT * cast ( int64_t value ) { return (TT *) value; }
static __forceinline TT * cast ( uint64_t value ) { return (TT *) value; }
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
};
template <typename TT>
struct das_upcast {
template <typename QQ>
static __forceinline TT & cast ( const QQ & expr ) {
return reinterpret_cast<TT &>(const_cast<QQ &>(expr));
}
};
template <typename TT>
struct das_upcast<TT *> {
template <typename QQ>
static __forceinline TT * cast ( QQ * expr ) {
return (TT *)(expr);
}
};
template <typename TT>
struct das_null_coalescing {
static __forceinline TT get ( const TT * ptr, TT value ) {
return ptr ? *((TT *)ptr) : value;
}
template <typename LPTRX>
static __forceinline TT get ( LPTRX ** ptr, nullptr_t pqq ) {
return ptr ? *((TT *)ptr) : ((TT)pqq);
}
static __forceinline TT get ( const smart_ptr_raw<TT> & sp, TT value ) {
TT * ptr = sp.get();
return ptr ? *((TT *)ptr) : value;
}
};
template <typename TT>
struct das_null_coalescing<TT &> {
static __forceinline TT & get ( const TT * ptr, TT & value ) {
return ptr ? *((TT *)ptr) : value;
}
static __forceinline TT get ( const smart_ptr_raw<TT> & sp, TT & value ) {
TT * ptr = sp.get();
return ptr ? *((TT *)ptr) : value;
}
};
template <typename TT>
struct das_null_coalescing<const TT &> {
static __forceinline const TT & get ( const TT * ptr, const TT & value ) {
return ptr ? *ptr : value;
}
static __forceinline TT get ( const smart_ptr_raw<TT> & sp, const TT & value ) {
TT * ptr = sp.get();
return ptr ? *ptr : value;
}
};
template <typename TT, typename RR, RR TT::*Member>
struct das_safe_navigation {
static __forceinline RR * get ( const TT * ptr ) {
if ( ptr ) {
return (RR *) &(ptr->*Member);
} else {
return nullptr;
}
}
static __forceinline RR * get ( const smart_ptr_raw<TT> & sp ) {
TT * ptr = sp.get();
if ( ptr ) {
return (RR *) &(ptr->*Member);
} else {
return nullptr;
}
}
};
template <typename TT, typename RR, RR TT::*Member>
struct das_safe_navigation_ptr {
static __forceinline RR get ( const TT * ptr ) {
if ( ptr ) {
return (RR)(ptr->*Member);
} else {
return nullptr;
}
}
static __forceinline RR get ( const smart_ptr_raw<TT> & sp ) {
TT * ptr = sp.get();
if ( ptr ) {
return (RR)(ptr->*Member);
} else {
return nullptr;
}
}
};
template <typename TT, typename RR>
struct das_safe_navigation_handle {
template <typename QQ>
static __forceinline RR * get ( const TT * ptr, QQ && nav ) {
if ( ptr ) {
return (RR *) & nav(ptr);
} else {
return nullptr;
}
}
template <typename QQ>
static __forceinline RR * get ( const smart_ptr_raw<TT> & sp, QQ && nav ) {
TT * ptr = sp.get();
if ( ptr ) {
return (RR *) & nav(ptr);
} else {
return nullptr;
}
}
};
template <typename TT, typename RR>
struct das_safe_navigation_handle_ptr {
template <typename QQ>
static __forceinline RR get ( const TT * ptr, QQ && nav ) {
if ( ptr ) {
return (RR) nav(ptr);
} else {
return nullptr;
}
}
template <typename QQ>
static __forceinline RR get ( const smart_ptr_raw<TT> & sp, QQ && nav ) {
TT * ptr = sp.get();
if ( ptr ) {
return (RR) nav(ptr);
} else {
return nullptr;
}
}
};
template <typename TT>
struct das_index;
template <typename TT>
struct das_default_vector_size;
template <typename TT>
struct das_default_vector_size<vector<TT>> {
static __forceinline uint32_t size( const vector<TT> & value ) {
return uint32_t(value.size());
}
};
template <typename TT, typename OT>
struct das_default_vector_index {
using SIZE_POLICY = das_default_vector_size<TT>;
static __forceinline OT & at ( TT & value, int32_t index, Context * __context__ ) {
uint32_t size = SIZE_POLICY::size(value);
uint32_t idx = uint32_t(index);
if ( idx>=size ) __context__->throw_error_ex("vector index out of range, %u of %u", idx, size);
return value[index];
}
static __forceinline const OT & at ( const TT & value, int32_t index, Context * __context__ ) {
uint32_t size = SIZE_POLICY::size(value);
uint32_t idx = uint32_t(index);
if ( idx>=size ) __context__->throw_error_ex("vector index out of range, %u of %u", idx, size);
return value[index];
}
static __forceinline OT & at ( TT & value, uint32_t idx, Context * __context__ ) {
uint32_t size = SIZE_POLICY::size(value);
if ( idx>=size ) __context__->throw_error_ex("vector index out of range, %u of %u", idx, size);
return value[idx];
}
static __forceinline const OT & at ( const TT & value, uint32_t idx, Context * __context__ ) {
uint32_t size = SIZE_POLICY::size(value);
if ( idx>=size ) __context__->throw_error_ex("vector index out of range, %u of %u", idx, size);
return value[idx];
}
};
template <typename TT, typename OT>
__forceinline OT & das_ati ( TT & value, int32_t index, Context * __context__, LineInfoArg * __info__ ) {
using SIZE_POLICY = das_default_vector_size<TT>;
uint32_t size = SIZE_POLICY::size(value);
uint32_t idx = uint32_t(index);
if ( idx>=size ) __context__->throw_error_at(__info__,"vector index out of range, %u of %u", idx, size);
return value[index];
}
template <typename TT, typename OT>
__forceinline OT & das_atu ( TT & value, uint32_t idx, Context * __context__, LineInfoArg * __info__ ) {
using SIZE_POLICY = das_default_vector_size<TT>;
uint32_t size = SIZE_POLICY::size(value);
if ( idx>=size ) __context__->throw_error_at(__info__,"vector index out of range, %u of %u", idx, size);
return value[idx];
}
template <typename TT, typename OT>
__forceinline const OT & das_atci ( const TT & value, int32_t index, Context * __context__, LineInfoArg * __info__ ) {
using SIZE_POLICY = das_default_vector_size<TT>;
uint32_t size = SIZE_POLICY::size(value);
uint32_t idx = uint32_t(index);
if ( idx>=size ) __context__->throw_error_at(__info__,"vector index out of range, %u of %u", idx, size);
return value[index];
}
template <typename TT, typename OT>
__forceinline const OT & das_atcu ( const TT & value, uint32_t idx, Context * __context__, LineInfoArg * __info__ ) {
using SIZE_POLICY = das_default_vector_size<TT>;
uint32_t size = SIZE_POLICY::size(value);
if ( idx>=size ) __context__->throw_error_at(__info__,"vector index out of range, %u of %u", idx, size);
return value[idx];
}
template <typename TT>
struct das_index<vector<TT>> : das_default_vector_index<vector<TT>, TT> {};
template <typename TT>
struct das_index<vector<TT> const> : das_default_vector_index<vector<TT>, TT> {};
template <typename TT, typename VecT, uint32_t size>
struct das_vec_index {
// index
static __forceinline TT & at ( VecT & value, int32_t index, Context * __context__ ) {
uint32_t idx = uint32_t(index);
if ( idx>=size ) __context__->throw_error_ex("vector index out of range, %u of %u", idx, size);
return (&value.x)[idx];
}
static __forceinline const TT & at ( const VecT & value, int32_t index, Context * __context__ ) {
uint32_t idx = uint32_t(index);
if ( idx>=size ) __context__->throw_error_ex("vector index out of range, %u of %u", idx, size);
return (&value.x)[idx];
}
static __forceinline TT & at ( VecT & value, uint32_t idx, Context * __context__ ) {
if ( idx>=size ) __context__->throw_error_ex("vector index out of range, %u of %u", idx, size);
return (&value.x)[idx];
}
static __forceinline const TT & at ( const VecT & value, uint32_t idx, Context * __context__ ) {
if ( idx>=size ) __context__->throw_error_ex("vector index out of range, %u of %u", idx, size);
return (&value.x)[idx];
}
// safe index
static __forceinline TT * safe_at ( VecT * value, int32_t index, Context * ) {
if (!value) return nullptr;
uint32_t idx = uint32_t(index);
if (idx >= size) return nullptr;
return (&value->x) + idx;
}
static __forceinline const TT * safe_at ( const VecT * value, int32_t index, Context * ) {
if (!value) return nullptr;
uint32_t idx = uint32_t(index);
if ( idx>=size ) return nullptr;
return (&value->x) + idx;
}
static __forceinline TT * safe_at ( VecT * value, uint32_t idx, Context * ) {
if (!value) return nullptr;
if ( idx>=size ) return nullptr;
return (&value->x) + idx;
}
static __forceinline const TT * safe_at ( const VecT * value, uint32_t idx, Context * ) {
if (!value) return nullptr;
if ( idx>=size ) return nullptr;
return (&value->x) + idx;
}
};
template <typename TT> struct das_index<vec2<TT>> : das_vec_index<TT, vec2<TT>, 2> {};
template <typename TT> struct das_index<vec3<TT>> : das_vec_index<TT, vec3<TT>, 3> {};
template <typename TT> struct das_index<vec4<TT>> : das_vec_index<TT, vec4<TT>, 4> {};
template <typename TT> struct das_index<const vec2<TT>> : das_vec_index<TT, vec2<TT>, 2> {};
template <typename TT> struct das_index<const vec3<TT>> : das_vec_index<TT, vec3<TT>, 3> {};
template <typename TT> struct das_index<const vec4<TT>> : das_vec_index<TT, vec4<TT>, 4> {};
template<> struct das_index<range> : das_vec_index<int32_t, range, 2> {};
template<> struct das_index<const range> : das_vec_index<int32_t, range, 2> {};
template<> struct das_index<urange> : das_vec_index<uint32_t, urange, 2> {};
template<> struct das_index<const urange> : das_vec_index<uint32_t, urange, 2> {};
template<> struct das_index<range64> : das_vec_index<int64_t, range64, 2> {};
template<> struct das_index<const range64> : das_vec_index<int64_t, range64, 2> {};
template<> struct das_index<urange64> : das_vec_index<uint64_t, urange64, 2> {};
template<> struct das_index<const urange64> : das_vec_index<uint64_t, urange64, 2> {};
template <typename VecT, int size>
struct das_index<Matrix<VecT,size>> {
using MatT = Matrix<VecT,size>;
static __forceinline VecT & at ( MatT & value, int32_t index, Context * __context__ ) {
uint32_t idx = uint32_t(index);
if ( idx>=uint32_t(size) ) __context__->throw_error_ex("matrix index out of range, %u of %u", idx, size);
return value.m[idx];
}
static __forceinline VecT & at ( MatT & value, uint32_t idx, Context * __context__ ) {
if ( idx>=uint32_t(size) ) __context__->throw_error_ex("matrix index out of range, %u of %u", idx, size);
return value.m[idx];
}
};
template <typename VecT, int size>
struct das_index<const Matrix<VecT,size>> {
using MatT = Matrix<VecT,size>;
static __forceinline const VecT & at ( const MatT & value, int32_t index, Context * __context__ ) {
uint32_t idx = uint32_t(index);
if ( idx>=uint32_t(size) ) __context__->throw_error_ex("matrix index out of range, %u of %u", idx, size);
return value.m[idx];
}
static __forceinline const VecT & at ( const MatT & value, uint32_t idx, Context * __context__ ) {
if ( idx>=uint32_t(size) ) __context__->throw_error_ex("matrix index out of range, %u of %u", idx, size);
return value.m[idx];
}
};
template <typename TT>
struct das_index<TT * const> {
static __forceinline TT & at ( TT * value, int32_t index, Context * ) {
return value[index];
}
static __forceinline TT & at ( TT * value, uint32_t index, Context * ) {
return value[index];
}
};
template <typename TT>
struct das_index<const TT * const> {
static __forceinline const TT & at ( const TT * value, int32_t index, Context * ) {
return value[index];
}
static __forceinline const TT & at ( const TT * value, uint32_t index, Context * ) {
return value[index];
}
};
template <typename TT>
struct das_index<TT *> {
static __forceinline TT & at ( TT * value, int32_t index, Context * ) {
return value[index];
}
static __forceinline TT & at ( TT * value, uint32_t index, Context * ) {
return value[index];
}
};
template <>
struct das_index<char * const * const> {
static __forceinline char * & at ( char * const * const value, int32_t index, Context * ) {
return (char * &)(value[index]);
}
static __forceinline char * & at ( const char **const value, int32_t index, Context * ) {
return (char * &)(value[index]);
}
};
template <typename TT, int size>
struct TDim {
using THIS_TYPE = TDim<TT, size>;
enum { capacity = size };
TT data[size];
TDim() {}
__forceinline TT & operator [] ( int32_t index ) {
return data[index];
}
__forceinline const TT & operator [] ( int32_t index ) const {
return data[index];
}
// index
__forceinline TT & operator () ( int32_t index, Context * __context__ ) {
uint32_t idx = uint32_t(index);
if ( idx>=uint32_t(size) ) __context__->throw_error_ex("index out of range, %u of %u", idx, size);
return data[index];
}
__forceinline const TT & operator () ( int32_t index, Context * __context__ ) const {
uint32_t idx = uint32_t(index);
if ( idx>=uint32_t(size) ) __context__->throw_error_ex("index out of range, %u of %u", idx, size);
return data[index];
}
__forceinline TT & operator () ( uint32_t idx, Context * __context__ ) {
if ( idx>=uint32_t(size) ) __context__->throw_error_ex("index out of range, %u of %u", idx, size);
return data[idx];
}
__forceinline const TT & operator () ( uint32_t idx, Context * __context__ ) const {
if ( idx>=uint32_t(size) ) __context__->throw_error_ex("index out of range, %u of %u", idx, size);
return data[idx];
}
// safe index
static __forceinline TT * safe_index ( THIS_TYPE * that, int32_t index, Context * ) {
if (!that) return nullptr;
uint32_t idx = uint32_t(index);
if (idx >= uint32_t(size)) return nullptr;
return that->data + index;
}
static __forceinline const TT * safe_index ( const THIS_TYPE * that, int32_t index, Context * ) {
if (!that) return nullptr;
uint32_t idx = uint32_t(index);
if ( idx>=uint32_t(size) ) return nullptr;
return that->data + index;
}
static __forceinline TT * safe_index ( THIS_TYPE * that, uint32_t idx, Context * ) {
if (!that) return nullptr;
if ( idx>=uint32_t(size) ) return nullptr;
return that->data + idx;
}
static __forceinline const TT * safe_index ( const THIS_TYPE * that, uint32_t idx, Context * ) {
if (!that) return nullptr;
if ( idx>=uint32_t(size) ) return nullptr;
return that->data + idx;
}
};
__forceinline bool is_char_in_set ( int32_t ch, const TDim<uint32_t,8> & bitset, Context * ctx, LineInfoArg * at ) {
if ( ch<0 || ch>=256 ) ctx->throw_error_at(at,"invalid character %d", ch);
return bitset[ch>>5] & (1u<<uint32_t(ch));
}
template <typename TT, int size>
struct das_cast< TDim<TT,size> > {
template <typename QQ>
static __forceinline TDim<TT,size> & cast ( const QQ * expr ) {
return *((TDim<TT,size> *)expr);
}
};
template <typename TT, int size>
struct das_cast< const TDim<TT,size> > {
template <typename QQ>
static __forceinline const TDim<TT,size> & cast ( const QQ * expr ) {
return *((const TDim<TT,size> *)expr);
}
};
template <typename TT>
struct TArray : Array {
using THIS_TYPE = TArray<TT>;
enum { stride = sizeof(TT) };
TArray() {}
TArray(TArray & arr) { moveA(arr); }
TArray(TArray && arr ) { moveA(arr); }
TArray & operator = ( TArray & arr ) { moveA(arr); return *this; }
TArray & operator = ( TArray && arr ) { moveA(arr); return *this; }
__forceinline void moveA ( Array & arr ) {
data = arr.data; arr.data = 0;
size = arr.size; arr.size = 0;
capacity = arr.capacity; arr.capacity = 0;
lock = arr.lock; arr.lock = 0;
flags = arr.flags; arr.flags = 0;
}
__forceinline TT & operator [] ( int32_t index ) {
return ((TT *)data)[index];
}
__forceinline const TT & operator [] ( int32_t index ) const {
return ((TT *)data)[index];
}
// index
__forceinline TT & operator () ( int32_t index, Context * __context__ ) {
uint32_t idx = uint32_t(index);
if ( idx>=size ) __context__->throw_error_ex("array index out of range, %u of %u", idx, size);
return ((TT *)data)[index];
}
__forceinline const TT & operator () ( int32_t index, Context * __context__ ) const {
uint32_t idx = uint32_t(index);
if ( idx>=size ) __context__->throw_error_ex("array index out of range, %u of %u", idx, size);
return ((const TT *)data)[index];
}
__forceinline TT & operator () ( uint32_t idx, Context * __context__ ) {
if ( idx>=size ) __context__->throw_error_ex("array index out of range, %u of %u", idx, size);
return ((TT *)data)[idx];
}
__forceinline const TT & operator () ( uint32_t idx, Context * __context__ ) const {
if ( idx>=size ) __context__->throw_error_ex("array index out of range, %u of %u", idx, size);
return ((const TT *)data)[idx];
}
// safe index
static __forceinline TT * safe_index ( THIS_TYPE * that, int32_t index, Context * ) {
if (!that) return nullptr;
uint32_t idx = uint32_t(index);
if (idx >= that->size) return nullptr;
return ((TT *)that->data) + index;
}
static __forceinline const TT * safe_index ( const THIS_TYPE * that, int32_t index, Context * ) {
if (!that) return nullptr;
uint32_t idx = uint32_t(index);
if ( idx>=that->size ) return nullptr;
return ((const TT *)that->data) + index;
}
static __forceinline TT * safe_index ( THIS_TYPE * that, uint32_t idx, Context * ) {
if (!that) return nullptr;
if ( idx>=that->size ) return nullptr;
return ((TT *)that->data) + idx;
}
static __forceinline const TT * safe_index ( const THIS_TYPE * that, uint32_t idx, Context * ) {
if (!that) return nullptr;
if ( idx>=that->size ) return nullptr;
return ((const TT *)that->data) + idx;
}
};
template <typename TT>
struct is_workhorse_type {
enum {
value =
is_enum<TT>::value
|| is_pointer<TT>::value
|| is_arithmetic<TT>::value
|| is_same<TT,bool>::value
|| is_same<TT,Bitfield>::value
|| is_same<TT,range>::value
|| is_same<TT,urange>::value
|| is_same<TT,range64>::value
|| is_same<TT,urange64>::value
|| is_same<TT,int2>::value
|| is_same<TT,int3>::value
|| is_same<TT,int4>::value
|| is_same<TT,uint2>::value
|| is_same<TT,uint3>::value
|| is_same<TT,uint4>::value
|| is_same<TT,float2>::value
|| is_same<TT,float3>::value
|| is_same<TT,float4>::value
};
};
template <typename TK, typename TV>
struct TTable : Table {
static_assert(is_workhorse_type<TK>::value,"only supported for `workhorse` types");
using THIS_TYPE = TTable<TK, TV>;
TTable() {}
TTable(TTable & arr) { moveT(arr); }
TTable(TTable && arr ) { moveT(arr); }
TTable & operator = ( TTable & arr ) { moveT(arr); return *this; }
TTable & operator = ( TTable && arr ) { moveT(arr); return *this; }
__forceinline void moveT ( Table & arr ) {
data = arr.data; arr.data = 0;
size = arr.size; arr.size = 0;
capacity = arr.capacity; arr.capacity = 0;
lock = arr.lock; arr.lock = 0;
flags = arr.flags; arr.flags = 0;
keys = arr.keys; arr.keys = 0;
hashes = arr.hashes; arr.hashes = 0;
maxLookups = arr.maxLookups; arr.maxLookups = 0;
shift = arr.shift; arr.shift = 0;
}
__forceinline TV & operator () ( const TK & key, Context * __context__ ) {
TableHash<TK> thh(__context__,sizeof(TV));
auto hfn = hash_function(*__context__, key);
int index = thh.reserve(*this, key, hfn);
return ((TV *)data)[index];
}
static __forceinline TV * safe_index ( THIS_TYPE * that, const TK & key, Context * __context__ ) {
if (!that) return nullptr;
TableHash<TK> thh(__context__,sizeof(TV));
auto hfn = hash_function(*__context__, key);
int index = thh.find(*that, key, hfn);
return index==-1 ? nullptr : ((TV *)that->data) + index;
}
static __forceinline const TV * safe_index ( const THIS_TYPE * that, const TK & key, Context * __context__ ) {
if (!that) return nullptr;
TableHash<TK> thh(__context__,sizeof(TV));
auto hfn = hash_function(*__context__, key);
int index = thh.find(*that, key, hfn);
return index==-1 ? nullptr : ((const TV *)that->data) + index;
}
};
template <typename TK>
struct TTable<TK,void> : Table {
static_assert(is_workhorse_type<TK>::value,"only supported for `workhorse` types");
using THIS_TYPE = TTable<TK, void>;
TTable() {}
TTable(TTable & arr) { moveT(arr); }
TTable(TTable && arr ) { moveT(arr); }
TTable & operator = ( TTable & arr ) { moveT(arr); return *this; }
TTable & operator = ( TTable && arr ) { moveT(arr); return *this; }
__forceinline void moveT ( Table & arr ) {
data = arr.data; arr.data = 0;
size = arr.size; arr.size = 0;
capacity = arr.capacity; arr.capacity = 0;
lock = arr.lock; arr.lock = 0;
flags = arr.flags; arr.flags = 0;
keys = arr.keys; arr.keys = 0;
hashes = arr.hashes; arr.hashes = 0;
maxLookups = arr.maxLookups; arr.maxLookups = 0;
shift = arr.shift; arr.shift = 0;
}
};
template <int tupleSize, typename ...TA>
struct TTuple : Tuple {
TTuple() {}
TTuple(const TTuple & arr) { moveT(arr); }
TTuple(TTuple && arr ) { moveT(arr); }
TTuple & operator = ( const TTuple & arr ) { moveT(arr); return *this; }
TTuple & operator = ( TTuple && arr ) { moveT(arr); return *this; }