Skip to content

Commit 7ba9597

Browse files
committed
[libc][math][c23] Add rintf16 MPFR unit tests
1 parent 52050f3 commit 7ba9597

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

libc/test/src/math/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ add_fp_unittest(
549549
RIntTest.h
550550
DEPENDS
551551
libc.src.math.rint
552+
libc.src.__support.CPP.algorithm
552553
libc.src.__support.FPUtil.fenv_impl
553554
libc.src.__support.FPUtil.fp_bits
554555
)
@@ -564,6 +565,7 @@ add_fp_unittest(
564565
RIntTest.h
565566
DEPENDS
566567
libc.src.math.rintf
568+
libc.src.__support.CPP.algorithm
567569
libc.src.__support.FPUtil.fenv_impl
568570
libc.src.__support.FPUtil.fp_bits
569571
)
@@ -579,6 +581,23 @@ add_fp_unittest(
579581
RIntTest.h
580582
DEPENDS
581583
libc.src.math.rintl
584+
libc.src.__support.CPP.algorithm
585+
libc.src.__support.FPUtil.fenv_impl
586+
libc.src.__support.FPUtil.fp_bits
587+
)
588+
589+
add_fp_unittest(
590+
rintf16_test
591+
NEED_MPFR
592+
SUITE
593+
libc-math-unittests
594+
SRCS
595+
rintf16_test.cpp
596+
HDRS
597+
RIntTest.h
598+
DEPENDS
599+
libc.src.math.rintf16
600+
libc.src.__support.CPP.algorithm
582601
libc.src.__support.FPUtil.fenv_impl
583602
libc.src.__support.FPUtil.fp_bits
584603
)

libc/test/src/math/RIntTest.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_TEST_SRC_MATH_RINTTEST_H
1010
#define LLVM_LIBC_TEST_SRC_MATH_RINTTEST_H
1111

12+
#include "src/__support/CPP/algorithm.h"
1213
#include "src/__support/FPUtil/FEnvImpl.h"
1314
#include "src/__support/FPUtil/FPBits.h"
1415
#include "test/UnitTest/FEnvSafeTest.h"
@@ -101,8 +102,10 @@ class RIntTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
101102
}
102103

103104
void testSubnormalRange(RIntFunc func) {
104-
constexpr StorageType COUNT = 100'001;
105-
constexpr StorageType STEP = (MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT;
105+
constexpr int COUNT = 100'001;
106+
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
107+
static_cast<StorageType>((MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT),
108+
StorageType(1));
106109
for (StorageType i = MIN_SUBNORMAL; i <= MAX_SUBNORMAL; i += STEP) {
107110
T x = FPBits(i).get_val();
108111
for (int mode : ROUNDING_MODES) {
@@ -114,13 +117,16 @@ class RIntTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
114117
}
115118

116119
void testNormalRange(RIntFunc func) {
117-
constexpr StorageType COUNT = 100'001;
118-
constexpr StorageType STEP = (MAX_NORMAL - MIN_NORMAL) / COUNT;
120+
constexpr int COUNT = 100'001;
121+
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
122+
static_cast<StorageType>((MAX_NORMAL - MIN_NORMAL) / COUNT),
123+
StorageType(1));
119124
for (StorageType i = MIN_NORMAL; i <= MAX_NORMAL; i += STEP) {
120-
T x = FPBits(i).get_val();
125+
FPBits xbits(i);
126+
T x = xbits.get_val();
121127
// In normal range on x86 platforms, the long double implicit 1 bit can be
122128
// zero making the numbers NaN. We will skip them.
123-
if (isnan(x)) {
129+
if (xbits.is_nan()) {
124130
continue;
125131
}
126132

libc/test/src/math/rintf16_test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for rintf16 ---------------------------------------------===//
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 "RIntTest.h"
10+
11+
#include "src/math/rintf16.h"
12+
13+
LIST_RINT_TESTS(float16, LIBC_NAMESPACE::rintf16)

libc/utils/MPFRWrapper/MPFRUtils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,7 @@ template <typename T> T round(T x, RoundingMode mode) {
11041104
template float round<float>(float, RoundingMode);
11051105
template double round<double>(double, RoundingMode);
11061106
template long double round<long double>(long double, RoundingMode);
1107+
template float16 round<float16>(float16, RoundingMode);
11071108

11081109
} // namespace mpfr
11091110
} // namespace testing

0 commit comments

Comments
 (0)