source: libcfa/src/containers/array.hfa@ 713905fd

Last change on this file since 713905fd was 997324c, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

Repair bad commit ad2424. Put back ix0 definition that was removed by mistake.

  • Property mode set to 100644
File size: 9.7 KB
Line 
1#pragma once
2
3#include <assert.h>
4
5
6forall( __CFA_tysys_id_only_X & ) struct tag {};
7#define ttag(T) ((tag(T)){})
8#define ztag(n) ttag(n)
9
10
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.
15//
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
36//
37forall( [N], S & | sized(S), Timmed &, Tbase & ) {
38
39 //
40 // Single-dim array sruct (with explicit packing and atom)
41 //
42 struct arpk {
43 S strides[N];
44 };
45
46 // About the choice of integral types offered as subscript overloads:
47 // Intent is to cover these use cases:
48 // a[0] // i : zero_t
49 // a[1] // i : one_t
50 // a[2] // i : int
51 // float foo( ptrdiff_t i ) { return a[i]; } // i : ptrdiff_t
52 // float foo( size_t i ) { return a[i]; } // i : size_t
53 // forall( [N] ) ... for( i; N ) { total += a[i]; } // i : typeof( sizeof(42) )
54 // for( i; 5 ) { total += a[i]; } // i : int
55 //
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.
62 //
63 // gcc -m32 cfa -m32 given bug gcc -m64 (and cfa)
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
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 }
87
88 static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, int i ) {
89 assert( i < N );
90 return (Timmed &) a.strides[i];
91 }
92
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
98 static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, unsigned int i ) {
99 assert( i < N );
100 return (Timmed &) a.strides[i];
101 }
102
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
108 static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, long int i ) {
109 assert( i < N );
110 return (Timmed &) a.strides[i];
111 }
112
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
118 static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, unsigned long int i ) {
119 assert( i < N );
120 return (Timmed &) a.strides[i];
121 }
122
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
128 static inline size_t ?`len( arpk(N, S, Timmed, Tbase) & a ) {
129 return N;
130 }
131
132 static inline void __taglen( tag(arpk(N, S, Timmed, Tbase)), tag(N) ) {}
133
134 // workaround #226 (and array relevance thereof demonstrated in mike102/otype-slow-ndims.cfa)
135 static inline void ?{}( arpk(N, S, Timmed, Tbase) & this ) {
136 void ?{}( S (&inner)[N] ) {}
137 ?{}(this.strides);
138 }
139 static inline void ^?{}( arpk(N, S, Timmed, Tbase) & this ) {
140 void ^?{}( S (&inner)[N] ) {}
141 ^?{}(this.strides);
142 }
143}
144
145//
146// Sugar for declaring array structure instances
147//
148
149forall( Te )
150static inline Te mkar_( tag(Te) ) {}
151
152forall( [N], ZTags ... , Trslt &, Tatom & | { Trslt mkar_( tag(Tatom), ZTags ); } )
153static inline arpk(N, Trslt, Trslt, Tatom) mkar_( tag(Tatom), tag(N), ZTags ) {}
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)
159 #define FE_1(WHAT, X) WHAT(X)
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
166 #define GET_MACRO(_0,_1,_2,_3,_4,_5,NAME,...) NAME
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
182#ifdef TRY_BROKEN_DESIRED_MD_SUBSCRIPT
183
184// Desired form. One definition with recursion on IxBC (worked until Jan 2021, see trac #__TODO__)
185
186forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } )
187static inline TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
188 return this[ab][bc];
189}
190
191#else
192
193// Workaround form. Listing all possibilities up to 4 dims.
194
195forall( TA &, TB &, TC &, IxAB_0, IxBC | { TB & ?[?]( TA &, IxAB_0 ); TC & ?[?]( TB &, IxBC ); } )
196static inline TC & ?[?]( TA & this, IxAB_0 ab, IxBC bc ) {
197 return this[ab][bc];
198}
199
200forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1 ); TC & ?[?]( TB &, IxBC ); } )
201static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxBC bc ) {
202 return this[[ab0,ab1]][bc];
203}
204
205forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxAB_2, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1, IxAB_2 ); TC & ?[?]( TB &, IxBC ); } )
206static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxAB_2 ab2, IxBC bc ) {
207 return this[[ab0,ab1,ab2]][bc];
208}
209
210#endif
211
212// Available for users to work around Trac #265
213// If `a[...0...]` isn't working, try `a[...ix0...]` instead.
214
215#define ix0 ((ptrdiff_t)0)
216
217
218
219//
220// Rotation
221//
222
223// Base
224forall( [Nq], Sq & | sized(Sq), Tbase & )
225static 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}
229
230// Rec
231forall( [Nq], Sq & | sized(Sq), [N], S & | sized(S), recq &, recr &, Tbase & | { tag(recr) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(recq) ); } )
232static 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}
236
237// Wrapper
238extern struct all_t {} all;
239forall( [N], S & | sized(S), Te &, result &, Tbase & | { tag(result) enq_( tag(Tbase), tag(N), tag(S), tag(Te) ); } )
240static inline result & ?[?]( arpk(N, S, Te, Tbase) & this, all_t ) {
241 return (result&) this;
242}
243
244//
245// Trait of array or slice
246//
247
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}
Note: See TracBrowser for help on using the repository browser.