127 lines · c
1//===-- Utility class to test different flavors of hypot ------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIBC_TEST_SRC_MATH_HYPOTTEST_H10#define LLVM_LIBC_TEST_SRC_MATH_HYPOTTEST_H11 12#include "src/__support/FPUtil/FPBits.h"13#include "test/UnitTest/FEnvSafeTest.h"14#include "test/UnitTest/FPMatcher.h"15#include "test/UnitTest/Test.h"16#include "utils/MPFRWrapper/MPFRUtils.h"17 18#include "hdr/math_macros.h"19 20using LIBC_NAMESPACE::Sign;21 22namespace mpfr = LIBC_NAMESPACE::testing::mpfr;23 24template <typename T>25class HypotTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {26private:27 using Func = T (*)(T, T);28 using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;29 30 using StorageType = typename FPBits::StorageType;31 const T nan = FPBits::quiet_nan().get_val();32 const T inf = FPBits::inf().get_val();33 const T neg_inf = FPBits::inf(Sign::NEG).get_val();34 const T zero = FPBits::zero().get_val();35 const T neg_zero = FPBits::zero(Sign::NEG).get_val();36 const T max_normal = FPBits::max_normal().get_val();37 const T min_normal = FPBits::min_normal().get_val();38 const T max_subnormal = FPBits::max_subnormal().get_val();39 const T min_subnormal = FPBits::min_subnormal().get_val();40 41 static constexpr StorageType MAX_NORMAL = FPBits::max_normal().uintval();42 static constexpr StorageType MIN_NORMAL = FPBits::min_normal().uintval();43 static constexpr StorageType MAX_SUBNORMAL =44 FPBits::max_subnormal().uintval();45 static constexpr StorageType MIN_SUBNORMAL =46 FPBits::min_subnormal().uintval();47 48public:49 void test_special_numbers(Func func) {50 constexpr int N = 13;51 const T SpecialInputs[N] = {inf, neg_inf, zero,52 neg_zero, max_normal, min_normal,53 max_subnormal, min_subnormal, -max_normal,54 -min_normal, -max_subnormal, -min_subnormal};55 56 EXPECT_FP_EQ(func(inf, nan), inf);57 EXPECT_FP_EQ(func(nan, neg_inf), inf);58 EXPECT_FP_EQ(func(nan, nan), nan);59 EXPECT_FP_EQ(func(nan, zero), nan);60 EXPECT_FP_EQ(func(neg_zero, nan), nan);61 62 for (int i = 0; i < N; ++i) {63 for (int j = 0; j < N; ++j) {64 mpfr::BinaryInput<T> input{SpecialInputs[i], SpecialInputs[j]};65 EXPECT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Hypot, input,66 func(SpecialInputs[i], SpecialInputs[j]),67 0.5);68 }69 }70 }71 72 void test_subnormal_range(Func func) {73 constexpr StorageType COUNT = 10'001;74 constexpr unsigned SCALE = (sizeof(T) < 4) ? 1 : 4;75 for (unsigned scale = 0; scale < SCALE; ++scale) {76 StorageType max_value = static_cast<StorageType>(MAX_SUBNORMAL << scale);77 StorageType step = (max_value - MIN_SUBNORMAL) / COUNT + 1;78 for (int signs = 0; signs < 4; ++signs) {79 for (StorageType v = MIN_SUBNORMAL, w = max_value;80 v <= max_value && w >= MIN_SUBNORMAL; v += step, w -= step) {81 T x = FPBits(v).get_val(), y = FPBits(w).get_val();82 if (signs % 2 == 1) {83 x = -x;84 }85 if (signs >= 2) {86 y = -y;87 }88 89 mpfr::BinaryInput<T> input{x, y};90 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Hypot, input,91 func(x, y), 0.5);92 }93 }94 }95 }96 97 void test_normal_range(Func func) {98 constexpr StorageType COUNT = 10'001;99 constexpr StorageType STEP = (MAX_NORMAL - MIN_NORMAL) / COUNT;100 for (int signs = 0; signs < 4; ++signs) {101 for (StorageType v = MIN_NORMAL, w = MAX_NORMAL;102 v <= MAX_NORMAL && w >= MIN_NORMAL; v += STEP, w -= STEP) {103 T x = FPBits(v).get_val(), y = FPBits(w).get_val();104 if (signs % 2 == 1) {105 x = -x;106 }107 if (signs >= 2) {108 y = -y;109 }110 111 mpfr::BinaryInput<T> input{x, y};112 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Hypot, input,113 func(x, y), 0.5);114 }115 }116 }117 118 void test_input_list(Func func, int n, const mpfr::BinaryInput<T> *inputs) {119 for (int i = 0; i < n; ++i) {120 ASSERT_MPFR_MATCH_ALL_ROUNDING(mpfr::Operation::Hypot, inputs[i],121 func(inputs[i].x, inputs[i].y), 0.5);122 }123 }124};125 126#endif // LLVM_LIBC_TEST_SRC_MATH_HYPOTTEST_H127