Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/prelude/builtins.c

    r8a30423 rb867278  
    1010// Created On       : Fri Jul 21 16:21:03 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Mar 26 23:10:36 2019
    13 // Update Count     : 95
     12// Last Modified On : Sun Aug  5 21:40:38 2018
     13// Update Count     : 20
    1414//
     15
     16// type that wraps a pointer and a destructor-like function - used in generating implicit destructor calls for struct members in user-defined functions
     17// Note: needs to occur early, because it is used to generate destructor calls during code generation
     18forall(dtype T)
     19struct __Destructor {
     20        T * object;
     21        void (*dtor)(T *);
     22};
     23
     24// defined destructor in the case that non-generated code wants to use __Destructor
     25forall(dtype T)
     26static inline void ^?{}(__Destructor(T) & x) {
     27        if (x.object && x.dtor) {
     28                x.dtor(x.object);
     29        }
     30}
     31
     32// easy interface into __Destructor's destructor for easy codegen purposes
     33extern "C" {
     34        forall(dtype T)
     35        static inline void __destroy_Destructor(__Destructor(T) * dtor) {
     36                ^(*dtor){};
     37        }
     38}
    1539
    1640// exception implementation
     
    1842typedef unsigned long long __cfaabi_abi_exception_type_t;
    1943
    20 #include <limits.h>                                                                             // CHAR_BIT
    2144#include "../src/virtual.h"
    2245#include "../src/exception.h"
     
    2750// increment/decrement unification
    2851
    29 static inline {
    30         forall( dtype DT | { DT & ?+=?( DT &, one_t ); } )
    31         DT & ++?( DT & x ) { return x += 1; }
     52static inline forall( dtype T | { T& ?+=?( T&, one_t ); } )
     53T& ++? ( T& x ) { return x += 1; }
    3254
    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; }
     55static inline forall( dtype T | sized(T) | { void ?{}( T&, T ); void ^?{}( T& ); T& ?+=?( T&, one_t ); } )
     56T& ?++ ( T& x ) { T tmp = x; x += 1; return tmp; }
    3557
    36         forall( dtype DT | { DT & ?-=?( DT &, one_t ); } )
    37         DT & --?( DT & x ) { return x -= 1; }
     58static inline forall( dtype T | { T& ?-=?( T&, one_t ); } )
     59T& --? ( T& x ) { return x -= 1; }
    3860
    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
    42 
    43 // universal typed pointer constant
    44 // Compiler issue: there is a problem with anonymous types that do not have a size.
    45 static inline forall( dtype DT | sized(DT) ) DT * intptr( uintptr_t addr ) { return (DT *)addr; }
     61static inline forall( dtype T | sized(T) | { void ?{}( T&, T ); void ^?{}( T& ); T& ?-=?( T&, one_t ); } )
     62T& ?-- ( T& x ) { T tmp = x; x -= 1; return tmp; }
    4663
    4764// exponentiation operator implementation
     
    5673} // extern "C"
    5774
    58 static 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
     75static inline float ?\?( float x, float y ) { return powf( x, y ); }
     76static inline double ?\?( double x, double y ) { return pow( x, y ); }
     77static inline long double ?\?( long double x, long double y ) { return powl( x, y ); }
     78static inline float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf(x, y ); }
     79static inline double _Complex ?\?( double _Complex x, _Complex double y ) { return cpow( x, y ); }
     80static inline long double _Complex ?\?( long double _Complex x, _Complex long double y ) { return cpowl( x, y ); }
    6681
    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
     82static inline long int ?\?( long int ep, unsigned long int y ) { // disallow negative exponent
     83        if ( y == 0 ) return 1;                                                         // base case
     84        if ( ep == 2 ) return ep << (y - 1);                            // special case, positive shifting only
     85        typeof( ep ) op = 1;                                                            // accumulate odd product
     86        for ( ; y > 1; y >>= 1 ) {                                                      // squaring exponentiation, O(log2 y)
     87                if ( (y & 1) == 1 ) op *= ep;                                   // odd ?
     88                ep *= ep;
     89        } // for
     90        return ep * op;
     91} // ?\?
    7092
    71 #define __CFA_EXP__() \
    72         if ( y == 0 ) return 1;                                                         /* convention */ \
    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 */ \
    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
     93static inline forall( otype T | { void ?{}( T & this, one_t ); T ?*?( T, T ); } )
     94T ?\?( T ep, unsigned long int y ) {
     95        if ( y == 0 ) return 1;
     96        T op = 1;
     97        for ( ; y > 1; y >>= 1 ) {                                                      // squaring exponentiation, O(log2 y)
     98                if ( (y & 1) == 1 ) op = op * ep;                               // odd ?
     99                ep = ep * ep;
     100        } // for
     101        return ep * op;
     102} // ?\?
    82103
    83 static 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
     104// unsigned computation may be faster and larger
     105static inline unsigned long int ?\?( unsigned long int ep, unsigned long int y ) { // disallow negative exponent
     106        if ( y == 0 ) return 1;                                                         // base case
     107        if ( ep == 2 ) return ep << (y - 1);                            // special case, positive shifting only
     108        typeof( ep ) op = 1;                                                            // accumulate odd product
     109        for ( ; y > 1; y >>= 1 ) {                                                      // squaring exponentiation, O(log2 y)
     110                if ( (y & 1) == 1 ) op *= ep;                                   // odd ?
     111                ep *= ep;
     112        } // for
     113        return ep * op;
     114} // ?\?
    90115
    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__()
     116static inline double ?\?( long int x, signed long int y ) {     // allow negative exponent
     117        if ( y >=  0 ) return (double)(x \ (unsigned long int)y);
     118        else return 1.0 / x \ (unsigned int)(-y);
     119} // ?\?
    97120
    98 static 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
     121// FIXME (x \ (unsigned long int)y) relies on X ?\?(T, unsigned long) a function that is neither
     122// defined, nor passed as an assertion parameter. Without user-defined conversions, cannot specify
     123// X as a type that casts to double, yet it doesn't make sense to write functions with that type
     124// signature where X is double.
    102125
    103 #undef __CFA_BASE_COMP_1__
    104 #undef __CFA_BASE_COMP_2__
    105 #undef __CFA_EXP_OVERFLOW__
     126// static inline forall( otype T | { void ?{}( T & this, one_t ); T ?*?( T, T ); double ?/?( double, T ); } )
     127// double ?\?( T x, signed long int y ) {
     128//     if ( y >=  0 ) return (double)(x \ (unsigned long int)y);
     129//     else return 1.0 / x \ (unsigned long int)(-y);
     130// } // ?\?
    106131
    107 static 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
     132static inline long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; }
     133static inline unsigned long int ?\=?( unsigned long int & x, unsigned long int y ) { x = x \ y; return x; }
     134static inline int ?\=?( int & x, unsigned long int y ) { x = x \ y; return x; }
     135static inline unsigned int ?\=?( unsigned int & x, unsigned long int y ) { x = x \ y; return x; }
    113136
    114137// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.