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

Last change on this file since b67d7a5b was b8e047a, checked in by Peter A. Buhr <pabuhr@…>, 4 days ago

formatting

  • Property mode set to 100644
File size: 10.3 KB
RevLine 
[a5e2682]1#pragma once
2
[fee4436]3//#include <assert.h>
[c7625e0]4
5
[6e50a6b]6forall( __CFA_tysys_id_only_X & ) struct tag {};
[c7625e0]7#define ttag(T) ((tag(T)){})
[6e50a6b]8#define ztag(n) ttag(n)
[c7625e0]9
[fee4436]10#ifdef __CFA_DEBUG__
[1bb0170]11#define subcheck( arr, sub, lb, ub ) \
12        if ( (sub) < (lb) || (sub) >= (ub) ) \
[5ff721a]13                abort( "subscript %ld exceeds dimension range [%d,%zd) for array %p.\n", \
[fee4436]14                           (sub), (lb), (ub), (arr) )
15#else
16#define subcheck( arr, sub, lb, ub ) do {} while (0)
17#endif
[c7625e0]18
[ad24245]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.
[c7625e0]23//
[ad24245]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
[c7625e0]44//
[63f42a8]45forall( [N], S & | sized(S), Timmed &, Tbase & ) {
[1bb0170]46        //
[b8e047a]47        // Single-dim array struct (with explicit packing and atom)
[1bb0170]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
[b8e047a]85        static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, zero_t ) {
[1bb0170]86                //assert( 0 < N );
87                subcheck( a, 0L, 0, N );
[b8e047a]88                return (Timmed &)a.strides[0];
[1bb0170]89        }
90
[b8e047a]91        static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, one_t ) {
[1bb0170]92                //assert( 1 < N );
93                subcheck( a, 1L, 0, N );
[b8e047a]94                return (Timmed &)a.strides[1];
[1bb0170]95        }
96
[b8e047a]97        static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, int i ) {
[1bb0170]98                //assert( i < N );
99                subcheck( a, (long int)i, 0, N );
[b8e047a]100                return (Timmed &)a.strides[i];
[1bb0170]101        }
102
[b8e047a]103        static inline const Timmed & ?[?]( const arpk( N, S, Timmed, Tbase ) & a, int i ) {
[1bb0170]104                //assert( i < N );
105                subcheck( a, (long int)i, 0, N );
[b8e047a]106                return (Timmed &)a.strides[i];
[1bb0170]107        }
108
[b8e047a]109        static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, unsigned int i ) {
[1bb0170]110                //assert( i < N );
111                subcheck( a, (long int)i, 0, 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 ) {
[1bb0170]116                //assert( i < N );
117                subcheck( a, (unsigned long int)i, 0, N );
[b8e047a]118                return (Timmed &)a.strides[i];
[1bb0170]119        }
120
[b8e047a]121        static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, long int i ) {
[1bb0170]122                //assert( i < N );
123                subcheck( a, i, 0, N );
[b8e047a]124                return (Timmed &)a.strides[i];
[1bb0170]125        }
126
[b8e047a]127        static inline const Timmed & ?[?]( const arpk( N, S, Timmed, Tbase ) & a, long int i ) {
[1bb0170]128                //assert( i < N );
129                subcheck( a, i, 0, N );
[b8e047a]130                return (Timmed &)a.strides[i];
[1bb0170]131        }
132
[b8e047a]133        static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, unsigned long int i ) {
[1bb0170]134                //assert( i < N );
135                subcheck( a, i, 0, N );
[b8e047a]136                return (Timmed &)a.strides[i];
[1bb0170]137        }
138
[b8e047a]139        static inline const Timmed & ?[?]( const arpk( N, S, Timmed, Tbase ) & a, unsigned long int i ) {
[1bb0170]140                //assert( i < N );
141                subcheck( a, i, 0, N );
[b8e047a]142                return (Timmed &)a.strides[i];
[1bb0170]143        }
144
[b8e047a]145        static inline size_t ?`len( arpk( N, S, Timmed, Tbase ) & a ) {
[1bb0170]146                return N;
147        }
148
[b8e047a]149        static inline void __taglen( tag(arpk( N, S, Timmed, Tbase )), tag(N) ) {}
[cfbc56ec]150}
[a5e2682]151
[cfbc56ec]152// RAII pattern has workarounds for
153//  - Trac 226:  Simplest handling would be, require immediate element to be otype, let autogen
[1bb0170]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 ^?{}.
[cfbc56ec]157
158forall( [N], S & | sized(S), Timmed &, Tbase & | { void ?{}( Timmed & ); } )
[b8e047a]159static inline void ?{}( arpk( N, S, Timmed, Tbase ) & this ) { 
[1bb0170]160        void ?{}( S (&)[N] ) {}
161        ?{}(this.strides);
[cfbc56ec]162
[b8e047a]163        for (i; N) ?{}( (Timmed &)this.strides[i] );
[cfbc56ec]164}
165
166forall( [N], S & | sized(S), Timmed &, Tbase & | { void ^?{}( Timmed & ); } )
[b8e047a]167static inline void ^?{}( arpk( N, S, Timmed, Tbase ) & this ) {
[1bb0170]168        void ^?{}( S (&)[N] ) {}
169        ^?{}(this.strides);
[cfbc56ec]170
[1bb0170]171        for (i; N ) {
[b8e047a]172                ^?{}( (Timmed &)this.strides[N-i-1] );
[1bb0170]173        }
[c7625e0]174}
175
176//
177// Sugar for declaring array structure instances
178//
179
[cfbc56ec]180forall( Te * )
[9fa538c]181static inline Te mkar_( tag(Te) ) {}
[c7625e0]182
[b9dae14c]183forall( [N], ZTags ... , Trslt &, Tatom & | { Trslt mkar_( tag(Tatom), ZTags ); } )
[b8e047a]184static inline arpk( N, Trslt, Trslt, Tatom) mkar_( tag(Tatom), tag(N), ZTags ) {}
[c7625e0]185
186// based on https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros
187
[1bb0170]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
[c7625e0]196
[1bb0170]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__)
[c7625e0]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
[63a4b92]213#ifdef TRY_BROKEN_DESIRED_MD_SUBSCRIPT
214
[c7625e0]215// Desired form.  One definition with recursion on IxBC (worked until Jan 2021, see trac #__TODO__)
216
[63a4b92]217forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } )
[9fa538c]218static inline TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
[1bb0170]219        return this[ab][bc];
[c7625e0]220}
221
[d1abc63c]222#else
[c7625e0]223
[63a4b92]224// Workaround form.  Listing all possibilities up to 4 dims.
[c7625e0]225
[63a4b92]226forall( TA &, TB &, TC &, IxAB_0, IxBC | { TB & ?[?]( TA &, IxAB_0 ); TC & ?[?]( TB &, IxBC ); } )
[9fa538c]227static inline TC & ?[?]( TA & this, IxAB_0 ab, IxBC bc ) {
[1bb0170]228        return this[ab][bc];
[c7625e0]229}
230
[63a4b92]231forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1 ); TC & ?[?]( TB &, IxBC ); } )
[9fa538c]232static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxBC bc ) {
[1bb0170]233        return this[[ab0,ab1]][bc];
[63a4b92]234}
235
236forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxAB_2, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1, IxAB_2 ); TC & ?[?]( TB &, IxBC ); } )
[9fa538c]237static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxAB_2 ab2, IxBC bc ) {
[1bb0170]238        return this[[ab0,ab1,ab2]][bc];
[63a4b92]239}
240
241#endif
242
[997324c]243// Available for users to work around Trac #265
244// If `a[...0...]` isn't working, try `a[...ix0...]` instead.
[a5e2682]245
[997324c]246#define ix0 ((ptrdiff_t)0)
[a5e2682]247
248
249
[c7625e0]250//
251// Rotation
252//
253
254// Base
[63f42a8]255forall( [Nq], Sq & | sized(Sq), Tbase & )
[b8e047a]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;
[1bb0170]258        return ret;
[6448f7d]259}
[c7625e0]260
261// Rec
[b8e047a]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;
[1bb0170]265        return ret;
[6448f7d]266}
[c7625e0]267
268// Wrapper
[058ece2]269extern struct all_t {} all;
[b8e047a]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 ) {
[1bb0170]272        return (result&) this;
[c7625e0]273}
274
275//
276// Trait of array or slice
277//
278
[a5e2682]279// desired:
[7882c58]280// forall(A &, Tv &, [N])
281// trait ar {
[1bb0170]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) );
[a5e2682]288// };
289
290// working around N's not being accepted as arguments to traits
291
[1bb0170]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) );        \
[a5e2682]301}
Note: See TracBrowser for help on using the repository browser.