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