source: tests/sum.cfa @ 884f1409

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 884f1409 was 8ddedf0, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

remove unnecessary cast

  • Property mode set to 100644
File size: 6.8 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// sum.cfa -- test resolvers ability to deal with many variables with the same name and to use the minimum number of
8//    casts necessary to disambiguate overloaded variable names.
9//
10// Author           : Peter A. Buhr
11// Created On       : Wed May 27 17:56:53 2015
12// Last Modified By : Peter A. Buhr
13// Last Modified On : Tue Jul 16 09:51:37 2019
14// Update Count     : 336
15//
16
17#include <fstream.hfa>
18#include <stdlib.hfa>
19
20trait sumable( otype T ) {
21        void ?{}( T &, zero_t );                                                        // 0 literal constructor
22        T ?+?( T, T );                                                                          // assortment of additions
23        T ?+=?( T &, T );
24        T ++?( T & );
25        T ?++( T & );
26}; // sumable
27
28forall( otype T | sumable( T ) )                                                // use trait
29T sum( size_t size, T a[] ) {
30        T total = 0;                                                                            // initialize by 0 constructor
31        for ( i; size )
32                total += a[i];                                                                  // select appropriate +
33        return total;
34} // sum
35
36int main( void ) {
37#if 0
38        const int low = 5, High = 15, size = High - low;
39
40        signed char s = 0, a[size], v = (char)low;
41        for ( int i = 0; i < size; i += 1, v += 1hh ) {
42                s += v;
43                a[i] = v;
44        } // for
45        sout | "sum from" | low | "to" | High | "is"
46                 | sum( size, (signed char *)a ) | ", check" | (signed char)s;
47
48        unsigned char s = 0, a[size], v = low;
49        for ( int i = 0; i < size; i += 1, v += 1hhu ) {
50                s += (unsigned char)v;
51                a[i] = (unsigned char)v;
52        } // for
53        sout | "sum from" | low | "to" | High | "is"
54                 | sum( size, (unsigned char *)a ) | ", check" | (unsigned char)s;
55
56        short int s = 0, a[size], v = low;
57        for ( int i = 0; i < size; i += 1, v += 1h ) {
58                s += (short int)v;
59                a[i] = (short int)v;
60        } // for
61        sout | "sum from" | low | "to" | High | "is"
62                 | sum( size, (short int *)a ) | ", check" | (short int)s;
63
64        int s = 0, a[size], v = low;
65        for ( int i = 0; i < size; i += 1, v += 1 ) {
66                s += (int)v;
67                a[i] = (int)v;
68        } // for
69        sout | "sum from" | low | "to" | High | "is"
70                 | sum( size, (int *)a ) | ", check" | (int)s;
71
72        float s = 0.0f, a[size], v = low / 10.0f;
73        for ( int i = 0; i < size; i += 1, v += 0.1f ) {
74                s += (float)v;
75                a[i] = (float)v;
76        } // for
77        sout | "sum from" | low / 10.0f | "to" | High / 10.0f | "is"
78                 | sum( size, (float *)a ) | ", check" | (float)s;
79
80        double s = 0.0, a[size], v = low / 10.0;
81        for ( int i = 0; i < size; i += 1, v += 0.1 ) {
82                s += (double)v;
83                a[i] = (double)v;
84        } // for
85        sout | "sum from" | low / 10.0 | "to" | High / 10.0 | "is"
86                 | sum( size, (double *)a ) | ", check" | (double)s;
87
88        struct S { int i, j; };
89        void ?{}( S & s ) { s.[i, j] = 0; }
90        void ?{}( S & s, int i ) { s.[i, j] = [i, 0]; }
91        void ?{}( S & s, int i, int j ) { s.[i, j] = [i, j]; }
92        void ?{}( S & s, zero_t ) { s.[i, j] = 0; }
93        void ?{}( S & s, one_t ) { s.[i, j] = 1; }
94        S ?+?( S t1, S t2 ) { return (S){ t1.i + t2.i, t1.j + t2.j }; }
95        S ?+=?( S & t1, S t2 ) { t1 = t1 + t2; return t1; }
96        S ++?( S & t ) { t += (S){1}; return t; }
97        S ?++( S & t ) { S temp = t; t += (S){1}; return temp; }
98        ofstream & ?|?( ofstream & os, S v ) { return os | v.i | v.j; }
99        void ?|?( ofstream & os, S v ) { (ofstream &)(os | v); ends( os ); }
100
101        S s = (S){0}, a[size], v = { low, low };
102        for ( int i = 0; i < size; i += 1, v += (S){1} ) {
103                s += (S)v;
104                a[i] = (S)v;
105        } // for
106        sout | "sum from" | low | "to" | High | "is"
107                 | sum( size, (S *)a ) | ", check" | (S)s;
108
109        forall( otype Impl | sumable( Impl ) )
110        struct GS {
111                Impl * x, * y;
112        };
113        GS(int) gs;
114        // FIX ME, resolution problem with anew not picking up the LH type
115        gs.x = (typeof(gs.x))anew( size );                                      // create array storage for field
116        s = 0; v = low;
117        for ( int i = 0; i < size; i += 1, v += 1 ) {
118                s += (int)v;
119                gs.x[i] = (int)v;                                                               // set field array in generic type
120        } // for
121        sout | "sum from" | low | "to" | High | "is"
122                 | sum( size, gs.x ) | ", check" | (int)s;              // add field array in generic type
123        delete( gs.x );
124#else
125        const int low = 5, High = 15, size = High - low;
126
127        signed char s = 0, a[size], v = (char)low;
128        for ( int i = 0; i < size; i += 1, v += 1hh ) {
129                s += v;
130                a[i] = v;
131        } // for
132        printf( "sum from %d to %d is %hhd, check %hhd\n", low, High,
133                 sum( size, (signed char *)a ), (signed char)s );
134
135        unsigned char s = 0, a[size], v = low;
136        for ( int i = 0; i < size; i += 1, v += 1hhu ) {
137                s += (unsigned char)v;
138                a[i] = (unsigned char)v;
139        } // for
140        printf( "sum from %d to %d is %hhu, check %hhu\n", low, High,
141                 sum( size, (unsigned char *)a ), (unsigned char)s );
142
143        short int s = 0, a[size], v = low;
144        for ( int i = 0; i < size; i += 1, v += 1h ) {
145                s += (short int)v;
146                a[i] = (short int)v;
147        } // for
148        printf( "sum from %d to %d is %hd, check %hd\n", low, High,
149                 sum( size, (short int *)a ), (short int)s );
150
151        int s = 0, a[size], v = low;
152        for ( int i = 0; i < size; i += 1, v += 1 ) {
153                s += (int)v;
154                a[i] = (int)v;
155        } // for
156        printf( "sum from %d to %d is %d, check %d\n", low, High,
157                 sum( size, (int *)a ), (int)s );
158
159        float s = 0.0f, a[size], v = low / 10.0f;
160        for ( int i = 0; i < size; i += 1, v += 0.1f ) {
161                s += (float)v;
162                a[i] = (float)v;
163        } // for
164        printf( "sum from %g to %g is %g, check %g\n", low / 10.0f, High / 10.0f,
165                 sum( size, (float *)a ), (float)s );
166
167        double s = 0.0, a[size], v = low / 10.0;
168        for ( int i = 0; i < size; i += 1, v += 0.1 ) {
169                s += (double)v;
170                a[i] = (double)v;
171        } // for
172        printf( "sum from %g to %g is %g, check %g\n", low / 10.0f, High / 10.0f,
173                 sum( size, (double *)a ), (double)s );
174
175        struct S { int i, j; };
176        void ?{}( S & s ) { s.[i, j] = 0; }
177        void ?{}( S & s, int i ) { s.[i, j] = [i, 0]; }
178        void ?{}( S & s, int i, int j ) { s.[i, j] = [i, j]; }
179        void ?{}( S & s, zero_t ) { s.[i, j] = 0; }
180        void ?{}( S & s, one_t ) { s.[i, j] = 1; }
181        S ?+?( S t1, S t2 ) { return (S){ t1.i + t2.i, t1.j + t2.j }; }
182        S ?+=?( S & t1, S t2 ) { t1 = t1 + t2; return t1; }
183        S ++?( S & t ) { t += (S){1}; return t; }
184        S ?++( S & t ) { S temp = t; t += (S){1}; return temp; }
185        ofstream & ?|?( ofstream & os, S v ) { return os | v.i | v.j; }
186        void ?|?( ofstream & os, S v ) { (ofstream &)(os | v); ends( os ); }
187
188        S s = 0, a[size], v = { low, low };
189        for ( int i = 0; i < size; i += 1, v += (S){1} ) {
190                s += (S)v;
191                a[i] = (S)v;
192        } // for
193        printf( "sum from %d to %d is %d %d, check %d %d\n", low, High,
194                 sum( size, (S *)a ).[i, j], s.[i, j] );
195
196        forall( otype Impl | sumable( Impl ) )
197        struct GS {
198                Impl * x, * y;
199        };
200        GS(int) gs;
201        // FIX ME, resolution problem with anew not picking up the LH type
202        gs.x = (typeof(gs.x))anew( size );                                      // create array storage for field
203        s = 0; v = low;
204        for ( int i = 0; i < size; i += 1, v += 1 ) {
205                s += (int)v;
206                gs.x[i] = (int)v;                                                               // set field array in generic type
207        } // for
208        printf( "sum from %d to %d is %d, check %d\n", low, High,
209                 sum( size, gs.x ), (int)s );           // add field array in generic type
210        delete( gs.x );
211#endif
212} // main
213
214// Local Variables: //
215// tab-width: 4 //
216// compile-command: "cfa sum.cfa" //
217// End: //
Note: See TracBrowser for help on using the repository browser.