Changeset 8dbfb7e


Ignore:
Timestamp:
Mar 26, 2019, 5:07:10 PM (5 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
363b48f
Parents:
ae6b6cf
Message:

update exponential operator, formatting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/prelude/builtins.c

    rae6b6cf r8dbfb7e  
    1010// Created On       : Fri Jul 21 16:21:03 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Mar 10 10:52:50 2019
    13 // Update Count     : 31
     12// Last Modified On : Tue Mar 26 14:05:49 2019
     13// Update Count     : 84
    1414//
    1515
     
    2626// increment/decrement unification
    2727
    28 static inline forall( dtype T | { T & ?+=?( T &, one_t ); } )
    29 T & ++? ( T & x ) { return x += 1; }
     28static inline forall( dtype DT | { DT & ?+=?( DT &, one_t ); } )
     29DT & ++?( DT & x ) { return x += 1; }
    3030
    31 static inline forall( dtype T | sized(T) | { void ?{}( T &, T ); void ^?{}( T & ); T & ?+=?( T &, one_t ); } )
    32 T & ?++ ( T & x ) { T tmp = x; x += 1; return tmp; }
     31static inline forall( dtype DT | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?+=?( DT &, one_t ); } )
     32DT & ?++( DT & x ) { DT tmp = x; x += 1; return tmp; }
    3333
    34 static inline forall( dtype T | { T & ?-=?( T &, one_t ); } )
    35 T & --? ( T & x ) { return x -= 1; }
     34static inline forall( dtype DT | { DT & ?-=?( DT &, one_t ); } )
     35DT & --?( DT & x ) { return x -= 1; }
    3636
    37 static inline forall( dtype T | sized(T) | { void ?{}( T &, T ); void ^?{}( T & ); T & ?-=?( T &, one_t ); } )
    38 T & ?-- ( T & x ) { T tmp = x; x -= 1; return tmp; }
     37static inline forall( dtype DT | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?-=?( DT &, one_t ); } )
     38DT & ?--( DT & x ) { DT tmp = x; x -= 1; return tmp; }
    3939
    4040// universal typed pointer constant
    4141
    42 static inline forall( dtype T ) T * intptr( uintptr_t addr ) { return (T *)addr; }
     42// Compiler issue: there is a problem with anonymous types that do not have  a size.
     43static inline forall( dtype DT | sized(DT) ) DT * intptr( uintptr_t addr ) { return (DT *)addr; }
    4344
    4445// exponentiation operator implementation
     
    6061static inline long double _Complex ?\?( long double _Complex x, _Complex long double y ) { return cpowl( x, y ); }
    6162
    62 static inline long int ?\?( long int ep, unsigned long int y ) { // disallow negative exponent
    63         if ( y == 0 ) return 1;                                                         // base case
    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)
    67                 if ( (y & 1) == 1 ) op *= ep;                                   // odd ?
    68                 ep *= ep;
    69         } // for
    70         return ep * op;
     63#define __CFA_EXP__() \
     64        if ( y == 0 ) return 1;                                                         /* base case */ \
     65        __CFA_BASE_COMP_1__()                                                           /* base case */ \
     66        __CFA_BASE_COMP_2__()                                                           /* special case, positive shifting for integral types */ \
     67        __CFA_EXP_OVERFLOW__()                                                          /* immediate overflow, negative exponent > 2^size-1 */ \
     68        typeof(ep) op = 1;                                                                      /* accumulate odd product */ \
     69        for ( ; y > 1; y >>= 1 ) {                                                      /* squaring exponentiation, O(log2 y) */ \
     70                if ( (y & 1) == 1 ) op = op * ep;                               /* odd ? */ \
     71                ep = ep * ep; \
     72        } \
     73        return ep * op
     74
     75#define __CFA_BASE_COMP_1__() if ( ep == 1 ) return 1;
     76#define __CFA_BASE_COMP_2__() if ( ep == 2 ) return ep << (y - 1);
     77#define __CFA_EXP_OVERFLOW__() if ( y >= sizeof(y) * 8 ) return 0;
     78
     79static inline long int ?\?( int ep, unsigned int y ) {
     80        __CFA_EXP__();
    7181} // ?\?
    7282
    73 static inline forall( otype T | { void ?{}( T & this, one_t ); T ?*?( T, T ); } )
    74 T ?\?( 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;
     83static inline long int ?\?( long int ep, unsigned long int y ) {
     84        __CFA_EXP__();
    8285} // ?\?
    8386
    8487// unsigned computation may be faster and larger
    85 static inline unsigned long int ?\?( unsigned long int ep, unsigned long int y ) { // disallow negative exponent
    86         if ( y == 0 ) return 1;                                                         // base case
    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)
    90                 if ( (y & 1) == 1 ) op *= ep;                                   // odd ?
    91                 ep *= ep;
    92         } // for
    93         return ep * op;
     88static inline unsigned long int ?\?( unsigned int ep, unsigned int y ) {
     89        __CFA_EXP__();
    9490} // ?\?
    9591
    96 static inline double ?\?( long int x, signed long int y ) {     // allow negative exponent
    97         if ( y >=  0 ) return (double)(x \ (unsigned long int)y);
    98         else return 1.0 / x \ (unsigned int)(-y);
     92static inline unsigned long int ?\?( unsigned long int ep, unsigned long int y ) {
     93        __CFA_EXP__();
    9994} // ?\?
    10095
    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
    104 // signature where X is double.
     96#undef __CFA_BASE_COMP_1__
     97#undef __CFA_BASE_COMP_2__
     98#undef __CFA_EXP_OVERFLOW__
     99#define __CFA_BASE_COMP_1__()
     100#define __CFA_BASE_COMP_2__()
     101#define __CFA_EXP_OVERFLOW__()
    105102
    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 // } // ?\?
     103static inline forall( otype OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } )
     104OT ?\?( OT ep, unsigned int y ) {
     105        __CFA_EXP__();
     106} // ?\?
     107
     108static inline forall( otype OT | { void ?{}( OT & this, zero_t ); void ?{}( OT & this, one_t ); int ?==?( OT, OT ); OT ?*?( OT, OT ); } )
     109OT ?\?( OT ep, unsigned long int y ) {
     110        __CFA_EXP__();
     111} // ?\?
     112
     113#undef __CFA_BASE_COMP_1__
     114#undef __CFA_BASE_COMP_2__
     115#undef __CFA_EXP_OVERFLOW__
    111116
    112117static inline long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; }
Note: See TracChangeset for help on using the changeset viewer.