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

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

Try to fix full build by removing missed x86-debug-specific workaround for #269

  • Property mode set to 100644
File size: 10.6 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__
[8ee211d]10#define subcheck( arr, sub, len ) \
11        if ( (sub) < 0 || (sub) >= (len) ) \
[1f6623c]12                abort( "Subscript %ld exceeds dimension range [0,%zu) for array %p.\n", \
[8ee211d]13                           (sub), (len), (arr) )
14#define subchecku( arr, sub, len ) \
15        if ( (sub) >= (len) ) \
[1f6623c]16                abort( "Subscript %ld exceeds dimension range [0,%zu) for array %p.\n", \
[8ee211d]17                           (sub), (len), (arr) )
[fee4436]18#else
[8ee211d]19#define subcheck( arr, sub, len ) do {} while (0)
20#define subchecku( arr, sub, len ) do {} while (0)
[fee4436]21#endif
[c7625e0]22
[ad24245]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.
[c7625e0]27//
[ad24245]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
[c7625e0]48//
[63f42a8]49forall( [N], S & | sized(S), Timmed &, Tbase & ) {
[1bb0170]50        //
[b8e047a]51        // Single-dim array struct (with explicit packing and atom)
[1bb0170]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        // -  Given bug of Trac #247, CFA gives sizeof expressions type unsigned long int, when it
72        //    should give them type size_t.
73        //
74        //                          gcc -m32         cfa -m32 given bug         gcc -m64 (and cfa)
75        // ptrdiff_t                int              int                        long int
76        // size_t                   unsigned int     unsigned int               unsigned long int
77        // typeof( sizeof(42) )     unsigned int     unsigned long int          unsigned long int
78        // int                      int              int                        int
79        //
80        // So the solution must support types {zero_t, one_t, int, unsigned int, long int, unsigned long int}
81        //
82        // The solution cannot rely on implicit conversions (e.g. just have one overload for ptrdiff_t)
83        // because assertion satisfaction requires types to match exacly.  Both higher-dimensional
84        // subscripting and operations on slices use asserted subscript operators.  The test case
85        // array-container/array-sbscr-cases covers the combinations.  Mike beleives that commenting out
86        // any of the current overloads leads to one of those cases failing, either on 64- or 32-bit.
87        // Mike is open to being shown a smaller set of overloads that still passes the test.
88
[b8e047a]89        static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, zero_t ) {
[8ee211d]90                subcheck( a, 0L, N );
[b8e047a]91                return (Timmed &)a.strides[0];
[1bb0170]92        }
93
[b8e047a]94        static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, one_t ) {
[8ee211d]95                subcheck( a, 1L, N );
[b8e047a]96                return (Timmed &)a.strides[1];
[1bb0170]97        }
98
[b8e047a]99        static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, int i ) {
[8ee211d]100                subcheck( a, (long int)i, N );
[b8e047a]101                return (Timmed &)a.strides[i];
[1bb0170]102        }
103
[b8e047a]104        static inline const Timmed & ?[?]( const arpk( N, S, Timmed, Tbase ) & a, int i ) {
[8ee211d]105                subcheck( a, (long int)i, 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 ) {
[8ee211d]110                subchecku( a, (unsigned long int)i, N );
[b8e047a]111                return (Timmed &)a.strides[i];
[1bb0170]112        }
113
[b8e047a]114        static inline const Timmed & ?[?]( const arpk( N, S, Timmed, Tbase ) & a, unsigned int i ) {
[8ee211d]115                subchecku( a, (unsigned long int)i, N );
[b8e047a]116                return (Timmed &)a.strides[i];
[1bb0170]117        }
118
[b8e047a]119        static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, long int i ) {
[8ee211d]120                subcheck( a, i, N );
[b8e047a]121                return (Timmed &)a.strides[i];
[1bb0170]122        }
123
[b8e047a]124        static inline const Timmed & ?[?]( const arpk( N, S, Timmed, Tbase ) & a, long int i ) {
[8ee211d]125                subcheck( a, i, N );
[b8e047a]126                return (Timmed &)a.strides[i];
[1bb0170]127        }
128
[b8e047a]129        static inline Timmed & ?[?]( arpk( N, S, Timmed, Tbase ) & a, unsigned long int i ) {
[8ee211d]130                subchecku( a, i, N );
[b8e047a]131                return (Timmed &)a.strides[i];
[1bb0170]132        }
133
[b8e047a]134        static inline const Timmed & ?[?]( const arpk( N, S, Timmed, Tbase ) & a, unsigned long int i ) {
[8ee211d]135                subchecku( a, i, N );
[b8e047a]136                return (Timmed &)a.strides[i];
[1bb0170]137        }
138
[8ee211d]139        static inline size_t ?`len( arpk( N, S, Timmed, Tbase ) & ) {
[1bb0170]140                return N;
141        }
142
[b8e047a]143        static inline void __taglen( tag(arpk( N, S, Timmed, Tbase )), tag(N) ) {}
[cfbc56ec]144}
[a5e26821]145
[cfbc56ec]146// RAII pattern has workarounds for
147//  - Trac 226:  Simplest handling would be, require immediate element to be otype, let autogen
[1bb0170]148//      raii happen.  Performance on even a couple dimensions is unacceptable because of exponential
149//      thunk creation: ?{}() needs all four otype funcs from next level, so does ^?{}(), so do the
150//      other two.  This solution offers ?{}() that needs only ?{}(), and similar for ^?{}.
[cfbc56ec]151
[1665ee5]152// skip initializing elements
153//   array(float, 5) x = { delay_init };
[cdf7d43]154enum () delay_init_t { delay_init };
[1665ee5]155forall( [N], S & | sized(S), Timmed &, Tbase & )
156static inline void ?{}( arpk( N, S, Timmed, Tbase ) & this, delay_init_t ) {
[1bb0170]157        void ?{}( S (&)[N] ) {}
158        ?{}(this.strides);
[1665ee5]159}
[cfbc56ec]160
[1665ee5]161// call default ctor on elements
162//   array(float, 5) x;
163forall( [N], S & | sized(S), Timmed &, Tbase & | { void ?{}( Timmed & ); } )
164static inline void ?{}( arpk( N, S, Timmed, Tbase ) & this ) { 
165        ?{}( this, delay_init );
[b8e047a]166        for (i; N) ?{}( (Timmed &)this.strides[i] );
[cfbc56ec]167}
168
169forall( [N], S & | sized(S), Timmed &, Tbase & | { void ^?{}( Timmed & ); } )
[b8e047a]170static inline void ^?{}( arpk( N, S, Timmed, Tbase ) & this ) {
[1bb0170]171        void ^?{}( S (&)[N] ) {}
172        ^?{}(this.strides);
[cfbc56ec]173
[1bb0170]174        for (i; N ) {
[b8e047a]175                ^?{}( (Timmed &)this.strides[N-i-1] );
[1bb0170]176        }
[c7625e0]177}
178
[1665ee5]179
[c7625e0]180//
181// Sugar for declaring array structure instances
182//
183
[cfbc56ec]184forall( Te * )
[9fa538c]185static inline Te mkar_( tag(Te) ) {}
[c7625e0]186
[b9dae14c]187forall( [N], ZTags ... , Trslt &, Tatom & | { Trslt mkar_( tag(Tatom), ZTags ); } )
[b8e047a]188static inline arpk( N, Trslt, Trslt, Tatom) mkar_( tag(Tatom), tag(N), ZTags ) {}
[c7625e0]189
190// based on https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros
191
[1bb0170]192        // Make a FOREACH macro
193        #define FE_0(WHAT)
194        #define FE_1(WHAT, X) WHAT(X)
195        #define FE_2(WHAT, X, ...) WHAT(X)FE_1(WHAT, __VA_ARGS__)
196        #define FE_3(WHAT, X, ...) WHAT(X)FE_2(WHAT, __VA_ARGS__)
197        #define FE_4(WHAT, X, ...) WHAT(X)FE_3(WHAT, __VA_ARGS__)
198        #define FE_5(WHAT, X, ...) WHAT(X)FE_4(WHAT, __VA_ARGS__)
199        //... repeat as needed
[c7625e0]200
[1bb0170]201        #define GET_MACRO(_0,_1,_2,_3,_4,_5,NAME,...) NAME
202        #define FOR_EACH(action,...) \
203        GET_MACRO(_0,__VA_ARGS__,FE_5,FE_4,FE_3,FE_2,FE_1,FE_0)(action,__VA_ARGS__)
[c7625e0]204
205#define COMMA_ttag(X) , ttag(X)
206#define array( TE, ...) typeof( mkar_( ttag(TE)  FOR_EACH( COMMA_ttag, __VA_ARGS__ ) ) )
207
208#define COMMA_ztag(X) , ztag(X)
209#define zarray( TE, ...) typeof( mkar_( ttag(TE)  FOR_EACH( COMMA_ztag, __VA_ARGS__ ) ) )
210
211//
212// Sugar for multidimensional indexing
213//
214
215// Core -[[-,-,-]] operator
216
[63a4b92]217#ifdef TRY_BROKEN_DESIRED_MD_SUBSCRIPT
218
[c7625e0]219// Desired form.  One definition with recursion on IxBC (worked until Jan 2021, see trac #__TODO__)
220
[63a4b92]221forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } )
[9fa538c]222static inline TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
[1bb0170]223        return this[ab][bc];
[c7625e0]224}
225
[d1abc63c]226#else
[c7625e0]227
[63a4b92]228// Workaround form.  Listing all possibilities up to 4 dims.
[c7625e0]229
[63a4b92]230forall( TA &, TB &, TC &, IxAB_0, IxBC | { TB & ?[?]( TA &, IxAB_0 ); TC & ?[?]( TB &, IxBC ); } )
[9fa538c]231static inline TC & ?[?]( TA & this, IxAB_0 ab, IxBC bc ) {
[1bb0170]232        return this[ab][bc];
[c7625e0]233}
234
[63a4b92]235forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1 ); TC & ?[?]( TB &, IxBC ); } )
[9fa538c]236static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxBC bc ) {
[1bb0170]237        return this[[ab0,ab1]][bc];
[63a4b92]238}
239
240forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxAB_2, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1, IxAB_2 ); TC & ?[?]( TB &, IxBC ); } )
[9fa538c]241static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxAB_2 ab2, IxBC bc ) {
[1bb0170]242        return this[[ab0,ab1,ab2]][bc];
[63a4b92]243}
244
245#endif
246
[997324c]247// Available for users to work around Trac #265
248// If `a[...0...]` isn't working, try `a[...ix0...]` instead.
[a5e26821]249
[997324c]250#define ix0 ((ptrdiff_t)0)
[a5e26821]251
252
253
[c7625e0]254//
255// Rotation
256//
257
258// Base
[63f42a8]259forall( [Nq], Sq & | sized(Sq), Tbase & )
[b8e047a]260static inline tag(arpk( Nq, Sq, Tbase, Tbase )) enq_( tag(Tbase ), tag(Nq), tag(Sq), tag(Tbase ) ) {
261        tag(arpk( Nq, Sq, Tbase, Tbase )) ret;
[1bb0170]262        return ret;
[6448f7d]263}
[c7625e0]264
265// Rec
[b8e047a]266forall( [Nq], Sq & | sized(Sq), [N], S & | sized(S), recq &, recr &, Tbase & | { tag(recr) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(recq) ); } )
267static inline tag(arpk( N, S, recr, Tbase )) enq_( tag(Tbase ), tag(Nq), tag(Sq), tag(arpk( N, S, recq, Tbase )) ) {
268        tag(arpk( N, S, recr, Tbase )) ret;
[1bb0170]269        return ret;
[6448f7d]270}
[c7625e0]271
272// Wrapper
[058ece2]273extern struct all_t {} all;
[b8e047a]274forall( [N], S & | sized(S), Te &, result &, Tbase & | { tag(result) enq_( tag(Tbase), tag(N), tag(S), tag(Te) ); } )
275static inline result & ?[?]( arpk( N, S, Te, Tbase ) & this, all_t ) {
[1bb0170]276        return (result&) this;
[c7625e0]277}
278
279//
280// Trait of array or slice
281//
282
[a5e26821]283// desired:
[7882c58]284// forall(A &, Tv &, [N])
285// trait ar {
[1bb0170]286//       Tv& ?[?]( A&, zero_t );
287//       Tv& ?[?]( A&, one_t  );
288//       Tv& ?[?]( A&, int      );
289//                                 ...
290//       size_t ?`len( A& );
291//       void __taglen( tag(C), tag(N) );
[a5e26821]292// };
293
294// working around N's not being accepted as arguments to traits
295
[1bb0170]296#define ar( A, Tv, N ) {                                \
297        Tv& ?[?]( A&, zero_t );                         \
298        Tv& ?[?]( A&, one_t );                          \
299        Tv& ?[?]( A&, int );                            \
300        Tv& ?[?]( A&, unsigned int );           \
301        Tv& ?[?]( A&, long int );                       \
302        Tv& ?[?]( A&, unsigned long int );      \
303        size_t ?`len( A& );                                     \
304        void __taglen( tag(A), tag(N) );        \
[a5e26821]305}
Note: See TracBrowser for help on using the repository browser.