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