1 |
|
---|
2 |
|
---|
3 | // a type whose size is n
|
---|
4 | #define Z(n) char[n]
|
---|
5 |
|
---|
6 | // the inverse of Z(-)
|
---|
7 | #define z(Zn) sizeof(Zn)
|
---|
8 |
|
---|
9 | // if you're expecting a Z(n), say so, by asking for a ztype, instead of dtype or otype
|
---|
10 | #define ztype(Zn) Zn & | sized(Zn)
|
---|
11 |
|
---|
12 | forall( T & ) struct tag {};
|
---|
13 | #define ttag(T) ((tag(T)){})
|
---|
14 | #define ztag(n) ttag(Z(n))
|
---|
15 |
|
---|
16 |
|
---|
17 | //
|
---|
18 | // Single-dim array sruct (with explicit packing and atom)
|
---|
19 | //
|
---|
20 |
|
---|
21 | forall( ztype(Zn), ztype(S), Timmed &, Tbase & ) {
|
---|
22 | struct arpk {
|
---|
23 | S strides[z(Zn)];
|
---|
24 | };
|
---|
25 |
|
---|
26 | Timmed & ?[?]( arpk(Zn, S, Timmed, Tbase) & a, ptrdiff_t i ) {
|
---|
27 | return (Timmed &) a.strides[i];
|
---|
28 | }
|
---|
29 |
|
---|
30 | // workaround #226 (and array relevance thereof demonstrated in mike102/otype-slow-ndims.cfa)
|
---|
31 | void ?{}( arpk(Zn, S, Timmed, Tbase) & this ) {
|
---|
32 | void ?{}( S (&inner)[z(Zn)] ) {}
|
---|
33 | ?{}(this.strides);
|
---|
34 | }
|
---|
35 | void ^?{}( arpk(Zn, S, Timmed, Tbase) & this ) {
|
---|
36 | void ^?{}( S (&inner)[z(Zn)] ) {}
|
---|
37 | ^?{}(this.strides);
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | //
|
---|
42 | // Sugar for declaring array structure instances
|
---|
43 | //
|
---|
44 |
|
---|
45 | forall( Te )
|
---|
46 | Te mkar_( tag(Te) ) {}
|
---|
47 |
|
---|
48 | forall( ztype(Zn), ZTags ... , Trslt &, Tatom & | { Trslt mkar_( tag(Tatom), ZTags ); } )
|
---|
49 | arpk(Zn, Trslt, Trslt, Tatom) mkar_( tag(Tatom), tag(Zn), ZTags ) {}
|
---|
50 |
|
---|
51 | // stolen from https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros
|
---|
52 |
|
---|
53 | // Make a FOREACH macro
|
---|
54 | #define FE_0(WHAT)
|
---|
55 | #define FE_1(WHAT, X) WHAT(X)
|
---|
56 | #define FE_2(WHAT, X, ...) WHAT(X)FE_1(WHAT, __VA_ARGS__)
|
---|
57 | #define FE_3(WHAT, X, ...) WHAT(X)FE_2(WHAT, __VA_ARGS__)
|
---|
58 | #define FE_4(WHAT, X, ...) WHAT(X)FE_3(WHAT, __VA_ARGS__)
|
---|
59 | #define FE_5(WHAT, X, ...) WHAT(X)FE_4(WHAT, __VA_ARGS__)
|
---|
60 | //... repeat as needed
|
---|
61 |
|
---|
62 | #define GET_MACRO(_0,_1,_2,_3,_4,_5,NAME,...) NAME
|
---|
63 | #define FOR_EACH(action,...) \
|
---|
64 | GET_MACRO(_0,__VA_ARGS__,FE_5,FE_4,FE_3,FE_2,FE_1,FE_0)(action,__VA_ARGS__)
|
---|
65 |
|
---|
66 | #define COMMA_ttag(X) , ttag(X)
|
---|
67 | #define array( TE, ...) typeof( mkar_( ttag(TE) FOR_EACH( COMMA_ttag, __VA_ARGS__ ) ) )
|
---|
68 |
|
---|
69 | #define COMMA_ztag(X) , ztag(X)
|
---|
70 | #define zarray( TE, ...) typeof( mkar_( ttag(TE) FOR_EACH( COMMA_ztag, __VA_ARGS__ ) ) )
|
---|
71 |
|
---|
72 | //
|
---|
73 | // Sugar for multidimensional indexing
|
---|
74 | //
|
---|
75 |
|
---|
76 | // Core -[[-,-,-]] operator
|
---|
77 |
|
---|
78 | // Desired form, one definition with recursion on IxBC (worked until Jan 2021, see trac #__TODO__)
|
---|
79 | // forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } )
|
---|
80 | // TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
|
---|
81 | // return this[ab][bc];
|
---|
82 | // }
|
---|
83 |
|
---|
84 | // Workaround form. Listing all possibilities up to 4 dims.
|
---|
85 | forall( TA &, TB &, IxAB | { TB & ?[?]( TA &, IxAB ); }
|
---|
86 | , TC &, IxBC | { TC & ?[?]( TB &, IxBC ); } )
|
---|
87 | TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
|
---|
88 | return this[ab][bc];
|
---|
89 | }
|
---|
90 | // forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } )
|
---|
91 | // TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
|
---|
92 | // return this[ab][bc];
|
---|
93 | // }
|
---|
94 |
|
---|
95 | // Adapters for "indexed by ptrdiff_t" implies "indexed by [this other integral type]"
|
---|
96 | // Work around restriction that assertions underlying -[[-,-,-]] must match excatly
|
---|
97 | forall( C &, E & | { E & ?[?]( C &, ptrdiff_t ); } ) {
|
---|
98 |
|
---|
99 | // Targeted to support: for( i; z(N) ) ... a[[ ..., i, ... ]]
|
---|
100 | E & ?[?]( C & this, size_t i ) {
|
---|
101 | return this[ (ptrdiff_t) i ];
|
---|
102 | }
|
---|
103 |
|
---|
104 | // Targeted to support: for( i; 5 ) ... a[[ ..., i, ... ]]
|
---|
105 | E & ?[?]( C & this, int i ) {
|
---|
106 | return this[ (ptrdiff_t) i ];
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | //
|
---|
111 | // Rotation
|
---|
112 | //
|
---|
113 |
|
---|
114 | // Base
|
---|
115 | forall( ztype(Zq), ztype(Sq), Tbase & )
|
---|
116 | tag(arpk(Zq, Sq, Tbase, Tbase)) enq_( tag(Tbase), tag(Zq), tag(Sq), tag(Tbase) ) {}
|
---|
117 |
|
---|
118 | // Rec
|
---|
119 | forall( ztype(Zq), ztype(Sq), ztype(Z), ztype(S), recq &, recr &, Tbase & | { tag(recr) enq_( tag(Tbase), tag(Zq), tag(Sq), tag(recq) ); } )
|
---|
120 | tag(arpk(Z, S, recr, Tbase)) enq_( tag(Tbase), tag(Zq), tag(Sq), tag(arpk(Z, S, recq, Tbase)) ) {}
|
---|
121 |
|
---|
122 | // Wrapper
|
---|
123 | struct all_t {} all;
|
---|
124 | forall( ztype(Z), ztype(S), Te &, result &, Tbase & | { tag(result) enq_( tag(Tbase), tag(Z), tag(S), tag(Te) ); } )
|
---|
125 | result & ?[?]( arpk(Z, S, Te, Tbase) & this, all_t ) {
|
---|
126 | return (result&) this;
|
---|
127 | }
|
---|