[86bd7c1f] | 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.c -- |
---|
| 8 | // |
---|
[097e2b0] | 9 | // Author : Peter A. Buhr |
---|
[86bd7c1f] | 10 | // Created On : Wed May 27 17:56:53 2015 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[cf16f94] | 12 | // Last Modified On : Sat Nov 21 18:08:18 2015 |
---|
| 13 | // Update Count : 119 |
---|
[86bd7c1f] | 14 | // |
---|
| 15 | |
---|
[a0d9f94] | 16 | extern "C" { |
---|
[097e2b0] | 17 | int printf( char *, ... ); |
---|
[a0d9f94] | 18 | } |
---|
[097e2b0] | 19 | #include "fstream.h" |
---|
[a0d9f94] | 20 | |
---|
| 21 | context sumable( type T ) { |
---|
[86bd7c1f] | 22 | const T 0; |
---|
| 23 | T ?+?( T, T ); |
---|
| 24 | T ?+=?( T *, T ); |
---|
[097e2b0] | 25 | T ++?( T * ); |
---|
| 26 | T ?++( T * ); |
---|
[a0d9f94] | 27 | }; |
---|
| 28 | |
---|
| 29 | forall( type T | sumable( T ) ) |
---|
[097e2b0] | 30 | T sum( unsigned int n, T a[] ) { |
---|
| 31 | T total = 0; // instantiate T, select 0 |
---|
| 32 | for ( unsigned int i = 0; i < n; i += 1 ) |
---|
| 33 | total += a[i]; // select + |
---|
[86bd7c1f] | 34 | return total; |
---|
[a0d9f94] | 35 | } |
---|
| 36 | |
---|
[bdd516a] | 37 | // Required to satisfy sumable as char does not have addition. |
---|
| 38 | const char 0; |
---|
[097e2b0] | 39 | char ?+?( char op1, char op2 ) { return (int)op1 + op2; } // cast forces integer addition or recursion |
---|
| 40 | char ++?( char *op ) { *op += 1; return *op; } |
---|
| 41 | char ?++( char *op ) { char temp = *op; *op += 1; return temp; } |
---|
[bdd516a] | 42 | |
---|
[a0d9f94] | 43 | int main() { |
---|
[6e7e2b36] | 44 | const int low = 5, High = 15, size = High - low; |
---|
[097e2b0] | 45 | |
---|
| 46 | ofstream *sout = ofstream_stdout(); |
---|
| 47 | |
---|
| 48 | char s = 0, a[size]; |
---|
| 49 | char v = low; |
---|
[6e7e2b36] | 50 | for ( int i = 0; i < size; i += 1, v += 1 ) { |
---|
[097e2b0] | 51 | s += v; |
---|
| 52 | a[i] = v; |
---|
[86bd7c1f] | 53 | } |
---|
[097e2b0] | 54 | sout << "sum from " << low << " to " << High << " is " |
---|
[cf16f94] | 55 | << (int)sum( size, a ) << ", check " << (int)s << endl; |
---|
[86bd7c1f] | 56 | |
---|
[097e2b0] | 57 | int s = 0, a[size]; |
---|
| 58 | int v = low; |
---|
| 59 | for ( int i = 0; i < size; i += 1, v += 1 ) { |
---|
| 60 | s += (int)v; |
---|
| 61 | a[i] = (int)v; |
---|
| 62 | } |
---|
| 63 | sout << "sum from " << low << " to " << High << " is " |
---|
[cf16f94] | 64 | << sum( size, (int *)a ) << ", check " << (int)s << endl; |
---|
[86bd7c1f] | 65 | |
---|
[097e2b0] | 66 | double s = 0.0, a[size]; |
---|
[6e7e2b36] | 67 | double v = low / 10.0; |
---|
| 68 | for ( int i = 0; i < size; i += 1, v += 0.1 ) { |
---|
[097e2b0] | 69 | s += (double)v; |
---|
| 70 | a[i] = (double)v; |
---|
| 71 | } |
---|
| 72 | printf( "%g\n", sum( size, (double *)a ) ); |
---|
| 73 | // sout << "sum from " << low / 10.0 << " to " << High / 10.0 << " is " |
---|
[cf16f94] | 74 | // << sum( size, (double *)a ) << ", check " << (double)s << endl; |
---|
[097e2b0] | 75 | |
---|
| 76 | float s = 0.0, a[size]; |
---|
| 77 | float v = low / 10.0; |
---|
| 78 | for ( int i = 0; i < size; i += 1, v += 0.1f ) { |
---|
| 79 | s += (float)v; |
---|
| 80 | a[i] = (float)v; |
---|
[86bd7c1f] | 81 | } |
---|
[097e2b0] | 82 | printf( "%g\n", sum( size, (float *)a ) ); |
---|
| 83 | // sout << "sum from " << low / 10.0 << " to " << High / 10.0 << " is " |
---|
[cf16f94] | 84 | // << sum( size, (float *)a ) << ", check " << (float)s << endl; |
---|
[a0d9f94] | 85 | } |
---|
[42dcae7] | 86 | |
---|
| 87 | // Local Variables: // |
---|
[86bd7c1f] | 88 | // tab-width: 4 // |
---|
[097e2b0] | 89 | // compile-command: "cfa sum.c fstream.o iostream.o" // |
---|
[42dcae7] | 90 | // End: // |
---|