[c7625e0] | 1 | |
---|
| 2 | |
---|
| 3 | // a type whose size is n |
---|
| 4 | #define Z(n) char[n] |
---|
| 5 | |
---|
| 6 | // the inverse of Z(-) |
---|
[b9dae14c] | 7 | #define z(N) sizeof(N) |
---|
[c7625e0] | 8 | |
---|
| 9 | forall( T & ) struct tag {}; |
---|
| 10 | #define ttag(T) ((tag(T)){}) |
---|
| 11 | #define ztag(n) ttag(Z(n)) |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | // |
---|
| 15 | // Single-dim array sruct (with explicit packing and atom) |
---|
| 16 | // |
---|
| 17 | |
---|
[63f42a8] | 18 | forall( [N], S & | sized(S), Timmed &, Tbase & ) { |
---|
[c7625e0] | 19 | struct arpk { |
---|
[b9dae14c] | 20 | S strides[z(N)]; |
---|
[c7625e0] | 21 | }; |
---|
| 22 | |
---|
[9fa538c] | 23 | // About the choice of integral types offered as subscript overloads: |
---|
| 24 | // Intent is to cover these use cases: |
---|
| 25 | // float foo( ptrdiff_t i ) { return a[i]; } // i : ptrdiff_t |
---|
| 26 | // forall( [N] ) ... for( i; N ) { total += a[i]; } // i : typeof( sizeof(42) ) |
---|
| 27 | // for( i; 5 ) { total += a[i]; } // i : int |
---|
| 28 | // It gets complicated by: |
---|
| 29 | // - CFA does overloading on concrete types, like int and unsigned int, not on typedefed |
---|
| 30 | // types like size_t. So trying to overload on ptrdiff_t vs int works in 64-bit mode |
---|
| 31 | // but not in 32-bit mode. |
---|
| 32 | // - Given bug of Trac #247, CFA gives sizeof expressions type unsigned long int, when it |
---|
| 33 | // should give them type size_t. |
---|
| 34 | // |
---|
| 35 | // gcc -m32 cfa -m32 given bug gcc -m64 |
---|
| 36 | // ptrdiff_t int int long int |
---|
| 37 | // size_t unsigned int unsigned int unsigned long int |
---|
| 38 | // typeof( sizeof(42) ) unsigned int unsigned long int unsigned long int |
---|
| 39 | // int int int int |
---|
| 40 | |
---|
| 41 | static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, int i ) { |
---|
[c7625e0] | 42 | return (Timmed &) a.strides[i]; |
---|
| 43 | } |
---|
| 44 | |
---|
[9fa538c] | 45 | static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, unsigned int i ) { |
---|
[63a4b92] | 46 | return (Timmed &) a.strides[i]; |
---|
| 47 | } |
---|
| 48 | |
---|
[9fa538c] | 49 | static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, long int i ) { |
---|
[63a4b92] | 50 | return (Timmed &) a.strides[i]; |
---|
| 51 | } |
---|
| 52 | |
---|
[9fa538c] | 53 | static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, unsigned long int i ) { |
---|
| 54 | return (Timmed &) a.strides[i]; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | static inline size_t ?`len( arpk(N, S, Timmed, Tbase) & a ) { |
---|
[b9dae14c] | 58 | return z(N); |
---|
[c7625e0] | 59 | } |
---|
| 60 | |
---|
| 61 | // workaround #226 (and array relevance thereof demonstrated in mike102/otype-slow-ndims.cfa) |
---|
[9fa538c] | 62 | static inline void ?{}( arpk(N, S, Timmed, Tbase) & this ) { |
---|
[b9dae14c] | 63 | void ?{}( S (&inner)[z(N)] ) {} |
---|
[c7625e0] | 64 | ?{}(this.strides); |
---|
| 65 | } |
---|
[9fa538c] | 66 | static inline void ^?{}( arpk(N, S, Timmed, Tbase) & this ) { |
---|
[b9dae14c] | 67 | void ^?{}( S (&inner)[z(N)] ) {} |
---|
[c7625e0] | 68 | ^?{}(this.strides); |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | // |
---|
| 73 | // Sugar for declaring array structure instances |
---|
| 74 | // |
---|
| 75 | |
---|
| 76 | forall( Te ) |
---|
[9fa538c] | 77 | static inline Te mkar_( tag(Te) ) {} |
---|
[c7625e0] | 78 | |
---|
[b9dae14c] | 79 | forall( [N], ZTags ... , Trslt &, Tatom & | { Trslt mkar_( tag(Tatom), ZTags ); } ) |
---|
[9fa538c] | 80 | static inline arpk(N, Trslt, Trslt, Tatom) mkar_( tag(Tatom), tag(N), ZTags ) {} |
---|
[c7625e0] | 81 | |
---|
| 82 | // based on https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros |
---|
| 83 | |
---|
| 84 | // Make a FOREACH macro |
---|
| 85 | #define FE_0(WHAT) |
---|
| 86 | #define FE_1(WHAT, X) WHAT(X) |
---|
| 87 | #define FE_2(WHAT, X, ...) WHAT(X)FE_1(WHAT, __VA_ARGS__) |
---|
| 88 | #define FE_3(WHAT, X, ...) WHAT(X)FE_2(WHAT, __VA_ARGS__) |
---|
| 89 | #define FE_4(WHAT, X, ...) WHAT(X)FE_3(WHAT, __VA_ARGS__) |
---|
| 90 | #define FE_5(WHAT, X, ...) WHAT(X)FE_4(WHAT, __VA_ARGS__) |
---|
| 91 | //... repeat as needed |
---|
| 92 | |
---|
| 93 | #define GET_MACRO(_0,_1,_2,_3,_4,_5,NAME,...) NAME |
---|
| 94 | #define FOR_EACH(action,...) \ |
---|
| 95 | GET_MACRO(_0,__VA_ARGS__,FE_5,FE_4,FE_3,FE_2,FE_1,FE_0)(action,__VA_ARGS__) |
---|
| 96 | |
---|
| 97 | #define COMMA_ttag(X) , ttag(X) |
---|
| 98 | #define array( TE, ...) typeof( mkar_( ttag(TE) FOR_EACH( COMMA_ttag, __VA_ARGS__ ) ) ) |
---|
| 99 | |
---|
| 100 | #define COMMA_ztag(X) , ztag(X) |
---|
| 101 | #define zarray( TE, ...) typeof( mkar_( ttag(TE) FOR_EACH( COMMA_ztag, __VA_ARGS__ ) ) ) |
---|
| 102 | |
---|
| 103 | // |
---|
| 104 | // Sugar for multidimensional indexing |
---|
| 105 | // |
---|
| 106 | |
---|
| 107 | // Core -[[-,-,-]] operator |
---|
| 108 | |
---|
[63a4b92] | 109 | #ifdef TRY_BROKEN_DESIRED_MD_SUBSCRIPT |
---|
| 110 | |
---|
[c7625e0] | 111 | // Desired form. One definition with recursion on IxBC (worked until Jan 2021, see trac #__TODO__) |
---|
| 112 | |
---|
[63a4b92] | 113 | forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } ) |
---|
[9fa538c] | 114 | static inline TC & ?[?]( TA & this, IxAB ab, IxBC bc ) { |
---|
[c7625e0] | 115 | return this[ab][bc]; |
---|
| 116 | } |
---|
| 117 | |
---|
[63a4b92] | 118 | #else |
---|
[c7625e0] | 119 | |
---|
[63a4b92] | 120 | // Workaround form. Listing all possibilities up to 4 dims. |
---|
[c7625e0] | 121 | |
---|
[63a4b92] | 122 | forall( TA &, TB &, TC &, IxAB_0, IxBC | { TB & ?[?]( TA &, IxAB_0 ); TC & ?[?]( TB &, IxBC ); } ) |
---|
[9fa538c] | 123 | static inline TC & ?[?]( TA & this, IxAB_0 ab, IxBC bc ) { |
---|
[63a4b92] | 124 | return this[ab][bc]; |
---|
[c7625e0] | 125 | } |
---|
| 126 | |
---|
[63a4b92] | 127 | forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1 ); TC & ?[?]( TB &, IxBC ); } ) |
---|
[9fa538c] | 128 | static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxBC bc ) { |
---|
[63a4b92] | 129 | return this[[ab0,ab1]][bc]; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxAB_2, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1, IxAB_2 ); TC & ?[?]( TB &, IxBC ); } ) |
---|
[9fa538c] | 133 | static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxAB_2 ab2, IxBC bc ) { |
---|
[63a4b92] | 134 | return this[[ab0,ab1,ab2]][bc]; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | #endif |
---|
| 138 | |
---|
[c7625e0] | 139 | // |
---|
| 140 | // Rotation |
---|
| 141 | // |
---|
| 142 | |
---|
| 143 | // Base |
---|
[63f42a8] | 144 | forall( [Nq], Sq & | sized(Sq), Tbase & ) |
---|
[9fa538c] | 145 | static inline tag(arpk(Nq, Sq, Tbase, Tbase)) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(Tbase) ) {} |
---|
[c7625e0] | 146 | |
---|
| 147 | // Rec |
---|
[63f42a8] | 148 | forall( [Nq], Sq & | sized(Sq), [N], S & | sized(S), recq &, recr &, Tbase & | { tag(recr) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(recq) ); } ) |
---|
[9fa538c] | 149 | static inline tag(arpk(N, S, recr, Tbase)) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(arpk(N, S, recq, Tbase)) ) {} |
---|
[c7625e0] | 150 | |
---|
| 151 | // Wrapper |
---|
| 152 | struct all_t {} all; |
---|
[63f42a8] | 153 | forall( [N], S & | sized(S), Te &, result &, Tbase & | { tag(result) enq_( tag(Tbase), tag(N), tag(S), tag(Te) ); } ) |
---|
[9fa538c] | 154 | static inline result & ?[?]( arpk(N, S, Te, Tbase) & this, all_t ) { |
---|
[c7625e0] | 155 | return (result&) this; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | // |
---|
| 159 | // Trait of array or slice |
---|
| 160 | // |
---|
| 161 | |
---|
| 162 | trait ar(A &, Tv &) { |
---|
| 163 | Tv& ?[?]( A&, ptrdiff_t ); |
---|
| 164 | size_t ?`len( A& ); |
---|
| 165 | }; |
---|