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