source: libcfa/src/collections/array.hfa @ 5ff721a

Last change on this file since 5ff721a was 5ff721a, checked in by Peter A. Buhr <pabuhr@…>, 5 hours ago

forgot period at end of error message.

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