1 | // |
---|
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. |
---|
6 | // |
---|
7 | // builtins.c -- |
---|
8 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Fri Jul 21 16:21:03 2017 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Tue Mar 26 21:33:54 2019 |
---|
13 | // Update Count : 92 |
---|
14 | // |
---|
15 | |
---|
16 | // exception implementation |
---|
17 | |
---|
18 | typedef unsigned long long __cfaabi_abi_exception_type_t; |
---|
19 | |
---|
20 | #include <limits.h> // CHAR_BIT |
---|
21 | #include "../src/virtual.h" |
---|
22 | #include "../src/exception.h" |
---|
23 | |
---|
24 | void exit( int status, const char fmt[], ... ) __attribute__ (( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ )); |
---|
25 | void abort( const char fmt[], ... ) __attribute__ (( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ )); |
---|
26 | |
---|
27 | // increment/decrement unification |
---|
28 | |
---|
29 | static inline forall( dtype DT | { DT & ?+=?( DT &, one_t ); } ) |
---|
30 | DT & ++?( DT & x ) { return x += 1; } |
---|
31 | |
---|
32 | static inline forall( dtype DT | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?+=?( DT &, one_t ); } ) |
---|
33 | DT & ?++( DT & x ) { DT tmp = x; x += 1; return tmp; } |
---|
34 | |
---|
35 | static inline forall( dtype DT | { DT & ?-=?( DT &, one_t ); } ) |
---|
36 | DT & --?( DT & x ) { return x -= 1; } |
---|
37 | |
---|
38 | static inline forall( dtype DT | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?-=?( DT &, one_t ); } ) |
---|
39 | DT & ?--( DT & x ) { DT tmp = x; x -= 1; return tmp; } |
---|
40 | |
---|
41 | // universal typed pointer constant |
---|
42 | |
---|
43 | // Compiler issue: there is a problem with anonymous types that do not have a size. |
---|
44 | static inline forall( dtype DT | sized(DT) ) DT * intptr( uintptr_t addr ) { return (DT *)addr; } |
---|
45 | |
---|
46 | // exponentiation operator implementation |
---|
47 | |
---|
48 | extern "C" { |
---|
49 | float powf( float x, float y ); |
---|
50 | double pow( double x, double y ); |
---|
51 | long double powl( long double x, long double y ); |
---|
52 | float _Complex cpowf( float _Complex x, _Complex float z ); |
---|
53 | double _Complex cpow( double _Complex x, _Complex double z ); |
---|
54 | long double _Complex cpowl( long double _Complex x, _Complex long double z ); |
---|
55 | } // extern "C" |
---|
56 | |
---|
57 | static inline float ?\?( float x, float y ) { return powf( x, y ); } |
---|
58 | static inline double ?\?( double x, double y ) { return pow( x, y ); } |
---|
59 | static inline long double ?\?( long double x, long double y ) { return powl( x, y ); } |
---|
60 | static inline float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf(x, y ); } |
---|
61 | static inline double _Complex ?\?( double _Complex x, _Complex double y ) { return cpow( x, y ); } |
---|
62 | static inline long double _Complex ?\?( long double _Complex x, _Complex long double y ) { return cpowl( x, y ); } |
---|
63 | |
---|
64 | #define __CFA_BASE_COMP_1__() if ( ep == 1 ) return 1 |
---|
65 | #define __CFA_BASE_COMP_2__() if ( ep == 2 ) return ep << (y - 1) |
---|
66 | #define __CFA_EXP_OVERFLOW__() if ( y >= sizeof(y) * CHAR_BIT ) return 0 |
---|
67 | |
---|
68 | #define __CFA_EXP__() \ |
---|
69 | if ( y == 0 ) return 1; /* base case */ \ |
---|
70 | __CFA_BASE_COMP_1__(); /* base case */ \ |
---|
71 | __CFA_BASE_COMP_2__(); /* special case, positive shifting for integral types */ \ |
---|
72 | __CFA_EXP_OVERFLOW__(); /* immediate overflow, negative exponent > 2^size-1 */ \ |
---|
73 | typeof(ep) op = 1; /* accumulate odd product */ \ |
---|
74 | for ( ; y > 1; y >>= 1 ) { /* squaring exponentiation, O(log2 y) */ \ |
---|
75 | if ( (y & 1) == 1 ) op = op * ep; /* odd ? */ \ |
---|
76 | ep = ep * ep; \ |
---|
77 | } \ |
---|
78 | return ep * op |
---|
79 | |
---|
80 | static inline long int ?\?( int ep, unsigned int y ) { |
---|
81 | __CFA_EXP__(); |
---|
82 | } // ?\? |
---|
83 | |
---|
84 | static inline long int ?\?( long int ep, unsigned long int y ) { |
---|
85 | __CFA_EXP__(); |
---|
86 | } // ?\? |
---|
87 | |
---|
88 | // unsigned computation may be faster and larger |
---|
89 | static inline unsigned long int ?\?( unsigned int ep, unsigned int y ) { |
---|
90 | __CFA_EXP__(); |
---|
91 | } // ?\? |
---|
92 | |
---|
93 | static inline unsigned long int ?\?( unsigned long int ep, unsigned long int y ) { |
---|
94 | __CFA_EXP__(); |
---|
95 | } // ?\? |
---|
96 | |
---|
97 | #undef __CFA_BASE_COMP_1__ |
---|
98 | #undef __CFA_BASE_COMP_2__ |
---|
99 | #undef __CFA_EXP_OVERFLOW__ |
---|
100 | #define __CFA_BASE_COMP_1__() |
---|
101 | #define __CFA_BASE_COMP_2__() |
---|
102 | #define __CFA_EXP_OVERFLOW__() |
---|
103 | |
---|
104 | static inline forall( otype OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } ) |
---|
105 | OT ?\?( OT ep, unsigned int y ) { |
---|
106 | __CFA_EXP__(); |
---|
107 | } // ?\? |
---|
108 | |
---|
109 | static inline forall( otype OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } ) |
---|
110 | OT ?\?( OT ep, unsigned long int y ) { |
---|
111 | __CFA_EXP__(); |
---|
112 | } // ?\? |
---|
113 | |
---|
114 | #undef __CFA_BASE_COMP_1__ |
---|
115 | #undef __CFA_BASE_COMP_2__ |
---|
116 | #undef __CFA_EXP_OVERFLOW__ |
---|
117 | |
---|
118 | static inline long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; } |
---|
119 | static inline unsigned long int ?\=?( unsigned long int & x, unsigned long int y ) { x = x \ y; return x; } |
---|
120 | static inline int ?\=?( int & x, unsigned long int y ) { x = x \ y; return x; } |
---|
121 | static inline unsigned int ?\=?( unsigned int & x, unsigned long int y ) { x = x \ y; return x; } |
---|
122 | |
---|
123 | // Local Variables: // |
---|
124 | // mode: c // |
---|
125 | // tab-width: 4 // |
---|
126 | // End: // |
---|