1*f1ec99f9STue Ly //===-- Exhaustive test for hypotf ----------------------------------------===//
2*f1ec99f9STue Ly //
3*f1ec99f9STue Ly // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*f1ec99f9STue Ly // See https://llvm.org/LICENSE.txt for license information.
5*f1ec99f9STue Ly // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*f1ec99f9STue Ly //
7*f1ec99f9STue Ly //===----------------------------------------------------------------------===//
8*f1ec99f9STue Ly 
9*f1ec99f9STue Ly #include "exhaustive_test.h"
10*f1ec99f9STue Ly #include "src/__support/FPUtil/FPBits.h"
11*f1ec99f9STue Ly #include "src/__support/FPUtil/Hypot.h"
12*f1ec99f9STue Ly #include "src/math/hypotf.h"
13*f1ec99f9STue Ly #include "utils/MPFRWrapper/MPFRUtils.h"
14*f1ec99f9STue Ly #include "utils/UnitTest/FPMatcher.h"
15*f1ec99f9STue Ly 
16*f1ec99f9STue Ly using FPBits = __llvm_libc::fputil::FPBits<float>;
17*f1ec99f9STue Ly 
18*f1ec99f9STue Ly namespace mpfr = __llvm_libc::testing::mpfr;
19*f1ec99f9STue Ly 
20*f1ec99f9STue Ly struct LlvmLibcHypotfExhaustiveTest : public LlvmLibcExhaustiveTest<uint32_t> {
21*f1ec99f9STue Ly   void check(uint32_t start, uint32_t stop,
22*f1ec99f9STue Ly              mpfr::RoundingMode rounding) override {
23*f1ec99f9STue Ly     // Range of the second input: [2^37, 2^48).
24*f1ec99f9STue Ly     constexpr uint32_t Y_START = (37U + 127U) << 23;
25*f1ec99f9STue Ly     constexpr uint32_t Y_STOP = (48U + 127U) << 23;
26*f1ec99f9STue Ly 
27*f1ec99f9STue Ly     mpfr::ForceRoundingMode r(rounding);
28*f1ec99f9STue Ly     uint32_t xbits = start;
29*f1ec99f9STue Ly     do {
30*f1ec99f9STue Ly       float x = float(FPBits(xbits));
31*f1ec99f9STue Ly       uint32_t ybits = Y_START;
32*f1ec99f9STue Ly       do {
33*f1ec99f9STue Ly         float y = float(FPBits(ybits));
34*f1ec99f9STue Ly         EXPECT_FP_EQ(__llvm_libc::fputil::hypot(x, y),
35*f1ec99f9STue Ly                      __llvm_libc::hypotf(x, y));
36*f1ec99f9STue Ly         // Using MPFR will be much slower.
37*f1ec99f9STue Ly         // mpfr::BinaryInput<float> input{x, y};
38*f1ec99f9STue Ly         // EXPECT_MPFR_MATCH(mpfr::Operation::Hypot, input,
39*f1ec99f9STue Ly         // __llvm_libc::hypotf(x, y), 0.5,
40*f1ec99f9STue Ly         //                   rounding);
41*f1ec99f9STue Ly       } while (ybits++ < Y_STOP);
42*f1ec99f9STue Ly     } while (xbits++ < stop);
43*f1ec99f9STue Ly   }
44*f1ec99f9STue Ly };
45*f1ec99f9STue Ly 
46*f1ec99f9STue Ly // Range of the first input: [2^23, 2^24);
47*f1ec99f9STue Ly static constexpr uint32_t START = (23U + 127U) << 23;
48*f1ec99f9STue Ly static constexpr uint32_t STOP = ((23U + 127U) << 23) + 1;
49*f1ec99f9STue Ly static constexpr int NUM_THREADS = 1;
50*f1ec99f9STue Ly 
51*f1ec99f9STue Ly TEST_F(LlvmLibcHypotfExhaustiveTest, RoundNearestTieToEven) {
52*f1ec99f9STue Ly   test_full_range(START, STOP, NUM_THREADS, mpfr::RoundingMode::Nearest);
53*f1ec99f9STue Ly }
54*f1ec99f9STue Ly 
55*f1ec99f9STue Ly TEST_F(LlvmLibcHypotfExhaustiveTest, RoundUp) {
56*f1ec99f9STue Ly   test_full_range(START, STOP, NUM_THREADS, mpfr::RoundingMode::Upward);
57*f1ec99f9STue Ly }
58*f1ec99f9STue Ly 
59*f1ec99f9STue Ly TEST_F(LlvmLibcHypotfExhaustiveTest, RoundDown) {
60*f1ec99f9STue Ly   test_full_range(START, STOP, NUM_THREADS, mpfr::RoundingMode::Downward);
61*f1ec99f9STue Ly }
62*f1ec99f9STue Ly 
63*f1ec99f9STue Ly TEST_F(LlvmLibcHypotfExhaustiveTest, RoundTowardZero) {
64*f1ec99f9STue Ly   test_full_range(START, STOP, NUM_THREADS, mpfr::RoundingMode::TowardZero);
65*f1ec99f9STue Ly }
66