Skip to content

Commit 0ff61dc

Browse files
committed
[libc][math][c23] Add setpayloadsigf16 C23 math function
1 parent 3076712 commit 0ff61dc

File tree

15 files changed

+179
-4
lines changed

15 files changed

+179
-4
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
546546
libc.src.math.roundf16
547547
libc.src.math.roundevenf16
548548
libc.src.math.setpayloadf16
549+
libc.src.math.setpayloadsigf16
549550
libc.src.math.totalorderf16
550551
libc.src.math.totalordermagf16
551552
libc.src.math.truncf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
576576
libc.src.math.roundf16
577577
libc.src.math.roundevenf16
578578
libc.src.math.setpayloadf16
579+
libc.src.math.setpayloadsigf16
579580
libc.src.math.totalorderf16
580581
libc.src.math.totalordermagf16
581582
libc.src.math.truncf16

libc/docs/c23.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Additions:
4646
* totalordermag* |check|
4747
* getpayload* |check|
4848
* setpayload* |check|
49+
* setpayloadsig* |check|
4950
* iscannonical
5051
* issignaling
5152
* issubnormal

libc/docs/math/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ Basic Operations
216216
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
217217
| setpayload | | | | |check| | | F.10.13.2 | N/A |
218218
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
219+
| setpayloadsig | | | | |check| | | F.10.13.3 | N/A |
220+
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
219221
| totalorder | | | | |check| | | F.10.12.1 | N/A |
220222
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
221223
| totalordermag | | | | |check| | | F.10.12.2 | N/A |

libc/spec/stdc.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,8 @@ def StdC : StandardSpec<"stdc"> {
720720

721721
GuardedFunctionSpec<"setpayloadf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
722722

723+
GuardedFunctionSpec<"setpayloadsigf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
724+
723725
GuardedFunctionSpec<"f16sqrtf", RetValSpec<Float16Type>, [ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
724726
]
725727
>;

libc/src/__support/FPUtil/BasicOperations.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,15 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> getpayload(T x) {
276276
return T(x_bits.uintval() & (FPBits::FRACTION_MASK >> 1));
277277
}
278278

279-
template <typename T>
279+
template <bool IsSignaling, typename T>
280280
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, bool>
281281
setpayload(T *res, T pl) {
282282
using FPBits = FPBits<T>;
283283
FPBits pl_bits(pl);
284284

285-
if (pl_bits.is_zero()) {
285+
// Signaling NaNs don't have the mantissa's MSB set to 1, so they need a
286+
// non-zero payload to distinguish them from infinities.
287+
if (!IsSignaling && pl_bits.is_zero()) {
286288
*res = FPBits::quiet_nan(Sign::POS).get_val();
287289
return false;
288290
}
@@ -297,7 +299,11 @@ setpayload(T *res, T pl) {
297299

298300
using StorageType = typename FPBits::StorageType;
299301
StorageType v(pl_bits.get_explicit_mantissa() >> (FPBits::SIG_LEN - pl_exp));
300-
*res = FPBits::quiet_nan(Sign::POS, v).get_val();
302+
303+
if constexpr (IsSignaling)
304+
*res = FPBits::signaling_nan(Sign::POS, v).get_val();
305+
else
306+
*res = FPBits::quiet_nan(Sign::POS, v).get_val();
301307
return false;
302308
}
303309

libc/src/math/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ add_math_entrypoint_object(scalbnf128)
354354

355355
add_math_entrypoint_object(setpayloadf16)
356356

357+
add_math_entrypoint_object(setpayloadsigf16)
358+
357359
add_math_entrypoint_object(sincos)
358360
add_math_entrypoint_object(sincosf)
359361

libc/src/math/generic/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3628,6 +3628,19 @@ add_entrypoint_object(
36283628
-O3
36293629
)
36303630

3631+
add_entrypoint_object(
3632+
setpayloadsigf16
3633+
SRCS
3634+
setpayloadsigf16.cpp
3635+
HDRS
3636+
../setpayloadsigf16.h
3637+
DEPENDS
3638+
libc.src.__support.macros.properties.types
3639+
libc.src.__support.FPUtil.basic_operations
3640+
COMPILE_OPTIONS
3641+
-O3
3642+
)
3643+
36313644
add_entrypoint_object(
36323645
f16fmaf
36333646
SRCS

libc/src/math/generic/setpayloadf16.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
namespace LIBC_NAMESPACE {
1414

1515
LLVM_LIBC_FUNCTION(int, setpayloadf16, (float16 * res, float16 pl)) {
16-
return static_cast<int>(fputil::setpayload(res, pl));
16+
return static_cast<int>(fputil::setpayload</*IsSignaling=*/false>(res, pl));
1717
}
1818

1919
} // namespace LIBC_NAMESPACE
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of setpayloadsigf16 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/setpayloadsigf16.h"
10+
#include "src/__support/FPUtil/BasicOperations.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(int, setpayloadsigf16, (float16 * res, float16 pl)) {
16+
return static_cast<int>(fputil::setpayload</*IsSignaling=*/true>(res, pl));
17+
}
18+
19+
} // namespace LIBC_NAMESPACE

0 commit comments

Comments
 (0)