Skip to content

Commit 1107575

Browse files
authored
[libc][math][c23] Add {getpayload,setpayload,setpayloadsig}f16 C23 math functions (#95159)
Part of #93566.
1 parent f1edc04 commit 1107575

21 files changed

+519
-2
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
522522
libc.src.math.frexpf16
523523
libc.src.math.fromfpf16
524524
libc.src.math.fromfpxf16
525+
libc.src.math.getpayloadf16
525526
libc.src.math.ilogbf16
526527
libc.src.math.llogbf16
527528
libc.src.math.llrintf16
@@ -544,6 +545,8 @@ if(LIBC_TYPES_HAS_FLOAT16)
544545
libc.src.math.rintf16
545546
libc.src.math.roundf16
546547
libc.src.math.roundevenf16
548+
libc.src.math.setpayloadf16
549+
libc.src.math.setpayloadsigf16
547550
libc.src.math.totalorderf16
548551
libc.src.math.totalordermagf16
549552
libc.src.math.truncf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
555555
libc.src.math.frexpf16
556556
libc.src.math.fromfpf16
557557
libc.src.math.fromfpxf16
558+
libc.src.math.getpayloadf16
558559
libc.src.math.ilogbf16
559560
libc.src.math.llogbf16
560561
libc.src.math.llrintf16
@@ -574,6 +575,8 @@ if(LIBC_TYPES_HAS_FLOAT16)
574575
libc.src.math.rintf16
575576
libc.src.math.roundf16
576577
libc.src.math.roundevenf16
578+
libc.src.math.setpayloadf16
579+
libc.src.math.setpayloadsigf16
577580
libc.src.math.totalorderf16
578581
libc.src.math.totalordermagf16
579582
libc.src.math.truncf16

libc/docs/c23.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ Additions:
4444
* compoundn*
4545
* totalorder* |check|
4646
* totalordermag* |check|
47-
* getpayload*
48-
* setpayload*
47+
* getpayload* |check|
48+
* setpayload* |check|
49+
* setpayloadsig* |check|
4950
* iscannonical
5051
* issignaling
5152
* issubnormal

libc/docs/math/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ Basic Operations
170170
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
171171
| fsub | N/A | | | N/A | | 7.12.14.2 | F.10.11 |
172172
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
173+
| getpayload | | | | |check| | | F.10.13.1 | N/A |
174+
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
173175
| ilogb | |check| | |check| | |check| | |check| | |check| | 7.12.6.8 | F.10.3.8 |
174176
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
175177
| ldexp | |check| | |check| | |check| | | |check| | 7.12.6.9 | F.10.3.9 |
@@ -212,6 +214,10 @@ Basic Operations
212214
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
213215
| scalbn | |check| | |check| | |check| | | |check| | 7.12.6.19 | F.10.3.19 |
214216
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
217+
| setpayload | | | | |check| | | F.10.13.2 | N/A |
218+
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
219+
| setpayloadsig | | | | |check| | | F.10.13.3 | N/A |
220+
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
215221
| totalorder | | | | |check| | | F.10.12.1 | N/A |
216222
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
217223
| totalordermag | | | | |check| | | F.10.12.2 | N/A |

libc/spec/stdc.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,12 @@ def StdC : StandardSpec<"stdc"> {
716716

717717
GuardedFunctionSpec<"totalordermagf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Ptr>], "LIBC_TYPES_HAS_FLOAT16">,
718718

719+
GuardedFunctionSpec<"getpayloadf16", RetValSpec<Float16Type>, [ArgSpec<Float16Ptr>], "LIBC_TYPES_HAS_FLOAT16">,
720+
721+
GuardedFunctionSpec<"setpayloadf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
722+
723+
GuardedFunctionSpec<"setpayloadsigf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,
724+
719725
GuardedFunctionSpec<"f16sqrtf", RetValSpec<Float16Type>, [ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
720726
]
721727
>;

libc/src/__support/FPUtil/BasicOperations.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,48 @@ totalordermag(T x, T y) {
265265
return FPBits<T>(x).abs().uintval() <= FPBits<T>(y).abs().uintval();
266266
}
267267

268+
template <typename T>
269+
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> getpayload(T x) {
270+
using FPBits = FPBits<T>;
271+
FPBits x_bits(x);
272+
273+
if (!x_bits.is_nan())
274+
return T(-1.0);
275+
276+
return T(x_bits.uintval() & (FPBits::FRACTION_MASK >> 1));
277+
}
278+
279+
template <bool IsSignaling, 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+
// 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()) {
288+
res = FPBits::quiet_nan(Sign::POS).get_val();
289+
return false;
290+
}
291+
292+
int pl_exp = pl_bits.get_exponent();
293+
294+
if (pl_bits.is_neg() || pl_exp < 0 || pl_exp >= FPBits::FRACTION_LEN - 1 ||
295+
((pl_bits.get_mantissa() << pl_exp) & FPBits::FRACTION_MASK) != 0) {
296+
res = T(0.0);
297+
return true;
298+
}
299+
300+
using StorageType = typename FPBits::StorageType;
301+
StorageType v(pl_bits.get_explicit_mantissa() >> (FPBits::SIG_LEN - pl_exp));
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();
307+
return false;
308+
}
309+
268310
} // namespace fputil
269311
} // namespace LIBC_NAMESPACE
270312

libc/src/math/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ add_math_entrypoint_object(fromfpxl)
210210
add_math_entrypoint_object(fromfpxf16)
211211
add_math_entrypoint_object(fromfpxf128)
212212

213+
add_math_entrypoint_object(getpayloadf16)
214+
213215
add_math_entrypoint_object(hypot)
214216
add_math_entrypoint_object(hypotf)
215217

@@ -350,6 +352,10 @@ add_math_entrypoint_object(scalbnf)
350352
add_math_entrypoint_object(scalbnl)
351353
add_math_entrypoint_object(scalbnf128)
352354

355+
add_math_entrypoint_object(setpayloadf16)
356+
357+
add_math_entrypoint_object(setpayloadsigf16)
358+
353359
add_math_entrypoint_object(sincos)
354360
add_math_entrypoint_object(sincosf)
355361

libc/src/math/generic/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3602,6 +3602,45 @@ add_entrypoint_object(
36023602
-O3
36033603
)
36043604

3605+
add_entrypoint_object(
3606+
getpayloadf16
3607+
SRCS
3608+
getpayloadf16.cpp
3609+
HDRS
3610+
../getpayloadf16.h
3611+
DEPENDS
3612+
libc.src.__support.macros.properties.types
3613+
libc.src.__support.FPUtil.basic_operations
3614+
COMPILE_OPTIONS
3615+
-O3
3616+
)
3617+
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+
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+
36053644
add_entrypoint_object(
36063645
f16fmaf
36073646
SRCS
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation of getpayloadf16 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/getpayloadf16.h"
10+
#include "src/__support/FPUtil/BasicOperations.h"
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
LLVM_LIBC_FUNCTION(float16, getpayloadf16, (const float16 *x)) {
16+
return fputil::getpayload(*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 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</*IsSignaling=*/false>(*res, pl));
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 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

libc/src/math/getpayloadf16.h

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

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/src/math/setpayloadsigf16.h

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

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3553,6 +3553,42 @@ add_fp_unittest(
35533553
libc.src.math.totalordermagf16
35543554
)
35553555

3556+
add_fp_unittest(
3557+
getpayloadf16_test
3558+
SUITE
3559+
libc-math-smoke-tests
3560+
SRCS
3561+
getpayloadf16_test.cpp
3562+
HDRS
3563+
GetPayloadTest.h
3564+
DEPENDS
3565+
libc.src.math.getpayloadf16
3566+
)
3567+
3568+
add_fp_unittest(
3569+
setpayloadf16_test
3570+
SUITE
3571+
libc-math-smoke-tests
3572+
SRCS
3573+
setpayloadf16_test.cpp
3574+
HDRS
3575+
SetPayloadTest.h
3576+
DEPENDS
3577+
libc.src.math.setpayloadf16
3578+
)
3579+
3580+
add_fp_unittest(
3581+
setpayloadsigf16_test
3582+
SUITE
3583+
libc-math-smoke-tests
3584+
SRCS
3585+
setpayloadsigf16_test.cpp
3586+
HDRS
3587+
SetPayloadSigTest.h
3588+
DEPENDS
3589+
libc.src.math.setpayloadsigf16
3590+
)
3591+
35563592
add_fp_unittest(
35573593
f16fmaf_test
35583594
SUITE

0 commit comments

Comments
 (0)