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

Last change on this file since ed96731 was cdf7d43, checked in by Michael Brooks <mlbrooks@…>, 2 months ago

Hopefully fix broken build, from multiple declarations of arrah.hfa's delay_init.

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