source: libcfa/prelude/builtins.c @ f1240b0

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

formatting and temporarily add polymorphic intptr

  • Property mode set to 100644
File size: 4.8 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
[7579ac0]12// Last Modified On : Sun Mar 10 10:52:50 2019
13// Update Count     : 31
[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
[7579ac0]28static inline forall( dtype T | { T & ?+=?( T &, one_t ); } )
29T & ++? ( T & x ) { return x += 1; }
[d2887f7]30
[7579ac0]31static inline forall( dtype T | sized(T) | { void ?{}( T &, T ); void ^?{}( T & ); T & ?+=?( T &, one_t ); } )
32T & ?++ ( T & x ) { T tmp = x; x += 1; return tmp; }
[d2887f7]33
[7579ac0]34static inline forall( dtype T | { T & ?-=?( T &, one_t ); } )
35T & --? ( T & x ) { return x -= 1; }
[d2887f7]36
[7579ac0]37static inline forall( dtype T | sized(T) | { void ?{}( T &, T ); void ^?{}( T & ); T & ?-=?( T &, one_t ); } )
38T & ?-- ( T & x ) { T tmp = x; x -= 1; return tmp; }
39
40// universal typed pointer constant
41
42static inline forall( dtype T ) T * intptr( uintptr_t addr ) { return (T *)addr; }
[d2887f7]43
[a1edafa]44// exponentiation operator implementation
45
46extern "C" {
47        float powf( float x, float y );
48        double pow( double x, double y );
49        long double powl( long double x, long double y );
50        float _Complex cpowf( float _Complex x, _Complex float z );
51        double _Complex cpow( double _Complex x, _Complex double z );
52        long double _Complex cpowl( long double _Complex x, _Complex long double z );
53} // extern "C"
54
55static inline float ?\?( float x, float y ) { return powf( x, y ); }
56static inline double ?\?( double x, double y ) { return pow( x, y ); }
57static inline long double ?\?( long double x, long double y ) { return powl( x, y ); }
58static inline float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf(x, y ); }
59static inline double _Complex ?\?( double _Complex x, _Complex double y ) { return cpow( x, y ); }
60static inline long double _Complex ?\?( long double _Complex x, _Complex long double y ) { return cpowl( x, y ); }
61
[fe97a7d8]62static inline long int ?\?( long int ep, unsigned long int y ) { // disallow negative exponent
[a1edafa]63        if ( y == 0 ) return 1;                                                         // base case
[df7a162]64        if ( ep == 2 ) return ep << (y - 1);                            // special case, positive shifting only
65        typeof( ep ) op = 1;                                                            // accumulate odd product
66        for ( ; y > 1; y >>= 1 ) {                                                      // squaring exponentiation, O(log2 y)
[fe97a7d8]67                if ( (y & 1) == 1 ) op *= ep;                                   // odd ?
68                ep *= ep;
69        } // for
[df7a162]70        return ep * op;
[a1edafa]71} // ?\?
72
[df7a162]73static inline forall( otype T | { void ?{}( T & this, one_t ); T ?*?( T, T ); } )
74T ?\?( T ep, unsigned long int y ) {
75        if ( y == 0 ) return 1;
76        T op = 1;
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        } // for
81        return ep * op;
82} // ?\?
[a1edafa]83
84// unsigned computation may be faster and larger
[fe97a7d8]85static inline unsigned long int ?\?( unsigned long int ep, unsigned long int y ) { // disallow negative exponent
[a1edafa]86        if ( y == 0 ) return 1;                                                         // base case
[df7a162]87        if ( ep == 2 ) return ep << (y - 1);                            // special case, positive shifting only
88        typeof( ep ) op = 1;                                                            // accumulate odd product
89        for ( ; y > 1; y >>= 1 ) {                                                      // squaring exponentiation, O(log2 y)
[fe97a7d8]90                if ( (y & 1) == 1 ) op *= ep;                                   // odd ?
91                ep *= ep;
92        } // for
[df7a162]93        return ep * op;
[a1edafa]94} // ?\?
95
[fe97a7d8]96static inline double ?\?( long int x, signed long int y ) {     // allow negative exponent
[df7a162]97        if ( y >=  0 ) return (double)(x \ (unsigned long int)y);
98        else return 1.0 / x \ (unsigned int)(-y);
[a1edafa]99} // ?\?
100
[36982fc]101// FIXME (x \ (unsigned long int)y) relies on X ?\?(T, unsigned long) a function that is neither
102// defined, nor passed as an assertion parameter. Without user-defined conversions, cannot specify
103// X as a type that casts to double, yet it doesn't make sense to write functions with that type
[e472d54]104// signature where X is double.
105
106// static inline forall( otype T | { void ?{}( T & this, one_t ); T ?*?( T, T ); double ?/?( double, T ); } )
107// double ?\?( T x, signed long int y ) {
108//     if ( y >=  0 ) return (double)(x \ (unsigned long int)y);
109//     else return 1.0 / x \ (unsigned long int)(-y);
110// } // ?\?
[a1edafa]111
[1ae06fa]112static inline long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; }
113static inline unsigned long int ?\=?( unsigned long int & x, unsigned long int y ) { x = x \ y; return x; }
114static inline int ?\=?( int & x, unsigned long int y ) { x = x \ y; return x; }
115static inline unsigned int ?\=?( unsigned int & x, unsigned long int y ) { x = x \ y; return x; }
[a1edafa]116
117// Local Variables: //
118// mode: c //
119// tab-width: 4 //
120// End: //
Note: See TracBrowser for help on using the repository browser.