xref: /aosp_15_r20/external/llvm-libc/test/src/math/exhaustive/hypotf_test.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Exhaustive test for hypotf ----------------------------------------===//
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/hypotf.h"
13 #include "test/UnitTest/FPMatcher.h"
14 #include "utils/MPFRWrapper/MPFRUtils.h"
15 
16 namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
17 
18 struct HypotfChecker : public virtual LIBC_NAMESPACE::testing::Test {
19   using FloatType = float;
20   using FPBits = LIBC_NAMESPACE::fputil::FPBits<float>;
21   using StorageType = typename FPBits::StorageType;
22 
checkHypotfChecker23   uint64_t check(uint32_t start, uint32_t stop, mpfr::RoundingMode rounding) {
24     // Range of the second input: [2^37, 2^48).
25     constexpr uint32_t Y_START = (37U + 127U) << 23;
26     constexpr uint32_t Y_STOP = (48U + 127U) << 23;
27 
28     mpfr::ForceRoundingMode r(rounding);
29     if (!r.success)
30       return true;
31     uint32_t xbits = start;
32     uint64_t failed = 0;
33     do {
34       float x = FPBits(xbits).get_val();
35       uint32_t ybits = Y_START;
36       do {
37         float y = FPBits(ybits).get_val();
38         bool correct = TEST_FP_EQ(LIBC_NAMESPACE::fputil::hypot(x, y),
39                                   LIBC_NAMESPACE::hypotf(x, y));
40         // Using MPFR will be much slower.
41         // mpfr::BinaryInput<float> input{x, y};
42         // bool correct = TEST_MPFR_MATCH_ROUNDING_SILENTLY(
43         //     mpfr::Operation::Hypot, input, LIBC_NAMESPACE::hypotf(x, y), 0.5,
44         //     rounding);
45         failed += (!correct);
46       } while (ybits++ < Y_STOP);
47     } while (xbits++ < stop);
48     return failed;
49   }
50 };
51 
52 using LlvmLibcHypotfExhaustiveTest = LlvmLibcExhaustiveMathTest<HypotfChecker>;
53 
54 // Range of the first input: [2^23, 2^24);
55 static constexpr uint32_t START = (23U + 127U) << 23;
56 static constexpr uint32_t STOP = ((23U + 127U) << 23) + 1;
57 
TEST_F(LlvmLibcHypotfExhaustiveTest,PositiveRange)58 TEST_F(LlvmLibcHypotfExhaustiveTest, PositiveRange) {
59   test_full_range_all_roundings(START, STOP);
60 }
61