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

ADT ast-experimental
Last change on this file since bc899d6 was 058ece2, checked in by Thierry Delisle <tdelisle@…>, 3 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
Line 
1#include <assert.h>
2
3
4forall( __CFA_tysys_id_only_X & ) struct tag {};
5#define ttag(T) ((tag(T)){})
6#define ztag(n) ttag(n)
7
8
9//
10// Single-dim array sruct (with explicit packing and atom)
11//
12
13forall( [N], S & | sized(S), Timmed &, Tbase & ) {
14 struct arpk {
15 S strides[N];
16 };
17
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.
29 //
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 ) {
37 assert( i < N );
38 return (Timmed &) a.strides[i];
39 }
40
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
46 static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, unsigned int i ) {
47 assert( i < N );
48 return (Timmed &) a.strides[i];
49 }
50
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
56 static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, long int i ) {
57 assert( i < N );
58 return (Timmed &) a.strides[i];
59 }
60
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
66 static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, unsigned long int i ) {
67 assert( i < N );
68 return (Timmed &) a.strides[i];
69 }
70
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
76 static inline size_t ?`len( arpk(N, S, Timmed, Tbase) & a ) {
77 return N;
78 }
79
80 // workaround #226 (and array relevance thereof demonstrated in mike102/otype-slow-ndims.cfa)
81 static inline void ?{}( arpk(N, S, Timmed, Tbase) & this ) {
82 void ?{}( S (&inner)[N] ) {}
83 ?{}(this.strides);
84 }
85 static inline void ^?{}( arpk(N, S, Timmed, Tbase) & this ) {
86 void ^?{}( S (&inner)[N] ) {}
87 ^?{}(this.strides);
88 }
89}
90
91//
92// Sugar for declaring array structure instances
93//
94
95forall( Te )
96static inline Te mkar_( tag(Te) ) {}
97
98forall( [N], ZTags ... , Trslt &, Tatom & | { Trslt mkar_( tag(Tatom), ZTags ); } )
99static inline arpk(N, Trslt, Trslt, Tatom) mkar_( tag(Tatom), tag(N), ZTags ) {}
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)
105 #define FE_1(WHAT, X) WHAT(X)
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
112 #define GET_MACRO(_0,_1,_2,_3,_4,_5,NAME,...) NAME
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
128#ifdef TRY_BROKEN_DESIRED_MD_SUBSCRIPT
129
130// Desired form. One definition with recursion on IxBC (worked until Jan 2021, see trac #__TODO__)
131
132forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } )
133static inline TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
134 return this[ab][bc];
135}
136
137#else
138
139// Workaround form. Listing all possibilities up to 4 dims.
140
141forall( TA &, TB &, TC &, IxAB_0, IxBC | { TB & ?[?]( TA &, IxAB_0 ); TC & ?[?]( TB &, IxBC ); } )
142static inline TC & ?[?]( TA & this, IxAB_0 ab, IxBC bc ) {
143 return this[ab][bc];
144}
145
146forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1 ); TC & ?[?]( TB &, IxBC ); } )
147static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxBC bc ) {
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 ); } )
152static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxAB_2 ab2, IxBC bc ) {
153 return this[[ab0,ab1,ab2]][bc];
154}
155
156#endif
157
158//
159// Rotation
160//
161
162// Base
163forall( [Nq], Sq & | sized(Sq), Tbase & )
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}
168
169// Rec
170forall( [Nq], Sq & | sized(Sq), [N], S & | sized(S), recq &, recr &, Tbase & | { tag(recr) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(recq) ); } )
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}
175
176// Wrapper
177extern struct all_t {} all;
178forall( [N], S & | sized(S), Te &, result &, Tbase & | { tag(result) enq_( tag(Tbase), tag(N), tag(S), tag(Te) ); } )
179static inline result & ?[?]( arpk(N, S, Te, Tbase) & this, all_t ) {
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.