1 //===-- Unittests 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 "include/math.h" 10 #include "src/math/hypotf.h" 11 #include "utils/FPUtil/FPBits.h" 12 #include "utils/FPUtil/TestHelpers.h" 13 #include "utils/MPFRWrapper/MPFRUtils.h" 14 #include "utils/UnitTest/Test.h" 15 16 using FPBits = __llvm_libc::fputil::FPBits<float>; 17 using UIntType = FPBits::UIntType; 18 19 namespace mpfr = __llvm_libc::testing::mpfr; 20 21 static const float zero = FPBits::zero(); 22 static const float negZero = FPBits::negZero(); 23 static const float nan = FPBits::buildNaN(1); 24 static const float inf = FPBits::inf(); 25 static const float negInf = FPBits::negInf(); 26 27 TEST(HypotfTest, SpecialNumbers) { 28 EXPECT_FP_EQ(__llvm_libc::hypotf(inf, nan), inf); 29 EXPECT_FP_EQ(__llvm_libc::hypotf(nan, negInf), inf); 30 EXPECT_FP_EQ(__llvm_libc::hypotf(zero, inf), inf); 31 EXPECT_FP_EQ(__llvm_libc::hypotf(negInf, negZero), inf); 32 33 EXPECT_FP_EQ(__llvm_libc::hypotf(nan, nan), nan); 34 EXPECT_FP_EQ(__llvm_libc::hypotf(nan, zero), nan); 35 EXPECT_FP_EQ(__llvm_libc::hypotf(negZero, nan), nan); 36 37 EXPECT_FP_EQ(__llvm_libc::hypotf(negZero, zero), zero); 38 } 39 40 TEST(HypotfTest, SubnormalRange) { 41 constexpr UIntType count = 1000001; 42 constexpr UIntType step = 43 (FPBits::maxSubnormal - FPBits::minSubnormal) / count; 44 for (UIntType v = FPBits::minSubnormal, w = FPBits::maxSubnormal; 45 v <= FPBits::maxSubnormal && w >= FPBits::minSubnormal; 46 v += step, w -= step) { 47 float x = FPBits(v), y = FPBits(w); 48 float result = __llvm_libc::hypotf(x, y); 49 mpfr::BinaryInput<float> input{x, y}; 50 ASSERT_MPFR_MATCH(mpfr::Operation::Hypot, input, result, 0.5); 51 } 52 } 53 54 TEST(HypotfTest, NormalRange) { 55 constexpr UIntType count = 1000001; 56 constexpr UIntType step = (FPBits::maxNormal - FPBits::minNormal) / count; 57 for (UIntType v = FPBits::minNormal, w = FPBits::maxNormal; 58 v <= FPBits::maxNormal && w >= FPBits::minNormal; v += step, w -= step) { 59 float x = FPBits(v), y = FPBits(w); 60 float result = __llvm_libc::hypotf(x, y); 61 ; 62 mpfr::BinaryInput<float> input{x, y}; 63 ASSERT_MPFR_MATCH(mpfr::Operation::Hypot, input, result, 0.5); 64 } 65 } 66