Skip to content

Commit a886cb8

Browse files
committed
[libc][math][c23] Add lrintf16 C23 math function
1 parent 3599323 commit a886cb8

File tree

11 files changed

+107
-3
lines changed

11 files changed

+107
-3
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
502502
libc.src.math.ceilf16
503503
libc.src.math.fabsf16
504504
libc.src.math.floorf16
505+
libc.src.math.lrintf16
505506
libc.src.math.nearbyintf16
506507
libc.src.math.rintf16
507508
libc.src.math.roundf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
535535
libc.src.math.ceilf16
536536
libc.src.math.fabsf16
537537
libc.src.math.floorf16
538+
libc.src.math.lrintf16
538539
libc.src.math.nearbyintf16
539540
libc.src.math.rintf16
540541
libc.src.math.roundf16

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ Basic Operations
180180
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
181181
| logb | |check| | |check| | |check| | | |check| | 7.12.6.17 | F.10.3.17 |
182182
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
183-
| lrint | |check| | |check| | |check| | | |check| | 7.12.9.5 | F.10.6.5 |
183+
| lrint | |check| | |check| | |check| | |check| | |check| | 7.12.9.5 | F.10.6.5 |
184184
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
185185
| lround | |check| | |check| | |check| | | |check| | 7.12.9.7 | F.10.6.7 |
186186
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ def StdC : StandardSpec<"stdc"> {
595595
FunctionSpec<"lrint", RetValSpec<LongType>, [ArgSpec<DoubleType>]>,
596596
FunctionSpec<"lrintf", RetValSpec<LongType>, [ArgSpec<FloatType>]>,
597597
FunctionSpec<"lrintl", RetValSpec<LongType>, [ArgSpec<LongDoubleType>]>,
598+
GuardedFunctionSpec<"lrintf16", RetValSpec<LongType>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
598599
GuardedFunctionSpec<"lrintf128", RetValSpec<LongType>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
599600

600601
FunctionSpec<"llrint", RetValSpec<LongLongType>, [ArgSpec<DoubleType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ add_math_entrypoint_object(llroundf128)
235235
add_math_entrypoint_object(lrint)
236236
add_math_entrypoint_object(lrintf)
237237
add_math_entrypoint_object(lrintl)
238+
add_math_entrypoint_object(lrintf16)
238239
add_math_entrypoint_object(lrintf128)
239240

240241
add_math_entrypoint_object(lround)

libc/src/math/generic/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,19 @@ add_entrypoint_object(
724724
libc.src.__support.FPUtil.nearest_integer_operations
725725
)
726726

727+
add_entrypoint_object(
728+
lrintf16
729+
SRCS
730+
lrintf16.cpp
731+
HDRS
732+
../lrintf16.h
733+
COMPILE_OPTIONS
734+
-O3
735+
DEPENDS
736+
libc.src.__support.macros.properties.types
737+
libc.src.__support.FPUtil.nearest_integer_operations
738+
)
739+
727740
add_entrypoint_object(
728741
lrintf128
729742
SRCS

libc/src/math/generic/lrintf16.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of lrintf16 function -------------------------------===//
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 "src/math/lrintf16.h"
10+
#include "src/__support/FPUtil/NearestIntegerOperations.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(long, lrintf16, (float16 x)) {
16+
return fputil::round_to_signed_integer_using_current_rounding_mode<float16,
17+
long>(x);
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

libc/src/math/lrintf16.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation header for lrintf16 ----------------------*- 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_SRC_MATH_LRINTF16_H
10+
#define LLVM_LIBC_SRC_MATH_LRINTF16_H
11+
12+
#include "src/__support/macros/properties/types.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
long lrintf16(float16 x);
17+
18+
} // namespace LIBC_NAMESPACE
19+
20+
#endif // LLVM_LIBC_SRC_MATH_LRINTF16_H

libc/test/src/math/smoke/CMakeLists.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ add_fp_unittest(
511511
libc.src.fenv.feraiseexcept
512512
libc.src.fenv.fetestexcept
513513
libc.src.math.lround
514+
libc.src.__support.CPP.algorithm
514515
libc.src.__support.FPUtil.fp_bits
515516
)
516517

@@ -528,6 +529,7 @@ add_fp_unittest(
528529
libc.src.fenv.feraiseexcept
529530
libc.src.fenv.fetestexcept
530531
libc.src.math.lroundf
532+
libc.src.__support.CPP.algorithm
531533
libc.src.__support.FPUtil.fp_bits
532534
)
533535

@@ -545,6 +547,7 @@ add_fp_unittest(
545547
libc.src.fenv.feraiseexcept
546548
libc.src.fenv.fetestexcept
547549
libc.src.math.lroundl
550+
libc.src.__support.CPP.algorithm
548551
libc.src.__support.FPUtil.fp_bits
549552
)
550553

@@ -562,6 +565,7 @@ add_fp_unittest(
562565
libc.src.fenv.feraiseexcept
563566
libc.src.fenv.fetestexcept
564567
libc.src.math.lroundf128
568+
libc.src.__support.CPP.algorithm
565569
libc.src.__support.FPUtil.fp_bits
566570
)
567571

@@ -579,6 +583,7 @@ add_fp_unittest(
579583
libc.src.fenv.feraiseexcept
580584
libc.src.fenv.fetestexcept
581585
libc.src.math.llround
586+
libc.src.__support.CPP.algorithm
582587
libc.src.__support.FPUtil.fp_bits
583588
)
584589

@@ -596,6 +601,7 @@ add_fp_unittest(
596601
libc.src.fenv.feraiseexcept
597602
libc.src.fenv.fetestexcept
598603
libc.src.math.llroundf
604+
libc.src.__support.CPP.algorithm
599605
libc.src.__support.FPUtil.fp_bits
600606
)
601607

@@ -613,6 +619,7 @@ add_fp_unittest(
613619
libc.src.fenv.feraiseexcept
614620
libc.src.fenv.fetestexcept
615621
libc.src.math.llroundl
622+
libc.src.__support.CPP.algorithm
616623
libc.src.__support.FPUtil.fp_bits
617624
)
618625

@@ -630,6 +637,7 @@ add_fp_unittest(
630637
libc.src.fenv.feraiseexcept
631638
libc.src.fenv.fetestexcept
632639
libc.src.math.llroundf128
640+
libc.src.__support.CPP.algorithm
633641
libc.src.__support.FPUtil.fp_bits
634642
)
635643

@@ -713,6 +721,7 @@ add_fp_unittest(
713721
RoundToIntegerTest.h
714722
DEPENDS
715723
libc.src.math.lrint
724+
libc.src.__support.CPP.algorithm
716725
libc.src.__support.FPUtil.fenv_impl
717726
libc.src.__support.FPUtil.fp_bits
718727
)
@@ -727,6 +736,7 @@ add_fp_unittest(
727736
RoundToIntegerTest.h
728737
DEPENDS
729738
libc.src.math.lrintf
739+
libc.src.__support.CPP.algorithm
730740
libc.src.__support.FPUtil.fenv_impl
731741
libc.src.__support.FPUtil.fp_bits
732742
)
@@ -741,6 +751,22 @@ add_fp_unittest(
741751
RoundToIntegerTest.h
742752
DEPENDS
743753
libc.src.math.lrintl
754+
libc.src.__support.CPP.algorithm
755+
libc.src.__support.FPUtil.fenv_impl
756+
libc.src.__support.FPUtil.fp_bits
757+
)
758+
759+
add_fp_unittest(
760+
lrintf16_test
761+
SUITE
762+
libc-math-smoke-tests
763+
SRCS
764+
lrintf16_test.cpp
765+
HDRS
766+
RoundToIntegerTest.h
767+
DEPENDS
768+
libc.src.math.lrintf16
769+
libc.src.__support.CPP.algorithm
744770
libc.src.__support.FPUtil.fenv_impl
745771
libc.src.__support.FPUtil.fp_bits
746772
)
@@ -755,6 +781,7 @@ add_fp_unittest(
755781
RoundToIntegerTest.h
756782
DEPENDS
757783
libc.src.math.lrintf128
784+
libc.src.__support.CPP.algorithm
758785
libc.src.__support.FPUtil.fenv_impl
759786
libc.src.__support.FPUtil.fp_bits
760787
)
@@ -769,6 +796,7 @@ add_fp_unittest(
769796
RoundToIntegerTest.h
770797
DEPENDS
771798
libc.src.math.llrint
799+
libc.src.__support.CPP.algorithm
772800
libc.src.__support.FPUtil.fenv_impl
773801
libc.src.__support.FPUtil.fp_bits
774802
)
@@ -783,6 +811,7 @@ add_fp_unittest(
783811
RoundToIntegerTest.h
784812
DEPENDS
785813
libc.src.math.llrintf
814+
libc.src.__support.CPP.algorithm
786815
libc.src.__support.FPUtil.fenv_impl
787816
libc.src.__support.FPUtil.fp_bits
788817
)
@@ -797,6 +826,7 @@ add_fp_unittest(
797826
RoundToIntegerTest.h
798827
DEPENDS
799828
libc.src.math.llrintl
829+
libc.src.__support.CPP.algorithm
800830
libc.src.__support.FPUtil.fenv_impl
801831
libc.src.__support.FPUtil.fp_bits
802832
)
@@ -811,6 +841,7 @@ add_fp_unittest(
811841
RoundToIntegerTest.h
812842
DEPENDS
813843
libc.src.math.llrintf128
844+
libc.src.__support.CPP.algorithm
814845
libc.src.__support.FPUtil.fenv_impl
815846
libc.src.__support.FPUtil.fp_bits
816847
)

libc/test/src/math/smoke/RoundToIntegerTest.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIBC_TEST_SRC_MATH_SMOKE_ROUNDTOINTEGERTEST_H
1010
#define LLVM_LIBC_TEST_SRC_MATH_SMOKE_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"
@@ -113,8 +114,10 @@ class RoundToIntegerTestTemplate
113114
}
114115

115116
void testSubnormalRange(RoundToIntegerFunc func) {
116-
constexpr StorageType COUNT = 1'000'001;
117-
constexpr StorageType STEP = (MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT;
117+
constexpr int COUNT = 1'000'001;
118+
constexpr StorageType STEP = LIBC_NAMESPACE::cpp::max(
119+
static_cast<StorageType>((MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT),
120+
StorageType(1));
118121
for (StorageType i = MIN_SUBNORMAL; i <= MAX_SUBNORMAL; i += STEP) {
119122
F x = FPBits(i).get_val();
120123
if (x == F(0.0))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for lrintf16 --------------------------------------------===//
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/lrintf16.h"
12+
13+
LIST_ROUND_TO_INTEGER_TESTS_WITH_MODES(float16, long, LIBC_NAMESPACE::lrintf16)

0 commit comments

Comments
 (0)