Skip to content

Commit 65310f3

Browse files
authored
Reapply "[libc][math][c23] Add MPFR unit tests for {ceil,floor,round,roundeven,trunc}f16 (#94383)" (#94807)
This reverts commit cbe97e9.
1 parent c7308d4 commit 65310f3

12 files changed

+264
-53
lines changed

libc/test/src/math/CMakeLists.txt

+90
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ add_fp_unittest(
141141
TruncTest.h
142142
DEPENDS
143143
libc.src.math.trunc
144+
libc.src.__support.CPP.algorithm
144145
libc.src.__support.FPUtil.fp_bits
145146
)
146147

@@ -155,6 +156,7 @@ add_fp_unittest(
155156
TruncTest.h
156157
DEPENDS
157158
libc.src.math.truncf
159+
libc.src.__support.CPP.algorithm
158160
libc.src.__support.FPUtil.fp_bits
159161
)
160162

@@ -169,6 +171,22 @@ add_fp_unittest(
169171
TruncTest.h
170172
DEPENDS
171173
libc.src.math.truncl
174+
libc.src.__support.CPP.algorithm
175+
libc.src.__support.FPUtil.fp_bits
176+
)
177+
178+
add_fp_unittest(
179+
truncf16_test
180+
NEED_MPFR
181+
SUITE
182+
libc-math-unittests
183+
SRCS
184+
truncf16_test.cpp
185+
HDRS
186+
TruncTest.h
187+
DEPENDS
188+
libc.src.math.truncf16
189+
libc.src.__support.CPP.algorithm
172190
libc.src.__support.FPUtil.fp_bits
173191
)
174192

@@ -183,6 +201,7 @@ add_fp_unittest(
183201
CeilTest.h
184202
DEPENDS
185203
libc.src.math.ceil
204+
libc.src.__support.CPP.algorithm
186205
libc.src.__support.FPUtil.fp_bits
187206
)
188207

@@ -197,6 +216,7 @@ add_fp_unittest(
197216
CeilTest.h
198217
DEPENDS
199218
libc.src.math.ceilf
219+
libc.src.__support.CPP.algorithm
200220
libc.src.__support.FPUtil.fp_bits
201221
)
202222

@@ -211,6 +231,22 @@ add_fp_unittest(
211231
CeilTest.h
212232
DEPENDS
213233
libc.src.math.ceill
234+
libc.src.__support.CPP.algorithm
235+
libc.src.__support.FPUtil.fp_bits
236+
)
237+
238+
add_fp_unittest(
239+
ceilf16_test
240+
NEED_MPFR
241+
SUITE
242+
libc-math-unittests
243+
SRCS
244+
ceilf16_test.cpp
245+
HDRS
246+
CeilTest.h
247+
DEPENDS
248+
libc.src.math.ceilf16
249+
libc.src.__support.CPP.algorithm
214250
libc.src.__support.FPUtil.fp_bits
215251
)
216252

@@ -225,6 +261,7 @@ add_fp_unittest(
225261
FloorTest.h
226262
DEPENDS
227263
libc.src.math.floor
264+
libc.src.__support.CPP.algorithm
228265
libc.src.__support.FPUtil.fp_bits
229266
)
230267

@@ -239,6 +276,7 @@ add_fp_unittest(
239276
FloorTest.h
240277
DEPENDS
241278
libc.src.math.floorf
279+
libc.src.__support.CPP.algorithm
242280
libc.src.__support.FPUtil.fp_bits
243281
)
244282

@@ -253,6 +291,22 @@ add_fp_unittest(
253291
FloorTest.h
254292
DEPENDS
255293
libc.src.math.floorl
294+
libc.src.__support.CPP.algorithm
295+
libc.src.__support.FPUtil.fp_bits
296+
)
297+
298+
add_fp_unittest(
299+
floorf16_test
300+
NEED_MPFR
301+
SUITE
302+
libc-math-unittests
303+
SRCS
304+
floorf16_test.cpp
305+
HDRS
306+
FloorTest.h
307+
DEPENDS
308+
libc.src.math.floorf16
309+
libc.src.__support.CPP.algorithm
256310
libc.src.__support.FPUtil.fp_bits
257311
)
258312

@@ -267,6 +321,7 @@ add_fp_unittest(
267321
RoundTest.h
268322
DEPENDS
269323
libc.src.math.round
324+
libc.src.__support.CPP.algorithm
270325
libc.src.__support.FPUtil.fp_bits
271326
)
272327

@@ -281,6 +336,7 @@ add_fp_unittest(
281336
RoundTest.h
282337
DEPENDS
283338
libc.src.math.roundf
339+
libc.src.__support.CPP.algorithm
284340
libc.src.__support.FPUtil.fp_bits
285341
)
286342

@@ -295,6 +351,22 @@ add_fp_unittest(
295351
RoundTest.h
296352
DEPENDS
297353
libc.src.math.roundl
354+
libc.src.__support.CPP.algorithm
355+
libc.src.__support.FPUtil.fp_bits
356+
)
357+
358+
add_fp_unittest(
359+
roundf16_test
360+
NEED_MPFR
361+
SUITE
362+
libc-math-unittests
363+
SRCS
364+
roundf16_test.cpp
365+
HDRS
366+
RoundTest.h
367+
DEPENDS
368+
libc.src.math.roundf16
369+
libc.src.__support.CPP.algorithm
298370
libc.src.__support.FPUtil.fp_bits
299371
)
300372

@@ -309,6 +381,7 @@ add_fp_unittest(
309381
RoundEvenTest.h
310382
DEPENDS
311383
libc.src.math.roundeven
384+
libc.src.__support.CPP.algorithm
312385
libc.src.__support.FPUtil.fp_bits
313386
)
314387

@@ -323,6 +396,7 @@ add_fp_unittest(
323396
RoundEvenTest.h
324397
DEPENDS
325398
libc.src.math.roundevenf
399+
libc.src.__support.CPP.algorithm
326400
libc.src.__support.FPUtil.fp_bits
327401
)
328402

@@ -337,6 +411,22 @@ add_fp_unittest(
337411
RoundEvenTest.h
338412
DEPENDS
339413
libc.src.math.roundevenl
414+
libc.src.__support.CPP.algorithm
415+
libc.src.__support.FPUtil.fp_bits
416+
)
417+
418+
add_fp_unittest(
419+
roundevenf16_test
420+
NEED_MPFR
421+
SUITE
422+
libc-math-unittests
423+
SRCS
424+
roundevenf16_test.cpp
425+
HDRS
426+
RoundEvenTest.h
427+
DEPENDS
428+
libc.src.math.roundevenf16
429+
libc.src.__support.CPP.algorithm
340430
libc.src.__support.FPUtil.fp_bits
341431
)
342432

libc/test/src/math/CeilTest.h

+18-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#ifndef LLVM_LIBC_TEST_SRC_MATH_CEILTEST_H
10+
#define LLVM_LIBC_TEST_SRC_MATH_CEILTEST_H
11+
12+
#include "src/__support/CPP/algorithm.h"
913
#include "test/UnitTest/FEnvSafeTest.h"
1014
#include "test/UnitTest/FPMatcher.h"
1115
#include "test/UnitTest/Test.h"
@@ -59,18 +63,21 @@ class CeilTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
5963
EXPECT_FP_EQ(T(-10.0), func(T(-10.32)));
6064
EXPECT_FP_EQ(T(11.0), func(T(10.65)));
6165
EXPECT_FP_EQ(T(-10.0), func(T(-10.65)));
62-
EXPECT_FP_EQ(T(1235.0), func(T(1234.38)));
63-
EXPECT_FP_EQ(T(-1234.0), func(T(-1234.38)));
64-
EXPECT_FP_EQ(T(1235.0), func(T(1234.96)));
65-
EXPECT_FP_EQ(T(-1234.0), func(T(-1234.96)));
66+
EXPECT_FP_EQ(T(124.0), func(T(123.38)));
67+
EXPECT_FP_EQ(T(-123.0), func(T(-123.38)));
68+
EXPECT_FP_EQ(T(124.0), func(T(123.96)));
69+
EXPECT_FP_EQ(T(-123.0), func(T(-123.96)));
6670
}
6771

6872
void testRange(CeilFunc func) {
69-
constexpr StorageType COUNT = 100'000;
70-
constexpr StorageType STEP = STORAGE_MAX / COUNT;
71-
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
72-
T x = FPBits(v).get_val();
73-
if (isnan(x) || isinf(x))
73+
constexpr int COUNT = 100'000;
74+
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
75+
static_cast<StorageType>(STORAGE_MAX / COUNT), StorageType(1));
76+
StorageType v = 0;
77+
for (int i = 0; i <= COUNT; ++i, v += STEP) {
78+
FPBits xbits(v);
79+
T x = xbits.get_val();
80+
if (xbits.is_inf_or_nan())
7481
continue;
7582

7683
ASSERT_MPFR_MATCH(mpfr::Operation::Ceil, x, func(x), 0.0);
@@ -84,3 +91,5 @@ class CeilTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
8491
TEST_F(LlvmLibcCeilTest, RoundedNubmers) { testRoundedNumbers(&func); } \
8592
TEST_F(LlvmLibcCeilTest, Fractions) { testFractions(&func); } \
8693
TEST_F(LlvmLibcCeilTest, Range) { testRange(&func); }
94+
95+
#endif // LLVM_LIBC_TEST_SRC_MATH_CEILTEST_H

libc/test/src/math/FloorTest.h

+13-9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_TEST_SRC_MATH_FLOORTEST_H
1010
#define LLVM_LIBC_TEST_SRC_MATH_FLOORTEST_H
1111

12+
#include "src/__support/CPP/algorithm.h"
1213
#include "test/UnitTest/FEnvSafeTest.h"
1314
#include "test/UnitTest/FPMatcher.h"
1415
#include "test/UnitTest/Test.h"
@@ -62,18 +63,21 @@ class FloorTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
6263
EXPECT_FP_EQ(T(-11.0), func(T(-10.32)));
6364
EXPECT_FP_EQ(T(10.0), func(T(10.65)));
6465
EXPECT_FP_EQ(T(-11.0), func(T(-10.65)));
65-
EXPECT_FP_EQ(T(1234.0), func(T(1234.38)));
66-
EXPECT_FP_EQ(T(-1235.0), func(T(-1234.38)));
67-
EXPECT_FP_EQ(T(1234.0), func(T(1234.96)));
68-
EXPECT_FP_EQ(T(-1235.0), func(T(-1234.96)));
66+
EXPECT_FP_EQ(T(123.0), func(T(123.38)));
67+
EXPECT_FP_EQ(T(-124.0), func(T(-123.38)));
68+
EXPECT_FP_EQ(T(123.0), func(T(123.96)));
69+
EXPECT_FP_EQ(T(-124.0), func(T(-123.96)));
6970
}
7071

7172
void testRange(FloorFunc func) {
72-
constexpr StorageType COUNT = 100'000;
73-
constexpr StorageType STEP = STORAGE_MAX / COUNT;
74-
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
75-
T x = FPBits(v).get_val();
76-
if (isnan(x) || isinf(x))
73+
constexpr int COUNT = 100'000;
74+
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
75+
static_cast<StorageType>(STORAGE_MAX / COUNT), StorageType(1));
76+
StorageType v = 0;
77+
for (int i = 0; i <= COUNT; ++i, v += STEP) {
78+
FPBits xbits(v);
79+
T x = xbits.get_val();
80+
if (xbits.is_inf_or_nan())
7781
continue;
7882

7983
ASSERT_MPFR_MATCH(mpfr::Operation::Floor, x, func(x), 0.0);

libc/test/src/math/RoundEvenTest.h

+17-13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_TEST_SRC_MATH_ROUNDEVENTEST_H
1010
#define LLVM_LIBC_TEST_SRC_MATH_ROUNDEVENTEST_H
1111

12+
#include "src/__support/CPP/algorithm.h"
1213
#include "test/UnitTest/FEnvSafeTest.h"
1314
#include "test/UnitTest/FPMatcher.h"
1415
#include "test/UnitTest/Test.h"
@@ -60,22 +61,25 @@ class RoundEvenTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
6061
EXPECT_FP_EQ(T(-2.0), func(T(-1.75)));
6162
EXPECT_FP_EQ(T(11.0), func(T(10.65)));
6263
EXPECT_FP_EQ(T(-11.0), func(T(-10.65)));
63-
EXPECT_FP_EQ(T(1233.0), func(T(1233.25)));
64-
EXPECT_FP_EQ(T(1234.0), func(T(1233.50)));
65-
EXPECT_FP_EQ(T(1234.0), func(T(1233.75)));
66-
EXPECT_FP_EQ(T(-1233.0), func(T(-1233.25)));
67-
EXPECT_FP_EQ(T(-1234.0), func(T(-1233.50)));
68-
EXPECT_FP_EQ(T(-1234.0), func(T(-1233.75)));
69-
EXPECT_FP_EQ(T(1234.0), func(T(1234.50)));
70-
EXPECT_FP_EQ(T(-1234.0), func(T(-1234.50)));
64+
EXPECT_FP_EQ(T(123.0), func(T(123.25)));
65+
EXPECT_FP_EQ(T(124.0), func(T(123.50)));
66+
EXPECT_FP_EQ(T(124.0), func(T(123.75)));
67+
EXPECT_FP_EQ(T(-123.0), func(T(-123.25)));
68+
EXPECT_FP_EQ(T(-124.0), func(T(-123.50)));
69+
EXPECT_FP_EQ(T(-124.0), func(T(-123.75)));
70+
EXPECT_FP_EQ(T(124.0), func(T(124.50)));
71+
EXPECT_FP_EQ(T(-124.0), func(T(-124.50)));
7172
}
7273

7374
void testRange(RoundEvenFunc func) {
74-
constexpr StorageType COUNT = 100'000;
75-
constexpr StorageType STEP = STORAGE_MAX / COUNT;
76-
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
77-
T x = FPBits(v).get_val();
78-
if (isnan(x) || isinf(x))
75+
constexpr int COUNT = 100'000;
76+
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
77+
static_cast<StorageType>(STORAGE_MAX / COUNT), StorageType(1));
78+
StorageType v = 0;
79+
for (int i = 0; i <= COUNT; ++i, v += STEP) {
80+
FPBits xbits(v);
81+
T x = xbits.get_val();
82+
if (xbits.is_inf_or_nan())
7983
continue;
8084

8185
ASSERT_MPFR_MATCH(mpfr::Operation::RoundEven, x, func(x), 0.0);

libc/test/src/math/RoundTest.h

+13-9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_TEST_SRC_MATH_ROUNDTEST_H
1010
#define LLVM_LIBC_TEST_SRC_MATH_ROUNDTEST_H
1111

12+
#include "src/__support/CPP/algorithm.h"
1213
#include "test/UnitTest/FEnvSafeTest.h"
1314
#include "test/UnitTest/FPMatcher.h"
1415
#include "test/UnitTest/Test.h"
@@ -62,18 +63,21 @@ class RoundTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
6263
EXPECT_FP_EQ(T(-10.0), func(T(-10.32)));
6364
EXPECT_FP_EQ(T(11.0), func(T(10.65)));
6465
EXPECT_FP_EQ(T(-11.0), func(T(-10.65)));
65-
EXPECT_FP_EQ(T(1234.0), func(T(1234.38)));
66-
EXPECT_FP_EQ(T(-1234.0), func(T(-1234.38)));
67-
EXPECT_FP_EQ(T(1235.0), func(T(1234.96)));
68-
EXPECT_FP_EQ(T(-1235.0), func(T(-1234.96)));
66+
EXPECT_FP_EQ(T(123.0), func(T(123.38)));
67+
EXPECT_FP_EQ(T(-123.0), func(T(-123.38)));
68+
EXPECT_FP_EQ(T(124.0), func(T(123.96)));
69+
EXPECT_FP_EQ(T(-124.0), func(T(-123.96)));
6970
}
7071

7172
void testRange(RoundFunc func) {
72-
constexpr StorageType COUNT = 100'000;
73-
constexpr StorageType STEP = STORAGE_MAX / COUNT;
74-
for (StorageType i = 0, v = 0; i <= COUNT; ++i, v += STEP) {
75-
T x = FPBits(v).get_val();
76-
if (isnan(x) || isinf(x))
73+
constexpr int COUNT = 100'000;
74+
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
75+
static_cast<StorageType>(STORAGE_MAX / COUNT), StorageType(1));
76+
StorageType v = 0;
77+
for (int i = 0; i <= COUNT; ++i, v += STEP) {
78+
FPBits xbits(v);
79+
T x = xbits.get_val();
80+
if (xbits.is_inf_or_nan())
7781
continue;
7882

7983
ASSERT_MPFR_MATCH(mpfr::Operation::Round, x, func(x), 0.0);

0 commit comments

Comments
 (0)