source: libcfa/prelude/builtins.c@ 697e484

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 697e484 was 7726839, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

exponential clean up and fix forall version

  • 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
[7726839]12// Last Modified On : Tue Mar 26 21:33:54 2019
13// Update Count : 92
[a5f0529]14//
[a1edafa]15
16// exception implementation
17
[36982fc]18typedef unsigned long long __cfaabi_abi_exception_type_t;
[fa4805f]19
[7726839]20#include <limits.h> // CHAR_BIT
[bf71cfd]21#include "../src/virtual.h"
22#include "../src/exception.h"
[a1edafa]23
[169d944]24void exit( int status, const char fmt[], ... ) __attribute__ (( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
[f79cdf8]25void abort( const char fmt[], ... ) __attribute__ (( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));
[169d944]26
[d2887f7]27// increment/decrement unification
28
[8dbfb7e]29static inline forall( dtype DT | { DT & ?+=?( DT &, one_t ); } )
30DT & ++?( DT & x ) { return x += 1; }
[d2887f7]31
[8dbfb7e]32static inline forall( dtype DT | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?+=?( DT &, one_t ); } )
33DT & ?++( DT & x ) { DT tmp = x; x += 1; return tmp; }
[d2887f7]34
[8dbfb7e]35static inline forall( dtype DT | { DT & ?-=?( DT &, one_t ); } )
36DT & --?( DT & x ) { return x -= 1; }
[d2887f7]37
[8dbfb7e]38static inline forall( dtype DT | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?-=?( DT &, one_t ); } )
39DT & ?--( DT & x ) { DT tmp = x; x -= 1; return tmp; }
[7579ac0]40
41// universal typed pointer constant
42
[8dbfb7e]43// Compiler issue: there is a problem with anonymous types that do not have a size.
44static inline forall( dtype DT | sized(DT) ) DT * intptr( uintptr_t addr ) { return (DT *)addr; }
[d2887f7]45
[a1edafa]46// exponentiation operator implementation
47
48extern "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
57static inline float ?\?( float x, float y ) { return powf( x, y ); }
58static inline double ?\?( double x, double y ) { return pow( x, y ); }
59static inline long double ?\?( long double x, long double y ) { return powl( x, y ); }
60static inline float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf(x, y ); }
61static inline double _Complex ?\?( double _Complex x, _Complex double y ) { return cpow( x, y ); }
62static inline long double _Complex ?\?( long double _Complex x, _Complex long double y ) { return cpowl( x, y ); }
63
[7726839]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
[8dbfb7e]68#define __CFA_EXP__() \
69 if ( y == 0 ) return 1; /* base case */ \
[7726839]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 */ \
[8dbfb7e]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
80static inline long int ?\?( int ep, unsigned int y ) {
81 __CFA_EXP__();
[a1edafa]82} // ?\?
83
[8dbfb7e]84static inline long int ?\?( long int ep, unsigned long int y ) {
85 __CFA_EXP__();
[df7a162]86} // ?\?
[a1edafa]87
88// unsigned computation may be faster and larger
[8dbfb7e]89static inline unsigned long int ?\?( unsigned int ep, unsigned int y ) {
90 __CFA_EXP__();
[a1edafa]91} // ?\?
92
[8dbfb7e]93static inline unsigned long int ?\?( unsigned long int ep, unsigned long int y ) {
94 __CFA_EXP__();
[a1edafa]95} // ?\?
96
[8dbfb7e]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__()
[e472d54]103
[8dbfb7e]104static inline forall( otype OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } )
105OT ?\?( OT ep, unsigned int y ) {
106 __CFA_EXP__();
107} // ?\?
108
[7726839]109static inline forall( otype OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } )
[8dbfb7e]110OT ?\?( 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__
[a1edafa]117
[1ae06fa]118static inline long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; }
119static inline unsigned long int ?\=?( unsigned long int & x, unsigned long int y ) { x = x \ y; return x; }
120static inline int ?\=?( int & x, unsigned long int y ) { x = x \ y; return x; }
121static inline unsigned int ?\=?( unsigned int & x, unsigned long int y ) { x = x \ y; return x; }
[a1edafa]122
123// Local Variables: //
124// mode: c //
125// tab-width: 4 //
126// End: //
Note: See TracBrowser for help on using the repository browser.