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