Skip to content

Commit 44a2589

Browse files
authored
[libc][math] Add MPFR unit tests for nearbyint{,f,l,f16} (#94479)
1 parent de68294 commit 44a2589

File tree

6 files changed

+230
-0
lines changed

6 files changed

+230
-0
lines changed

libc/test/src/math/CMakeLists.txt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,66 @@ add_fp_unittest(
586586
libc.src.__support.FPUtil.fp_bits
587587
)
588588

589+
add_fp_unittest(
590+
nearbyint_test
591+
NEED_MPFR
592+
SUITE
593+
libc-math-unittests
594+
SRCS
595+
nearbyint_test.cpp
596+
HDRS
597+
NearbyIntTest.h
598+
DEPENDS
599+
libc.src.math.nearbyint
600+
libc.src.__support.CPP.algorithm
601+
libc.src.__support.CPP.array
602+
)
603+
604+
add_fp_unittest(
605+
nearbyintf_test
606+
NEED_MPFR
607+
SUITE
608+
libc-math-unittests
609+
SRCS
610+
nearbyintf_test.cpp
611+
HDRS
612+
NearbyIntTest.h
613+
DEPENDS
614+
libc.src.math.nearbyintf
615+
libc.src.__support.CPP.algorithm
616+
libc.src.__support.CPP.array
617+
)
618+
619+
add_fp_unittest(
620+
nearbyintl_test
621+
NEED_MPFR
622+
SUITE
623+
libc-math-unittests
624+
SRCS
625+
nearbyintl_test.cpp
626+
HDRS
627+
NearbyIntTest.h
628+
DEPENDS
629+
libc.src.math.nearbyintl
630+
libc.src.__support.CPP.algorithm
631+
libc.src.__support.CPP.array
632+
)
633+
634+
add_fp_unittest(
635+
nearbyintf16_test
636+
NEED_MPFR
637+
SUITE
638+
libc-math-unittests
639+
SRCS
640+
nearbyintf16_test.cpp
641+
HDRS
642+
NearbyIntTest.h
643+
DEPENDS
644+
libc.src.math.nearbyintf16
645+
libc.src.__support.CPP.algorithm
646+
libc.src.__support.CPP.array
647+
)
648+
589649
add_fp_unittest(
590650
rint_test
591651
NEED_MPFR

libc/test/src/math/NearbyIntTest.h

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
//===-- Utility class to test different flavors of nearbyint ----*- C++ -*-===//
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+
#ifndef LLVM_LIBC_TEST_SRC_MATH_NEARBYINTTEST_H
10+
#define LLVM_LIBC_TEST_SRC_MATH_NEARBYINTTEST_H
11+
12+
#include "src/__support/CPP/algorithm.h"
13+
#include "src/__support/CPP/array.h"
14+
#include "test/UnitTest/FEnvSafeTest.h"
15+
#include "test/UnitTest/FPMatcher.h"
16+
#include "test/UnitTest/Test.h"
17+
#include "utils/MPFRWrapper/MPFRUtils.h"
18+
19+
using namespace LIBC_NAMESPACE::fputil::testing;
20+
21+
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
22+
23+
template <typename T>
24+
class NearbyIntTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
25+
26+
DECLARE_SPECIAL_CONSTANTS(T)
27+
28+
static constexpr LIBC_NAMESPACE::cpp::array<RoundingMode, 4> ROUNDING_MODES =
29+
{
30+
RoundingMode::Upward,
31+
RoundingMode::Downward,
32+
RoundingMode::TowardZero,
33+
RoundingMode::Nearest,
34+
};
35+
36+
static constexpr StorageType MIN_SUBNORMAL =
37+
FPBits::min_subnormal().uintval();
38+
static constexpr StorageType MAX_SUBNORMAL =
39+
FPBits::max_subnormal().uintval();
40+
static constexpr StorageType MIN_NORMAL = FPBits::min_normal().uintval();
41+
static constexpr StorageType MAX_NORMAL = FPBits::max_normal().uintval();
42+
43+
public:
44+
using NearbyIntFunc = T (*)(T);
45+
46+
void test_round_numbers(NearbyIntFunc func) {
47+
for (RoundingMode mode : ROUNDING_MODES) {
48+
if (ForceRoundingMode r(mode); r.success) {
49+
EXPECT_FP_EQ(func(T(1.0)), mpfr::round(T(1.0), mode));
50+
EXPECT_FP_EQ(func(T(-1.0)), mpfr::round(T(-1.0), mode));
51+
EXPECT_FP_EQ(func(T(10.0)), mpfr::round(T(10.0), mode));
52+
EXPECT_FP_EQ(func(T(-10.0)), mpfr::round(T(-10.0), mode));
53+
EXPECT_FP_EQ(func(T(1234.0)), mpfr::round(T(1234.0), mode));
54+
EXPECT_FP_EQ(func(T(-1234.0)), mpfr::round(T(-1234.0), mode));
55+
}
56+
}
57+
}
58+
59+
void test_fractions(NearbyIntFunc func) {
60+
for (RoundingMode mode : ROUNDING_MODES) {
61+
if (ForceRoundingMode r(mode); r.success) {
62+
EXPECT_FP_EQ(func(T(0.5)), mpfr::round(T(0.5), mode));
63+
EXPECT_FP_EQ(func(T(-0.5)), mpfr::round(T(-0.5), mode));
64+
EXPECT_FP_EQ(func(T(0.115)), mpfr::round(T(0.115), mode));
65+
EXPECT_FP_EQ(func(T(-0.115)), mpfr::round(T(-0.115), mode));
66+
EXPECT_FP_EQ(func(T(0.715)), mpfr::round(T(0.715), mode));
67+
EXPECT_FP_EQ(func(T(-0.715)), mpfr::round(T(-0.715), mode));
68+
}
69+
}
70+
}
71+
72+
void test_subnormal_range(NearbyIntFunc func) {
73+
constexpr int COUNT = 100'001;
74+
const StorageType STEP = LIBC_NAMESPACE::cpp::max(
75+
static_cast<StorageType>((MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT),
76+
StorageType(1));
77+
for (StorageType i = MIN_SUBNORMAL; i <= MAX_SUBNORMAL; i += STEP) {
78+
T x = FPBits(i).get_val();
79+
for (RoundingMode mode : ROUNDING_MODES) {
80+
if (ForceRoundingMode r(mode); r.success) {
81+
EXPECT_FP_EQ(func(x), mpfr::round(x, mode));
82+
}
83+
}
84+
}
85+
}
86+
87+
void test_normal_range(NearbyIntFunc func) {
88+
constexpr int COUNT = 100'001;
89+
const StorageType STEP = LIBC_NAMESPACE::cpp::max(
90+
static_cast<StorageType>((MAX_NORMAL - MIN_NORMAL) / COUNT),
91+
StorageType(1));
92+
for (StorageType i = MIN_NORMAL; i <= MAX_NORMAL; i += STEP) {
93+
FPBits xbits(i);
94+
T x = xbits.get_val();
95+
// In normal range on x86 platforms, the long double implicit 1 bit can be
96+
// zero making the numbers NaN. We will skip them.
97+
if (xbits.is_nan())
98+
continue;
99+
100+
for (RoundingMode mode : ROUNDING_MODES) {
101+
if (ForceRoundingMode r(mode); r.success) {
102+
EXPECT_FP_EQ(func(x), mpfr::round(x, mode));
103+
}
104+
}
105+
}
106+
}
107+
};
108+
109+
#define LIST_NEARBYINT_TESTS(F, func) \
110+
using LlvmLibcNearbyIntTest = NearbyIntTestTemplate<F>; \
111+
TEST_F(LlvmLibcNearbyIntTest, RoundNumbers) { test_round_numbers(&func); } \
112+
TEST_F(LlvmLibcNearbyIntTest, Fractions) { test_fractions(&func); } \
113+
TEST_F(LlvmLibcNearbyIntTest, SubnormalRange) { \
114+
test_subnormal_range(&func); \
115+
} \
116+
TEST_F(LlvmLibcNearbyIntTest, NormalRange) { test_normal_range(&func); }
117+
118+
#endif // LLVM_LIBC_TEST_SRC_MATH_NEARBYINTTEST_H

libc/test/src/math/nearbyint_test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for nearbyint -------------------------------------------===//
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 "NearbyIntTest.h"
10+
11+
#include "src/math/nearbyint.h"
12+
13+
LIST_NEARBYINT_TESTS(double, LIBC_NAMESPACE::nearbyint)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for nearbyintf16 ----------------------------------------===//
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 "NearbyIntTest.h"
10+
11+
#include "src/math/nearbyintf16.h"
12+
13+
LIST_NEARBYINT_TESTS(float16, LIBC_NAMESPACE::nearbyintf16)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for nearbyintf ------------------------------------------===//
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 "NearbyIntTest.h"
10+
11+
#include "src/math/nearbyintf.h"
12+
13+
LIST_NEARBYINT_TESTS(float, LIBC_NAMESPACE::nearbyintf)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for nearbyintl ------------------------------------------===//
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 "NearbyIntTest.h"
10+
11+
#include "src/math/nearbyintl.h"
12+
13+
LIST_NEARBYINT_TESTS(long double, LIBC_NAMESPACE::nearbyintl)

0 commit comments

Comments
 (0)