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