source: libcfa/prelude/builtins.c@ 052cd71

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 052cd71 was 8a30423, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

more exponential clean up

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