source: tests/array-container/array-basic.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: 3.6 KB
Line 
1#include <containers/array.hfa>
2
3//
4// Type-theory demo (success is "it compiles")
5//
6
7forall( [Nx], [Ny], [Nz] )
8void typesTest( tag(Nx), tag(Ny), tag(Nz) ) {
9
10    array( float, Nx, Ny, Nz )  xyz;
11
12    // numeric subscripts
13    ptrdiff_t ix = 1, iy = 2, iz = 3;
14    array( float, Ny, Nz ) & yz = xyz[ix];
15    array( float, Nz ) & z = xyz[ix][iy];
16    float & val = xyz[ix][iy][iz];
17
18    // deferral subscript
19    typeof( xyz[all] ) yzx = xyz[all];
20
21    // longform check that -[all] gives intended type (user doesn't write this)
22    arpk( Ny
23        , array( float, Nz)
24        , arpk( Nz
25              , float
26              , arpk( Nx
27                    , array( float, Ny, Nz )
28                    , float
29                    , float
30                    )
31              , float
32              )
33        , float
34        )
35        & yzx_long = xyz[all];
36    & yzx_long = & yzx;
37    & yzx  = & yzx_long;
38}
39
40//
41// Runtime demo
42//
43
44#include <assert.h>
45
46float getMagicNumber( ptrdiff_t w, ptrdiff_t x, ptrdiff_t y, ptrdiff_t z ) {
47
48    assert( 0 <= w && w < 3 );
49    assert( 0 <= x && x < 4 );
50    assert( 0 <= y && y < 5 );
51    assert( 0 <= z && z < 6 );
52
53    float ww = (2.0f \ w) / 1.0f;
54    float xx = (2.0f \ x) / 100.0f;
55    float yy = (2.0f \ y) / 10000.0f;
56    float Nz = (2.0f \ z) / 1000000.0f;
57
58    return ww+xx+yy+Nz;
59}
60
61forall( [Nw], [Nx], [Ny], [Nz] )
62void fillHelloData( array( float, Nw, Nx, Ny, Nz ) & wxyz ) {
63    for (w; z(Nw))
64    for (x; z(Nx))
65    for (y; z(Ny))
66    for (z; z(Nz))
67        wxyz[w][x][y][z] = getMagicNumber(w, x, y, z);
68}
69
70forall( [Zn]
71      , S & | sized(S)
72      )
73float total1d_low( arpk(Zn, S, float, float ) & a ) {
74    float total = 0.0f;
75    for (i; z(Zn))
76        total += a[i];
77    return total;
78}
79
80forall( A & | ar(A, float) )
81float total1d_hi( A & a ) {
82    float total = 0.0f;
83    for (i; a`len)
84        total += a[i];
85    return total;
86}
87
88forall( [Nw], [Nx], [Ny], [Nz] )
89void runtimeTest( tag(Nw), tag(Nx), tag(Ny), tag(Nz) ) {
90
91    array( float, Nw, Nx, Ny, Nz ) wxyz;
92    fillHelloData(wxyz);
93
94    float expect, result;
95    ptrdiff_t slice_ix = 1;
96
97    // summing across W, with x=y=z=1
98
99    expect = 0;
100    for (i; z(Nw))
101        expect += getMagicNumber( i, slice_ix, slice_ix, slice_ix );
102    printf("expect Ws             = %f\n", expect);
103
104    result = total1d_low( wxyz[all][slice_ix][slice_ix][slice_ix] );
105    printf("result Ws [][][][] lo = %f\n", result);
106
107    result = total1d_low( wxyz[[all, slice_ix, slice_ix, slice_ix]] );
108    printf("result Ws [,,,]    lo = %f\n", result);
109
110    result = total1d_hi( wxyz[all][slice_ix][slice_ix][slice_ix] );
111    printf("result Ws [][][][] hi = %f\n", result);
112
113    result = total1d_hi( wxyz[[all, slice_ix, slice_ix, slice_ix]] );
114    printf("result Ws [,,,]    hi = %f\n", result);
115
116    // summing across X, with w=y=z=1
117
118    expect = 0;
119    for (i; z(Nx))
120        expect += getMagicNumber( slice_ix, i, slice_ix, slice_ix );
121    printf("expect Xs             = %f\n", expect);
122
123    result = total1d_low( wxyz[slice_ix][all][slice_ix][slice_ix] );   
124    printf("result Xs [][][][] lo = %f\n", result);
125
126    result = total1d_low( wxyz[[slice_ix, all, slice_ix, slice_ix]] );
127    printf("result Xs [,,,]    lo = %f\n", result);
128
129    result = total1d_hi( wxyz[slice_ix][all][slice_ix][slice_ix] );   
130    printf("result Xs [][][][] hi = %f\n", result);
131
132    result = total1d_hi( wxyz[[slice_ix, all, slice_ix, slice_ix]] );
133    printf("result Xs [,,,]    hi = %f\n", result);
134
135}
136
137const size_t  KW = 3,  KX = 4,  KY = 5,  KZ = 6;
138
139int main() {
140
141    typesTest  (           ztag(KX), ztag(KY), ztag(KZ) );
142    runtimeTest( ztag(KW), ztag(KX), ztag(KY), ztag(KZ) );
143}
Note: See TracBrowser for help on using the repository browser.