[aabc60c] | 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 | // polymorphism.c -- |
---|
| 8 | // |
---|
| 9 | // Author : Rob Schluntz |
---|
| 10 | // Created On : Tue Oct 17 12:19:48 2017 |
---|
[f498c51] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Tue Dec 25 14:40:24 2018 |
---|
| 13 | // Update Count : 3 |
---|
[aabc60c] | 14 | // |
---|
| 15 | |
---|
[3787dc1] | 16 | #include <assert.h> |
---|
| 17 | #include <inttypes.h> |
---|
[f498c51] | 18 | #include <fstream.hfa> |
---|
[3787dc1] | 19 | |
---|
[fd54fef] | 20 | forall(T) |
---|
[aabc60c] | 21 | T f(T x, T y) { |
---|
[598f50e] | 22 | x = y; |
---|
| 23 | return x; |
---|
| 24 | } |
---|
| 25 | |
---|
[fd54fef] | 26 | forall(T) T ident(T x) { |
---|
[598f50e] | 27 | return x; |
---|
[aabc60c] | 28 | } |
---|
| 29 | |
---|
[fd54fef] | 30 | forall( T, U ) |
---|
[3787dc1] | 31 | size_t struct_size( T i, U j ) { |
---|
| 32 | struct S { T i; U j; }; |
---|
| 33 | return sizeof(S); |
---|
| 34 | } |
---|
| 35 | |
---|
[fd54fef] | 36 | forall( T, U ) |
---|
[3787dc1] | 37 | size_t union_size( T i, U j ) { |
---|
| 38 | union B { T i; U j; }; |
---|
| 39 | return sizeof(B); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | // perform some simple operations on aggregates of T and U |
---|
[fd54fef] | 43 | forall( T | { void print(T); int ?==?(T, T); }, U | { void print(U); U ?=?(U&, zero_t); } ) |
---|
[3787dc1] | 44 | U foo(T i, U j) { |
---|
| 45 | struct S { T i; U j; }; |
---|
| 46 | union B { T i; U j; }; |
---|
| 47 | |
---|
| 48 | S s; |
---|
| 49 | s.i = i; |
---|
[a7caf3d] | 50 | assertf(s.i == i, "struct operation fails in polymorphic context."); |
---|
[3787dc1] | 51 | |
---|
| 52 | B b; |
---|
| 53 | b.j = 0; |
---|
| 54 | b.i = s.i; |
---|
| 55 | return b.j; |
---|
| 56 | } |
---|
| 57 | |
---|
[aabc60c] | 58 | int main() { |
---|
[3787dc1] | 59 | { |
---|
| 60 | // ensure that x is not changed by the invocation of a polymorphic function |
---|
| 61 | int x = 123; |
---|
| 62 | int y = 456; |
---|
| 63 | int z = f(x, y); |
---|
[f498c51] | 64 | sout | x | y | z; |
---|
[3787dc1] | 65 | } |
---|
| 66 | { |
---|
| 67 | // explicitly specialize function |
---|
| 68 | int (*f)(int) = ident; |
---|
| 69 | ((int(*)(int))ident); |
---|
[f498c51] | 70 | sout | f(5) | ((int(*)(int))ident)(5); |
---|
[3787dc1] | 71 | } |
---|
| 72 | { |
---|
| 73 | // test aggregates with polymorphic members |
---|
[a7caf3d] | 74 | typedef __attribute__((aligned(8))) uint32_t x_type; |
---|
| 75 | typedef __attribute__((aligned(8))) uint64_t y_type; |
---|
[3787dc1] | 76 | |
---|
| 77 | x_type x = 3; |
---|
| 78 | y_type y = 3; |
---|
| 79 | |
---|
| 80 | struct S { |
---|
| 81 | x_type f1; |
---|
| 82 | y_type f2; |
---|
| 83 | }; |
---|
| 84 | union U { |
---|
| 85 | x_type f1; |
---|
| 86 | y_type f2; |
---|
| 87 | }; |
---|
| 88 | // ensure that the size of aggregates with polymorphic members |
---|
| 89 | // matches the size of the aggregates in a monomorphic context |
---|
[1f370451] | 90 | size_t ssz = struct_size(x, y); |
---|
| 91 | size_t usz = union_size(x, y); |
---|
| 92 | assertf( ssz == sizeof(S), "struct size differs in polymorphic context: %zd / %zd", ssz, sizeof(S)); |
---|
| 93 | assertf( usz == sizeof(U), "union size differs in polymorphic context: %zd / %zd", usz, sizeof(U)); |
---|
[3787dc1] | 94 | |
---|
| 95 | y_type ?=?(y_type & this, zero_t) { |
---|
| 96 | this = (int)0; |
---|
| 97 | return this; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void print(x_type x) { |
---|
[f498c51] | 101 | sout | x; |
---|
[3787dc1] | 102 | } |
---|
| 103 | |
---|
| 104 | void print(y_type y) { |
---|
[f498c51] | 105 | sout | y; |
---|
[3787dc1] | 106 | } |
---|
| 107 | |
---|
| 108 | y_type ret = foo(x, y); |
---|
| 109 | |
---|
| 110 | // duplicate logic from inside of foo to ensure the same results |
---|
| 111 | U u; |
---|
| 112 | u.f2 = 0; |
---|
| 113 | u.f1 = x; |
---|
[a7caf3d] | 114 | assertf(ret == u.f2, "union operation fails in polymorphic context."); |
---|
[3787dc1] | 115 | } |
---|
[aabc60c] | 116 | } |
---|
| 117 | |
---|
| 118 | // Local Variables: // |
---|
| 119 | // tab-width: 4 // |
---|
| 120 | // End: // |
---|