source: libcfa/src/containers/array.hfa@ 4dba1da

ADT ast-experimental pthread-emulation
Last change on this file since 4dba1da was 8d76f2b, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

Adding runtime bound checking for array subscripts and showing the optimizer removing them.

Adding draft thesis content on dependent types and bound checks.

  • Property mode set to 100644
File size: 5.6 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 Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, unsigned int i ) {
42 assert( i < N );
43 return (Timmed &) a.strides[i];
44 }
45
46 static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, long int i ) {
47 assert( i < N );
48 return (Timmed &) a.strides[i];
49 }
50
51 static inline Timmed & ?[?]( arpk(N, S, Timmed, Tbase) & a, unsigned long int i ) {
52 assert( i < N );
53 return (Timmed &) a.strides[i];
54 }
55
56 static inline size_t ?`len( arpk(N, S, Timmed, Tbase) & a ) {
57 return N;
58 }
59
60 // workaround #226 (and array relevance thereof demonstrated in mike102/otype-slow-ndims.cfa)
61 static inline void ?{}( arpk(N, S, Timmed, Tbase) & this ) {
62 void ?{}( S (&inner)[N] ) {}
63 ?{}(this.strides);
64 }
65 static inline void ^?{}( arpk(N, S, Timmed, Tbase) & this ) {
66 void ^?{}( S (&inner)[N] ) {}
67 ^?{}(this.strides);
68 }
69}
70
71//
72// Sugar for declaring array structure instances
73//
74
75forall( Te )
76static inline Te mkar_( tag(Te) ) {}
77
78forall( [N], ZTags ... , Trslt &, Tatom & | { Trslt mkar_( tag(Tatom), ZTags ); } )
79static inline arpk(N, Trslt, Trslt, Tatom) mkar_( tag(Tatom), tag(N), ZTags ) {}
80
81// based on https://stackoverflow.com/questions/1872220/is-it-possible-to-iterate-over-arguments-in-variadic-macros
82
83 // Make a FOREACH macro
84 #define FE_0(WHAT)
85 #define FE_1(WHAT, X) WHAT(X)
86 #define FE_2(WHAT, X, ...) WHAT(X)FE_1(WHAT, __VA_ARGS__)
87 #define FE_3(WHAT, X, ...) WHAT(X)FE_2(WHAT, __VA_ARGS__)
88 #define FE_4(WHAT, X, ...) WHAT(X)FE_3(WHAT, __VA_ARGS__)
89 #define FE_5(WHAT, X, ...) WHAT(X)FE_4(WHAT, __VA_ARGS__)
90 //... repeat as needed
91
92 #define GET_MACRO(_0,_1,_2,_3,_4,_5,NAME,...) NAME
93 #define FOR_EACH(action,...) \
94 GET_MACRO(_0,__VA_ARGS__,FE_5,FE_4,FE_3,FE_2,FE_1,FE_0)(action,__VA_ARGS__)
95
96#define COMMA_ttag(X) , ttag(X)
97#define array( TE, ...) typeof( mkar_( ttag(TE) FOR_EACH( COMMA_ttag, __VA_ARGS__ ) ) )
98
99#define COMMA_ztag(X) , ztag(X)
100#define zarray( TE, ...) typeof( mkar_( ttag(TE) FOR_EACH( COMMA_ztag, __VA_ARGS__ ) ) )
101
102//
103// Sugar for multidimensional indexing
104//
105
106// Core -[[-,-,-]] operator
107
108#ifdef TRY_BROKEN_DESIRED_MD_SUBSCRIPT
109
110// Desired form. One definition with recursion on IxBC (worked until Jan 2021, see trac #__TODO__)
111
112forall( TA &, TB &, TC &, IxAB, IxBC ... | { TB & ?[?]( TA &, IxAB ); TC & ?[?]( TB &, IxBC ); } )
113static inline TC & ?[?]( TA & this, IxAB ab, IxBC bc ) {
114 return this[ab][bc];
115}
116
117#else
118
119// Workaround form. Listing all possibilities up to 4 dims.
120
121forall( TA &, TB &, TC &, IxAB_0, IxBC | { TB & ?[?]( TA &, IxAB_0 ); TC & ?[?]( TB &, IxBC ); } )
122static inline TC & ?[?]( TA & this, IxAB_0 ab, IxBC bc ) {
123 return this[ab][bc];
124}
125
126forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1 ); TC & ?[?]( TB &, IxBC ); } )
127static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxBC bc ) {
128 return this[[ab0,ab1]][bc];
129}
130
131forall( TA &, TB &, TC &, IxAB_0, IxAB_1, IxAB_2, IxBC | { TB & ?[?]( TA &, IxAB_0, IxAB_1, IxAB_2 ); TC & ?[?]( TB &, IxBC ); } )
132static inline TC & ?[?]( TA & this, IxAB_0 ab0, IxAB_1 ab1, IxAB_2 ab2, IxBC bc ) {
133 return this[[ab0,ab1,ab2]][bc];
134}
135
136#endif
137
138//
139// Rotation
140//
141
142// Base
143forall( [Nq], Sq & | sized(Sq), Tbase & )
144static inline tag(arpk(Nq, Sq, Tbase, Tbase)) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(Tbase) ) {
145 tag(arpk(Nq, Sq, Tbase, Tbase)) ret;
146 return ret;
147}
148
149// Rec
150forall( [Nq], Sq & | sized(Sq), [N], S & | sized(S), recq &, recr &, Tbase & | { tag(recr) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(recq) ); } )
151static inline tag(arpk(N, S, recr, Tbase)) enq_( tag(Tbase), tag(Nq), tag(Sq), tag(arpk(N, S, recq, Tbase)) ) {
152 tag(arpk(N, S, recr, Tbase)) ret;
153 return ret;
154}
155
156// Wrapper
157struct all_t {} all;
158forall( [N], S & | sized(S), Te &, result &, Tbase & | { tag(result) enq_( tag(Tbase), tag(N), tag(S), tag(Te) ); } )
159static inline result & ?[?]( arpk(N, S, Te, Tbase) & this, all_t ) {
160 return (result&) this;
161}
162
163//
164// Trait of array or slice
165//
166
167trait ar(A &, Tv &) {
168 Tv& ?[?]( A&, ptrdiff_t );
169 size_t ?`len( A& );
170};
Note: See TracBrowser for help on using the repository browser.