Skip to content

Commit b841bd8

Browse files
committed
[libc][math][c23] Add setpayloadf16 C23 math function
1 parent 7c033a8 commit b841bd8

File tree

13 files changed

+177
-1
lines changed

13 files changed

+177
-1
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.rintf16
544544
libc.src.math.roundf16
545545
libc.src.math.roundevenf16
546+
libc.src.math.setpayloadf16
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.rintf16
574574
libc.src.math.roundf16
575575
libc.src.math.roundevenf16
576+
libc.src.math.setpayloadf16
576577
libc.src.math.totalorderf16
577578
libc.src.math.totalordermagf16
578579
libc.src.math.truncf16

libc/docs/c23.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Additions:
4545
* totalorder* |check|
4646
* totalordermag* |check|
4747
* getpayload* |check|
48-
* setpayload*
48+
* setpayload* |check|
4949
* iscannonical
5050
* issignaling
5151
* issubnormal

libc/docs/math/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ Basic Operations
212212
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
213213
| scalbn | |check| | |check| | |check| | | |check| | 7.12.6.19 | F.10.3.19 |
214214
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
215+
| setpayload | | | | |check| | | F.10.13.2 | N/A |
216+
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
215217
| totalorder | | | | |check| | | F.10.12.1 | N/A |
216218
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
217219
| 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
@@ -717,6 +717,8 @@ def StdC : StandardSpec<"stdc"> {
717717

718718
GuardedFunctionSpec<"getpayloadf16", RetValSpec<Float16Type>, [ArgSpec<Float16Ptr>], "LIBC_TYPES_HAS_FLOAT16">,
719719

720+
GuardedFunctionSpec<"setpayloadf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
721+
720722
GuardedFunctionSpec<"f16sqrtf", RetValSpec<Float16Type>, [ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
721723
]
722724
>;

libc/src/__support/FPUtil/BasicOperations.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,31 @@ 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>
280+
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, bool>
281+
setpayload(T *res, T pl) {
282+
using FPBits = FPBits<T>;
283+
FPBits pl_bits(pl);
284+
285+
if (pl_bits.is_zero()) {
286+
*res = FPBits::quiet_nan(Sign::POS).get_val();
287+
return false;
288+
}
289+
290+
int pl_exp = pl_bits.get_exponent();
291+
292+
if (pl_bits.is_neg() || pl_exp < 0 || pl_exp >= FPBits::FRACTION_LEN - 1 ||
293+
((pl_bits.get_mantissa() << pl_exp) & FPBits::FRACTION_MASK) != 0) {
294+
*res = T(0.0);
295+
return true;
296+
}
297+
298+
using StorageType = typename FPBits::StorageType;
299+
StorageType v(pl_bits.get_explicit_mantissa() >> (FPBits::SIG_LEN - pl_exp));
300+
*res = FPBits::quiet_nan(Sign::POS, v).get_val();
301+
return false;
302+
}
303+
279304
} // namespace fputil
280305
} // namespace LIBC_NAMESPACE
281306

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(scalbnf)
350350
add_math_entrypoint_object(scalbnl)
351351
add_math_entrypoint_object(scalbnf128)
352352

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

libc/src/math/generic/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3615,6 +3615,19 @@ add_entrypoint_object(
36153615
-O3
36163616
)
36173617

3618+
add_entrypoint_object(
3619+
setpayloadf16
3620+
SRCS
3621+
setpayloadf16.cpp
3622+
HDRS
3623+
../setpayloadf16.h
3624+
DEPENDS
3625+
libc.src.__support.macros.properties.types
3626+
libc.src.__support.FPUtil.basic_operations
3627+
COMPILE_OPTIONS
3628+
-O3
3629+
)
3630+
36183631
add_entrypoint_object(
36193632
f16sqrtf
36203633
SRCS
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of setpayloadf16 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/setpayloadf16.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, setpayloadf16, (float16 * res, float16 pl)) {
16+
return static_cast<int>(fputil::setpayload(res, pl));
17+
}
18+
19+
} // namespace LIBC_NAMESPACE

libc/src/math/setpayloadf16.h

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

0 commit comments

Comments
 (0)