| 1 | //===-- Exhaustive test for hypotf16 --------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "exhaustive_test.h" |
| 10 | #include "src/__support/FPUtil/FPBits.h" |
| 11 | #include "src/__support/FPUtil/Hypot.h" |
| 12 | #include "src/math/hypotf16.h" |
| 13 | #include "test/UnitTest/FPMatcher.h" |
| 14 | #include "utils/MPFRWrapper/MPFRUtils.h" |
| 15 | |
| 16 | namespace mpfr = LIBC_NAMESPACE::testing::mpfr; |
| 17 | |
| 18 | struct Hypotf16Checker : public virtual LIBC_NAMESPACE::testing::Test { |
| 19 | using FloatType = float16; |
| 20 | using FPBits = LIBC_NAMESPACE::fputil::FPBits<float16>; |
| 21 | using StorageType = typename FPBits::StorageType; |
| 22 | |
| 23 | uint64_t check(uint16_t x_start, uint16_t x_stop, uint16_t y_start, |
| 24 | uint16_t y_stop, mpfr::RoundingMode rounding) { |
| 25 | mpfr::ForceRoundingMode r(rounding); |
| 26 | if (!r.success) |
| 27 | return true; |
| 28 | uint16_t xbits = x_start; |
| 29 | uint64_t failed = 0; |
| 30 | do { |
| 31 | float16 x = FPBits(xbits).get_val(); |
| 32 | uint16_t ybits = xbits; |
| 33 | do { |
| 34 | float16 y = FPBits(ybits).get_val(); |
| 35 | bool correct = TEST_FP_EQ(LIBC_NAMESPACE::fputil::hypot<float16>(x, y), |
| 36 | LIBC_NAMESPACE::hypotf16(x, y)); |
| 37 | // Using MPFR will be much slower. |
| 38 | // mpfr::BinaryInput<float16> input{x, y}; |
| 39 | // bool correct = TEST_MPFR_MATCH_ROUNDING_SILENTLY( |
| 40 | // mpfr::Operation::Hypot, input, LIBC_NAMESPACE::hypotf16(x, y), |
| 41 | // 0.5, |
| 42 | // rounding); |
| 43 | failed += (!correct); |
| 44 | } while (ybits++ < y_stop); |
| 45 | } while (xbits++ < x_stop); |
| 46 | return failed; |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | using LlvmLibcHypotf16ExhaustiveTest = |
| 51 | LlvmLibcExhaustiveMathTest<Hypotf16Checker, 1 << 2>; |
| 52 | |
| 53 | // Range of both inputs: [0, inf] |
| 54 | static constexpr uint16_t POS_START = 0x0000U; |
| 55 | static constexpr uint16_t POS_STOP = 0x7C00U; |
| 56 | |
| 57 | TEST_F(LlvmLibcHypotf16ExhaustiveTest, PositiveRange) { |
| 58 | test_full_range_all_roundings(POS_START, POS_STOP, POS_START, POS_STOP); |
| 59 | } |
| 60 | |
| 61 | // Range of both inputs: [-0, -inf] |
| 62 | static constexpr uint16_t NEG_START = 0x8000U; |
| 63 | static constexpr uint16_t NEG_STOP = 0xFC00U; |
| 64 | |
| 65 | TEST_F(LlvmLibcHypotf16ExhaustiveTest, NegativeRange) { |
| 66 | test_full_range_all_roundings(NEG_START, NEG_STOP, NEG_START, NEG_STOP); |
| 67 | } |
| 68 | |