source: libcfa/prelude/builtins.c@ ee7a29f

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 ee7a29f 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
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
[8a30423]12// Last Modified On : Tue Mar 26 23:10:36 2019
13// Update Count : 95
[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
[8a30423]29static inline {
30 forall( dtype DT | { DT & ?+=?( DT &, one_t ); } )
31 DT & ++?( DT & x ) { return x += 1; }
[d2887f7]32
[8a30423]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; }
[d2887f7]35
[8a30423]36 forall( dtype DT | { DT & ?-=?( DT &, one_t ); } )
37 DT & --?( DT & x ) { return x -= 1; }
[d2887f7]38
[8a30423]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
[7579ac0]42
43// universal typed pointer constant
[8a30423]44// Compiler issue: there is a problem with anonymous types that do not have a size.
[8dbfb7e]45static inline forall( dtype DT | sized(DT) ) DT * intptr( uintptr_t addr ) { return (DT *)addr; }
[d2887f7]46
[a1edafa]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
[8a30423]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
[a1edafa]66
[7726839]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
[8dbfb7e]71#define __CFA_EXP__() \
[8a30423]72 if ( y == 0 ) return 1; /* convention */ \
[7726839]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 */ \
[8dbfb7e]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
[8a30423]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
[a1edafa]90
[8dbfb7e]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__()
[e472d54]97
[8a30423]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
[8dbfb7e]102
103#undef __CFA_BASE_COMP_1__
104#undef __CFA_BASE_COMP_2__
105#undef __CFA_EXP_OVERFLOW__
[a1edafa]106
[8a30423]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
[a1edafa]113
114// Local Variables: //
115// mode: c //
116// tab-width: 4 //
117// End: //
Note: See TracBrowser for help on using the repository browser.