Skip to content

Commit f9fb6c9

Browse files
committed
[libc][math][c23] Add setpayloadsigf16 C23 math function
1 parent 846a5fb commit f9fb6c9

File tree

15 files changed

+178
-4
lines changed

15 files changed

+178
-4
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
543543
libc.src.math.roundf16
544544
libc.src.math.roundevenf16
545545
libc.src.math.setpayloadf16
546+
libc.src.math.setpayloadsigf16
546547
libc.src.math.totalorderf16
547548
libc.src.math.totalordermagf16
548549
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
@@ -573,6 +573,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
573573
libc.src.math.roundf16
574574
libc.src.math.roundevenf16
575575
libc.src.math.setpayloadf16
576+
libc.src.math.setpayloadsigf16
576577
libc.src.math.totalorderf16
577578
libc.src.math.totalordermagf16
578579
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
@@ -214,6 +214,8 @@ Basic Operations
214214
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
215215
| setpayload | | | | |check| | | F.10.13.2 | N/A |
216216
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
217+
| setpayloadsig | | | | |check| | | F.10.13.3 | N/A |
218+
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
217219
| totalorder | | | | |check| | | F.10.12.1 | N/A |
218220
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
219221
| 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
@@ -718,6 +718,8 @@ def StdC : StandardSpec<"stdc"> {
718718
GuardedFunctionSpec<"getpayloadf16", RetValSpec<Float16Type>, [ArgSpec<Float16Ptr>], "LIBC_TYPES_HAS_FLOAT16">,
719719

720720
GuardedFunctionSpec<"setpayloadf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
721+
722+
GuardedFunctionSpec<"setpayloadsigf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
721723
]
722724
>;
723725

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
@@ -350,6 +350,8 @@ add_math_entrypoint_object(scalbnf128)
350350

351351
add_math_entrypoint_object(setpayloadf16)
352352

353+
add_math_entrypoint_object(setpayloadsigf16)
354+
353355
add_math_entrypoint_object(sincos)
354356
add_math_entrypoint_object(sincosf)
355357

libc/src/math/generic/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3625,3 +3625,15 @@ add_entrypoint_object(
36253625
COMPILE_OPTIONS
36263626
-O3
36273627
)
3628+
3629+
add_entrypoint_object(
3630+
setpayloadsigf16
3631+
SRCS
3632+
setpayloadsigf16.cpp
3633+
HDRS
3634+
../setpayloadsigf16.h
3635+
DEPENDS
3636+
libc.src.__support.FPUtil.basic_operations
3637+
COMPILE_OPTIONS
3638+
-O3
3639+
)

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)