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