source: src/examples/sum.c @ c44e622

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since c44e622 was bd85400, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

remove offsetof keyword, parser 0/1 names as structure fields, update examples to new stdlib, rename algorithms to stdlib, extend stdlib, use correct type for box parameters

  • Property mode set to 100644
File size: 2.6 KB
RevLine 
[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
[bd85400]12// Last Modified On : Fri Feb  5 16:47:44 2016
13// Update Count     : 139
[86bd7c1f]14//
15
[d3b7937]16#include <fstream>
[a0d9f94]17
18context sumable( type T ) {
[86bd7c1f]19        const T 0;
20        T ?+?( T, T );
21        T ?+=?( T *, T );
[097e2b0]22        T ++?( T * );
23        T ?++( T * );
[d3b7937]24}; // sumable
[a0d9f94]25
26forall( type T | sumable( T ) )
[097e2b0]27T sum( unsigned int n, T a[] ) {
28        T total = 0;                                                                            // instantiate T, select 0
29        for ( unsigned int i = 0; i < n; i += 1 )
30                total += a[i];                                                                  // select +
[86bd7c1f]31        return total;
[d3b7937]32} // sum
[a0d9f94]33
[bdd516a]34// Required to satisfy sumable as char does not have addition.
[bd85400]35// const char 0;
36// char ?+?( char op1, char op2 ) { return (int)op1 + op2; } // cast forces integer addition or recursion
37// char ++?( char *op ) { *op += 1; return *op; }
38// char ?++( char *op ) { char temp = *op; *op += 1; return temp; }
[bdd516a]39
[784deab]40int main( void ) {
[6e7e2b36]41        const int low = 5, High = 15, size = High - low;
[097e2b0]42        ofstream *sout = ofstream_stdout();
[bd85400]43#if 0
[097e2b0]44
45        char s = 0, a[size];
46        char v = low;
[6e7e2b36]47        for ( int i = 0; i < size; i += 1, v += 1 ) {
[097e2b0]48                s += v;
49                a[i] = v;
[86bd7c1f]50        }
[784deab]51        sout | "sum from " | low | " to " | High | " is "
52                 | (int)sum( size, a ) | ", check " | (int)s | endl;
[86bd7c1f]53
[097e2b0]54        int s = 0, a[size];
55        int v = low;
56        for ( int i = 0; i < size; i += 1, v += 1 ) {
57                s += (int)v;
58                a[i] = (int)v;
59        }
[784deab]60        sout | "sum from " | low | " to " | High | " is "
61                 | sum( size, (int *)a ) | ", check " | (int)s | endl;
[86bd7c1f]62
[097e2b0]63        float s = 0.0, a[size];
64        float v = low / 10.0;
65        for ( int i = 0; i < size; i += 1, v += 0.1f ) {
66                s += (float)v;
67                a[i] = (float)v;
[86bd7c1f]68        }
[784deab]69        sout | "sum from " | low / 10.0 | " to " | High / 10.0 | " is "
70                 | sum( size, (float *)a ) | ", check " | (float)s | endl;
[bd85400]71#endif
72        double s = 0, a[size];
[d3b7937]73        double v = low / 10.0;
[bd85400]74
[d3b7937]75        for ( int i = 0; i < size; i += 1, v += 0.1 ) {
76                s += (double)v;
77                a[i] = (double)v;
78        }
79        sout | "sum from " | low / 10.0 | " to " | High / 10.0 | " is "
80                 | sum( size, (double *)a ) | ", check " | (double)s | endl;
[bd85400]81
82        // struct S { int i, j; } sarr[size];
83        // struct S 0 = { 0, 0 };
84        // struct S 1 = { 1, 1 };
85        // S ?+?( S t1, S t2 ) { S s = { t1.i + t1.j, t2.i + t2.j }; return s; }
86        // S ?+=?( S *t1, S t2 ) { *t1 = *t1 + t2; return *t1; }
87        // S ++?( S *t ) { *t += 1; return *t; }
88        // S ?++( S *t ) { S temp = *t; *t += 1; return temp; }
89        // sum( size, sarr );
[d3b7937]90} // main
[42dcae7]91
92// Local Variables: //
[86bd7c1f]93// tab-width: 4 //
[d3b7937]94// compile-command: "cfa sum.c" //
[42dcae7]95// End: //
Note: See TracBrowser for help on using the repository browser.