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 DECLARE_SPECIAL_CONSTANTS(float)
22 
23 TEST(HypotfTest, SpecialNumbers) {
24   EXPECT_FP_EQ(__llvm_libc::hypotf(inf, nan), inf);
25   EXPECT_FP_EQ(__llvm_libc::hypotf(nan, negInf), inf);
26   EXPECT_FP_EQ(__llvm_libc::hypotf(zero, inf), inf);
27   EXPECT_FP_EQ(__llvm_libc::hypotf(negInf, negZero), inf);
28 
29   EXPECT_FP_EQ(__llvm_libc::hypotf(nan, nan), nan);
30   EXPECT_FP_EQ(__llvm_libc::hypotf(nan, zero), nan);
31   EXPECT_FP_EQ(__llvm_libc::hypotf(negZero, nan), nan);
32 
33   EXPECT_FP_EQ(__llvm_libc::hypotf(negZero, zero), zero);
34 }
35 
36 TEST(HypotfTest, SubnormalRange) {
37   constexpr UIntType count = 1000001;
38   constexpr UIntType step =
39       (FPBits::maxSubnormal - FPBits::minSubnormal) / count;
40   for (UIntType v = FPBits::minSubnormal, w = FPBits::maxSubnormal;
41        v <= FPBits::maxSubnormal && w >= FPBits::minSubnormal;
42        v += step, w -= step) {
43     float x = FPBits(v), y = FPBits(w);
44     float result = __llvm_libc::hypotf(x, y);
45     mpfr::BinaryInput<float> input{x, y};
46     ASSERT_MPFR_MATCH(mpfr::Operation::Hypot, input, result, 0.5);
47   }
48 }
49 
50 TEST(HypotfTest, NormalRange) {
51   constexpr UIntType count = 1000001;
52   constexpr UIntType step = (FPBits::maxNormal - FPBits::minNormal) / count;
53   for (UIntType v = FPBits::minNormal, w = FPBits::maxNormal;
54        v <= FPBits::maxNormal && w >= FPBits::minNormal; v += step, w -= step) {
55     float x = FPBits(v), y = FPBits(w);
56     float result = __llvm_libc::hypotf(x, y);
57     ;
58     mpfr::BinaryInput<float> input{x, y};
59     ASSERT_MPFR_MATCH(mpfr::Operation::Hypot, input, result, 0.5);
60   }
61 }
62