source: src/prelude/builtins.c@ 764db43

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 764db43 was fe97a7d8, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

updates to exponential operator

  • Property mode set to 100644
File size: 3.7 KB
RevLine 
[a1edafa]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 : Peter A. Buhr
[fe97a7d8]12// Last Modified On : Sat Jul 22 10:34:20 2017
13// Update Count : 13
[a1edafa]14//
15
16// exception implementation
17
[fa4805f]18typedef unsigned long long __cfaabi_exception_type_t;
19
20#include "../libcfa/exception.h"
[a1edafa]21
22// exponentiation operator implementation
23
24extern "C" {
25 float powf( float x, float y );
26 double pow( double x, double y );
27 long double powl( long double x, long double y );
28 float _Complex cpowf( float _Complex x, _Complex float z );
29 double _Complex cpow( double _Complex x, _Complex double z );
30 long double _Complex cpowl( long double _Complex x, _Complex long double z );
31} // extern "C"
32
33static inline float ?\?( float x, float y ) { return powf( x, y ); }
34static inline double ?\?( double x, double y ) { return pow( x, y ); }
35static inline long double ?\?( long double x, long double y ) { return powl( x, y ); }
36static inline float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf(x, y ); }
37static inline double _Complex ?\?( double _Complex x, _Complex double y ) { return cpow( x, y ); }
38static inline long double _Complex ?\?( long double _Complex x, _Complex long double y ) { return cpowl( x, y ); }
39
[fe97a7d8]40static inline long int ?\?( long int ep, unsigned long int y ) { // disallow negative exponent
[a1edafa]41 if ( y == 0 ) return 1; // base case
[fe97a7d8]42 if ( ep == 2 ) return ep << (y - 1); // special case, positive shifting only
43 typeof( ep ) op = 1; // accumulate odd product
[a1edafa]44 for ( ; y > 1; y >>= 1 ) { // squaring exponentiation, O(log2 y)
[fe97a7d8]45 if ( (y & 1) == 1 ) op *= ep; // odd ?
46 ep *= ep;
47 } // for
48 return ep * op;
[a1edafa]49} // ?\?
50
[fe97a7d8]51// FIX ME, cannot resolve the "T op = 1".
[a1edafa]52
53// static inline forall( otype T | { void ?{}( T * this, one_t ); T ?*?( T, T ); } )
[fe97a7d8]54// T ?\?( T ep, unsigned long int y ) {
[a1edafa]55// if ( y == 0 ) return 1;
[fe97a7d8]56// T op = 1;
[a1edafa]57// for ( ; y > 1; y >>= 1 ) { // squaring exponentiation, O(log2 y)
[fe97a7d8]58// if ( (y & 1) == 1 ) op = op * ep; // odd ?
59// ep = ep * ep;
[a1edafa]60// } // for
[fe97a7d8]61// return ep * op;
[a1edafa]62// } // ?\?
63
64// unsigned computation may be faster and larger
[fe97a7d8]65static inline unsigned long int ?\?( unsigned long int ep, unsigned long int y ) { // disallow negative exponent
[a1edafa]66 if ( y == 0 ) return 1; // base case
[fe97a7d8]67 if ( ep == 2 ) return ep << (y - 1); // special case, positive shifting only
68 typeof( ep ) op = 1; // accumulate odd product
[a1edafa]69 for ( ; y > 1; y >>= 1 ) { // squaring exponentiation, O(log2 y)
[fe97a7d8]70 if ( (y & 1) == 1 ) op *= ep; // odd ?
71 ep *= ep;
72 } // for
73 return ep * op;
[a1edafa]74} // ?\?
75
[fe97a7d8]76static inline double ?\?( long int x, signed long int y ) { // allow negative exponent
[a1edafa]77 if ( y >= 0 ) return (double)(x \ (unsigned long int)y);
78 else return 1.0 / x \ (unsigned int)(-y);
79} // ?\?
80
81static inline forall( otype T | { void ?{}( T * this, one_t ); T ?*?( T, T ); double ?/?( double, T ); } )
[fe97a7d8]82double ?\?( T x, signed long int y ) {
[a1edafa]83 if ( y >= 0 ) return (double)(x \ (unsigned long int)y);
[fe97a7d8]84 else return 1.0 / x \ (unsigned long int)(-y);
[a1edafa]85} // ?\?
86
[fe97a7d8]87static inline long int ?\=?( long int * x, unsigned long int y ) { *x = *x \ y; return *x; }
88static inline unsigned long int ?\=?( unsigned long int * x, unsigned long int y ) { *x = *x \ y; return *x; }
89static inline int ?\=?( int * x, unsigned long int y ) { *x = *x \ y; return *x; }
90static inline unsigned int ?\=?( unsigned int * x, unsigned long int y ) { *x = *x \ y; return *x; }
[a1edafa]91
92// Local Variables: //
93// mode: c //
94// tab-width: 4 //
95// End: //
Note: See TracBrowser for help on using the repository browser.