source: libcfa/src/collections/array.hfa@ 9b01ed1

Last change on this file since 9b01ed1 was 5db913f, checked in by Michael Brooks <mlbrooks@…>, 10 months ago

Work around #269 in array bound-check printing, to avoid 32-bit-specific warning. Try to fix nightly build.

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