source: libcfa/src/collections/array.hfa@ a16f2b6

Last change on this file since a16f2b6 was 5db913f, checked in by Michael Brooks <mlbrooks@…>, 13 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
RevLine 
[a5e26821]1#pragma once
2
[c7625e0]3
4
[6e50a6b]5forall( __CFA_tysys_id_only_X & ) struct tag {};
[c7625e0]6#define ttag(T) ((tag(T)){})
[6e50a6b]7#define ztag(n) ttag(n)
[c7625e0]8
[fee4436]9#ifdef __CFA_DEBUG__
[5db913f]10// FIXME: `len` printing format %ld is a workaround for #269; once fixed, it should be %zd
[8ee211d]11#define subcheck( arr, sub, len ) \
12 if ( (sub) < 0 || (sub) >= (len) ) \
[5db913f]13 abort( "Subscript %ld exceeds dimension range [0,%ld) for array %p.\n", \
[8ee211d]14 (sub), (len), (arr) )
15#define subchecku( arr, sub, len ) \
16 if ( (sub) >= (len) ) \
[5db913f]17 abort( "Subscript %ld exceeds dimension range [0,%ld) for array %p.\n", \
[8ee211d]18 (sub), (len), (arr) )
[fee4436]19#else
[8ee211d]20#define subcheck( arr, sub, len ) do {} while (0)
21#define subchecku( arr, sub, len ) do {} while (0)
[fee4436]22#endif
[c7625e0]23
[ad24245]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.
[c7625e0]28//
[ad24245]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
[c7625e0]49//
[63f42a8]50forall( [N], S & | sized(S), Timmed &, Tbase & ) {
[1bb0170]51 //
[b8e047a]52 // Single-dim array struct (with explicit packing and atom)
[1bb0170]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
[b8e047a]90 static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, zero_t ) {
[8ee211d]91 subcheck( a, 0L, N );
[b8e047a]92 return (Timmed &)a.strides[0];
[1bb0170]93 }
94
[b8e047a]95 static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, one_t ) {
[8ee211d]96 subcheck( a, 1L, N );
[b8e047a]97 return (Timmed &)a.strides[1];
[1bb0170]98 }
99
[b8e047a]100 static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, int i ) {
[8ee211d]101 subcheck( a, (long int)i, N );
[b8e047a]102 return (Timmed &)a.strides[i];
[1bb0170]103 }
104
[b8e047a]105 static inline const Timmed & ?[?]( const arpk( N, S, Timmed, Tbase ) & a, int i ) {
[8ee211d]106 subcheck( a, (long int)i, N );
[b8e047a]107 return (Timmed &)a.strides[i];
[1bb0170]108 }
109
[b8e047a]110 static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, unsigned int i ) {
[8ee211d]111 subchecku( a, (unsigned long int)i, N );
[b8e047a]112 return (Timmed &)a.strides[i];
[1bb0170]113 }
114
[b8e047a]115 static inline const Timmed & ?[?]( const arpk( N, S, Timmed, Tbase ) & a, unsigned int i ) {
[8ee211d]116 subchecku( a, (unsigned long int)i, N );
[b8e047a]117 return (Timmed &)a.strides[i];
[1bb0170]118 }
119
[b8e047a]120 static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, long int i ) {
[8ee211d]121 subcheck( a, i, N );
[b8e047a]122 return (Timmed &)a.strides[i];
[1bb0170]123 }
124
[b8e047a]125 static inline const Timmed & ?[?]( const arpk( N, S, Timmed, Tbase ) & a, long int i ) {
[8ee211d]126 subcheck( a, i, N );
[b8e047a]127 return (Timmed &)a.strides[i];
[1bb0170]128 }
129
[b8e047a]130 static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, unsigned long int i ) {
[8ee211d]131 subchecku( a, i, N );
[b8e047a]132 return (Timmed &)a.strides[i];
[1bb0170]133 }
134
[b8e047a]135 static inline const Timmed & ?[?]( const arpk( N, S, Timmed, Tbase ) & a, unsigned long int i ) {
[8ee211d]136 subchecku( a, i, N );
[b8e047a]137 return (Timmed &)a.strides[i];
[1bb0170]138 }
139
[8ee211d]140 static inline size_t ?`len( arpk( N, S, Timmed, Tbase ) & ) {
[1bb0170]141 return N;
142 }
143
[b8e047a]144 static inline void __taglen( tag(arpk( N, S, Timmed, Tbase )), tag(N) ) {}
[cfbc56ec]145}
[a5e26821]146
[cfbc56ec]147// RAII pattern has workarounds for
148// - Trac 226: Simplest handling would be, require immediate element to be otype, let autogen
[1bb0170]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 ^?{}.
[cfbc56ec]152
[1665ee5]153// skip initializing elements
154// array(float, 5) x = { delay_init };
[cdf7d43]155enum () delay_init_t { delay_init };
[1665ee5]156forall( [N], S & | sized(S), Timmed &, Tbase & )
157static inline void ?{}( arpk( N, S, Timmed, Tbase ) & this, delay_init_t ) {
[1bb0170]158 void ?{}( S (&)[N] ) {}
159 ?{}(this.strides);
[1665ee5]160}
[cfbc56ec]161
[1665ee5]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 );
[b8e047a]167 for (i; N) ?{}( (Timmed &)this.strides[i] );
[cfbc56ec]168}
169
170forall( [N], S & | sized(S), Timmed &, Tbase & | { void ^?{}( Timmed & ); } )
[b8e047a]171static inline void ^?{}( arpk( N, S, Timmed, Tbase ) & this ) {
[1bb0170]172 void ^?{}( S (&)[N] ) {}
173 ^?{}(this.strides);
[cfbc56ec]174
[1bb0170]175 for (i; N ) {
[b8e047a]176 ^?{}( (Timmed &)this.strides[N-i-1] );
[1bb0170]177 }
[c7625e0]178}
179
[1665ee5]180
[c7625e0]181//
182// Sugar for declaring array structure instances
183//
184
[cfbc56ec]185forall( Te * )
[9fa538c]186static inline Te mkar_( tag(Te) ) {}
[c7625e0]187
[b9dae14c]188forall( [N], ZTags ... , Trslt &, Tatom & | { Trslt mkar_( tag(Tatom), ZTags ); } )
[b8e047a]189static inline arpk( N, Trslt, Trslt, Tatom) mkar_( tag(Tatom), tag(N), ZTags ) {}
[c7625e0]190
191// based on https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros
192
[1bb0170]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
[c7625e0]201
[1bb0170]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__)
[c7625e0]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
[63a4b92]218#ifdef TRY_BROKEN_DESIRED_MD_SUBSCRIPT
219
[c7625e0]220// Desired form. One definition with recursion on IxBC (worked until Jan 2021, see trac #__TODO__)
221
[63a4b92]222forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } )
[9fa538c]223static inline TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
[1bb0170]224 return this[ab][bc];
[c7625e0]225}
226
[d1abc63c]227#else
[c7625e0]228
[63a4b92]229// Workaround form. Listing all possibilities up to 4 dims.
[c7625e0]230
[63a4b92]231forall( TA &, TB &, TC &, IxAB_0, IxBC | { TB & ?[?]( TA &, IxAB_0 ); TC & ?[?]( TB &, IxBC ); } )
[9fa538c]232static inline TC & ?[?]( TA & this, IxAB_0 ab, IxBC bc ) {
[1bb0170]233 return this[ab][bc];
[c7625e0]234}
235
[63a4b92]236forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1 ); TC & ?[?]( TB &, IxBC ); } )
[9fa538c]237static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxBC bc ) {
[1bb0170]238 return this[[ab0,ab1]][bc];
[63a4b92]239}
240
241forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxAB_2, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1, IxAB_2 ); TC & ?[?]( TB &, IxBC ); } )
[9fa538c]242static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxAB_2 ab2, IxBC bc ) {
[1bb0170]243 return this[[ab0,ab1,ab2]][bc];
[63a4b92]244}
245
246#endif
247
[997324c]248// Available for users to work around Trac #265
249// If `a[...0...]` isn't working, try `a[...ix0...]` instead.
[a5e26821]250
[997324c]251#define ix0 ((ptrdiff_t)0)
[a5e26821]252
253
254
[c7625e0]255//
256// Rotation
257//
258
259// Base
[63f42a8]260forall( [Nq], Sq & | sized(Sq), Tbase & )
[b8e047a]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;
[1bb0170]263 return ret;
[6448f7d]264}
[c7625e0]265
266// Rec
[b8e047a]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;
[1bb0170]270 return ret;
[6448f7d]271}
[c7625e0]272
273// Wrapper
[058ece2]274extern struct all_t {} all;
[b8e047a]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 ) {
[1bb0170]277 return (result&) this;
[c7625e0]278}
279
280//
281// Trait of array or slice
282//
283
[a5e26821]284// desired:
[7882c58]285// forall(A &, Tv &, [N])
286// trait ar {
[1bb0170]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) );
[a5e26821]293// };
294
295// working around N's not being accepted as arguments to traits
296
[1bb0170]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) ); \
[a5e26821]306}
Note: See TracBrowser for help on using the repository browser.