[af0c8da] | 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 | // |
---|
[dc8511c] | 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. |
---|
[af0c8da] | 9 | // |
---|
| 10 | // Author : Peter A. Buhr |
---|
| 11 | // Created On : Wed May 27 17:56:53 2015 |
---|
| 12 | // Last Modified By : Peter A. Buhr |
---|
[7780bd2] | 13 | // Last Modified On : Thu Aug 5 21:27:25 2021 |
---|
| 14 | // Update Count : 346 |
---|
[af0c8da] | 15 | // |
---|
| 16 | |
---|
[73abe95] | 17 | #include <fstream.hfa> |
---|
| 18 | #include <stdlib.hfa> |
---|
[af0c8da] | 19 | |
---|
[fd54fef] | 20 | trait sumable( T ) { |
---|
[7d69095] | 21 | void ?{}( T &, zero_t ); // 0 literal constructor |
---|
[7780bd2] | 22 | void ?{}( T &, one_t ); // 1 literal constructor |
---|
[af0c8da] | 23 | T ?+?( T, T ); // assortment of additions |
---|
[7780bd2] | 24 | T ?+=?( T &, T ); // get pre/post ++ with += and one_t |
---|
[af0c8da] | 25 | }; // sumable |
---|
| 26 | |
---|
[7780bd2] | 27 | forall( T | sumable( T ) ) // use trait |
---|
[7d69095] | 28 | T sum( size_t size, T a[] ) { |
---|
| 29 | T total = 0; // initialize by 0 constructor |
---|
[292642a] | 30 | for ( i; size ) |
---|
[2b9ddf2] | 31 | total += a[i]; // select appropriate + |
---|
[af0c8da] | 32 | return total; |
---|
| 33 | } // sum |
---|
| 34 | |
---|
| 35 | int main( void ) { |
---|
| 36 | const int low = 5, High = 15, size = High - low; |
---|
| 37 | |
---|
[ee06e41b] | 38 | signed char s = 0, a[size], v = (char)low; |
---|
| 39 | for ( int i = 0; i < size; i += 1, v += 1hh ) { |
---|
[af0c8da] | 40 | s += v; |
---|
| 41 | a[i] = v; |
---|
| 42 | } // for |
---|
| 43 | sout | "sum from" | low | "to" | High | "is" |
---|
[43a5603] | 44 | | sum( size, (signed char *)a ) | ", check" | (signed char)s; |
---|
[ee06e41b] | 45 | |
---|
| 46 | unsigned char s = 0, a[size], v = low; |
---|
| 47 | for ( int i = 0; i < size; i += 1, v += 1hhu ) { |
---|
| 48 | s += (unsigned char)v; |
---|
| 49 | a[i] = (unsigned char)v; |
---|
| 50 | } // for |
---|
| 51 | sout | "sum from" | low | "to" | High | "is" |
---|
| 52 | | sum( size, (unsigned char *)a ) | ", check" | (unsigned char)s; |
---|
| 53 | |
---|
| 54 | short int s = 0, a[size], v = low; |
---|
| 55 | for ( int i = 0; i < size; i += 1, v += 1h ) { |
---|
| 56 | s += (short int)v; |
---|
| 57 | a[i] = (short int)v; |
---|
| 58 | } // for |
---|
| 59 | sout | "sum from" | low | "to" | High | "is" |
---|
| 60 | | sum( size, (short int *)a ) | ", check" | (short int)s; |
---|
[af0c8da] | 61 | |
---|
| 62 | int s = 0, a[size], v = low; |
---|
| 63 | for ( int i = 0; i < size; i += 1, v += 1 ) { |
---|
| 64 | s += (int)v; |
---|
| 65 | a[i] = (int)v; |
---|
| 66 | } // for |
---|
| 67 | sout | "sum from" | low | "to" | High | "is" |
---|
[200fcb3] | 68 | | sum( size, (int *)a ) | ", check" | (int)s; |
---|
[af0c8da] | 69 | |
---|
| 70 | float s = 0.0f, a[size], v = low / 10.0f; |
---|
| 71 | for ( int i = 0; i < size; i += 1, v += 0.1f ) { |
---|
| 72 | s += (float)v; |
---|
| 73 | a[i] = (float)v; |
---|
| 74 | } // for |
---|
| 75 | sout | "sum from" | low / 10.0f | "to" | High / 10.0f | "is" |
---|
[200fcb3] | 76 | | sum( size, (float *)a ) | ", check" | (float)s; |
---|
[af0c8da] | 77 | |
---|
| 78 | double s = 0.0, a[size], v = low / 10.0; |
---|
| 79 | for ( int i = 0; i < size; i += 1, v += 0.1 ) { |
---|
| 80 | s += (double)v; |
---|
| 81 | a[i] = (double)v; |
---|
| 82 | } // for |
---|
| 83 | sout | "sum from" | low / 10.0 | "to" | High / 10.0 | "is" |
---|
[200fcb3] | 84 | | sum( size, (double *)a ) | ", check" | (double)s; |
---|
[af0c8da] | 85 | |
---|
| 86 | struct S { int i, j; }; |
---|
| 87 | void ?{}( S & s ) { s.[i, j] = 0; } |
---|
[c0b4db0] | 88 | void ?{}( S & s, int i ) { s.[i, j] = [i, 0]; } |
---|
| 89 | void ?{}( S & s, int i, int j ) { s.[i, j] = [i, j]; } |
---|
| 90 | void ?{}( S & s, zero_t ) { s.[i, j] = 0; } |
---|
| 91 | void ?{}( S & s, one_t ) { s.[i, j] = 1; } |
---|
[af0c8da] | 92 | S ?+?( S t1, S t2 ) { return (S){ t1.i + t2.i, t1.j + t2.j }; } |
---|
| 93 | S ?+=?( S & t1, S t2 ) { t1 = t1 + t2; return t1; } |
---|
[09687aa] | 94 | ofstream & ?|?( ofstream & os, S v ) { return os | v.i | v.j; } |
---|
[65240bb] | 95 | void ?|?( ofstream & os, S v ) { (ofstream &)(os | v); ends( os ); } |
---|
[af0c8da] | 96 | |
---|
[7780bd2] | 97 | S s = 0, a[size], v = { low, low }; |
---|
[af0c8da] | 98 | for ( int i = 0; i < size; i += 1, v += (S){1} ) { |
---|
| 99 | s += (S)v; |
---|
| 100 | a[i] = (S)v; |
---|
| 101 | } // for |
---|
| 102 | sout | "sum from" | low | "to" | High | "is" |
---|
[200fcb3] | 103 | | sum( size, (S *)a ) | ", check" | (S)s; |
---|
[2b9ddf2] | 104 | |
---|
[fd54fef] | 105 | forall( Impl | sumable( Impl ) ) |
---|
[2b9ddf2] | 106 | struct GS { |
---|
| 107 | Impl * x, * y; |
---|
| 108 | }; |
---|
| 109 | GS(int) gs; |
---|
[b326277] | 110 | // FIX ME, resolution problem with anew not picking up the LH type |
---|
| 111 | gs.x = (typeof(gs.x))anew( size ); // create array storage for field |
---|
[2b9ddf2] | 112 | s = 0; v = low; |
---|
| 113 | for ( int i = 0; i < size; i += 1, v += 1 ) { |
---|
| 114 | s += (int)v; |
---|
[7d69095] | 115 | gs.x[i] = (int)v; // set field array in generic type |
---|
[2b9ddf2] | 116 | } // for |
---|
| 117 | sout | "sum from" | low | "to" | High | "is" |
---|
[200fcb3] | 118 | | sum( size, gs.x ) | ", check" | (int)s; // add field array in generic type |
---|
[b3763ca] | 119 | delete( gs.x ); |
---|
[af0c8da] | 120 | } // main |
---|
| 121 | |
---|
| 122 | // Local Variables: // |
---|
| 123 | // tab-width: 4 // |
---|
[dc8511c] | 124 | // compile-command: "cfa sum.cfa" // |
---|
[af0c8da] | 125 | // End: // |
---|