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

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

Improved support for new arrays subscripting by tuples, --,-,-?.

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