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> { 21 void 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 do { 30 float x = float(FPBits(xbits)); 31 uint32_t ybits = Y_START; 32 do { 33 float y = float(FPBits(ybits)); 34 EXPECT_FP_EQ(__llvm_libc::fputil::hypot(x, y), 35 __llvm_libc::hypotf(x, y)); 36 // Using MPFR will be much slower. 37 // mpfr::BinaryInput<float> input{x, y}; 38 // EXPECT_MPFR_MATCH(mpfr::Operation::Hypot, input, 39 // __llvm_libc::hypotf(x, y), 0.5, 40 // rounding); 41 } while (ybits++ < Y_STOP); 42 } while (xbits++ < stop); 43 } 44 }; 45 46 // Range of the first input: [2^23, 2^24); 47 static constexpr uint32_t START = (23U + 127U) << 23; 48 static constexpr uint32_t STOP = ((23U + 127U) << 23) + 1; 49 static constexpr int NUM_THREADS = 1; 50 51 TEST_F(LlvmLibcHypotfExhaustiveTest, RoundNearestTieToEven) { 52 test_full_range(START, STOP, NUM_THREADS, mpfr::RoundingMode::Nearest); 53 } 54 55 TEST_F(LlvmLibcHypotfExhaustiveTest, RoundUp) { 56 test_full_range(START, STOP, NUM_THREADS, mpfr::RoundingMode::Upward); 57 } 58 59 TEST_F(LlvmLibcHypotfExhaustiveTest, RoundDown) { 60 test_full_range(START, STOP, NUM_THREADS, mpfr::RoundingMode::Downward); 61 } 62 63 TEST_F(LlvmLibcHypotfExhaustiveTest, RoundTowardZero) { 64 test_full_range(START, STOP, NUM_THREADS, mpfr::RoundingMode::TowardZero); 65 } 66