1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 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 | // math.cpp -- |
---|
8 | // |
---|
9 | // Author : Andrew Beach |
---|
10 | // Created On : Mon Nov 25 16:20:00 2024 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Created On : Mon Nov 27 15:11:00 2024 |
---|
13 | // Update Count : 0 |
---|
14 | // |
---|
15 | |
---|
16 | #include "math.hfa" |
---|
17 | |
---|
18 | #include <limits.h> |
---|
19 | |
---|
20 | #pragma GCC visibility push(default) |
---|
21 | |
---|
22 | // Implementation of power functions (from the prelude): |
---|
23 | |
---|
24 | #define __CFA_EXP__() \ |
---|
25 | if ( y == 0 ) return 1; /* convention */ \ |
---|
26 | __CFA_EXP_INT__( /* special cases for integral types */ \ |
---|
27 | if ( x == 1 ) return 1; /* base case */ \ |
---|
28 | if ( x == 2 ) return x << (y - 1); /* positive shifting */ \ |
---|
29 | if ( y >= sizeof(y) * CHAR_BIT ) return 0; /* immediate overflow, negative exponent > 2^size-1 */ \ |
---|
30 | ) \ |
---|
31 | typeof(x) op = 1; /* accumulate odd product */ \ |
---|
32 | typeof(x) w = x; /* FIX-ME: possible bug in the box pass changing value argument through parameter */ \ |
---|
33 | for ( ; y > 1; y >>= 1 ) { /* squaring exponentiation, O(log2 y) */ \ |
---|
34 | if ( (y & 1) == 1 ) op = op * w; /* odd ? */ \ |
---|
35 | w = w * w; \ |
---|
36 | } \ |
---|
37 | return w * op |
---|
38 | #define __CFA_EXP_INT__(...) __VA_ARGS__ |
---|
39 | |
---|
40 | int ?\?( int x, unsigned int y ) { __CFA_EXP__(); } |
---|
41 | long int ?\?( long int x, unsigned long int y ) { __CFA_EXP__(); } |
---|
42 | long long int ?\?( long long int x, unsigned long long int y ) { __CFA_EXP__(); } |
---|
43 | unsigned int ?\?( unsigned int x, unsigned int y ) { __CFA_EXP__(); } |
---|
44 | unsigned long int ?\?( unsigned long int x, unsigned long int y ) { __CFA_EXP__(); } |
---|
45 | unsigned long long int ?\?( unsigned long long int x, unsigned long long int y ) { __CFA_EXP__(); } |
---|
46 | |
---|
47 | #undef __CFA_EXP_INT__ |
---|
48 | #define __CFA_EXP_INT__(...) |
---|
49 | |
---|
50 | forall( OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } ) { |
---|
51 | OT ?\?( OT x, unsigned int y ) { __CFA_EXP__(); } |
---|
52 | OT ?\?( OT x, unsigned long int y ) { __CFA_EXP__(); } |
---|
53 | OT ?\?( OT x, unsigned long long int y ) { __CFA_EXP__(); } |
---|
54 | } // distribution |
---|
55 | |
---|
56 | #undef __CFA_EXP_INT__ |
---|
57 | #undef __CFA_EXP__ |
---|