[a5e2682] | 1 | #pragma once |
---|
| 2 | |
---|
[8d76f2b] | 3 | #include <assert.h> |
---|
[c7625e0] | 4 | |
---|
| 5 | |
---|
[6e50a6b] | 6 | forall( __CFA_tysys_id_only_X & ) struct tag {}; |
---|
[c7625e0] | 7 | #define ttag(T) ((tag(T)){}) |
---|
[6e50a6b] | 8 | #define ztag(n) ttag(n) |
---|
[c7625e0] | 9 | |
---|
| 10 | |
---|
[ad24245] | 11 | // |
---|
| 12 | // The `array` macro is the public interface. |
---|
| 13 | // It computes the type of a dense (trivially strided) array. |
---|
| 14 | // All user-declared objects are dense arrays. |
---|
[c7625e0] | 15 | // |
---|
[ad24245] | 16 | // The `arpk` (ARray with PacKing info explicit) type is, generally, a slice with _any_ striding. |
---|
| 17 | // This type is meant for internal use. |
---|
| 18 | // CFA programmers should not instantiate it directly, nor access its field. |
---|
| 19 | // CFA programmers should call ?[?] on it. |
---|
| 20 | // Yet user-given `array(stuff)` expands to `arpk(stuff')`. |
---|
| 21 | // The comments here explain the resulting internals. |
---|
| 22 | // |
---|
| 23 | // Just as a plain-C "multidimesional" array is really array-of-array-of-..., |
---|
| 24 | // so does arpk generally show up as arpk-of-arpk-of... |
---|
| 25 | // |
---|
| 26 | // In the example of `array(float, 3, 4, 5) a;`, |
---|
| 27 | // `typeof(a)` is an `arpk` instantiation. |
---|
| 28 | // These comments explain _its_ arguments, i.e. those of the topmost `arpk` level. |
---|
| 29 | // |
---|
| 30 | // [N] : the number of elements in `a`; 3 in the example |
---|
| 31 | // S : carries the stride size (distance in bytes between &myA[0] and &myA[1]), in sizeof(S); |
---|
| 32 | // same as Timmed when striding is trivial, same as Timmed in the example |
---|
| 33 | // Timmed : (T-immediate) the inner type; conceptually, `typeof(a)` is "arpk of Timmed"; |
---|
| 34 | // array(float, 4, 5) in the example |
---|
| 35 | // Tbase : (T-base) the deepest element type that is not arpk; float in the example |
---|
[c7625e0] | 36 | // |
---|
[63f42a8] | 37 | forall( [N], S & | sized(S), Timmed &, Tbase & ) { |
---|
[ad24245] | 38 | |
---|
| 39 | // |
---|
| 40 | // Single-dim array sruct (with explicit packing and atom) |
---|
| 41 | // |
---|
[c7625e0] | 42 | struct arpk { |
---|
[6e50a6b] | 43 | S strides[N]; |
---|
[c7625e0] | 44 | }; |
---|
| 45 | |
---|
[9fa538c] | 46 | // About the choice of integral types offered as subscript overloads: |
---|
| 47 | // Intent is to cover these use cases: |
---|
[a5e2682] | 48 | // a[0] // i : zero_t |
---|
| 49 | // a[1] // i : one_t |
---|
| 50 | // a[2] // i : int |
---|
[9fa538c] | 51 | // float foo( ptrdiff_t i ) { return a[i]; } // i : ptrdiff_t |
---|
[a5e2682] | 52 | // float foo( size_t i ) { return a[i]; } // i : size_t |
---|
[9fa538c] | 53 | // forall( [N] ) ... for( i; N ) { total += a[i]; } // i : typeof( sizeof(42) ) |
---|
| 54 | // for( i; 5 ) { total += a[i]; } // i : int |
---|
[a5e2682] | 55 | // |
---|
[9fa538c] | 56 | // It gets complicated by: |
---|
| 57 | // - CFA does overloading on concrete types, like int and unsigned int, not on typedefed |
---|
| 58 | // types like size_t. So trying to overload on ptrdiff_t vs int works in 64-bit mode |
---|
| 59 | // but not in 32-bit mode. |
---|
| 60 | // - Given bug of Trac #247, CFA gives sizeof expressions type unsigned long int, when it |
---|
| 61 | // should give them type size_t. |
---|
[d1abc63c] | 62 | // |
---|
[a5e2682] | 63 | // gcc -m32 cfa -m32 given bug gcc -m64 (and cfa) |
---|
[9fa538c] | 64 | // ptrdiff_t int int long int |
---|
| 65 | // size_t unsigned int unsigned int unsigned long int |
---|
| 66 | // typeof( sizeof(42) ) unsigned int unsigned long int unsigned long int |
---|
| 67 | // int int int int |
---|
[a5e2682] | 68 | // |
---|
| 69 | // So the solution must support types {zero_t, one_t, int, unsigned int, long int, unsigned long int} |
---|
| 70 | // |
---|
| 71 | // The solution cannot rely on implicit conversions (e.g. just have one overload for ptrdiff_t) |
---|
| 72 | // because assertion satisfaction requires types to match exacly. Both higher-dimensional |
---|
| 73 | // subscripting and operations on slices use asserted subscript operators. The test case |
---|
| 74 | // array-container/array-sbscr-cases covers the combinations. Mike beleives that commenting out |
---|
| 75 | // any of the current overloads leads to one of those cases failing, either on 64- or 32-bit. |
---|
| 76 | // Mike is open to being shown a smaller set of overloads that still passes the test. |
---|
| 77 | |
---|
| 78 | static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, zero_t ) { |
---|
| 79 | assert( 0 < N ); |
---|
| 80 | return (Timmed &) a.strides[0]; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, one_t ) { |
---|
| 84 | assert( 1 < N ); |
---|
| 85 | return (Timmed &) a.strides[1]; |
---|
| 86 | } |
---|
[9fa538c] | 87 | |
---|
| 88 | static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, int i ) { |
---|
[8d76f2b] | 89 | assert( i < N ); |
---|
[c7625e0] | 90 | return (Timmed &) a.strides[i]; |
---|
| 91 | } |
---|
| 92 | |
---|
[d1abc63c] | 93 | static inline const Timmed & ?[?]( const arpk(N, S, Timmed, Tbase) & a, int i ) { |
---|
| 94 | assert( i < N ); |
---|
| 95 | return (Timmed &) a.strides[i]; |
---|
| 96 | } |
---|
| 97 | |
---|
[9fa538c] | 98 | static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, unsigned int i ) { |
---|
[8d76f2b] | 99 | assert( i < N ); |
---|
[63a4b92] | 100 | return (Timmed &) a.strides[i]; |
---|
| 101 | } |
---|
| 102 | |
---|
[d1abc63c] | 103 | static inline const Timmed & ?[?]( const arpk(N, S, Timmed, Tbase) & a, unsigned int i ) { |
---|
| 104 | assert( i < N ); |
---|
| 105 | return (Timmed &) a.strides[i]; |
---|
| 106 | } |
---|
| 107 | |
---|
[9fa538c] | 108 | static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, long int i ) { |
---|
[8d76f2b] | 109 | assert( i < N ); |
---|
[63a4b92] | 110 | return (Timmed &) a.strides[i]; |
---|
| 111 | } |
---|
| 112 | |
---|
[d1abc63c] | 113 | static inline const Timmed & ?[?]( const arpk(N, S, Timmed, Tbase) & a, long int i ) { |
---|
| 114 | assert( i < N ); |
---|
| 115 | return (Timmed &) a.strides[i]; |
---|
| 116 | } |
---|
| 117 | |
---|
[9fa538c] | 118 | static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, unsigned long int i ) { |
---|
[8d76f2b] | 119 | assert( i < N ); |
---|
[9fa538c] | 120 | return (Timmed &) a.strides[i]; |
---|
| 121 | } |
---|
| 122 | |
---|
[d1abc63c] | 123 | static inline const Timmed & ?[?]( const arpk(N, S, Timmed, Tbase) & a, unsigned long int i ) { |
---|
| 124 | assert( i < N ); |
---|
| 125 | return (Timmed &) a.strides[i]; |
---|
| 126 | } |
---|
| 127 | |
---|
[9fa538c] | 128 | static inline size_t ?`len( arpk(N, S, Timmed, Tbase) & a ) { |
---|
[6e50a6b] | 129 | return N; |
---|
[c7625e0] | 130 | } |
---|
| 131 | |
---|
[a5e2682] | 132 | static inline void __taglen( tag(arpk(N, S, Timmed, Tbase)), tag(N) ) {} |
---|
[cfbc56ec] | 133 | } |
---|
[a5e2682] | 134 | |
---|
[cfbc56ec] | 135 | // RAII pattern has workarounds for |
---|
| 136 | // - Trac 226: Simplest handling would be, require immediate element to be otype, let autogen |
---|
| 137 | // raii happen. Performance on even a couple dimensions is unacceptable because of exponential |
---|
| 138 | // thunk creation: ?{}() needs all four otype funcs from next level, so does ^?{}(), so do the |
---|
| 139 | // other two. This solution offers ?{}() that needs only ?{}(), and similar for ^?{}. |
---|
| 140 | |
---|
| 141 | forall( [N], S & | sized(S), Timmed &, Tbase & | { void ?{}( Timmed & ); } ) |
---|
| 142 | static inline void ?{}( arpk(N, S, Timmed, Tbase) & this ) { |
---|
| 143 | void ?{}( S (&)[N] ) {} |
---|
| 144 | ?{}(this.strides); |
---|
| 145 | |
---|
| 146 | for (i; N) ?{}( (Timmed &) this.strides[i] ); |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | forall( [N], S & | sized(S), Timmed &, Tbase & | { void ^?{}( Timmed & ); } ) |
---|
| 150 | static inline void ^?{}( arpk(N, S, Timmed, Tbase) & this ) { |
---|
| 151 | void ^?{}( S (&)[N] ) {} |
---|
| 152 | ^?{}(this.strides); |
---|
| 153 | |
---|
| 154 | for (i; N ) { |
---|
| 155 | ^?{}( (Timmed &) this.strides[N-i-1] ); |
---|
[c7625e0] | 156 | } |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | // |
---|
| 160 | // Sugar for declaring array structure instances |
---|
| 161 | // |
---|
| 162 | |
---|
[cfbc56ec] | 163 | forall( Te * ) |
---|
[9fa538c] | 164 | static inline Te mkar_( tag(Te) ) {} |
---|
[c7625e0] | 165 | |
---|
[b9dae14c] | 166 | forall( [N], ZTags ... , Trslt &, Tatom & | { Trslt mkar_( tag(Tatom), ZTags ); } ) |
---|
[9fa538c] | 167 | static inline arpk(N, Trslt, Trslt, Tatom) mkar_( tag(Tatom), tag(N), ZTags ) {} |
---|
[c7625e0] | 168 | |
---|
| 169 | // based on https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros |
---|
| 170 | |
---|
| 171 | // Make a FOREACH macro |
---|
| 172 | #define FE_0(WHAT) |
---|
[d1abc63c] | 173 | #define FE_1(WHAT, X) WHAT(X) |
---|
[c7625e0] | 174 | #define FE_2(WHAT, X, ...) WHAT(X)FE_1(WHAT, __VA_ARGS__) |
---|
| 175 | #define FE_3(WHAT, X, ...) WHAT(X)FE_2(WHAT, __VA_ARGS__) |
---|
| 176 | #define FE_4(WHAT, X, ...) WHAT(X)FE_3(WHAT, __VA_ARGS__) |
---|
| 177 | #define FE_5(WHAT, X, ...) WHAT(X)FE_4(WHAT, __VA_ARGS__) |
---|
| 178 | //... repeat as needed |
---|
| 179 | |
---|
[d1abc63c] | 180 | #define GET_MACRO(_0,_1,_2,_3,_4,_5,NAME,...) NAME |
---|
[c7625e0] | 181 | #define FOR_EACH(action,...) \ |
---|
| 182 | GET_MACRO(_0,__VA_ARGS__,FE_5,FE_4,FE_3,FE_2,FE_1,FE_0)(action,__VA_ARGS__) |
---|
| 183 | |
---|
| 184 | #define COMMA_ttag(X) , ttag(X) |
---|
| 185 | #define array( TE, ...) typeof( mkar_( ttag(TE) FOR_EACH( COMMA_ttag, __VA_ARGS__ ) ) ) |
---|
| 186 | |
---|
| 187 | #define COMMA_ztag(X) , ztag(X) |
---|
| 188 | #define zarray( TE, ...) typeof( mkar_( ttag(TE) FOR_EACH( COMMA_ztag, __VA_ARGS__ ) ) ) |
---|
| 189 | |
---|
| 190 | // |
---|
| 191 | // Sugar for multidimensional indexing |
---|
| 192 | // |
---|
| 193 | |
---|
| 194 | // Core -[[-,-,-]] operator |
---|
| 195 | |
---|
[63a4b92] | 196 | #ifdef TRY_BROKEN_DESIRED_MD_SUBSCRIPT |
---|
| 197 | |
---|
[c7625e0] | 198 | // Desired form. One definition with recursion on IxBC (worked until Jan 2021, see trac #__TODO__) |
---|
| 199 | |
---|
[63a4b92] | 200 | forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } ) |
---|
[9fa538c] | 201 | static inline TC & ?[?]( TA & this, IxAB ab, IxBC bc ) { |
---|
[c7625e0] | 202 | return this[ab][bc]; |
---|
| 203 | } |
---|
| 204 | |
---|
[d1abc63c] | 205 | #else |
---|
[c7625e0] | 206 | |
---|
[63a4b92] | 207 | // Workaround form. Listing all possibilities up to 4 dims. |
---|
[c7625e0] | 208 | |
---|
[63a4b92] | 209 | forall( TA &, TB &, TC &, IxAB_0, IxBC | { TB & ?[?]( TA &, IxAB_0 ); TC & ?[?]( TB &, IxBC ); } ) |
---|
[9fa538c] | 210 | static inline TC & ?[?]( TA & this, IxAB_0 ab, IxBC bc ) { |
---|
[63a4b92] | 211 | return this[ab][bc]; |
---|
[c7625e0] | 212 | } |
---|
| 213 | |
---|
[63a4b92] | 214 | forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1 ); TC & ?[?]( TB &, IxBC ); } ) |
---|
[9fa538c] | 215 | static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxBC bc ) { |
---|
[63a4b92] | 216 | return this[[ab0,ab1]][bc]; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxAB_2, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1, IxAB_2 ); TC & ?[?]( TB &, IxBC ); } ) |
---|
[9fa538c] | 220 | static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxAB_2 ab2, IxBC bc ) { |
---|
[63a4b92] | 221 | return this[[ab0,ab1,ab2]][bc]; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | #endif |
---|
| 225 | |
---|
[997324c] | 226 | // Available for users to work around Trac #265 |
---|
| 227 | // If `a[...0...]` isn't working, try `a[...ix0...]` instead. |
---|
[a5e2682] | 228 | |
---|
[997324c] | 229 | #define ix0 ((ptrdiff_t)0) |
---|
[a5e2682] | 230 | |
---|
| 231 | |
---|
| 232 | |
---|
[c7625e0] | 233 | // |
---|
| 234 | // Rotation |
---|
| 235 | // |
---|
| 236 | |
---|
| 237 | // Base |
---|
[63f42a8] | 238 | forall( [Nq], Sq & | sized(Sq), Tbase & ) |
---|
[6448f7d] | 239 | static inline tag(arpk(Nq, Sq, Tbase, Tbase)) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(Tbase) ) { |
---|
| 240 | tag(arpk(Nq, Sq, Tbase, Tbase)) ret; |
---|
| 241 | return ret; |
---|
| 242 | } |
---|
[c7625e0] | 243 | |
---|
| 244 | // Rec |
---|
[63f42a8] | 245 | forall( [Nq], Sq & | sized(Sq), [N], S & | sized(S), recq &, recr &, Tbase & | { tag(recr) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(recq) ); } ) |
---|
[6448f7d] | 246 | static inline tag(arpk(N, S, recr, Tbase)) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(arpk(N, S, recq, Tbase)) ) { |
---|
| 247 | tag(arpk(N, S, recr, Tbase)) ret; |
---|
| 248 | return ret; |
---|
| 249 | } |
---|
[c7625e0] | 250 | |
---|
| 251 | // Wrapper |
---|
[058ece2] | 252 | extern struct all_t {} all; |
---|
[63f42a8] | 253 | forall( [N], S & | sized(S), Te &, result &, Tbase & | { tag(result) enq_( tag(Tbase), tag(N), tag(S), tag(Te) ); } ) |
---|
[9fa538c] | 254 | static inline result & ?[?]( arpk(N, S, Te, Tbase) & this, all_t ) { |
---|
[c7625e0] | 255 | return (result&) this; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | // |
---|
| 259 | // Trait of array or slice |
---|
| 260 | // |
---|
| 261 | |
---|
[a5e2682] | 262 | // desired: |
---|
[7882c58] | 263 | // forall(A &, Tv &, [N]) |
---|
| 264 | // trait ar { |
---|
[a5e2682] | 265 | // Tv& ?[?]( A&, zero_t ); |
---|
| 266 | // Tv& ?[?]( A&, one_t ); |
---|
| 267 | // Tv& ?[?]( A&, int ); |
---|
| 268 | // ... |
---|
| 269 | // size_t ?`len( A& ); |
---|
| 270 | // void __taglen( tag(C), tag(N) ); |
---|
| 271 | // }; |
---|
| 272 | |
---|
| 273 | // working around N's not being accepted as arguments to traits |
---|
| 274 | |
---|
| 275 | #define ar(A, Tv, N) { \ |
---|
| 276 | Tv& ?[?]( A&, zero_t ); \ |
---|
| 277 | Tv& ?[?]( A&, one_t ); \ |
---|
| 278 | Tv& ?[?]( A&, int ); \ |
---|
| 279 | Tv& ?[?]( A&, unsigned int ); \ |
---|
| 280 | Tv& ?[?]( A&, long int ); \ |
---|
| 281 | Tv& ?[?]( A&, unsigned long int ); \ |
---|
| 282 | size_t ?`len( A& ); \ |
---|
| 283 | void __taglen( tag(A), tag(N) ); \ |
---|
| 284 | } |
---|