| 1 |
// { dg-do run } |
| 2 |
// { dg-additional-options "-std=c++20" } |
| 3 |
|
| 4 |
#include <cmath> |
| 5 |
#include <numbers> |
| 6 |
|
| 7 |
#define FP_EQUAL(x,y) (std::abs ((x) - (y)) < 1E-6) |
| 8 |
|
| 9 |
#pragma omp declare target |
| 10 |
template<typename T> bool test_pi () |
| 11 |
{ |
| 12 |
if (!FP_EQUAL (std::sin (std::numbers::pi_v<T>), (T) 0.0)) |
| 13 |
return false; |
| 14 |
if (!FP_EQUAL (std::cos (std::numbers::pi_v<T>), (T) -1.0)) |
| 15 |
return false; |
| 16 |
if (!FP_EQUAL (std::numbers::pi_v<T> * std::numbers::inv_pi_v<T>, (T) 1.0)) |
| 17 |
return false; |
| 18 |
if (!FP_EQUAL (std::numbers::pi_v<T> * std::numbers::inv_sqrtpi_v<T> |
| 19 |
* std::numbers::inv_sqrtpi_v<T>, (T) 1.0)) |
| 20 |
return false; |
| 21 |
return true; |
| 22 |
} |
| 23 |
|
| 24 |
template<typename T> bool test_sqrt () |
| 25 |
{ |
| 26 |
if (!FP_EQUAL (std::numbers::sqrt2_v<T> * std::numbers::sqrt2_v<T>, (T) 2.0)) |
| 27 |
return false; |
| 28 |
if (!FP_EQUAL (std::numbers::sqrt3_v<T> * std::numbers::sqrt3_v<T>, (T) 3.0)) |
| 29 |
return false; |
| 30 |
return true; |
| 31 |
} |
| 32 |
|
| 33 |
template<typename T> bool test_phi () |
| 34 |
{ |
| 35 |
T myphi = ((T) 1.0 + std::sqrt ((T) 5.0)) / (T) 2.0; |
| 36 |
if (!FP_EQUAL (myphi, std::numbers::phi_v<T>)) |
| 37 |
return false; |
| 38 |
return true; |
| 39 |
} |
| 40 |
|
| 41 |
template<typename T> bool test_log () |
| 42 |
{ |
| 43 |
if (!FP_EQUAL (std::log ((T) 2.0), std::numbers::ln2_v<T>)) |
| 44 |
return false; |
| 45 |
if (!FP_EQUAL (std::log ((T) 10.0), std::numbers::ln10_v<T>)) |
| 46 |
return false; |
| 47 |
if (!FP_EQUAL (std::log2 ((T) std::numbers::e), std::numbers::log2e_v<T>)) |
| 48 |
return false; |
| 49 |
if (!FP_EQUAL (std::log10 ((T) std::numbers::e), std::numbers::log10e_v<T>)) |
| 50 |
return false; |
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
template<typename T> bool test_egamma () |
| 55 |
{ |
| 56 |
T myegamma = 0.0; |
| 57 |
#pragma omp parallel for reduction(+:myegamma) |
| 58 |
for (int k = 2; k < 100000; ++k) |
| 59 |
myegamma += (std::riemann_zeta (k) - 1) / k; |
| 60 |
myegamma = (T) 1 - myegamma; |
| 61 |
if (!FP_EQUAL (myegamma, std::numbers::egamma_v<T>)) |
| 62 |
return false; |
| 63 |
return true; |
| 64 |
} |
| 65 |
#pragma omp end declare target |
| 66 |
|
| 67 |
#define RUN_TEST(func) \ |
| 68 |
{ \ |
| 69 |
pass++; \ |
| 70 |
bool ok = test_##func<float> (); \ |
| 71 |
if (!ok) { result = pass; break; } \ |
| 72 |
pass++; \ |
| 73 |
ok = test_##func<double> (); \ |
| 74 |
if (!ok) { result = pass; break; } \ |
| 75 |
} |
| 76 |
|
| 77 |
int main (void) |
| 78 |
{ |
| 79 |
int result = 0; |
| 80 |
|
| 81 |
#pragma omp target map (tofrom: result) |
| 82 |
do { |
| 83 |
int pass = 0; |
| 84 |
|
| 85 |
RUN_TEST (pi); |
| 86 |
RUN_TEST (sqrt); |
| 87 |
RUN_TEST (phi); |
| 88 |
RUN_TEST (log); |
| 89 |
RUN_TEST (egamma); |
| 90 |
} while (false); |
| 91 |
|
| 92 |
return result; |
| 93 |
} |