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 "utils/MPFRWrapper/MPFRUtils.h"
14 #include "utils/UnitTest/FPMatcher.h"
15 
16 using FPBits = __llvm_libc::fputil::FPBits<float>;
17 
18 namespace mpfr = __llvm_libc::testing::mpfr;
19 
20 struct LlvmLibcHypotfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
checkLlvmLibcHypotfExhaustiveTest21   bool check(uint32_t start, uint32_t stop,
22              mpfr::RoundingMode rounding) override {
23     // Range of the second input: [2^37, 2^48).
24     constexpr uint32_t Y_START = (37U + 127U) << 23;
25     constexpr uint32_t Y_STOP = (48U + 127U) << 23;
26 
27     mpfr::ForceRoundingMode r(rounding);
28     uint32_t xbits = start;
29     bool result = true;
30     do {
31       float x = float(FPBits(xbits));
32       uint32_t ybits = Y_START;
33       do {
34         float y = float(FPBits(ybits));
35         result &= EXPECT_FP_EQ(__llvm_libc::fputil::hypot(x, y),
36                                __llvm_libc::hypotf(x, y));
37         // Using MPFR will be much slower.
38         // mpfr::BinaryInput<float> input{x, y};
39         // EXPECT_MPFR_MATCH(mpfr::Operation::Hypot, input,
40         // __llvm_libc::hypotf(x, y), 0.5,
41         //                   rounding);
42       } while (ybits++ < Y_STOP);
43     } while (xbits++ < stop);
44     return result;
45   }
46 };
47 
48 // Range of the first input: [2^23, 2^24);
49 static constexpr uint32_t START = (23U + 127U) << 23;
50 static constexpr uint32_t STOP = ((23U + 127U) << 23) + 1;
51 
TEST_F(LlvmLibcHypotfExhaustiveTest,RoundNearestTieToEven)52 TEST_F(LlvmLibcHypotfExhaustiveTest, RoundNearestTieToEven) {
53   test_full_range(START, STOP, mpfr::RoundingMode::Nearest);
54 }
55 
TEST_F(LlvmLibcHypotfExhaustiveTest,RoundUp)56 TEST_F(LlvmLibcHypotfExhaustiveTest, RoundUp) {
57   test_full_range(START, STOP, mpfr::RoundingMode::Upward);
58 }
59 
TEST_F(LlvmLibcHypotfExhaustiveTest,RoundDown)60 TEST_F(LlvmLibcHypotfExhaustiveTest, RoundDown) {
61   test_full_range(START, STOP, mpfr::RoundingMode::Downward);
62 }
63 
TEST_F(LlvmLibcHypotfExhaustiveTest,RoundTowardZero)64 TEST_F(LlvmLibcHypotfExhaustiveTest, RoundTowardZero) {
65   test_full_range(START, STOP, mpfr::RoundingMode::TowardZero);
66 }
67