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