Skip to content

Commit 2635d04

Browse files
authored
[libc][math][c23] Add {nearbyint,rint,lrint,llrint,lround,llround}f16 C23 math functions (#94218)
#93566
1 parent d999ce0 commit 2635d04

26 files changed

+552
-32
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,12 @@ if(LIBC_TYPES_HAS_FLOAT16)
502502
libc.src.math.ceilf16
503503
libc.src.math.fabsf16
504504
libc.src.math.floorf16
505+
libc.src.math.llrintf16
506+
libc.src.math.llroundf16
507+
libc.src.math.lrintf16
508+
libc.src.math.lroundf16
509+
libc.src.math.nearbyintf16
510+
libc.src.math.rintf16
505511
libc.src.math.roundf16
506512
libc.src.math.roundevenf16
507513
libc.src.math.truncf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,12 @@ if(LIBC_TYPES_HAS_FLOAT16)
535535
libc.src.math.ceilf16
536536
libc.src.math.fabsf16
537537
libc.src.math.floorf16
538+
libc.src.math.llrintf16
539+
libc.src.math.llroundf16
540+
libc.src.math.lrintf16
541+
libc.src.math.lroundf16
542+
libc.src.math.nearbyintf16
543+
libc.src.math.rintf16
538544
libc.src.math.roundf16
539545
libc.src.math.roundevenf16
540546
libc.src.math.truncf16

libc/docs/math/index.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,21 +174,21 @@ Basic Operations
174174
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
175175
| llogb | |check| | |check| | |check| | | |check| | 7.12.6.10 | F.10.3.10 |
176176
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
177-
| llrint | |check| | |check| | |check| | | |check| | 7.12.9.5 | F.10.6.5 |
177+
| llrint | |check| | |check| | |check| | |check| | |check| | 7.12.9.5 | F.10.6.5 |
178178
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
179-
| llround | |check| | |check| | |check| | | |check| | 7.12.9.7 | F.10.6.7 |
179+
| llround | |check| | |check| | |check| | |check| | |check| | 7.12.9.7 | F.10.6.7 |
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
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
185-
| lround | |check| | |check| | |check| | | |check| | 7.12.9.7 | F.10.6.7 |
185+
| lround | |check| | |check| | |check| | |check| | |check| | 7.12.9.7 | F.10.6.7 |
186186
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
187187
| modf | |check| | |check| | |check| | | |check| | 7.12.6.18 | F.10.3.18 |
188188
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
189189
| nan | |check| | |check| | |check| | | |check| | 7.12.11.2 | F.10.8.2 |
190190
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
191-
| nearbyint | |check| | |check| | |check| | | |check| | 7.12.9.3 | F.10.6.3 |
191+
| nearbyint | |check| | |check| | |check| | |check| | |check| | 7.12.9.3 | F.10.6.3 |
192192
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
193193
| nextafter | |check| | |check| | |check| | | |check| | 7.12.11.3 | F.10.8.3 |
194194
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
@@ -202,7 +202,7 @@ Basic Operations
202202
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
203203
| remquo | |check| | |check| | |check| | | | 7.12.10.3 | F.10.7.3 |
204204
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
205-
| rint | |check| | |check| | |check| | | |check| | 7.12.9.4 | F.10.6.4 |
205+
| rint | |check| | |check| | |check| | |check| | |check| | 7.12.9.4 | F.10.6.4 |
206206
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
207207
| round | |check| | |check| | |check| | |check| | |check| | 7.12.9.6 | F.10.6.6 |
208208
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/spec/stdc.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,26 +579,31 @@ def StdC : StandardSpec<"stdc"> {
579579
FunctionSpec<"lround", RetValSpec<LongType>, [ArgSpec<DoubleType>]>,
580580
FunctionSpec<"lroundf", RetValSpec<LongType>, [ArgSpec<FloatType>]>,
581581
FunctionSpec<"lroundl", RetValSpec<LongType>, [ArgSpec<LongDoubleType>]>,
582+
GuardedFunctionSpec<"lroundf16", RetValSpec<LongType>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
582583
GuardedFunctionSpec<"lroundf128", RetValSpec<LongType>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
583584

584585
FunctionSpec<"llround", RetValSpec<LongLongType>, [ArgSpec<DoubleType>]>,
585586
FunctionSpec<"llroundf", RetValSpec<LongLongType>, [ArgSpec<FloatType>]>,
586587
FunctionSpec<"llroundl", RetValSpec<LongLongType>, [ArgSpec<LongDoubleType>]>,
588+
GuardedFunctionSpec<"llroundf16", RetValSpec<LongLongType>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
587589
GuardedFunctionSpec<"llroundf128", RetValSpec<LongLongType>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
588590

589591
FunctionSpec<"rint", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
590592
FunctionSpec<"rintf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
591593
FunctionSpec<"rintl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
594+
GuardedFunctionSpec<"rintf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
592595
GuardedFunctionSpec<"rintf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
593596

594597
FunctionSpec<"lrint", RetValSpec<LongType>, [ArgSpec<DoubleType>]>,
595598
FunctionSpec<"lrintf", RetValSpec<LongType>, [ArgSpec<FloatType>]>,
596599
FunctionSpec<"lrintl", RetValSpec<LongType>, [ArgSpec<LongDoubleType>]>,
600+
GuardedFunctionSpec<"lrintf16", RetValSpec<LongType>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
597601
GuardedFunctionSpec<"lrintf128", RetValSpec<LongType>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
598602

599603
FunctionSpec<"llrint", RetValSpec<LongLongType>, [ArgSpec<DoubleType>]>,
600604
FunctionSpec<"llrintf", RetValSpec<LongLongType>, [ArgSpec<FloatType>]>,
601605
FunctionSpec<"llrintl", RetValSpec<LongLongType>, [ArgSpec<LongDoubleType>]>,
606+
GuardedFunctionSpec<"llrintf16", RetValSpec<LongLongType>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
602607
GuardedFunctionSpec<"llrintf128", RetValSpec<LongLongType>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
603608

604609
FunctionSpec<"sqrt", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
@@ -615,6 +620,7 @@ def StdC : StandardSpec<"stdc"> {
615620
FunctionSpec<"nearbyint", RetValSpec<DoubleType>, [ArgSpec<DoubleType>]>,
616621
FunctionSpec<"nearbyintf", RetValSpec<FloatType>, [ArgSpec<FloatType>]>,
617622
FunctionSpec<"nearbyintl", RetValSpec<LongDoubleType>, [ArgSpec<LongDoubleType>]>,
623+
GuardedFunctionSpec<"nearbyintf16", RetValSpec<Float16Type>, [ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
618624
GuardedFunctionSpec<"nearbyintf128", RetValSpec<Float128Type>, [ArgSpec<Float128Type>], "LIBC_TYPES_HAS_FLOAT128">,
619625

620626
FunctionSpec<"nextafterf", RetValSpec<FloatType>, [ArgSpec<FloatType>, ArgSpec<FloatType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,21 +225,25 @@ add_math_entrypoint_object(logbf128)
225225
add_math_entrypoint_object(llrint)
226226
add_math_entrypoint_object(llrintf)
227227
add_math_entrypoint_object(llrintl)
228+
add_math_entrypoint_object(llrintf16)
228229
add_math_entrypoint_object(llrintf128)
229230

230231
add_math_entrypoint_object(llround)
231232
add_math_entrypoint_object(llroundf)
232233
add_math_entrypoint_object(llroundl)
234+
add_math_entrypoint_object(llroundf16)
233235
add_math_entrypoint_object(llroundf128)
234236

235237
add_math_entrypoint_object(lrint)
236238
add_math_entrypoint_object(lrintf)
237239
add_math_entrypoint_object(lrintl)
240+
add_math_entrypoint_object(lrintf16)
238241
add_math_entrypoint_object(lrintf128)
239242

240243
add_math_entrypoint_object(lround)
241244
add_math_entrypoint_object(lroundf)
242245
add_math_entrypoint_object(lroundl)
246+
add_math_entrypoint_object(lroundf16)
243247
add_math_entrypoint_object(lroundf128)
244248

245249
add_math_entrypoint_object(modf)
@@ -255,6 +259,7 @@ add_math_entrypoint_object(nanf128)
255259
add_math_entrypoint_object(nearbyint)
256260
add_math_entrypoint_object(nearbyintf)
257261
add_math_entrypoint_object(nearbyintl)
262+
add_math_entrypoint_object(nearbyintf16)
258263
add_math_entrypoint_object(nearbyintf128)
259264

260265
add_math_entrypoint_object(nextafter)
@@ -290,6 +295,7 @@ add_math_entrypoint_object(remquol)
290295
add_math_entrypoint_object(rint)
291296
add_math_entrypoint_object(rintf)
292297
add_math_entrypoint_object(rintl)
298+
add_math_entrypoint_object(rintf16)
293299
add_math_entrypoint_object(rintf128)
294300

295301
add_math_entrypoint_object(round)

libc/src/math/generic/CMakeLists.txt

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,19 @@ add_entrypoint_object(
564564
libc.src.__support.FPUtil.nearest_integer_operations
565565
)
566566

567+
add_entrypoint_object(
568+
lroundf16
569+
SRCS
570+
lroundf16.cpp
571+
HDRS
572+
../lroundf16.h
573+
COMPILE_OPTIONS
574+
-O3
575+
DEPENDS
576+
libc.src.__support.macros.properties.types
577+
libc.src.__support.FPUtil.nearest_integer_operations
578+
)
579+
567580
add_entrypoint_object(
568581
lroundf128
569582
SRCS
@@ -613,6 +626,19 @@ add_entrypoint_object(
613626
libc.src.__support.FPUtil.nearest_integer_operations
614627
)
615628

629+
add_entrypoint_object(
630+
llroundf16
631+
SRCS
632+
llroundf16.cpp
633+
HDRS
634+
../llroundf16.h
635+
COMPILE_OPTIONS
636+
-O3
637+
DEPENDS
638+
libc.src.__support.macros.properties.types
639+
libc.src.__support.FPUtil.nearest_integer_operations
640+
)
641+
616642
add_entrypoint_object(
617643
llroundf128
618644
SRCS
@@ -662,6 +688,19 @@ add_entrypoint_object(
662688
libc.src.__support.FPUtil.nearest_integer_operations
663689
)
664690

691+
add_entrypoint_object(
692+
rintf16
693+
SRCS
694+
rintf16.cpp
695+
HDRS
696+
../rintf16.h
697+
COMPILE_OPTIONS
698+
-O3
699+
DEPENDS
700+
libc.src.__support.macros.properties.types
701+
libc.src.__support.FPUtil.nearest_integer_operations
702+
)
703+
665704
add_entrypoint_object(
666705
rintf128
667706
SRCS
@@ -711,6 +750,19 @@ add_entrypoint_object(
711750
libc.src.__support.FPUtil.nearest_integer_operations
712751
)
713752

753+
add_entrypoint_object(
754+
lrintf16
755+
SRCS
756+
lrintf16.cpp
757+
HDRS
758+
../lrintf16.h
759+
COMPILE_OPTIONS
760+
-O3
761+
DEPENDS
762+
libc.src.__support.macros.properties.types
763+
libc.src.__support.FPUtil.nearest_integer_operations
764+
)
765+
714766
add_entrypoint_object(
715767
lrintf128
716768
SRCS
@@ -760,6 +812,19 @@ add_entrypoint_object(
760812
libc.src.__support.FPUtil.nearest_integer_operations
761813
)
762814

815+
add_entrypoint_object(
816+
llrintf16
817+
SRCS
818+
llrintf16.cpp
819+
HDRS
820+
../llrintf16.h
821+
COMPILE_OPTIONS
822+
-O3
823+
DEPENDS
824+
libc.src.__support.macros.properties.types
825+
libc.src.__support.FPUtil.nearest_integer_operations
826+
)
827+
763828
add_entrypoint_object(
764829
llrintf128
765830
SRCS
@@ -809,6 +874,19 @@ add_entrypoint_object(
809874
-O3
810875
)
811876

877+
add_entrypoint_object(
878+
nearbyintf16
879+
SRCS
880+
nearbyintf16.cpp
881+
HDRS
882+
../nearbyintf16.h
883+
DEPENDS
884+
libc.src.__support.macros.properties.types
885+
libc.src.__support.FPUtil.nearest_integer_operations
886+
COMPILE_OPTIONS
887+
-O3
888+
)
889+
812890
add_entrypoint_object(
813891
nearbyintf128
814892
SRCS

libc/src/math/generic/llrintf16.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation of llrintf16 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/llrintf16.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 long, llrintf16, (float16 x)) {
16+
return fputil::round_to_signed_integer_using_current_rounding_mode<float16,
17+
long long>(
18+
x);
19+
}
20+
21+
} // namespace LIBC_NAMESPACE

libc/src/math/generic/llroundf16.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of llroundf16 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/llroundf16.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 long, llroundf16, (float16 x)) {
16+
return fputil::round_to_signed_integer<float16, long long>(x);
17+
}
18+
19+
} // namespace LIBC_NAMESPACE

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/generic/lroundf16.cpp

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

libc/src/math/generic/rintf16.cpp

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

0 commit comments

Comments
 (0)