source: libcfa/src/collections/array.hfa @ 7b05de4

Last change on this file since 7b05de4 was cfbc56ec, checked in by Michael Brooks <mlbrooks@…>, 10 months ago

Enable array RAII and provide uninit(-), a uNoCtor equivalent.

Enable construction/destruction of "new" CFA array elements,
which was previously deactivated to avoid a compiler performance issue.
The enabled RAII steps more carefully around the performance issue.

Provide uninit(-), with tests covering the typical use case:

struct Foo;
void ?{}( Foo & this, int i ) { printf( "ctor at %d\n", i ); }
uninit(Foo) a[10]; no prints
for (i; 10) (a[i]){ i };
prints
array(uninit(Foo), 10) b; no prints
for (i; 10) (b[i]){ i };
prints

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