[a5f0529] | 1 | // |
---|
[a1edafa] | 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
[a5f0529] | 6 | // |
---|
| 7 | // builtins.c -- |
---|
| 8 | // |
---|
[a1edafa] | 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Fri Jul 21 16:21:03 2017 |
---|
[88ac8672] | 11 | // Last Modified By : Peter A. Buhr |
---|
[2ead704] | 12 | // Last Modified On : Wed Jul 21 13:31:34 2021 |
---|
| 13 | // Update Count : 129 |
---|
[a5f0529] | 14 | // |
---|
[a1edafa] | 15 | |
---|
[c960331] | 16 | #define __cforall_builtins__ |
---|
| 17 | |
---|
[b867278] | 18 | // type that wraps a pointer and a destructor-like function - used in generating implicit destructor calls for struct members in user-defined functions |
---|
| 19 | // Note: needs to occur early, because it is used to generate destructor calls during code generation |
---|
[fd54fef] | 20 | forall(T &) |
---|
[b867278] | 21 | struct __Destructor { |
---|
| 22 | T * object; |
---|
| 23 | void (*dtor)(T *); |
---|
| 24 | }; |
---|
| 25 | |
---|
| 26 | // defined destructor in the case that non-generated code wants to use __Destructor |
---|
[fd54fef] | 27 | forall(T &) |
---|
[b867278] | 28 | static inline void ^?{}(__Destructor(T) & x) { |
---|
| 29 | if (x.object && x.dtor) { |
---|
| 30 | x.dtor(x.object); |
---|
| 31 | } |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | // easy interface into __Destructor's destructor for easy codegen purposes |
---|
| 35 | extern "C" { |
---|
[fd54fef] | 36 | forall(T &) |
---|
[b867278] | 37 | static inline void __destroy_Destructor(__Destructor(T) * dtor) { |
---|
| 38 | ^(*dtor){}; |
---|
| 39 | } |
---|
| 40 | } |
---|
| 41 | |
---|
[a1edafa] | 42 | // exception implementation |
---|
| 43 | |
---|
[36982fc] | 44 | typedef unsigned long long __cfaabi_abi_exception_type_t; |
---|
[fa4805f] | 45 | |
---|
[7726839] | 46 | #include <limits.h> // CHAR_BIT |
---|
[bf71cfd] | 47 | #include "../src/virtual.h" |
---|
| 48 | #include "../src/exception.h" |
---|
[a1edafa] | 49 | |
---|
[169d944] | 50 | void exit( int status, const char fmt[], ... ) __attribute__ (( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ )); |
---|
[f79cdf8] | 51 | void abort( const char fmt[], ... ) __attribute__ (( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )); |
---|
[169d944] | 52 | |
---|
[fd54fef] | 53 | forall(T &) |
---|
[427854b] | 54 | static inline T & identity(T & i) { |
---|
| 55 | return i; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | // generator support |
---|
[e84ab3d] | 59 | struct generator$ { |
---|
[427854b] | 60 | inline int; |
---|
| 61 | }; |
---|
| 62 | |
---|
[e84ab3d] | 63 | static inline void ?{}(generator$ & this) { ((int&)this) = 0; } |
---|
| 64 | static inline void ^?{}(generator$ &) {} |
---|
[427854b] | 65 | |
---|
[fd54fef] | 66 | trait is_generator(T &) { |
---|
[427854b] | 67 | void main(T & this); |
---|
[e84ab3d] | 68 | generator$ * get_generator(T & this); |
---|
[427854b] | 69 | }; |
---|
| 70 | |
---|
[fd54fef] | 71 | forall(T & | is_generator(T)) |
---|
[427854b] | 72 | static inline T & resume(T & gen) { |
---|
| 73 | main(gen); |
---|
| 74 | return gen; |
---|
| 75 | } |
---|
| 76 | |
---|
[0d8266c] | 77 | // implicit increment, decrement if += defined, and implicit not if != defined |
---|
[d2887f7] | 78 | |
---|
[2ead704] | 79 | // C11 reference manual Section 6.5.16 (page101): "An assignment expression has the value of the left operand after the |
---|
| 80 | // assignment, but is not an lvalue." Hence, return a value not a reference. |
---|
[8a30423] | 81 | static inline { |
---|
[8477fc4] | 82 | forall( T | { T ?+=?( T &, one_t ); } ) |
---|
| 83 | T ++?( T & x ) { return x += 1; } |
---|
[d2887f7] | 84 | |
---|
[8477fc4] | 85 | forall( T | { T ?+=?( T &, one_t ); } ) |
---|
| 86 | T ?++( T & x ) { T tmp = x; x += 1; return tmp; } |
---|
[d2887f7] | 87 | |
---|
[8477fc4] | 88 | forall( T | { T ?-=?( T &, one_t ); } ) |
---|
| 89 | T --?( T & x ) { return x -= 1; } |
---|
[d2887f7] | 90 | |
---|
[8477fc4] | 91 | forall( T | { T ?-=?( T &, one_t ); } ) |
---|
| 92 | T ?--( T & x ) { T tmp = x; x -= 1; return tmp; } |
---|
[0d8266c] | 93 | |
---|
[8477fc4] | 94 | forall( T | { int ?!=?( T, zero_t ); } ) |
---|
| 95 | int !?( T & x ) { return !( x != 0 ); } |
---|
[8a30423] | 96 | } // distribution |
---|
[7579ac0] | 97 | |
---|
| 98 | // universal typed pointer constant |
---|
[fd54fef] | 99 | static inline forall( DT & ) DT * intptr( uintptr_t addr ) { return (DT *)addr; } |
---|
[04423b53] | 100 | static inline forall( ftype FT ) FT * intptr( uintptr_t addr ) { return (FT *)addr; } |
---|
[a1edafa] | 101 | |
---|
[cf5af9c] | 102 | #if defined(__SIZEOF_INT128__) |
---|
[b56f55c] | 103 | // constructor for 128-bit numbers (all constants are unsigned as +/- are operators) |
---|
| 104 | static inline void ?{}( unsigned int128 & this, unsigned long int h, unsigned long int l ) { |
---|
| 105 | this = (unsigned int128)h << 64 | (unsigned int128)l; |
---|
| 106 | } // ?{} |
---|
[cf5af9c] | 107 | #endif // __SIZEOF_INT128__ |
---|
[b56f55c] | 108 | |
---|
[a1edafa] | 109 | // exponentiation operator implementation |
---|
| 110 | |
---|
| 111 | extern "C" { |
---|
| 112 | float powf( float x, float y ); |
---|
| 113 | double pow( double x, double y ); |
---|
| 114 | long double powl( long double x, long double y ); |
---|
| 115 | float _Complex cpowf( float _Complex x, _Complex float z ); |
---|
| 116 | double _Complex cpow( double _Complex x, _Complex double z ); |
---|
| 117 | long double _Complex cpowl( long double _Complex x, _Complex long double z ); |
---|
| 118 | } // extern "C" |
---|
| 119 | |
---|
[8a30423] | 120 | static inline { |
---|
| 121 | float ?\?( float x, float y ) { return powf( x, y ); } |
---|
| 122 | double ?\?( double x, double y ) { return pow( x, y ); } |
---|
| 123 | long double ?\?( long double x, long double y ) { return powl( x, y ); } |
---|
| 124 | float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf(x, y ); } |
---|
| 125 | double _Complex ?\?( double _Complex x, _Complex double y ) { return cpow( x, y ); } |
---|
| 126 | long double _Complex ?\?( long double _Complex x, _Complex long double y ) { return cpowl( x, y ); } |
---|
| 127 | } // distribution |
---|
[a1edafa] | 128 | |
---|
[88ac8672] | 129 | #define __CFA_BASE_COMP_1__() if ( x == 1 ) return 1 |
---|
| 130 | #define __CFA_BASE_COMP_2__() if ( x == 2 ) return x << (y - 1) |
---|
[7726839] | 131 | #define __CFA_EXP_OVERFLOW__() if ( y >= sizeof(y) * CHAR_BIT ) return 0 |
---|
| 132 | |
---|
[8dbfb7e] | 133 | #define __CFA_EXP__() \ |
---|
[8a30423] | 134 | if ( y == 0 ) return 1; /* convention */ \ |
---|
[7726839] | 135 | __CFA_BASE_COMP_1__(); /* base case */ \ |
---|
| 136 | __CFA_BASE_COMP_2__(); /* special case, positive shifting for integral types */ \ |
---|
| 137 | __CFA_EXP_OVERFLOW__(); /* immediate overflow, negative exponent > 2^size-1 */ \ |
---|
[88ac8672] | 138 | typeof(x) op = 1; /* accumulate odd product */ \ |
---|
[8dbfb7e] | 139 | for ( ; y > 1; y >>= 1 ) { /* squaring exponentiation, O(log2 y) */ \ |
---|
[88ac8672] | 140 | if ( (y & 1) == 1 ) op = op * x; /* odd ? */ \ |
---|
| 141 | x = x * x; \ |
---|
[8dbfb7e] | 142 | } \ |
---|
[88ac8672] | 143 | return x * op |
---|
[8dbfb7e] | 144 | |
---|
[8a30423] | 145 | static inline { |
---|
[88ac8672] | 146 | long int ?\?( int x, unsigned int y ) { __CFA_EXP__(); } |
---|
| 147 | long int ?\?( long int x, unsigned long int y ) { __CFA_EXP__(); } |
---|
| 148 | long long int ?\?( long long int x, unsigned long long int y ) { __CFA_EXP__(); } |
---|
[8a30423] | 149 | // unsigned computation may be faster and larger |
---|
[88ac8672] | 150 | unsigned long int ?\?( unsigned int x, unsigned int y ) { __CFA_EXP__(); } |
---|
| 151 | unsigned long int ?\?( unsigned long int x, unsigned long int y ) { __CFA_EXP__(); } |
---|
| 152 | unsigned long long int ?\?( unsigned long long int x, unsigned long long int y ) { __CFA_EXP__(); } |
---|
[8a30423] | 153 | } // distribution |
---|
[a1edafa] | 154 | |
---|
[8dbfb7e] | 155 | #undef __CFA_BASE_COMP_1__ |
---|
| 156 | #undef __CFA_BASE_COMP_2__ |
---|
| 157 | #undef __CFA_EXP_OVERFLOW__ |
---|
| 158 | #define __CFA_BASE_COMP_1__() |
---|
| 159 | #define __CFA_BASE_COMP_2__() |
---|
| 160 | #define __CFA_EXP_OVERFLOW__() |
---|
[e472d54] | 161 | |
---|
[fd54fef] | 162 | static inline forall( OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } ) { |
---|
[88ac8672] | 163 | OT ?\?( OT x, unsigned int y ) { __CFA_EXP__(); } |
---|
| 164 | OT ?\?( OT x, unsigned long int y ) { __CFA_EXP__(); } |
---|
| 165 | OT ?\?( OT x, unsigned long long int y ) { __CFA_EXP__(); } |
---|
[8a30423] | 166 | } // distribution |
---|
[8dbfb7e] | 167 | |
---|
| 168 | #undef __CFA_BASE_COMP_1__ |
---|
| 169 | #undef __CFA_BASE_COMP_2__ |
---|
| 170 | #undef __CFA_EXP_OVERFLOW__ |
---|
[a1edafa] | 171 | |
---|
[8a30423] | 172 | static inline { |
---|
[88ac8672] | 173 | long int ?\=?( int & x, unsigned int y ) { x = x \ y; return x; } |
---|
[8a30423] | 174 | long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; } |
---|
[88ac8672] | 175 | long long int ?\=?( long long int & x, unsigned long long int y ) { x = x \ y; return x; } |
---|
| 176 | unsigned long int ?\=?( unsigned int & x, unsigned int y ) { x = x \ y; return x; } |
---|
[8a30423] | 177 | unsigned long int ?\=?( unsigned long int & x, unsigned long int y ) { x = x \ y; return x; } |
---|
[88ac8672] | 178 | unsigned long long int ?\=?( unsigned long long int & x, unsigned long long int y ) { x = x \ y; return x; } |
---|
[8a30423] | 179 | } // distribution |
---|
[a1edafa] | 180 | |
---|
| 181 | // Local Variables: // |
---|
| 182 | // mode: c // |
---|
| 183 | // tab-width: 4 // |
---|
| 184 | // End: // |
---|