[a5e26821] | 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:
|
---|
[a5e26821] | 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
|
---|
[a5e26821] | 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
|
---|
[a5e26821] | 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 | //
|
---|
[a5e26821] | 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
|
---|
[a5e26821] | 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 |
|
---|
[a5e26821] | 132 | static inline void __taglen( tag(arpk(N, S, Timmed, Tbase)), tag(N) ) {}
|
---|
| 133 |
|
---|
[c7625e0] | 134 | // workaround #226 (and array relevance thereof demonstrated in mike102/otype-slow-ndims.cfa)
|
---|
[9fa538c] | 135 | static inline void ?{}( arpk(N, S, Timmed, Tbase) & this ) {
|
---|
[6e50a6b] | 136 | void ?{}( S (&inner)[N] ) {}
|
---|
[c7625e0] | 137 | ?{}(this.strides);
|
---|
| 138 | }
|
---|
[9fa538c] | 139 | static inline void ^?{}( arpk(N, S, Timmed, Tbase) & this ) {
|
---|
[6e50a6b] | 140 | void ^?{}( S (&inner)[N] ) {}
|
---|
[c7625e0] | 141 | ^?{}(this.strides);
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | //
|
---|
| 146 | // Sugar for declaring array structure instances
|
---|
| 147 | //
|
---|
| 148 |
|
---|
| 149 | forall( Te )
|
---|
[9fa538c] | 150 | static inline Te mkar_( tag(Te) ) {}
|
---|
[c7625e0] | 151 |
|
---|
[b9dae14c] | 152 | forall( [N], ZTags ... , Trslt &, Tatom & | { Trslt mkar_( tag(Tatom), ZTags ); } )
|
---|
[9fa538c] | 153 | static inline arpk(N, Trslt, Trslt, Tatom) mkar_( tag(Tatom), tag(N), ZTags ) {}
|
---|
[c7625e0] | 154 |
|
---|
| 155 | // based on https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros
|
---|
| 156 |
|
---|
| 157 | // Make a FOREACH macro
|
---|
| 158 | #define FE_0(WHAT)
|
---|
[d1abc63c] | 159 | #define FE_1(WHAT, X) WHAT(X)
|
---|
[c7625e0] | 160 | #define FE_2(WHAT, X, ...) WHAT(X)FE_1(WHAT, __VA_ARGS__)
|
---|
| 161 | #define FE_3(WHAT, X, ...) WHAT(X)FE_2(WHAT, __VA_ARGS__)
|
---|
| 162 | #define FE_4(WHAT, X, ...) WHAT(X)FE_3(WHAT, __VA_ARGS__)
|
---|
| 163 | #define FE_5(WHAT, X, ...) WHAT(X)FE_4(WHAT, __VA_ARGS__)
|
---|
| 164 | //... repeat as needed
|
---|
| 165 |
|
---|
[d1abc63c] | 166 | #define GET_MACRO(_0,_1,_2,_3,_4,_5,NAME,...) NAME
|
---|
[c7625e0] | 167 | #define FOR_EACH(action,...) \
|
---|
| 168 | GET_MACRO(_0,__VA_ARGS__,FE_5,FE_4,FE_3,FE_2,FE_1,FE_0)(action,__VA_ARGS__)
|
---|
| 169 |
|
---|
| 170 | #define COMMA_ttag(X) , ttag(X)
|
---|
| 171 | #define array( TE, ...) typeof( mkar_( ttag(TE) FOR_EACH( COMMA_ttag, __VA_ARGS__ ) ) )
|
---|
| 172 |
|
---|
| 173 | #define COMMA_ztag(X) , ztag(X)
|
---|
| 174 | #define zarray( TE, ...) typeof( mkar_( ttag(TE) FOR_EACH( COMMA_ztag, __VA_ARGS__ ) ) )
|
---|
| 175 |
|
---|
| 176 | //
|
---|
| 177 | // Sugar for multidimensional indexing
|
---|
| 178 | //
|
---|
| 179 |
|
---|
| 180 | // Core -[[-,-,-]] operator
|
---|
| 181 |
|
---|
[63a4b92] | 182 | #ifdef TRY_BROKEN_DESIRED_MD_SUBSCRIPT
|
---|
| 183 |
|
---|
[c7625e0] | 184 | // Desired form. One definition with recursion on IxBC (worked until Jan 2021, see trac #__TODO__)
|
---|
| 185 |
|
---|
[63a4b92] | 186 | forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } )
|
---|
[9fa538c] | 187 | static inline TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
|
---|
[c7625e0] | 188 | return this[ab][bc];
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[d1abc63c] | 191 | #else
|
---|
[c7625e0] | 192 |
|
---|
[63a4b92] | 193 | // Workaround form. Listing all possibilities up to 4 dims.
|
---|
[c7625e0] | 194 |
|
---|
[63a4b92] | 195 | forall( TA &, TB &, TC &, IxAB_0, IxBC | { TB & ?[?]( TA &, IxAB_0 ); TC & ?[?]( TB &, IxBC ); } )
|
---|
[9fa538c] | 196 | static inline TC & ?[?]( TA & this, IxAB_0 ab, IxBC bc ) {
|
---|
[63a4b92] | 197 | return this[ab][bc];
|
---|
[c7625e0] | 198 | }
|
---|
| 199 |
|
---|
[63a4b92] | 200 | forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1 ); TC & ?[?]( TB &, IxBC ); } )
|
---|
[9fa538c] | 201 | static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxBC bc ) {
|
---|
[63a4b92] | 202 | return this[[ab0,ab1]][bc];
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxAB_2, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1, IxAB_2 ); TC & ?[?]( TB &, IxBC ); } )
|
---|
[9fa538c] | 206 | static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxAB_2 ab2, IxBC bc ) {
|
---|
[63a4b92] | 207 | return this[[ab0,ab1,ab2]][bc];
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | #endif
|
---|
| 211 |
|
---|
[997324c] | 212 | // Available for users to work around Trac #265
|
---|
| 213 | // If `a[...0...]` isn't working, try `a[...ix0...]` instead.
|
---|
[a5e26821] | 214 |
|
---|
[997324c] | 215 | #define ix0 ((ptrdiff_t)0)
|
---|
[a5e26821] | 216 |
|
---|
| 217 |
|
---|
| 218 |
|
---|
[c7625e0] | 219 | //
|
---|
| 220 | // Rotation
|
---|
| 221 | //
|
---|
| 222 |
|
---|
| 223 | // Base
|
---|
[63f42a8] | 224 | forall( [Nq], Sq & | sized(Sq), Tbase & )
|
---|
[6448f7d] | 225 | static inline tag(arpk(Nq, Sq, Tbase, Tbase)) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(Tbase) ) {
|
---|
| 226 | tag(arpk(Nq, Sq, Tbase, Tbase)) ret;
|
---|
| 227 | return ret;
|
---|
| 228 | }
|
---|
[c7625e0] | 229 |
|
---|
| 230 | // Rec
|
---|
[63f42a8] | 231 | forall( [Nq], Sq & | sized(Sq), [N], S & | sized(S), recq &, recr &, Tbase & | { tag(recr) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(recq) ); } )
|
---|
[6448f7d] | 232 | static inline tag(arpk(N, S, recr, Tbase)) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(arpk(N, S, recq, Tbase)) ) {
|
---|
| 233 | tag(arpk(N, S, recr, Tbase)) ret;
|
---|
| 234 | return ret;
|
---|
| 235 | }
|
---|
[c7625e0] | 236 |
|
---|
| 237 | // Wrapper
|
---|
[058ece2] | 238 | extern struct all_t {} all;
|
---|
[63f42a8] | 239 | forall( [N], S & | sized(S), Te &, result &, Tbase & | { tag(result) enq_( tag(Tbase), tag(N), tag(S), tag(Te) ); } )
|
---|
[9fa538c] | 240 | static inline result & ?[?]( arpk(N, S, Te, Tbase) & this, all_t ) {
|
---|
[c7625e0] | 241 | return (result&) this;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | //
|
---|
| 245 | // Trait of array or slice
|
---|
| 246 | //
|
---|
| 247 |
|
---|
[a5e26821] | 248 | // desired:
|
---|
| 249 | // trait ar(A &, Tv &, [N]) {
|
---|
| 250 | // Tv& ?[?]( A&, zero_t );
|
---|
| 251 | // Tv& ?[?]( A&, one_t );
|
---|
| 252 | // Tv& ?[?]( A&, int );
|
---|
| 253 | // ...
|
---|
| 254 | // size_t ?`len( A& );
|
---|
| 255 | // void __taglen( tag(C), tag(N) );
|
---|
| 256 | // };
|
---|
| 257 |
|
---|
| 258 | // working around N's not being accepted as arguments to traits
|
---|
| 259 |
|
---|
| 260 | #define ar(A, Tv, N) { \
|
---|
| 261 | Tv& ?[?]( A&, zero_t ); \
|
---|
| 262 | Tv& ?[?]( A&, one_t ); \
|
---|
| 263 | Tv& ?[?]( A&, int ); \
|
---|
| 264 | Tv& ?[?]( A&, unsigned int ); \
|
---|
| 265 | Tv& ?[?]( A&, long int ); \
|
---|
| 266 | Tv& ?[?]( A&, unsigned long int ); \
|
---|
| 267 | size_t ?`len( A& ); \
|
---|
| 268 | void __taglen( tag(A), tag(N) ); \
|
---|
| 269 | }
|
---|