source: src/prelude/builtins.c @ c6e2c18

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since c6e2c18 was c0d00b6, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Merge branch 'master' into cleanup-dtors

  • Property mode set to 100644
File size: 4.6 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 : Andrew Beach
12// Last Modified On : Tus Jul 25 15:33:00 2017
13// Update Count     : 14
14//
15
16// exception implementation
17
18typedef unsigned long long __cfaabi_exception_type_t;
19
20#include "../libcfa/virtual.h"
21#include "../libcfa/exception.h"
22
23// exponentiation operator implementation
24
25extern "C" {
26        float powf( float x, float y );
27        double pow( double x, double y );
28        long double powl( long double x, long double y );
29        float _Complex cpowf( float _Complex x, _Complex float z );
30        double _Complex cpow( double _Complex x, _Complex double z );
31        long double _Complex cpowl( long double _Complex x, _Complex long double z );
32} // extern "C"
33
34static inline float ?\?( float x, float y ) { return powf( x, y ); }
35static inline double ?\?( double x, double y ) { return pow( x, y ); }
36static inline long double ?\?( long double x, long double y ) { return powl( x, y ); }
37static inline float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf(x, y ); }
38static inline double _Complex ?\?( double _Complex x, _Complex double y ) { return cpow( x, y ); }
39static inline long double _Complex ?\?( long double _Complex x, _Complex long double y ) { return cpowl( x, y ); }
40
41static inline long int ?\?( long int ep, unsigned long int y ) { // disallow negative exponent
42        if ( y == 0 ) return 1;                                                         // base case
43    if ( ep == 2 ) return ep << (y - 1);                                // special case, positive shifting only
44    typeof( ep ) op = 1;                                                                // accumulate odd product
45    for ( ; y > 1; y >>= 1 ) {                                                  // squaring exponentiation, O(log2 y)
46                if ( (y & 1) == 1 ) op *= ep;                                   // odd ?
47                ep *= ep;
48        } // for
49    return ep * op;
50} // ?\?
51
52// FIX ME, cannot resolve the "T op = 1".
53
54// static inline forall( otype T | { void ?{}( T * this, one_t ); T ?*?( T, T ); } )
55// T ?\?( T ep, unsigned long int y ) {
56//     if ( y == 0 ) return 1;
57//     T op = 1;
58//     for ( ; y > 1; y >>= 1 ) {                                                       // squaring exponentiation, O(log2 y)
59//              if ( (y & 1) == 1 ) op = op * ep;                               // odd ?
60//              ep = ep * ep;
61//     } // for
62//     return ep * op;
63// } // ?\?
64
65// unsigned computation may be faster and larger
66static inline unsigned long int ?\?( unsigned long int ep, unsigned long int y ) { // disallow negative exponent
67        if ( y == 0 ) return 1;                                                         // base case
68    if ( ep == 2 ) return ep << (y - 1);                                // special case, positive shifting only
69    typeof( ep ) op = 1;                                                                // accumulate odd product
70    for ( ; y > 1; y >>= 1 ) {                                                  // squaring exponentiation, O(log2 y)
71                if ( (y & 1) == 1 ) op *= ep;                                   // odd ?
72                ep *= ep;
73        } // for
74    return ep * op;
75} // ?\?
76
77static inline double ?\?( long int x, signed long int y ) {     // allow negative exponent
78    if ( y >=  0 ) return (double)(x \ (unsigned long int)y);
79    else return 1.0 / x \ (unsigned int)(-y);
80} // ?\?
81
82// FIXME (x \ (unsigned long int)y) relies on X ?\?(T, unsigned long) a function that is neither
83// defined, nor passed as an assertion parameter. Without user-defined conversions, cannot specify
84// X as a type that casts to double, yet it doesn't make sense to write functions with that type
85// signature where X is double.
86
87// static inline forall( otype T | { void ?{}( T & this, one_t ); T ?*?( T, T ); double ?/?( double, T ); } )
88// double ?\?( T x, signed long int y ) {
89//     if ( y >=  0 ) return (double)(x \ (unsigned long int)y);
90//     else return 1.0 / x \ (unsigned long int)(-y);
91// } // ?\?
92
93static inline long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; }
94static inline unsigned long int ?\=?( unsigned long int & x, unsigned long int y ) { x = x \ y; return x; }
95static inline int ?\=?( int & x, unsigned long int y ) { x = x \ y; return x; }
96static inline unsigned int ?\=?( unsigned int & x, unsigned long int y ) { x = x \ y; return x; }
97
98// type that wraps a pointer and a destructor-like function - used in generating implicit destructor calls for struct members in user-defined functions
99forall(dtype T)
100struct __Destructor {
101  T * object;
102  void (*dtor)(T *);
103};
104
105// defined destructor in the case that non-generated code wants to use __Destructor
106forall(dtype T)
107static inline void ^?{}(__Destructor(T) & x) {
108  x.dtor(x.object);
109}
110
111// easy interface into __Destructor's destructor for easy codegen purposes
112extern "C" {
113  forall(dtype T)
114  static inline void __destroy_Destructor(__Destructor(T) * dtor) {
115    ^(*dtor){};
116  }
117}
118
119// Local Variables: //
120// mode: c //
121// tab-width: 4 //
122// End: //
Note: See TracBrowser for help on using the repository browser.