source: libcfa/src/collections/array.hfa @ 4558df2

Last change on this file since 4558df2 was b8e047a, checked in by Peter A. Buhr <pabuhr@…>, 3 months ago

formatting

  • Property mode set to 100644
File size: 10.3 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
158forall( [N], S & | sized(S), Timmed &, Tbase & | { void ?{}( Timmed & ); } )
159static inline void ?{}( arpk( N, S, Timmed, Tbase ) & this ) { 
160        void ?{}( S (&)[N] ) {}
161        ?{}(this.strides);
162
163        for (i; N) ?{}( (Timmed &)this.strides[i] );
164}
165
166forall( [N], S & | sized(S), Timmed &, Tbase & | { void ^?{}( Timmed & ); } )
167static inline void ^?{}( arpk( N, S, Timmed, Tbase ) & this ) {
168        void ^?{}( S (&)[N] ) {}
169        ^?{}(this.strides);
170
171        for (i; N ) {
172                ^?{}( (Timmed &)this.strides[N-i-1] );
173        }
174}
175
176//
177// Sugar for declaring array structure instances
178//
179
180forall( Te * )
181static inline Te mkar_( tag(Te) ) {}
182
183forall( [N], ZTags ... , Trslt &, Tatom & | { Trslt mkar_( tag(Tatom), ZTags ); } )
184static inline arpk( N, Trslt, Trslt, Tatom) mkar_( tag(Tatom), tag(N), ZTags ) {}
185
186// based on https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros
187
188        // Make a FOREACH macro
189        #define FE_0(WHAT)
190        #define FE_1(WHAT, X) WHAT(X)
191        #define FE_2(WHAT, X, ...) WHAT(X)FE_1(WHAT, __VA_ARGS__)
192        #define FE_3(WHAT, X, ...) WHAT(X)FE_2(WHAT, __VA_ARGS__)
193        #define FE_4(WHAT, X, ...) WHAT(X)FE_3(WHAT, __VA_ARGS__)
194        #define FE_5(WHAT, X, ...) WHAT(X)FE_4(WHAT, __VA_ARGS__)
195        //... repeat as needed
196
197        #define GET_MACRO(_0,_1,_2,_3,_4,_5,NAME,...) NAME
198        #define FOR_EACH(action,...) \
199        GET_MACRO(_0,__VA_ARGS__,FE_5,FE_4,FE_3,FE_2,FE_1,FE_0)(action,__VA_ARGS__)
200
201#define COMMA_ttag(X) , ttag(X)
202#define array( TE, ...) typeof( mkar_( ttag(TE)  FOR_EACH( COMMA_ttag, __VA_ARGS__ ) ) )
203
204#define COMMA_ztag(X) , ztag(X)
205#define zarray( TE, ...) typeof( mkar_( ttag(TE)  FOR_EACH( COMMA_ztag, __VA_ARGS__ ) ) )
206
207//
208// Sugar for multidimensional indexing
209//
210
211// Core -[[-,-,-]] operator
212
213#ifdef TRY_BROKEN_DESIRED_MD_SUBSCRIPT
214
215// Desired form.  One definition with recursion on IxBC (worked until Jan 2021, see trac #__TODO__)
216
217forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } )
218static inline TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
219        return this[ab][bc];
220}
221
222#else
223
224// Workaround form.  Listing all possibilities up to 4 dims.
225
226forall( TA &, TB &, TC &, IxAB_0, IxBC | { TB & ?[?]( TA &, IxAB_0 ); TC & ?[?]( TB &, IxBC ); } )
227static inline TC & ?[?]( TA & this, IxAB_0 ab, IxBC bc ) {
228        return this[ab][bc];
229}
230
231forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1 ); TC & ?[?]( TB &, IxBC ); } )
232static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxBC bc ) {
233        return this[[ab0,ab1]][bc];
234}
235
236forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxAB_2, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1, IxAB_2 ); TC & ?[?]( TB &, IxBC ); } )
237static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxAB_2 ab2, IxBC bc ) {
238        return this[[ab0,ab1,ab2]][bc];
239}
240
241#endif
242
243// Available for users to work around Trac #265
244// If `a[...0...]` isn't working, try `a[...ix0...]` instead.
245
246#define ix0 ((ptrdiff_t)0)
247
248
249
250//
251// Rotation
252//
253
254// Base
255forall( [Nq], Sq & | sized(Sq), Tbase & )
256static inline tag(arpk( Nq, Sq, Tbase, Tbase )) enq_( tag(Tbase ), tag(Nq), tag(Sq), tag(Tbase ) ) {
257        tag(arpk( Nq, Sq, Tbase, Tbase )) ret;
258        return ret;
259}
260
261// Rec
262forall( [Nq], Sq & | sized(Sq), [N], S & | sized(S), recq &, recr &, Tbase & | { tag(recr) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(recq) ); } )
263static inline tag(arpk( N, S, recr, Tbase )) enq_( tag(Tbase ), tag(Nq), tag(Sq), tag(arpk( N, S, recq, Tbase )) ) {
264        tag(arpk( N, S, recr, Tbase )) ret;
265        return ret;
266}
267
268// Wrapper
269extern struct all_t {} all;
270forall( [N], S & | sized(S), Te &, result &, Tbase & | { tag(result) enq_( tag(Tbase), tag(N), tag(S), tag(Te) ); } )
271static inline result & ?[?]( arpk( N, S, Te, Tbase ) & this, all_t ) {
272        return (result&) this;
273}
274
275//
276// Trait of array or slice
277//
278
279// desired:
280// forall(A &, Tv &, [N])
281// trait ar {
282//       Tv& ?[?]( A&, zero_t );
283//       Tv& ?[?]( A&, one_t  );
284//       Tv& ?[?]( A&, int      );
285//                                 ...
286//       size_t ?`len( A& );
287//       void __taglen( tag(C), tag(N) );
288// };
289
290// working around N's not being accepted as arguments to traits
291
292#define ar( A, Tv, N ) {                                \
293        Tv& ?[?]( A&, zero_t );                         \
294        Tv& ?[?]( A&, one_t );                          \
295        Tv& ?[?]( A&, int );                            \
296        Tv& ?[?]( A&, unsigned int );           \
297        Tv& ?[?]( A&, long int );                       \
298        Tv& ?[?]( A&, unsigned long int );      \
299        size_t ?`len( A& );                                     \
300        void __taglen( tag(A), tag(N) );        \
301}
Note: See TracBrowser for help on using the repository browser.