Skip to content

Commit 23b9596

Browse files
committed
[libc][math] Add MPFR unit tests for nearbyint{,f,l,f16}
1 parent b7762f2 commit 23b9596

File tree

6 files changed

+244
-0
lines changed

6 files changed

+244
-0
lines changed

libc/test/src/math/CMakeLists.txt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,70 @@ add_fp_unittest(
574574
libc.src.__support.FPUtil.fp_bits
575575
)
576576

577+
add_fp_unittest(
578+
nearbyint_test
579+
NEED_MPFR
580+
SUITE
581+
libc-math-unittests
582+
SRCS
583+
nearbyint_test.cpp
584+
HDRS
585+
NearbyIntTest.h
586+
DEPENDS
587+
libc.hdr.fenv_macros
588+
libc.src.math.nearbyint
589+
libc.src.__support.CPP.algorithm
590+
libc.src.__support.FPUtil.fenv_impl
591+
)
592+
593+
add_fp_unittest(
594+
nearbyintf_test
595+
NEED_MPFR
596+
SUITE
597+
libc-math-unittests
598+
SRCS
599+
nearbyintf_test.cpp
600+
HDRS
601+
NearbyIntTest.h
602+
DEPENDS
603+
libc.hdr.fenv_macros
604+
libc.src.math.nearbyintf
605+
libc.src.__support.CPP.algorithm
606+
libc.src.__support.FPUtil.fenv_impl
607+
)
608+
609+
add_fp_unittest(
610+
nearbyintl_test
611+
NEED_MPFR
612+
SUITE
613+
libc-math-unittests
614+
SRCS
615+
nearbyintl_test.cpp
616+
HDRS
617+
NearbyIntTest.h
618+
DEPENDS
619+
libc.hdr.fenv_macros
620+
libc.src.math.nearbyintl
621+
libc.src.__support.CPP.algorithm
622+
libc.src.__support.FPUtil.fenv_impl
623+
)
624+
625+
add_fp_unittest(
626+
nearbyintf16_test
627+
NEED_MPFR
628+
SUITE
629+
libc-math-unittests
630+
SRCS
631+
nearbyintf16_test.cpp
632+
HDRS
633+
NearbyIntTest.h
634+
DEPENDS
635+
libc.hdr.fenv_macros
636+
libc.src.math.nearbyintf16
637+
libc.src.__support.CPP.algorithm
638+
libc.src.__support.FPUtil.fenv_impl
639+
)
640+
577641
add_fp_unittest(
578642
rint_test
579643
NEED_MPFR

libc/test/src/math/NearbyIntTest.h

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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/FPUtil/FEnvImpl.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+
#include "hdr/fenv_macros.h"
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 int ROUNDING_MODES[4] = {FE_UPWARD, FE_DOWNWARD,
29+
FE_TOWARDZERO, FE_TONEAREST};
30+
31+
static constexpr StorageType MIN_SUBNORMAL =
32+
FPBits::min_subnormal().uintval();
33+
static constexpr StorageType MAX_SUBNORMAL =
34+
FPBits::max_subnormal().uintval();
35+
static constexpr StorageType MIN_NORMAL = FPBits::min_normal().uintval();
36+
static constexpr StorageType MAX_NORMAL = FPBits::max_normal().uintval();
37+
38+
static mpfr::RoundingMode to_mpfr_rounding_mode(int mode) {
39+
switch (mode) {
40+
case FE_UPWARD:
41+
return mpfr::RoundingMode::Upward;
42+
case FE_DOWNWARD:
43+
return mpfr::RoundingMode::Downward;
44+
case FE_TOWARDZERO:
45+
return mpfr::RoundingMode::TowardZero;
46+
case FE_TONEAREST:
47+
return mpfr::RoundingMode::Nearest;
48+
default:
49+
__builtin_unreachable();
50+
}
51+
}
52+
53+
public:
54+
using NearbyIntFunc = T (*)(T);
55+
56+
void test_round_numbers(NearbyIntFunc func) {
57+
for (int mode : ROUNDING_MODES) {
58+
LIBC_NAMESPACE::fputil::set_round(mode);
59+
mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode);
60+
EXPECT_FP_EQ(func(T(1.0)), mpfr::round(T(1.0), mpfr_mode));
61+
EXPECT_FP_EQ(func(T(-1.0)), mpfr::round(T(-1.0), mpfr_mode));
62+
EXPECT_FP_EQ(func(T(10.0)), mpfr::round(T(10.0), mpfr_mode));
63+
EXPECT_FP_EQ(func(T(-10.0)), mpfr::round(T(-10.0), mpfr_mode));
64+
EXPECT_FP_EQ(func(T(1234.0)), mpfr::round(T(1234.0), mpfr_mode));
65+
EXPECT_FP_EQ(func(T(-1234.0)), mpfr::round(T(-1234.0), mpfr_mode));
66+
}
67+
}
68+
69+
void test_fractions(NearbyIntFunc func) {
70+
for (int mode : ROUNDING_MODES) {
71+
LIBC_NAMESPACE::fputil::set_round(mode);
72+
mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode);
73+
EXPECT_FP_EQ(func(T(0.5)), mpfr::round(T(0.5), mpfr_mode));
74+
EXPECT_FP_EQ(func(T(-0.5)), mpfr::round(T(-0.5), mpfr_mode));
75+
EXPECT_FP_EQ(func(T(0.115)), mpfr::round(T(0.115), mpfr_mode));
76+
EXPECT_FP_EQ(func(T(-0.115)), mpfr::round(T(-0.115), mpfr_mode));
77+
EXPECT_FP_EQ(func(T(0.715)), mpfr::round(T(0.715), mpfr_mode));
78+
EXPECT_FP_EQ(func(T(-0.715)), mpfr::round(T(-0.715), mpfr_mode));
79+
}
80+
}
81+
82+
void test_subnormal_range(NearbyIntFunc func) {
83+
constexpr int COUNT = 100'001;
84+
const StorageType STEP = LIBC_NAMESPACE::cpp::max(
85+
static_cast<StorageType>((MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT),
86+
StorageType(1));
87+
for (StorageType i = MIN_SUBNORMAL; i <= MAX_SUBNORMAL; i += STEP) {
88+
T x = FPBits(i).get_val();
89+
for (int mode : ROUNDING_MODES) {
90+
LIBC_NAMESPACE::fputil::set_round(mode);
91+
mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode);
92+
EXPECT_FP_EQ(func(x), mpfr::round(x, mpfr_mode));
93+
}
94+
}
95+
}
96+
97+
void test_normal_range(NearbyIntFunc func) {
98+
constexpr int COUNT = 100'001;
99+
const StorageType STEP = LIBC_NAMESPACE::cpp::max(
100+
static_cast<StorageType>((MAX_NORMAL - MIN_NORMAL) / COUNT),
101+
StorageType(1));
102+
for (StorageType i = MIN_NORMAL; i <= MAX_NORMAL; i += STEP) {
103+
FPBits xbits(i);
104+
T x = xbits.get_val();
105+
// In normal range on x86 platforms, the long double implicit 1 bit can be
106+
// zero making the numbers NaN. We will skip them.
107+
if (xbits.is_nan())
108+
continue;
109+
110+
for (int mode : ROUNDING_MODES) {
111+
LIBC_NAMESPACE::fputil::set_round(mode);
112+
mpfr::RoundingMode mpfr_mode = to_mpfr_rounding_mode(mode);
113+
EXPECT_FP_EQ(func(x), mpfr::round(x, mpfr_mode));
114+
}
115+
}
116+
}
117+
};
118+
119+
#define LIST_NEARBYINT_TESTS(F, func) \
120+
using LlvmLibcNearbyIntTest = NearbyIntTestTemplate<F>; \
121+
TEST_F(LlvmLibcNearbyIntTest, RoundNumbers) { test_round_numbers(&func); } \
122+
TEST_F(LlvmLibcNearbyIntTest, Fractions) { test_fractions(&func); } \
123+
TEST_F(LlvmLibcNearbyIntTest, SubnormalRange) { \
124+
test_subnormal_range(&func); \
125+
} \
126+
TEST_F(LlvmLibcNearbyIntTest, NormalRange) { test_normal_range(&func); }
127+
128+
#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)