source: libcfa/src/containers/array.hfa @ f48577a

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since f48577a was 63f42a8, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

Re-worked new array declarations to avoid suggestion that a type-system managed value needs to appear in type position. The arpk S parameter is now clearly a type, not a length.

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