source: tests/array-container/array-md-sbscr-cases.cfa @ b9dae14c

arm-ehjacob/cs343-translation
Last change on this file since b9dae14c was b9dae14c, checked in by Michael Brooks <mlbrooks@…>, 3 months ago

Activated syntax, forall( [N] ).

This implementation desugars in the parser, as forall( ztype(N) ) did in
the preprocessor. Semantic analysis is still required to lock down treating N as a
a traditional type (forbid: N x = 17; vector(N) v;). Deferring that work until
the N--value adapters ( Z(17), z(N) ) are settled.

  • Property mode set to 100644
File size: 8.8 KB
Line 
1#include <containers/array.hfa>
2
3#include <assert.h>
4
5float getMagicNumber( ptrdiff_t w, ptrdiff_t x, ptrdiff_t y, ptrdiff_t z ) {
6
7    assert( 0 <= w && w < 3 );
8    assert( 0 <= x && x < 4 );
9    assert( 0 <= y && y < 5 );
10    assert( 0 <= z && z < 6 );
11
12    float ww = (2.0f \ w) / 1.0f;
13    float xx = (2.0f \ x) / 100.0f;
14    float yy = (2.0f \ y) / 10000.0f;
15    float Nz = (2.0f \ z) / 1000000.0f;
16
17    return ww+xx+yy+Nz;
18}
19
20forall( [Nw], [Nx], [Ny], [Nz] )
21void fillHelloData( array( float, Nw, Nx, Ny, Nz ) & wxyz ) {
22    for (w; z(Nw))
23    for (x; z(Nx))
24    for (y; z(Ny))
25    for (z; z(Nz))
26        wxyz[w][x][y][z] = getMagicNumber(w, x, y, z);
27}
28
29// Tests all the ways to split dimensions into CFA-supported chunks, by the only order that C supports: coarsest to finest stride.
30forall( [Nw], [Nx], [Ny], [Nz] )
31void test_inOrderSplits( tag(Nw), tag(Nx), tag(Ny), tag(Nz) ) {
32
33    array( float, Nw, Nx, Ny, Nz ) wxyz;
34    fillHelloData(wxyz);
35
36    ptrdiff_t iw = 2, ix = 3, iy=4, iz=5;
37
38    const float valExpected = getMagicNumber(iw, ix, iy, iz);
39    assert( wxyz[iw][ix][iy][iz] == valExpected );
40
41    // order wxyz, natural split (4-0 or 0-4, no intermediate to declare)
42
43    assert(( wxyz[[iw, ix, iy, iz]] == valExpected ));
44
45    // order wxyz, unnatural split 1-3  (three ways declared)
46
47    typeof( wxyz[iw] ) xyz1 = wxyz[iw];
48    assert(( xyz1[[ix, iy, iz]]  == valExpected ));
49
50    typeof( wxyz[iw] ) xyz2;
51    &xyz2 = &wxyz[iw];
52    assert(( xyz2[[ix, iy, iz]] == valExpected ));
53
54    assert(( wxyz[iw][[ix, iy, iz]] == valExpected ));
55
56    // order wxyz, unnatural split 2-2  (three ways declared)
57
58    typeof( wxyz[[iw, ix]] ) yz1 = wxyz[[iw,ix]];
59    assert(( yz1[[iy, iz]]  == valExpected ));
60
61    typeof( wxyz[[iw, ix]] ) yz2;
62    &yz2 = &wxyz[[iw, ix]];
63    assert(( yz2[[iy, iz]]  == valExpected ));
64
65    assert(( wxyz[[iw, ix]][[iy, iz]] == valExpected ));
66
67    // order wxyz, unnatural split 3-1  (three ways declared)
68
69    typeof( wxyz[[iw, ix, iy]] ) z1 = wxyz[[iw, ix, iy]];
70    assert(( z1[iz]  == valExpected ));
71
72    typeof( wxyz[[iw, ix, iy]] ) z2;
73    &z2 = &wxyz[[iw, ix, iy]];
74    assert(( z2[iz] == valExpected ));
75
76    assert(( wxyz[[iw, ix, iy]][iz] == valExpected ));
77}
78
79// All orders that skip a single dimension, each in its most natural split.
80forall( [Nw], [Nx], [Ny], [Nz] )
81void test_skipSingle( tag(Nw), tag(Nx), tag(Ny), tag(Nz) ) {
82
83    array( float, Nw, Nx, Ny, Nz ) wxyz;
84    fillHelloData(wxyz);
85
86    ptrdiff_t iw = 2, ix = 3, iy=4, iz=5;
87
88    const float valExpected = getMagicNumber(iw, ix, iy, iz);
89    assert( wxyz[iw][ix][iy][iz] == valExpected );
90
91
92    // order wxyz (no intermediates to declare)
93
94    assert(( wxyz[[iw  , ix  , iy  , iz  ]]       == valExpected ));
95    assert(( wxyz[[iw-1, ix  , iy  , iz  ]]       != valExpected ));
96
97    // order xyzw: *xyz, w
98
99    assert(( wxyz[[all , ix  , iy  , iz  ]][iw  ] == valExpected ));
100    assert(( wxyz[[all , ix-1, iy  , iz  ]][iw  ] != valExpected ));
101    assert(( wxyz[[all , ix  , iy  , iz  ]][iw-1] != valExpected ));
102
103    // order wyzx: w*yz, x
104
105    assert(( wxyz[[iw  , all , iy  , iz  ]][ix  ] == valExpected ));
106    assert(( wxyz[[iw  , all , iy-1, iz  ]][ix  ] != valExpected ));
107    assert(( wxyz[[iw  , all , iy  , iz  ]][ix-1] != valExpected ));
108
109    // order wxzy: wx*z, y
110
111    assert(( wxyz[[iw  , ix  , all , iz  ]][iy  ] == valExpected ));
112    assert(( wxyz[[iw  , ix  , all , iz-1]][iy  ] != valExpected ));
113    assert(( wxyz[[iw  , ix  , all , iz  ]][iy-1] != valExpected ));
114}
115
116
117// The comments specify a covering set of orders, each in its most natural split.
118// Covering means that each edge on the lattice of dimesnions-provided is used.
119// Natural split means the arity of every -[[-,...]] tuple equals the dimensionality of its "this" operand, then that the fewest "all" subscripts are given.
120// The commented-out test code shows cases that don't work.  We wish all the comment-coverd cases worked.
121forall( [Nw], [Nx], [Ny], [Nz] )
122void test_latticeCoverage( tag(Nw), tag(Nx), tag(Ny), tag(Nz) ) {
123
124    array( float, Nw, Nx, Ny, Nz ) wxyz;
125    fillHelloData(wxyz);
126
127    ptrdiff_t iw = 2, ix = 3, iy=4, iz=5;
128
129    const float valExpected = getMagicNumber(iw, ix, iy, iz);
130    assert( wxyz[iw][ix][iy][iz] == valExpected );
131
132
133    // order wxyz (no intermediates to declare)
134
135    assert(( wxyz[[iw, ix, iy, iz]] == valExpected ));
136
137    {
138        // order wyxz: w*y*, xz
139        assert( wxyz[iw][all][iy][all] [ix][iz] == valExpected );
140
141        typeof( wxyz[[iw, all, iy, all]] ) xz1 = wxyz[[iw, all, iy, all]];
142        assert(( xz1[[ix, iz]]  == valExpected ));
143
144        typeof( wxyz[[iw, all, iy, all]] ) xz2;
145        &xz2 = &wxyz[[iw, all, iy, all]];
146        assert(( xz2[[ix, iz]]  == valExpected ));
147
148        assert(( wxyz[[iw  , all, iy  , all]][[ix  , iz  ]] == valExpected ));
149        assert(( wxyz[[iw-1, all, iy  , all]][[ix  , iz  ]] != valExpected ));
150        assert(( wxyz[[iw  , all, iy-1, all]][[ix  , iz  ]] != valExpected ));
151        assert(( wxyz[[iw  , all, iy  , all]][[ix-1, iz  ]] != valExpected ));
152        assert(( wxyz[[iw  , all, iy  , all]][[ix  , iz-1]] != valExpected ));
153    }
154    {
155        // order wzxy: w**z, xy
156        assert( wxyz[iw][all][all][iz] [ix][iy] == valExpected );
157
158        // typeof( wxyz[[iw, all, all, iz]] ) xy1 = wxyz[[iw, all, all, iz]];
159        // assert(( xy1[[ix, iy]]  == valExpected ));
160
161        // typeof(  wxyz[[iw, all, all, iz]] ) xy2;
162        // &xy2 = &wxyz[[iw, all, all, iz]];
163        // assert(( xy2[[ix, iy]]  == valExpected ));
164
165        // assert(( wxyz[[iw  , all, all, iz  ]][[ix  , iy  ]] == valExpected ));
166        // assert(( wxyz[[iw-1, all, all, iz  ]][[ix  , iy  ]] != valExpected ));
167        // assert(( wxyz[[iw  , all, all, iz-1]][[ix  , iy  ]] != valExpected ));
168        // assert(( wxyz[[iw  , all, all, iz  ]][[ix-1, iy  ]] != valExpected ));
169        // assert(( wxyz[[iw  , all, all, iz  ]][[ix  , iy-1]] != valExpected ));
170    }
171    {
172        // order xywz: *xy*, wz
173        assert( wxyz[all][ix][iy][all] [iw][iz] == valExpected );
174
175        typeof( wxyz[[all, ix, iy, all]] ) wz1 = wxyz[[all, ix, iy, all]];
176        assert(( wz1[[iw, iz]]  == valExpected ));
177
178        assert(( wxyz[[all  , ix, iy  , all]][[iw  , iz  ]] == valExpected ));
179    }
180    {
181        // order xzwy: *x*z, wy
182        assert( wxyz[all][ix][all][iz] [iw][iy] == valExpected );
183
184        // assert(( wxyz[[all , ix  , all , iz  ]][[iw  , iy  ]] == valExpected ));
185    }
186    {
187        // order yzwx: **yz, wx
188        assert( wxyz[all][all][iy][iz] [iw][ix] == valExpected );
189
190        // assert(( wxyz[[all , all , iy  , iz  ]][[iw  , ix  ]] == valExpected ));
191    }
192    {
193        // order xwzy: *x**, w*z, y
194        assert( wxyz[all][ix][all][all] [iw][all][iz] [iy] == valExpected );
195
196        typeof( wxyz[all][ix][all][all] ) wyz_workaround = wxyz[[all , ix , all  , all  ]];
197        typeof( wyz_workaround[iw][all][iz] ) y_workaround = wyz_workaround[[iw , all , iz  ]];
198        assert( y_workaround[iy] == valExpected );
199
200        // assert(( wxyz[[all , ix , all  , all  ]][[iw  , all , iz  ]][iy  ] == valExpected ));
201    }
202    {
203        // order ywzx: **y*, w*z, x
204    }
205    {
206        // order zwyx: ***z, w*y, x
207    }
208    {
209        // order yxzw: **y*, *xz, w
210    }
211    {
212        // order zxyw: ***z, *xy, w
213    }
214    {
215        // order zyxw: ***z, **y, *x, w
216    }
217}
218
219forall( [Nw], [Nx], [Ny], [Nz] )
220void test_numSubscrTypeCompatibility( tag(Nw), tag(Nx), tag(Ny), tag(Nz) ) {
221
222    array( float, Nw, Nx, Ny, Nz ) wxyz;
223    fillHelloData(wxyz);
224
225    const float valExpected = getMagicNumber(2, 3, 4, 5);
226    assert(( wxyz [2] [3] [4] [5]  == valExpected ));
227    assert(( wxyz[[2,  3]][4] [5]  == valExpected ));
228    assert(( wxyz [2][[3,  4]][5]  == valExpected ));
229    assert(( wxyz [2] [3][[4,  5]] == valExpected ));
230    assert(( wxyz[[2,  3,  4]][5]  == valExpected ));
231    assert(( wxyz [2][[3,  4,  5]] == valExpected ));
232    assert(( wxyz[[2,  3,  4,  5]] == valExpected ));
233
234    for ( i; z(Nw) ) {
235        assert(( wxyz[[ i, 3, 4, 5 ]] == getMagicNumber(i, 3, 4, 5) ));
236    }
237
238    for ( i; z(Nx) ) {
239        assert(( wxyz[[ 2, i, 4, 5 ]] == getMagicNumber(2, i, 4, 5) ));
240    }
241
242    for ( i; z(Ny) ) {
243        assert(( wxyz[[ 2, 3, i, 5 ]] == getMagicNumber(2, 3, i, 5) ));
244    }
245
246    for ( i; z(Nz) ) {
247        assert(( wxyz[[ 2, 3, 4, i ]] == getMagicNumber(2, 3, 4, i) ));
248    }
249
250    for ( i; z(Nw) ) {
251        assert(( wxyz[[ i, all, 4, 5 ]][3] == getMagicNumber(i, 3, 4, 5) ));
252    }
253
254    for ( i; z(Nw) ) {
255        assert(( wxyz[[ all, 3, 4, 5 ]][i] == getMagicNumber(i, 3, 4, 5) ));
256    }
257}
258
259const size_t  KW = 3,  KX = 4,  KY = 5,  KZ = 6;
260
261int main() {
262
263    test_inOrderSplits  ( ztag(KW), ztag(KX), ztag(KY), ztag(KZ) );
264    test_skipSingle     ( ztag(KW), ztag(KX), ztag(KY), ztag(KZ) );
265    test_latticeCoverage( ztag(KW), ztag(KX), ztag(KY), ztag(KZ) );
266    test_numSubscrTypeCompatibility( ztag(KW), ztag(KX), ztag(KY), ztag(KZ) );
267    printf("done\n");
268}
Note: See TracBrowser for help on using the repository browser.