source: libcfa/prelude/builtins.c @ 8dbfb7e

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 8dbfb7e was 8dbfb7e, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

update exponential operator, formatting

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