source: libcfa/src/collections/array.hfa@ 0210a543

Last change on this file since 0210a543 was 0210a543, checked in by Michael Brooks <mlbrooks@…>, 6 weeks ago

Revise array's overload-selection comment now that #247 is closed.

The set of overloads required is unaffected, just there's a simpler derivation of this set.

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