[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 |
---|
[f79cdf8] | 12 | // Last Modified On : Sun Aug 5 21:40:38 2018 |
---|
| 13 | // Update Count : 20 |
---|
[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 | |
---|
[bf71cfd] | 44 | #include "../src/virtual.h" |
---|
| 45 | #include "../src/exception.h" |
---|
[a1edafa] | 46 | |
---|
[169d944] | 47 | void exit( int status, const char fmt[], ... ) __attribute__ (( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ )); |
---|
[f79cdf8] | 48 | void abort( const char fmt[], ... ) __attribute__ (( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )); |
---|
[169d944] | 49 | |
---|
[d2887f7] | 50 | // increment/decrement unification |
---|
| 51 | |
---|
| 52 | static inline forall( dtype T | { T& ?+=?( T&, one_t ); } ) |
---|
| 53 | T& ++? ( T& x ) { return x += 1; } |
---|
| 54 | |
---|
| 55 | static inline forall( dtype T | sized(T) | { void ?{}( T&, T ); void ^?{}( T& ); T& ?+=?( T&, one_t ); } ) |
---|
| 56 | T& ?++ ( T& x ) { T tmp = x; x += 1; return tmp; } |
---|
| 57 | |
---|
| 58 | static inline forall( dtype T | { T& ?-=?( T&, one_t ); } ) |
---|
| 59 | T& --? ( T& x ) { return x -= 1; } |
---|
| 60 | |
---|
| 61 | static inline forall( dtype T | sized(T) | { void ?{}( T&, T ); void ^?{}( T& ); T& ?-=?( T&, one_t ); } ) |
---|
| 62 | T& ?-- ( T& x ) { T tmp = x; x -= 1; return tmp; } |
---|
[a1edafa] | 63 | |
---|
| 64 | // exponentiation operator implementation |
---|
| 65 | |
---|
| 66 | extern "C" { |
---|
| 67 | float powf( float x, float y ); |
---|
| 68 | double pow( double x, double y ); |
---|
| 69 | long double powl( long double x, long double y ); |
---|
| 70 | float _Complex cpowf( float _Complex x, _Complex float z ); |
---|
| 71 | double _Complex cpow( double _Complex x, _Complex double z ); |
---|
| 72 | long double _Complex cpowl( long double _Complex x, _Complex long double z ); |
---|
| 73 | } // extern "C" |
---|
| 74 | |
---|
| 75 | static inline float ?\?( float x, float y ) { return powf( x, y ); } |
---|
| 76 | static inline double ?\?( double x, double y ) { return pow( x, y ); } |
---|
| 77 | static inline long double ?\?( long double x, long double y ) { return powl( x, y ); } |
---|
| 78 | static inline float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf(x, y ); } |
---|
| 79 | static inline double _Complex ?\?( double _Complex x, _Complex double y ) { return cpow( x, y ); } |
---|
| 80 | static inline long double _Complex ?\?( long double _Complex x, _Complex long double y ) { return cpowl( x, y ); } |
---|
| 81 | |
---|
[fe97a7d8] | 82 | static inline long int ?\?( long int ep, unsigned long int y ) { // disallow negative exponent |
---|
[a1edafa] | 83 | if ( y == 0 ) return 1; // base case |
---|
[df7a162] | 84 | if ( ep == 2 ) return ep << (y - 1); // special case, positive shifting only |
---|
| 85 | typeof( ep ) op = 1; // accumulate odd product |
---|
| 86 | for ( ; y > 1; y >>= 1 ) { // squaring exponentiation, O(log2 y) |
---|
[fe97a7d8] | 87 | if ( (y & 1) == 1 ) op *= ep; // odd ? |
---|
| 88 | ep *= ep; |
---|
| 89 | } // for |
---|
[df7a162] | 90 | return ep * op; |
---|
[a1edafa] | 91 | } // ?\? |
---|
| 92 | |
---|
[df7a162] | 93 | static inline forall( otype T | { void ?{}( T & this, one_t ); T ?*?( T, T ); } ) |
---|
| 94 | T ?\?( T ep, unsigned long int y ) { |
---|
| 95 | if ( y == 0 ) return 1; |
---|
| 96 | T op = 1; |
---|
| 97 | for ( ; y > 1; y >>= 1 ) { // squaring exponentiation, O(log2 y) |
---|
| 98 | if ( (y & 1) == 1 ) op = op * ep; // odd ? |
---|
| 99 | ep = ep * ep; |
---|
| 100 | } // for |
---|
| 101 | return ep * op; |
---|
| 102 | } // ?\? |
---|
[a1edafa] | 103 | |
---|
| 104 | // unsigned computation may be faster and larger |
---|
[fe97a7d8] | 105 | static inline unsigned long int ?\?( unsigned long int ep, unsigned long int y ) { // disallow negative exponent |
---|
[a1edafa] | 106 | if ( y == 0 ) return 1; // base case |
---|
[df7a162] | 107 | if ( ep == 2 ) return ep << (y - 1); // special case, positive shifting only |
---|
| 108 | typeof( ep ) op = 1; // accumulate odd product |
---|
| 109 | for ( ; y > 1; y >>= 1 ) { // squaring exponentiation, O(log2 y) |
---|
[fe97a7d8] | 110 | if ( (y & 1) == 1 ) op *= ep; // odd ? |
---|
| 111 | ep *= ep; |
---|
| 112 | } // for |
---|
[df7a162] | 113 | return ep * op; |
---|
[a1edafa] | 114 | } // ?\? |
---|
| 115 | |
---|
[fe97a7d8] | 116 | static inline double ?\?( long int x, signed long int y ) { // allow negative exponent |
---|
[df7a162] | 117 | if ( y >= 0 ) return (double)(x \ (unsigned long int)y); |
---|
| 118 | else return 1.0 / x \ (unsigned int)(-y); |
---|
[a1edafa] | 119 | } // ?\? |
---|
| 120 | |
---|
[36982fc] | 121 | // FIXME (x \ (unsigned long int)y) relies on X ?\?(T, unsigned long) a function that is neither |
---|
| 122 | // defined, nor passed as an assertion parameter. Without user-defined conversions, cannot specify |
---|
| 123 | // X as a type that casts to double, yet it doesn't make sense to write functions with that type |
---|
[e472d54] | 124 | // signature where X is double. |
---|
| 125 | |
---|
| 126 | // static inline forall( otype T | { void ?{}( T & this, one_t ); T ?*?( T, T ); double ?/?( double, T ); } ) |
---|
| 127 | // double ?\?( T x, signed long int y ) { |
---|
| 128 | // if ( y >= 0 ) return (double)(x \ (unsigned long int)y); |
---|
| 129 | // else return 1.0 / x \ (unsigned long int)(-y); |
---|
| 130 | // } // ?\? |
---|
[a1edafa] | 131 | |
---|
[1ae06fa] | 132 | static inline long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; } |
---|
| 133 | static inline unsigned long int ?\=?( unsigned long int & x, unsigned long int y ) { x = x \ y; return x; } |
---|
| 134 | static inline int ?\=?( int & x, unsigned long int y ) { x = x \ y; return x; } |
---|
| 135 | static inline unsigned int ?\=?( unsigned int & x, unsigned long int y ) { x = x \ y; return x; } |
---|
[a1edafa] | 136 | |
---|
| 137 | // Local Variables: // |
---|
| 138 | // mode: c // |
---|
| 139 | // tab-width: 4 // |
---|
| 140 | // End: // |
---|