| 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_basic () |
| 11 |
{ |
| 12 |
T x = -3.456789; |
| 13 |
T y = 1.234567; |
| 14 |
T z = 5.678901; |
| 15 |
|
| 16 |
if (std::abs (x) != -x) |
| 17 |
return false; |
| 18 |
if (!FP_EQUAL (std::trunc (x / y) * y + std::fmod (x, y), x)) |
| 19 |
return false; |
| 20 |
if (!FP_EQUAL (x - std::round (x / y) * y, std::remainder (x, y))) |
| 21 |
return false; |
| 22 |
if (!FP_EQUAL (std::fma (x, y, z), x * y + z)) |
| 23 |
return false; |
| 24 |
if (std::fmax (x, y) != (x > y ? x : y)) |
| 25 |
return false; |
| 26 |
if (std::fmin (x, y) != (x < y ? x : y)) |
| 27 |
return false; |
| 28 |
if (std::fdim (x, y) != std::max(x - y, (T) 0.0)) |
| 29 |
return false; |
| 30 |
if (std::fdim (y, x) != std::max(y - x, (T) 0.0)) |
| 31 |
return false; |
| 32 |
return true; |
| 33 |
} |
| 34 |
|
| 35 |
template<typename T> bool test_exp () |
| 36 |
{ |
| 37 |
T x = -4.567890; |
| 38 |
T y = 2.345678; |
| 39 |
|
| 40 |
if (!FP_EQUAL (std::exp (x), std::pow (std::numbers::e_v<T>, x))) |
| 41 |
return false; |
| 42 |
if (!FP_EQUAL (std::exp2 (y), std::pow ((T) 2.0, y))) |
| 43 |
return false; |
| 44 |
if (!FP_EQUAL (std::expm1 (y), std::exp (y) - (T) 1.0)) |
| 45 |
return false; |
| 46 |
if (!FP_EQUAL (std::log (std::exp (x)), x)) |
| 47 |
return false; |
| 48 |
if (!FP_EQUAL (std::log10 (std::pow ((T) 10.0, y)), y)) |
| 49 |
return false; |
| 50 |
if (!FP_EQUAL (std::log2 (std::exp2 (y)), y)) |
| 51 |
return false; |
| 52 |
if (!FP_EQUAL (std::log1p (std::expm1 (y)), y)) |
| 53 |
return false; |
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
template<typename T> bool test_power () |
| 58 |
{ |
| 59 |
T x = 7.234251; |
| 60 |
T y = 0.340128; |
| 61 |
|
| 62 |
if (!FP_EQUAL (std::log (std::pow (x, y)) / std::log (x), y)) |
| 63 |
return false; |
| 64 |
if (!FP_EQUAL (std::sqrt (x) * std::sqrt (x), x)) |
| 65 |
return false; |
| 66 |
if (!FP_EQUAL (std::cbrt (x) * std::cbrt (x) * std::cbrt (x), x)) |
| 67 |
return false; |
| 68 |
if (!FP_EQUAL (std::hypot (x, y), std::sqrt (x * x + y * y))) |
| 69 |
return false; |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
template<typename T> bool test_trig () |
| 74 |
{ |
| 75 |
T theta = std::numbers::pi / 4; |
| 76 |
T phi = std::numbers::pi / 6; |
| 77 |
|
| 78 |
if (!FP_EQUAL (std::sin (theta), std::sqrt ((T) 2) / 2)) |
| 79 |
return false; |
| 80 |
if (!FP_EQUAL (std::sin (phi), 0.5)) |
| 81 |
return false; |
| 82 |
if (!FP_EQUAL (std::cos (theta), std::sqrt ((T) 2) / 2)) |
| 83 |
return false; |
| 84 |
if (!FP_EQUAL (std::cos (phi), std::sqrt ((T) 3) / 2)) |
| 85 |
return false; |
| 86 |
if (!FP_EQUAL (std::tan (theta), 1.0)) |
| 87 |
return false; |
| 88 |
if (!FP_EQUAL (std::tan (phi), std::sqrt ((T) 3) / 3)) |
| 89 |
return false; |
| 90 |
|
| 91 |
T x = 0.33245623; |
| 92 |
|
| 93 |
if (!FP_EQUAL (std::asin (std::sin (x)), x)) |
| 94 |
return false; |
| 95 |
if (!FP_EQUAL (std::acos (std::cos (x)), x)) |
| 96 |
return false; |
| 97 |
if (!FP_EQUAL (std::atan (std::tan (x)), x)) |
| 98 |
return false; |
| 99 |
if (!FP_EQUAL (std::atan2 (std::sin (x), std::cos (x)), x)) |
| 100 |
return false; |
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
template<typename T> bool test_hyperbolic () |
| 105 |
{ |
| 106 |
T x = 0.7423532; |
| 107 |
|
| 108 |
if (!FP_EQUAL (std::sinh (x), (std::exp (x) - std::exp (-x)) / (T) 2.0)) |
| 109 |
return false; |
| 110 |
if (!FP_EQUAL (std::cosh (x), (std::exp (x) + std::exp (-x)) / (T) 2.0)) |
| 111 |
return false; |
| 112 |
if (!FP_EQUAL (std::tanh (x), std::sinh (x) / std::cosh (x))) |
| 113 |
return false; |
| 114 |
if (!FP_EQUAL (std::asinh (std::sinh (x)), x)) |
| 115 |
return false; |
| 116 |
if (!FP_EQUAL (std::acosh (std::cosh (x)), x)) |
| 117 |
return false; |
| 118 |
if (!FP_EQUAL (std::atanh (std::tanh (x)), x)) |
| 119 |
return false; |
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
template<typename T> bool test_erf () |
| 124 |
{ |
| 125 |
if (!FP_EQUAL (std::erf ((T) 0), 0)) |
| 126 |
return false; |
| 127 |
if (!FP_EQUAL (std::erf ((T) INFINITY), 1)) |
| 128 |
return false; |
| 129 |
if (!FP_EQUAL (std::erf ((T) -INFINITY), -1)) |
| 130 |
return false; |
| 131 |
|
| 132 |
if (!FP_EQUAL (std::erfc (0), 1)) |
| 133 |
return false; |
| 134 |
if (!FP_EQUAL (std::erfc ((T) INFINITY), 0)) |
| 135 |
return false; |
| 136 |
if (!FP_EQUAL (std::erfc ((T) -INFINITY), 2)) |
| 137 |
return false; |
| 138 |
|
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
template<typename T> bool test_gamma () |
| 143 |
{ |
| 144 |
if (!FP_EQUAL (std::tgamma ((T) 5), 4*3*2*1)) |
| 145 |
return false; |
| 146 |
if (!FP_EQUAL (std::tgamma ((T) 0.5), std::sqrt (std::numbers::pi_v<T>))) |
| 147 |
return false; |
| 148 |
if (!FP_EQUAL (std::tgamma ((T) -0.5), (T) -2 * std::sqrt (std::numbers::pi_v<T>))) |
| 149 |
return false; |
| 150 |
if (!FP_EQUAL (std::tgamma ((T) 2.5), (T) 0.75 * std::sqrt (std::numbers::pi_v<T>))) |
| 151 |
return false; |
| 152 |
if (!FP_EQUAL (std::tgamma ((T) -2.5), (T) -8.0/15 * std::sqrt (std::numbers::pi_v<T>))) |
| 153 |
return false; |
| 154 |
|
| 155 |
if (!FP_EQUAL (std::lgamma ((T) 5), std::log ((T) 4*3*2*1))) |
| 156 |
return false; |
| 157 |
if (!FP_EQUAL (std::lgamma ((T) 0.5), std::log (std::sqrt (std::numbers::pi_v<T>)))) |
| 158 |
return false; |
| 159 |
if (!FP_EQUAL (std::lgamma ((T) 2.5), |
| 160 |
std::log ((T) 0.75 * std::sqrt (std::numbers::pi_v<T>)))) |
| 161 |
return false; |
| 162 |
|
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
template<typename T> bool test_rounding () |
| 167 |
{ |
| 168 |
T x = -2.5678; |
| 169 |
T y = 3.6789; |
| 170 |
|
| 171 |
if (std::ceil (x) != -2) |
| 172 |
return false; |
| 173 |
if (std::floor (x) != -3) |
| 174 |
return false; |
| 175 |
if (std::trunc (x) != -2) |
| 176 |
return false; |
| 177 |
if (std::round (x) != -3) |
| 178 |
return false; |
| 179 |
|
| 180 |
if (std::ceil (y) != 4) |
| 181 |
return false; |
| 182 |
if (std::floor (y) != 3) |
| 183 |
return false; |
| 184 |
if (std::trunc (y) != 3) |
| 185 |
return false; |
| 186 |
if (std::round (y) != 4) |
| 187 |
return false; |
| 188 |
|
| 189 |
/* Not testing std::rint and std::nearbyint due to dependence on |
| 190 |
floating-point environment. */ |
| 191 |
|
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
template<typename T> bool test_fpmanip () |
| 196 |
{ |
| 197 |
T x = -2.3456789; |
| 198 |
T y = 3.6789012; |
| 199 |
int exp; |
| 200 |
|
| 201 |
T mantissa = std::frexp (x, &exp); |
| 202 |
if (std::ldexp (mantissa, exp) != x) |
| 203 |
return false; |
| 204 |
if (std::logb (x) + 1 != exp) |
| 205 |
return false; |
| 206 |
if (std::ilogb (x) + 1 != exp) |
| 207 |
return false; |
| 208 |
if (std::scalbn (x, -exp) != mantissa) |
| 209 |
return false; |
| 210 |
|
| 211 |
T next = std::nextafter (x, y); |
| 212 |
if (!(next > x && next < y)) |
| 213 |
return false; |
| 214 |
|
| 215 |
#if 0 |
| 216 |
/* TODO Due to 'std::nexttoward' using 'long double to', this triggers a |
| 217 |
'80-bit-precision floating-point numbers unsupported (mode ‘XF’)' error |
| 218 |
with x86_64 host and nvptx, GCN offload compilers, or |
| 219 |
'128-bit-precision floating-point numbers unsupported (mode ‘TF’)' error |
| 220 |
with powerpc64le host and nvptx offload compiler, for example; |
| 221 |
PR71064 'nvptx offloading: "long double" data type'. |
| 222 |
It ought to work on systems where the host's 'long double' is the same as |
| 223 |
'double' ('DF'): aarch64, for example? */ |
| 224 |
next = std::nexttoward (x, y); |
| 225 |
if (!(next > x && next < y)) |
| 226 |
return false; |
| 227 |
#endif |
| 228 |
|
| 229 |
if (std::copysign (x, y) != std::abs (x)) |
| 230 |
return false; |
| 231 |
if (std::copysign (y, x) != -y) |
| 232 |
return false; |
| 233 |
|
| 234 |
return true; |
| 235 |
} |
| 236 |
|
| 237 |
template<typename T> bool test_classify () |
| 238 |
{ |
| 239 |
T x = -2.3456789; |
| 240 |
T y = 3.6789012; |
| 241 |
|
| 242 |
if (std::fpclassify (x) != FP_NORMAL || std::fpclassify (y) != FP_NORMAL) |
| 243 |
return false; |
| 244 |
if (std::fpclassify ((T) INFINITY) != FP_INFINITE |
| 245 |
|| std::fpclassify ((T) -INFINITY) != FP_INFINITE) |
| 246 |
return false; |
| 247 |
if (std::fpclassify ((T) 0.0) != FP_ZERO) |
| 248 |
return false; |
| 249 |
if (std::fpclassify ((T) NAN) != FP_NAN) |
| 250 |
return false; |
| 251 |
if (!std::isfinite (x) || !std::isfinite (y)) |
| 252 |
return false; |
| 253 |
if (std::isfinite ((T) INFINITY) || std::isfinite ((T) -INFINITY)) |
| 254 |
return false; |
| 255 |
if (std::isinf (x) || std::isinf (y)) |
| 256 |
return false; |
| 257 |
if (!std::isinf ((T) INFINITY) || !std::isinf ((T) -INFINITY)) |
| 258 |
return false; |
| 259 |
if (std::isnan (x) || std::isnan (y)) |
| 260 |
return false; |
| 261 |
if (!std::isnan ((T) 0.0 / (T) 0.0)) |
| 262 |
return false; |
| 263 |
if (std::isnan (x) || std::isnan (y)) |
| 264 |
return false; |
| 265 |
if (!std::isnormal (x) || !std::isnormal (y)) |
| 266 |
return false; |
| 267 |
if (std::isnormal ((T) 0.0) || std::isnormal ((T) INFINITY) || std::isnormal ((T) NAN)) |
| 268 |
return false; |
| 269 |
if (!std::signbit (x) || std::signbit (y)) |
| 270 |
return false; |
| 271 |
|
| 272 |
return true; |
| 273 |
} |
| 274 |
|
| 275 |
template<typename T> bool test_compare () |
| 276 |
{ |
| 277 |
T x = 5.6789012; |
| 278 |
T y = 8.9012345; |
| 279 |
|
| 280 |
if (std::isgreater (x, y)) |
| 281 |
return false; |
| 282 |
if (std::isgreater (x, x)) |
| 283 |
return false; |
| 284 |
if (std::isgreaterequal (x, y)) |
| 285 |
return false; |
| 286 |
if (!std::isgreaterequal (x, x)) |
| 287 |
return false; |
| 288 |
if (!std::isless (x, y)) |
| 289 |
return false; |
| 290 |
if (std::isless (x, x)) |
| 291 |
return false; |
| 292 |
if (!std::islessequal (x, y)) |
| 293 |
return false; |
| 294 |
if (!std::islessequal (x, x)) |
| 295 |
return false; |
| 296 |
if (!std::islessgreater (x, y)) |
| 297 |
return false; |
| 298 |
if (std::islessgreater (x, x)) |
| 299 |
return false; |
| 300 |
if (std::isunordered (x, y)) |
| 301 |
return false; |
| 302 |
if (!std::isunordered (x, NAN)) |
| 303 |
return false; |
| 304 |
return true; |
| 305 |
} |
| 306 |
#pragma omp end declare target |
| 307 |
|
| 308 |
#define RUN_TEST(func) \ |
| 309 |
{ \ |
| 310 |
pass++; \ |
| 311 |
bool ok = test_##func<float> (); \ |
| 312 |
if (!ok) { result = pass; break; } \ |
| 313 |
pass++; \ |
| 314 |
ok = test_##func<double> (); \ |
| 315 |
if (!ok) { result = pass; break; } \ |
| 316 |
} |
| 317 |
|
| 318 |
int main (void) |
| 319 |
{ |
| 320 |
int result = 0; |
| 321 |
|
| 322 |
#pragma omp target map (tofrom: result) |
| 323 |
do { |
| 324 |
int pass = 0; |
| 325 |
|
| 326 |
RUN_TEST (basic); |
| 327 |
RUN_TEST (exp); |
| 328 |
RUN_TEST (power); |
| 329 |
RUN_TEST (trig); |
| 330 |
RUN_TEST (hyperbolic); |
| 331 |
RUN_TEST (erf); |
| 332 |
RUN_TEST (gamma); |
| 333 |
RUN_TEST (rounding); |
| 334 |
RUN_TEST (fpmanip); |
| 335 |
RUN_TEST (classify); |
| 336 |
RUN_TEST (compare); |
| 337 |
} while (false); |
| 338 |
|
| 339 |
return result; |
| 340 |
} |