source: libcfa/src/containers/array.hfa @ 1a45263

ADTast-experimental
Last change on this file since 1a45263 was 058ece2, checked in by Thierry Delisle <tdelisle@…>, 2 years ago

made all_t all extern. There is no definition but since it is a zero-byte object it doesn't actually matter

  • Property mode set to 100644
File size: 6.3 KB
RevLine 
[8d76f2b]1#include <assert.h>
[c7625e0]2
3
[6e50a6b]4forall( __CFA_tysys_id_only_X & ) struct tag {};
[c7625e0]5#define ttag(T) ((tag(T)){})
[6e50a6b]6#define ztag(n) ttag(n)
[c7625e0]7
8
9//
10// Single-dim array sruct (with explicit packing and atom)
11//
12
[63f42a8]13forall( [N], S & | sized(S), Timmed &, Tbase & ) {
[c7625e0]14    struct arpk {
[6e50a6b]15        S strides[N];
[c7625e0]16    };
17
[9fa538c]18    // About the choice of integral types offered as subscript overloads:
19    // Intent is to cover these use cases:
20    //    float foo( ptrdiff_t i ) { return a[i]; }           // i : ptrdiff_t
21    //    forall( [N] ) ... for( i; N ) { total += a[i]; }    // i : typeof( sizeof(42) )
22    //    for( i; 5 ) { total += a[i]; }                      // i : int
23    // It gets complicated by:
24    // -  CFA does overloading on concrete types, like int and unsigned int, not on typedefed
25    //    types like size_t.  So trying to overload on ptrdiff_t vs int works in 64-bit mode
26    //    but not in 32-bit mode.
27    // -  Given bug of Trac #247, CFA gives sizeof expressions type unsigned long int, when it
28    //    should give them type size_t.
[d1abc63c]29    //
[9fa538c]30    //                          gcc -m32         cfa -m32 given bug         gcc -m64
31    // ptrdiff_t                int              int                        long int
32    // size_t                   unsigned int     unsigned int               unsigned long int
33    // typeof( sizeof(42) )     unsigned int     unsigned long int          unsigned long int
34    // int                      int              int                        int
35
36    static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, int i ) {
[8d76f2b]37        assert( i < N );
[c7625e0]38        return (Timmed &) a.strides[i];
39    }
40
[d1abc63c]41    static inline const Timmed & ?[?]( const arpk(N, S, Timmed, Tbase) & a, int i ) {
42        assert( i < N );
43        return (Timmed &) a.strides[i];
44    }
45
[9fa538c]46    static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, unsigned int i ) {
[8d76f2b]47        assert( i < N );
[63a4b92]48        return (Timmed &) a.strides[i];
49    }
50
[d1abc63c]51    static inline const Timmed & ?[?]( const arpk(N, S, Timmed, Tbase) & a, unsigned int i ) {
52        assert( i < N );
53        return (Timmed &) a.strides[i];
54    }
55
[9fa538c]56    static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, long int i ) {
[8d76f2b]57        assert( i < N );
[63a4b92]58        return (Timmed &) a.strides[i];
59    }
60
[d1abc63c]61    static inline const Timmed & ?[?]( const arpk(N, S, Timmed, Tbase) & a, long int i ) {
62        assert( i < N );
63        return (Timmed &) a.strides[i];
64    }
65
[9fa538c]66    static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, unsigned long int i ) {
[8d76f2b]67        assert( i < N );
[9fa538c]68        return (Timmed &) a.strides[i];
69    }
70
[d1abc63c]71    static inline const Timmed & ?[?]( const arpk(N, S, Timmed, Tbase) & a, unsigned long int i ) {
72        assert( i < N );
73        return (Timmed &) a.strides[i];
74    }
75
[9fa538c]76    static inline size_t ?`len( arpk(N, S, Timmed, Tbase) & a ) {
[6e50a6b]77        return N;
[c7625e0]78    }
79
80    // workaround #226 (and array relevance thereof demonstrated in mike102/otype-slow-ndims.cfa)
[9fa538c]81    static inline void ?{}( arpk(N, S, Timmed, Tbase) & this ) {
[6e50a6b]82        void ?{}( S (&inner)[N] ) {}
[c7625e0]83        ?{}(this.strides);
84    }
[9fa538c]85    static inline void ^?{}( arpk(N, S, Timmed, Tbase) & this ) {
[6e50a6b]86        void ^?{}( S (&inner)[N] ) {}
[c7625e0]87        ^?{}(this.strides);
88    }
89}
90
91//
92// Sugar for declaring array structure instances
93//
94
95forall( Te )
[9fa538c]96static inline Te mkar_( tag(Te) ) {}
[c7625e0]97
[b9dae14c]98forall( [N], ZTags ... , Trslt &, Tatom & | { Trslt mkar_( tag(Tatom), ZTags ); } )
[9fa538c]99static inline arpk(N, Trslt, Trslt, Tatom) mkar_( tag(Tatom), tag(N), ZTags ) {}
[c7625e0]100
101// based on https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros
102
103    // Make a FOREACH macro
104    #define FE_0(WHAT)
[d1abc63c]105    #define FE_1(WHAT, X) WHAT(X)
[c7625e0]106    #define FE_2(WHAT, X, ...) WHAT(X)FE_1(WHAT, __VA_ARGS__)
107    #define FE_3(WHAT, X, ...) WHAT(X)FE_2(WHAT, __VA_ARGS__)
108    #define FE_4(WHAT, X, ...) WHAT(X)FE_3(WHAT, __VA_ARGS__)
109    #define FE_5(WHAT, X, ...) WHAT(X)FE_4(WHAT, __VA_ARGS__)
110    //... repeat as needed
111
[d1abc63c]112    #define GET_MACRO(_0,_1,_2,_3,_4,_5,NAME,...) NAME
[c7625e0]113    #define FOR_EACH(action,...) \
114    GET_MACRO(_0,__VA_ARGS__,FE_5,FE_4,FE_3,FE_2,FE_1,FE_0)(action,__VA_ARGS__)
115
116#define COMMA_ttag(X) , ttag(X)
117#define array( TE, ...) typeof( mkar_( ttag(TE)  FOR_EACH( COMMA_ttag, __VA_ARGS__ ) ) )
118
119#define COMMA_ztag(X) , ztag(X)
120#define zarray( TE, ...) typeof( mkar_( ttag(TE)  FOR_EACH( COMMA_ztag, __VA_ARGS__ ) ) )
121
122//
123// Sugar for multidimensional indexing
124//
125
126// Core -[[-,-,-]] operator
127
[63a4b92]128#ifdef TRY_BROKEN_DESIRED_MD_SUBSCRIPT
129
[c7625e0]130// Desired form.  One definition with recursion on IxBC (worked until Jan 2021, see trac #__TODO__)
131
[63a4b92]132forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } )
[9fa538c]133static inline TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
[c7625e0]134    return this[ab][bc];
135}
136
[d1abc63c]137#else
[c7625e0]138
[63a4b92]139// Workaround form.  Listing all possibilities up to 4 dims.
[c7625e0]140
[63a4b92]141forall( TA &, TB &, TC &, IxAB_0, IxBC | { TB & ?[?]( TA &, IxAB_0 ); TC & ?[?]( TB &, IxBC ); } )
[9fa538c]142static inline TC & ?[?]( TA & this, IxAB_0 ab, IxBC bc ) {
[63a4b92]143    return this[ab][bc];
[c7625e0]144}
145
[63a4b92]146forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1 ); TC & ?[?]( TB &, IxBC ); } )
[9fa538c]147static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxBC bc ) {
[63a4b92]148    return this[[ab0,ab1]][bc];
149}
150
151forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxAB_2, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1, IxAB_2 ); TC & ?[?]( TB &, IxBC ); } )
[9fa538c]152static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxAB_2 ab2, IxBC bc ) {
[63a4b92]153    return this[[ab0,ab1,ab2]][bc];
154}
155
156#endif
157
[c7625e0]158//
159// Rotation
160//
161
162// Base
[63f42a8]163forall( [Nq], Sq & | sized(Sq), Tbase & )
[6448f7d]164static inline tag(arpk(Nq, Sq, Tbase, Tbase)) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(Tbase) ) {
165    tag(arpk(Nq, Sq, Tbase, Tbase)) ret;
166    return ret;
167}
[c7625e0]168
169// Rec
[63f42a8]170forall( [Nq], Sq & | sized(Sq), [N], S & | sized(S), recq &, recr &, Tbase & | { tag(recr) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(recq) ); } )
[6448f7d]171static inline tag(arpk(N, S, recr, Tbase)) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(arpk(N, S, recq, Tbase)) ) {
172    tag(arpk(N, S, recr, Tbase)) ret;
173    return ret;
174}
[c7625e0]175
176// Wrapper
[058ece2]177extern struct all_t {} all;
[63f42a8]178forall( [N], S & | sized(S), Te &, result &, Tbase & | { tag(result) enq_( tag(Tbase), tag(N), tag(S), tag(Te) ); } )
[9fa538c]179static inline result & ?[?]( arpk(N, S, Te, Tbase) & this, all_t ) {
[c7625e0]180    return (result&) this;
181}
182
183//
184// Trait of array or slice
185//
186
187trait ar(A &, Tv &) {
188    Tv& ?[?]( A&, ptrdiff_t );
189    size_t ?`len( A& );
190};
Note: See TracBrowser for help on using the repository browser.