Skip to content

Commit 846a5fb

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

File tree

13 files changed

+176
-1
lines changed

13 files changed

+176
-1
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
542542
libc.src.math.rintf16
543543
libc.src.math.roundf16
544544
libc.src.math.roundevenf16
545+
libc.src.math.setpayloadf16
545546
libc.src.math.totalorderf16
546547
libc.src.math.totalordermagf16
547548
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
@@ -572,6 +572,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
572572
libc.src.math.rintf16
573573
libc.src.math.roundf16
574574
libc.src.math.roundevenf16
575+
libc.src.math.setpayloadf16
575576
libc.src.math.totalorderf16
576577
libc.src.math.totalordermagf16
577578
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
@@ -716,6 +716,8 @@ def StdC : StandardSpec<"stdc"> {
716716
GuardedFunctionSpec<"totalordermagf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Ptr>], "LIBC_TYPES_HAS_FLOAT16">,
717717

718718
GuardedFunctionSpec<"getpayloadf16", RetValSpec<Float16Type>, [ArgSpec<Float16Ptr>], "LIBC_TYPES_HAS_FLOAT16">,
719+
720+
GuardedFunctionSpec<"setpayloadf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
719721
]
720722
>;
721723

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
@@ -348,6 +348,8 @@ add_math_entrypoint_object(scalbnf)
348348
add_math_entrypoint_object(scalbnl)
349349
add_math_entrypoint_object(scalbnf128)
350350

351+
add_math_entrypoint_object(setpayloadf16)
352+
351353
add_math_entrypoint_object(sincos)
352354
add_math_entrypoint_object(sincosf)
353355

libc/src/math/generic/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3613,3 +3613,15 @@ add_entrypoint_object(
36133613
COMPILE_OPTIONS
36143614
-O3
36153615
)
3616+
3617+
add_entrypoint_object(
3618+
setpayloadf16
3619+
SRCS
3620+
setpayloadf16.cpp
3621+
HDRS
3622+
../setpayloadf16.h
3623+
DEPENDS
3624+
libc.src.__support.FPUtil.basic_operations
3625+
COMPILE_OPTIONS
3626+
-O3
3627+
)
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

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3555,3 +3555,15 @@ add_fp_unittest(
35553555
DEPENDS
35563556
libc.src.math.getpayloadf16
35573557
)
3558+
3559+
add_fp_unittest(
3560+
setpayloadf16_test
3561+
SUITE
3562+
libc-math-smoke-tests
3563+
SRCS
3564+
setpayloadf16_test.cpp
3565+
HDRS
3566+
SetPayloadTest.h
3567+
DEPENDS
3568+
libc.src.math.setpayloadf16
3569+
)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===-- Utility class to test different flavors of setpayload ---*- 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 LIBC_TEST_SRC_MATH_SMOKE_SETPAYLOADTEST_H
10+
#define LIBC_TEST_SRC_MATH_SMOKE_SETPAYLOADTEST_H
11+
12+
#include "test/UnitTest/FEnvSafeTest.h"
13+
#include "test/UnitTest/FPMatcher.h"
14+
#include "test/UnitTest/Test.h"
15+
16+
template <typename T>
17+
class SetPayloadTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
18+
19+
DECLARE_SPECIAL_CONSTANTS(T)
20+
21+
public:
22+
typedef int (*SetPayloadFunc)(T *, T);
23+
24+
void testInvalidPayloads(SetPayloadFunc func) {
25+
T res;
26+
27+
EXPECT_EQ(1, func(&res, T(aNaN)));
28+
EXPECT_EQ(1, func(&res, T(neg_aNaN)));
29+
EXPECT_EQ(1, func(&res, T(inf)));
30+
EXPECT_EQ(1, func(&res, T(neg_inf)));
31+
EXPECT_EQ(1, func(&res, T(0.1)));
32+
EXPECT_EQ(1, func(&res, T(-0.1)));
33+
EXPECT_EQ(1, func(&res, T(-1.0)));
34+
EXPECT_EQ(1, func(&res, T(0x42.1p+0)));
35+
EXPECT_EQ(1, func(&res, T(-0x42.1p+0)));
36+
EXPECT_EQ(1, func(&res, T(StorageType(1) << (FPBits::FRACTION_LEN - 1))));
37+
}
38+
39+
void testValidPayloads(SetPayloadFunc func) {
40+
T res;
41+
42+
EXPECT_EQ(0, func(&res, T(0.0)));
43+
EXPECT_EQ(FPBits(aNaN).uintval(), FPBits(res).uintval());
44+
EXPECT_EQ(0, func(&res, T(1.0)));
45+
EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 1).uintval(), FPBits(res).uintval());
46+
EXPECT_EQ(0, func(&res, T(0x42.0p+0)));
47+
EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 0x42).uintval(),
48+
FPBits(res).uintval());
49+
EXPECT_EQ(0, func(&res, T(0x123.0p+0)));
50+
EXPECT_EQ(FPBits::quiet_nan(Sign::POS, 0x123).uintval(),
51+
FPBits(res).uintval());
52+
EXPECT_EQ(0, func(&res, T(FPBits::FRACTION_MASK >> 1)));
53+
EXPECT_EQ(
54+
FPBits::quiet_nan(Sign::POS, FPBits::FRACTION_MASK >> 1).uintval(),
55+
FPBits(res).uintval());
56+
}
57+
};
58+
59+
#define LIST_SETPAYLOAD_TESTS(T, func) \
60+
using LlvmLibcSetPayloadTest = SetPayloadTestTemplate<T>; \
61+
TEST_F(LlvmLibcSetPayloadTest, InvalidPayloads) { \
62+
testInvalidPayloads(&func); \
63+
} \
64+
TEST_F(LlvmLibcSetPayloadTest, ValidPayloads) { testValidPayloads(&func); }
65+
66+
#endif // LIBC_TEST_SRC_MATH_SMOKE_SETPAYLOADTEST_H
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-- Unittests for setpayloadf16 ---------------------------------------===//
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 "SetPayloadTest.h"
10+
11+
#include "src/math/setpayloadf16.h"
12+
13+
LIST_SETPAYLOAD_TESTS(float16, LIBC_NAMESPACE::setpayloadf16)

0 commit comments

Comments
 (0)