| 1 |
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
// |
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// only: float|double|ldouble * * * |
| 19 |
// expensive: * [1-9] * * |
| 20 |
#include "bits/main.h" |
| 21 |
|
| 22 |
template <typename V> |
| 23 |
void |
| 24 |
test() |
| 25 |
{ |
| 26 |
using int_v = std::experimental::fixed_size_simd<int, V::size()>; |
| 27 |
using T = typename V::value_type; |
| 28 |
#if __GCC_IEC_559 >= 2 || defined __STDC_IEC_559__ |
| 29 |
constexpr auto denorm_min = std::__denorm_min_v<T>; |
| 30 |
#endif |
| 31 |
#if __GCC_IEC_559 >= 2 |
| 32 |
constexpr auto norm_min = std::__norm_min_v<T>; |
| 33 |
#endif |
| 34 |
constexpr auto max = std::__finite_max_v<T>; |
| 35 |
#if defined __STDC_IEC_559__ |
| 36 |
constexpr auto nan = std::__quiet_NaN_v<T>; |
| 37 |
constexpr auto inf = std::__infinity_v<T>; |
| 38 |
#endif |
| 39 |
test_values<V>( |
| 40 |
{0, 0.25, 0.5, 1, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| 41 |
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 32, 31, -0., -0.25, -0.5, -1, |
| 42 |
-3, -4, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16, -17, -18, |
| 43 |
-19, -20, -21, -22, -23, -24, -25, -26, -27, -28, -29, -32, -31, |
| 44 |
#if __GCC_IEC_559 >= 2 |
| 45 |
denorm_min, -denorm_min, norm_min / 2, -norm_min / 2, |
| 46 |
#endif |
| 47 |
max, -max, max * 0.123f, -max * 0.123f}, |
| 48 |
[](const V input) { |
| 49 |
V expectedFraction; |
| 50 |
const int_v expectedExponent([&](auto i) { |
| 51 |
int exp; |
| 52 |
expectedFraction[i] = std::frexp(input[i], &exp); |
| 53 |
return exp; |
| 54 |
}); |
| 55 |
int_v exponent = {}; |
| 56 |
const V fraction = frexp(input, &exponent); |
| 57 |
COMPARE(fraction, expectedFraction) << ", input = " << input |
| 58 |
<< ", delta: " << fraction - expectedFraction; |
| 59 |
COMPARE(exponent, expectedExponent) |
| 60 |
<< "\ninput: " << input << ", fraction: " << fraction; |
| 61 |
}); |
| 62 |
#ifdef __STDC_IEC_559__ |
| 63 |
test_values<V>( |
| 64 |
// If x is a NaN, a NaN is returned, and the value of *exp is unspecified. |
| 65 |
// |
| 66 |
// If x is positive infinity (negative infinity), positive infinity |
| 67 |
// (negative infinity) is returned, and the value of *exp is unspecified. |
| 68 |
// This behavior is only guaranteed with C's Annex F when __STDC_IEC_559__ |
| 69 |
// is defined. |
| 70 |
{nan, inf, -inf, denorm_min, denorm_min * 1.72, -denorm_min, |
| 71 |
-denorm_min * 1.72, 0., -0., 1, -1}, |
| 72 |
[](const V input) { |
| 73 |
const V expectedFraction([&](auto i) { |
| 74 |
int exp; |
| 75 |
return std::frexp(input[i], &exp); |
| 76 |
}); |
| 77 |
int_v exponent = {}; |
| 78 |
const V fraction = frexp(input, &exponent); |
| 79 |
COMPARE(isnan(fraction), isnan(expectedFraction)) |
| 80 |
<< fraction << ", input = " << input |
| 81 |
<< ", delta: " << fraction - expectedFraction; |
| 82 |
COMPARE(isinf(fraction), isinf(expectedFraction)) |
| 83 |
<< fraction << ", input = " << input |
| 84 |
<< ", delta: " << fraction - expectedFraction; |
| 85 |
COMPARE(signbit(fraction), signbit(expectedFraction)) |
| 86 |
<< fraction << ", input = " << input |
| 87 |
<< ", delta: " << fraction - expectedFraction; |
| 88 |
}); |
| 89 |
#endif |
| 90 |
} |