Skip to content

Commit f50656c

Browse files
authored
[libc][math][c23] Add MPFR unit tests for {rint,lrint,llrint,lround,llround}f16 (#94473)
1 parent 0babff9 commit f50656c

9 files changed

+222
-35
lines changed

libc/test/src/math/CMakeLists.txt

Lines changed: 115 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,11 @@ add_fp_unittest(
440440
HDRS
441441
RoundToIntegerTest.h
442442
DEPENDS
443+
libc.hdr.fenv_macros
443444
libc.src.errno.errno
444-
libc.src.fenv.feclearexcept
445-
libc.src.fenv.feraiseexcept
446-
libc.src.fenv.fetestexcept
447445
libc.src.math.lround
446+
libc.src.__support.CPP.algorithm
447+
libc.src.__support.FPUtil.fenv_impl
448448
libc.src.__support.FPUtil.fp_bits
449449
)
450450

@@ -458,11 +458,11 @@ add_fp_unittest(
458458
HDRS
459459
RoundToIntegerTest.h
460460
DEPENDS
461+
libc.hdr.fenv_macros
461462
libc.src.errno.errno
462-
libc.src.fenv.feclearexcept
463-
libc.src.fenv.feraiseexcept
464-
libc.src.fenv.fetestexcept
465463
libc.src.math.lroundf
464+
libc.src.__support.CPP.algorithm
465+
libc.src.__support.FPUtil.fenv_impl
466466
libc.src.__support.FPUtil.fp_bits
467467
)
468468

@@ -476,11 +476,29 @@ add_fp_unittest(
476476
HDRS
477477
RoundToIntegerTest.h
478478
DEPENDS
479+
libc.hdr.fenv_macros
479480
libc.src.errno.errno
480-
libc.src.fenv.feclearexcept
481-
libc.src.fenv.feraiseexcept
482-
libc.src.fenv.fetestexcept
483481
libc.src.math.lroundl
482+
libc.src.__support.CPP.algorithm
483+
libc.src.__support.FPUtil.fenv_impl
484+
libc.src.__support.FPUtil.fp_bits
485+
)
486+
487+
add_fp_unittest(
488+
lroundf16_test
489+
NEED_MPFR
490+
SUITE
491+
libc-math-unittests
492+
SRCS
493+
lroundf16_test.cpp
494+
HDRS
495+
RoundToIntegerTest.h
496+
DEPENDS
497+
libc.hdr.fenv_macros
498+
libc.src.errno.errno
499+
libc.src.math.lroundf16
500+
libc.src.__support.CPP.algorithm
501+
libc.src.__support.FPUtil.fenv_impl
484502
libc.src.__support.FPUtil.fp_bits
485503
)
486504

@@ -494,11 +512,11 @@ add_fp_unittest(
494512
HDRS
495513
RoundToIntegerTest.h
496514
DEPENDS
515+
libc.hdr.fenv_macros
497516
libc.src.errno.errno
498-
libc.src.fenv.feclearexcept
499-
libc.src.fenv.feraiseexcept
500-
libc.src.fenv.fetestexcept
501517
libc.src.math.llround
518+
libc.src.__support.CPP.algorithm
519+
libc.src.__support.FPUtil.fenv_impl
502520
libc.src.__support.FPUtil.fp_bits
503521
)
504522

@@ -512,11 +530,11 @@ add_fp_unittest(
512530
HDRS
513531
RoundToIntegerTest.h
514532
DEPENDS
533+
libc.hdr.fenv_macros
515534
libc.src.errno.errno
516-
libc.src.fenv.feclearexcept
517-
libc.src.fenv.feraiseexcept
518-
libc.src.fenv.fetestexcept
519535
libc.src.math.llroundf
536+
libc.src.__support.CPP.algorithm
537+
libc.src.__support.FPUtil.fenv_impl
520538
libc.src.__support.FPUtil.fp_bits
521539
)
522540

@@ -530,11 +548,29 @@ add_fp_unittest(
530548
HDRS
531549
RoundToIntegerTest.h
532550
DEPENDS
551+
libc.hdr.fenv_macros
533552
libc.src.errno.errno
534-
libc.src.fenv.feclearexcept
535-
libc.src.fenv.feraiseexcept
536-
libc.src.fenv.fetestexcept
537553
libc.src.math.llroundl
554+
libc.src.__support.CPP.algorithm
555+
libc.src.__support.FPUtil.fenv_impl
556+
libc.src.__support.FPUtil.fp_bits
557+
)
558+
559+
add_fp_unittest(
560+
llroundf16_test
561+
NEED_MPFR
562+
SUITE
563+
libc-math-unittests
564+
SRCS
565+
llroundf16_test.cpp
566+
HDRS
567+
RoundToIntegerTest.h
568+
DEPENDS
569+
libc.hdr.fenv_macros
570+
libc.src.errno.errno
571+
libc.src.math.llroundf16
572+
libc.src.__support.CPP.algorithm
573+
libc.src.__support.FPUtil.fenv_impl
538574
libc.src.__support.FPUtil.fp_bits
539575
)
540576

@@ -548,7 +584,9 @@ add_fp_unittest(
548584
HDRS
549585
RIntTest.h
550586
DEPENDS
587+
libc.hdr.fenv_macros
551588
libc.src.math.rint
589+
libc.src.__support.CPP.algorithm
552590
libc.src.__support.FPUtil.fenv_impl
553591
libc.src.__support.FPUtil.fp_bits
554592
)
@@ -563,7 +601,9 @@ add_fp_unittest(
563601
HDRS
564602
RIntTest.h
565603
DEPENDS
604+
libc.hdr.fenv_macros
566605
libc.src.math.rintf
606+
libc.src.__support.CPP.algorithm
567607
libc.src.__support.FPUtil.fenv_impl
568608
libc.src.__support.FPUtil.fp_bits
569609
)
@@ -578,7 +618,26 @@ add_fp_unittest(
578618
HDRS
579619
RIntTest.h
580620
DEPENDS
621+
libc.hdr.fenv_macros
581622
libc.src.math.rintl
623+
libc.src.__support.CPP.algorithm
624+
libc.src.__support.FPUtil.fenv_impl
625+
libc.src.__support.FPUtil.fp_bits
626+
)
627+
628+
add_fp_unittest(
629+
rintf16_test
630+
NEED_MPFR
631+
SUITE
632+
libc-math-unittests
633+
SRCS
634+
rintf16_test.cpp
635+
HDRS
636+
RIntTest.h
637+
DEPENDS
638+
libc.hdr.fenv_macros
639+
libc.src.math.rintf16
640+
libc.src.__support.CPP.algorithm
582641
libc.src.__support.FPUtil.fenv_impl
583642
libc.src.__support.FPUtil.fp_bits
584643
)
@@ -594,6 +653,7 @@ add_fp_unittest(
594653
RoundToIntegerTest.h
595654
DEPENDS
596655
libc.src.math.lrint
656+
libc.src.__support.CPP.algorithm
597657
libc.src.__support.FPUtil.fenv_impl
598658
libc.src.__support.FPUtil.fp_bits
599659
)
@@ -609,6 +669,7 @@ add_fp_unittest(
609669
RoundToIntegerTest.h
610670
DEPENDS
611671
libc.src.math.lrintf
672+
libc.src.__support.CPP.algorithm
612673
libc.src.__support.FPUtil.fenv_impl
613674
libc.src.__support.FPUtil.fp_bits
614675
)
@@ -624,6 +685,23 @@ add_fp_unittest(
624685
RoundToIntegerTest.h
625686
DEPENDS
626687
libc.src.math.lrintl
688+
libc.src.__support.CPP.algorithm
689+
libc.src.__support.FPUtil.fenv_impl
690+
libc.src.__support.FPUtil.fp_bits
691+
)
692+
693+
add_fp_unittest(
694+
lrintf16_test
695+
NEED_MPFR
696+
SUITE
697+
libc-math-unittests
698+
SRCS
699+
lrintf16_test.cpp
700+
HDRS
701+
RoundToIntegerTest.h
702+
DEPENDS
703+
libc.src.math.lrintf16
704+
libc.src.__support.CPP.algorithm
627705
libc.src.__support.FPUtil.fenv_impl
628706
libc.src.__support.FPUtil.fp_bits
629707
)
@@ -639,6 +717,7 @@ add_fp_unittest(
639717
RoundToIntegerTest.h
640718
DEPENDS
641719
libc.src.math.llrint
720+
libc.src.__support.CPP.algorithm
642721
libc.src.__support.FPUtil.fenv_impl
643722
libc.src.__support.FPUtil.fp_bits
644723
)
@@ -654,6 +733,7 @@ add_fp_unittest(
654733
RoundToIntegerTest.h
655734
DEPENDS
656735
libc.src.math.llrintf
736+
libc.src.__support.CPP.algorithm
657737
libc.src.__support.FPUtil.fenv_impl
658738
libc.src.__support.FPUtil.fp_bits
659739
)
@@ -669,6 +749,23 @@ add_fp_unittest(
669749
RoundToIntegerTest.h
670750
DEPENDS
671751
libc.src.math.llrintl
752+
libc.src.__support.CPP.algorithm
753+
libc.src.__support.FPUtil.fenv_impl
754+
libc.src.__support.FPUtil.fp_bits
755+
)
756+
757+
add_fp_unittest(
758+
llrintf16_test
759+
NEED_MPFR
760+
SUITE
761+
libc-math-unittests
762+
SRCS
763+
llrintf16_test.cpp
764+
HDRS
765+
RoundToIntegerTest.h
766+
DEPENDS
767+
libc.src.math.llrintf16
768+
libc.src.__support.CPP.algorithm
672769
libc.src.__support.FPUtil.fenv_impl
673770
libc.src.__support.FPUtil.fp_bits
674771
)

libc/test/src/math/RIntTest.h

Lines changed: 12 additions & 8 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"
@@ -18,7 +19,6 @@
1819

1920
#include "hdr/fenv_macros.h"
2021
#include "hdr/math_macros.h"
21-
#include <stdio.h>
2222

2323
namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
2424

@@ -101,8 +101,10 @@ class RIntTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
101101
}
102102

103103
void testSubnormalRange(RIntFunc func) {
104-
constexpr StorageType COUNT = 100'001;
105-
constexpr StorageType STEP = (MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT;
104+
constexpr int COUNT = 100'001;
105+
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
106+
static_cast<StorageType>((MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT),
107+
StorageType(1));
106108
for (StorageType i = MIN_SUBNORMAL; i <= MAX_SUBNORMAL; i += STEP) {
107109
T x = FPBits(i).get_val();
108110
for (int mode : ROUNDING_MODES) {
@@ -114,15 +116,17 @@ class RIntTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
114116
}
115117

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

127131
for (int mode : ROUNDING_MODES) {
128132
LIBC_NAMESPACE::fputil::set_round(mode);

libc/test/src/math/RoundToIntegerTest.h

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_TEST_SRC_MATH_ROUNDTOINTEGERTEST_H
1010
#define LLVM_LIBC_TEST_SRC_MATH_ROUNDTOINTEGERTEST_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"
@@ -136,10 +137,13 @@ class RoundToIntegerTestTemplate
136137
return;
137138

138139
constexpr int EXPONENT_LIMIT = sizeof(I) * 8 - 1;
140+
constexpr int BIASED_EXPONENT_LIMIT = EXPONENT_LIMIT + FPBits::EXP_BIAS;
141+
if (BIASED_EXPONENT_LIMIT > FPBits::MAX_BIASED_EXPONENT)
142+
return;
139143
// We start with 1.0 so that the implicit bit for x86 long doubles
140144
// is set.
141145
FPBits bits(F(1.0));
142-
bits.set_biased_exponent(EXPONENT_LIMIT + FPBits::EXP_BIAS);
146+
bits.set_biased_exponent(BIASED_EXPONENT_LIMIT);
143147
bits.set_sign(Sign::NEG);
144148
bits.set_mantissa(0);
145149

@@ -200,10 +204,13 @@ class RoundToIntegerTestTemplate
200204
return;
201205

202206
constexpr int EXPONENT_LIMIT = sizeof(I) * 8 - 1;
207+
constexpr int BIASED_EXPONENT_LIMIT = EXPONENT_LIMIT + FPBits::EXP_BIAS;
208+
if (BIASED_EXPONENT_LIMIT > FPBits::MAX_BIASED_EXPONENT)
209+
return;
203210
// We start with 1.0 so that the implicit bit for x86 long doubles
204211
// is set.
205212
FPBits bits(F(1.0));
206-
bits.set_biased_exponent(EXPONENT_LIMIT + FPBits::EXP_BIAS);
213+
bits.set_biased_exponent(BIASED_EXPONENT_LIMIT);
207214
bits.set_sign(Sign::NEG);
208215
bits.set_mantissa(FPBits::FRACTION_MASK);
209216

@@ -226,8 +233,10 @@ class RoundToIntegerTestTemplate
226233
}
227234

228235
void testSubnormalRange(RoundToIntegerFunc func) {
229-
constexpr StorageType COUNT = 1'000'001;
230-
constexpr StorageType STEP = (MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT;
236+
constexpr int COUNT = 1'000'001;
237+
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
238+
static_cast<StorageType>((MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT),
239+
StorageType(1));
231240
for (StorageType i = MIN_SUBNORMAL; i <= MAX_SUBNORMAL; i += STEP) {
232241
F x = FPBits(i).get_val();
233242
if (x == F(0.0))
@@ -268,15 +277,17 @@ class RoundToIntegerTestTemplate
268277
if (sizeof(I) > sizeof(long))
269278
return;
270279

271-
constexpr StorageType COUNT = 1'000'001;
272-
constexpr StorageType STEP = (MAX_NORMAL - MIN_NORMAL) / COUNT;
280+
constexpr int COUNT = 1'000'001;
281+
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
282+
static_cast<StorageType>((MAX_NORMAL - MIN_NORMAL) / COUNT),
283+
StorageType(1));
273284
for (StorageType i = MIN_NORMAL; i <= MAX_NORMAL; i += STEP) {
274-
F x = FPBits(i).get_val();
285+
FPBits xbits(i);
286+
F x = xbits.get_val();
275287
// In normal range on x86 platforms, the long double implicit 1 bit can be
276288
// zero making the numbers NaN. We will skip them.
277-
if (isnan(x)) {
289+
if (xbits.is_nan())
278290
continue;
279-
}
280291

281292
if (TestModes) {
282293
for (int m : ROUNDING_MODES) {

libc/test/src/math/llrintf16_test.cpp

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

0 commit comments

Comments
 (0)