Skip to content

[libc][math][c23] Add {getpayload,setpayload,setpayloadsig}f16 C23 math functions #95159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.frexpf16
libc.src.math.fromfpf16
libc.src.math.fromfpxf16
libc.src.math.getpayloadf16
libc.src.math.ilogbf16
libc.src.math.llogbf16
libc.src.math.llrintf16
Expand All @@ -544,6 +545,8 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.rintf16
libc.src.math.roundf16
libc.src.math.roundevenf16
libc.src.math.setpayloadf16
libc.src.math.setpayloadsigf16
libc.src.math.totalorderf16
libc.src.math.totalordermagf16
libc.src.math.truncf16
Expand Down
3 changes: 3 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.frexpf16
libc.src.math.fromfpf16
libc.src.math.fromfpxf16
libc.src.math.getpayloadf16
libc.src.math.ilogbf16
libc.src.math.llogbf16
libc.src.math.llrintf16
Expand All @@ -574,6 +575,8 @@ if(LIBC_TYPES_HAS_FLOAT16)
libc.src.math.rintf16
libc.src.math.roundf16
libc.src.math.roundevenf16
libc.src.math.setpayloadf16
libc.src.math.setpayloadsigf16
libc.src.math.totalorderf16
libc.src.math.totalordermagf16
libc.src.math.truncf16
Expand Down
5 changes: 3 additions & 2 deletions libc/docs/c23.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ Additions:
* compoundn*
* totalorder* |check|
* totalordermag* |check|
* getpayload*
* setpayload*
* getpayload* |check|
* setpayload* |check|
* setpayloadsig* |check|
* iscannonical
* issignaling
* issubnormal
Expand Down
6 changes: 6 additions & 0 deletions libc/docs/math/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| fsub | N/A | | | N/A | | 7.12.14.2 | F.10.11 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| getpayload | | | | |check| | | F.10.13.1 | N/A |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| ilogb | |check| | |check| | |check| | |check| | |check| | 7.12.6.8 | F.10.3.8 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| ldexp | |check| | |check| | |check| | | |check| | 7.12.6.9 | F.10.3.9 |
Expand Down Expand Up @@ -212,6 +214,10 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| scalbn | |check| | |check| | |check| | | |check| | 7.12.6.19 | F.10.3.19 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| setpayload | | | | |check| | | F.10.13.2 | N/A |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| setpayloadsig | | | | |check| | | F.10.13.3 | N/A |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| totalorder | | | | |check| | | F.10.12.1 | N/A |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
| totalordermag | | | | |check| | | F.10.12.2 | N/A |
Expand Down
6 changes: 6 additions & 0 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,12 @@ def StdC : StandardSpec<"stdc"> {

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

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

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

GuardedFunctionSpec<"setpayloadsigf16", RetValSpec<IntType>, [ArgSpec<Float16Ptr>, ArgSpec<Float16Type>], "LIBC_TYPES_HAS_FLOAT16">,

GuardedFunctionSpec<"f16sqrtf", RetValSpec<Float16Type>, [ArgSpec<FloatType>], "LIBC_TYPES_HAS_FLOAT16">,
]
>;
Expand Down
42 changes: 42 additions & 0 deletions libc/src/__support/FPUtil/BasicOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,48 @@ totalordermag(T x, T y) {
return FPBits<T>(x).abs().uintval() <= FPBits<T>(y).abs().uintval();
}

template <typename T>
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> getpayload(T x) {
using FPBits = FPBits<T>;
FPBits x_bits(x);

if (!x_bits.is_nan())
return T(-1.0);

return T(x_bits.uintval() & (FPBits::FRACTION_MASK >> 1));
}

template <bool IsSignaling, typename T>
LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, bool>
setpayload(T &res, T pl) {
using FPBits = FPBits<T>;
FPBits pl_bits(pl);

// Signaling NaNs don't have the mantissa's MSB set to 1, so they need a
// non-zero payload to distinguish them from infinities.
if (!IsSignaling && pl_bits.is_zero()) {
res = FPBits::quiet_nan(Sign::POS).get_val();
return false;
}

int pl_exp = pl_bits.get_exponent();

if (pl_bits.is_neg() || pl_exp < 0 || pl_exp >= FPBits::FRACTION_LEN - 1 ||
((pl_bits.get_mantissa() << pl_exp) & FPBits::FRACTION_MASK) != 0) {
res = T(0.0);
return true;
}

using StorageType = typename FPBits::StorageType;
StorageType v(pl_bits.get_explicit_mantissa() >> (FPBits::SIG_LEN - pl_exp));

if constexpr (IsSignaling)
res = FPBits::signaling_nan(Sign::POS, v).get_val();
else
res = FPBits::quiet_nan(Sign::POS, v).get_val();
return false;
}

} // namespace fputil
} // namespace LIBC_NAMESPACE

Expand Down
6 changes: 6 additions & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ add_math_entrypoint_object(fromfpxl)
add_math_entrypoint_object(fromfpxf16)
add_math_entrypoint_object(fromfpxf128)

add_math_entrypoint_object(getpayloadf16)

add_math_entrypoint_object(hypot)
add_math_entrypoint_object(hypotf)

Expand Down Expand Up @@ -350,6 +352,10 @@ add_math_entrypoint_object(scalbnf)
add_math_entrypoint_object(scalbnl)
add_math_entrypoint_object(scalbnf128)

add_math_entrypoint_object(setpayloadf16)

add_math_entrypoint_object(setpayloadsigf16)

add_math_entrypoint_object(sincos)
add_math_entrypoint_object(sincosf)

Expand Down
39 changes: 39 additions & 0 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3602,6 +3602,45 @@ add_entrypoint_object(
-O3
)

add_entrypoint_object(
getpayloadf16
SRCS
getpayloadf16.cpp
HDRS
../getpayloadf16.h
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.basic_operations
COMPILE_OPTIONS
-O3
)

add_entrypoint_object(
setpayloadf16
SRCS
setpayloadf16.cpp
HDRS
../setpayloadf16.h
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.basic_operations
COMPILE_OPTIONS
-O3
)

add_entrypoint_object(
setpayloadsigf16
SRCS
setpayloadsigf16.cpp
HDRS
../setpayloadsigf16.h
DEPENDS
libc.src.__support.macros.properties.types
libc.src.__support.FPUtil.basic_operations
COMPILE_OPTIONS
-O3
)

add_entrypoint_object(
f16fmaf
SRCS
Expand Down
19 changes: 19 additions & 0 deletions libc/src/math/generic/getpayloadf16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Implementation of getpayloadf16 function --------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/math/getpayloadf16.h"
#include "src/__support/FPUtil/BasicOperations.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(float16, getpayloadf16, (const float16 *x)) {
return fputil::getpayload(*x);
}

} // namespace LIBC_NAMESPACE
19 changes: 19 additions & 0 deletions libc/src/math/generic/setpayloadf16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Implementation of setpayloadf16 function --------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/math/setpayloadf16.h"
#include "src/__support/FPUtil/BasicOperations.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

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

} // namespace LIBC_NAMESPACE
19 changes: 19 additions & 0 deletions libc/src/math/generic/setpayloadsigf16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===-- Implementation of setpayloadsigf16 function -----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/math/setpayloadsigf16.h"
#include "src/__support/FPUtil/BasicOperations.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(int, setpayloadsigf16, (float16 * res, float16 pl)) {
return static_cast<int>(fputil::setpayload</*IsSignaling=*/true>(*res, pl));
}

} // namespace LIBC_NAMESPACE
20 changes: 20 additions & 0 deletions libc/src/math/getpayloadf16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for getpayloadf16 -----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_MATH_GETPAYLOADF16_H
#define LLVM_LIBC_SRC_MATH_GETPAYLOADF16_H

#include "src/__support/macros/properties/types.h"

namespace LIBC_NAMESPACE {

float16 getpayloadf16(const float16 *x);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_GETPAYLOADF16_H
20 changes: 20 additions & 0 deletions libc/src/math/setpayloadf16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for setpayloadf16 -----------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_MATH_SETPAYLOADF16_H
#define LLVM_LIBC_SRC_MATH_SETPAYLOADF16_H

#include "src/__support/macros/properties/types.h"

namespace LIBC_NAMESPACE {

int setpayloadf16(float16 *res, float16 pl);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_SETPAYLOADF16_H
20 changes: 20 additions & 0 deletions libc/src/math/setpayloadsigf16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation header for setpayloadsigf16 --------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_MATH_SETPAYLOADSIGF16_H
#define LLVM_LIBC_SRC_MATH_SETPAYLOADSIGF16_H

#include "src/__support/macros/properties/types.h"

namespace LIBC_NAMESPACE {

int setpayloadsigf16(float16 *res, float16 pl);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_MATH_SETPAYLOADSIGF16_H
36 changes: 36 additions & 0 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3553,6 +3553,42 @@ add_fp_unittest(
libc.src.math.totalordermagf16
)

add_fp_unittest(
getpayloadf16_test
SUITE
libc-math-smoke-tests
SRCS
getpayloadf16_test.cpp
HDRS
GetPayloadTest.h
DEPENDS
libc.src.math.getpayloadf16
)

add_fp_unittest(
setpayloadf16_test
SUITE
libc-math-smoke-tests
SRCS
setpayloadf16_test.cpp
HDRS
SetPayloadTest.h
DEPENDS
libc.src.math.setpayloadf16
)

add_fp_unittest(
setpayloadsigf16_test
SUITE
libc-math-smoke-tests
SRCS
setpayloadsigf16_test.cpp
HDRS
SetPayloadSigTest.h
DEPENDS
libc.src.math.setpayloadsigf16
)

add_fp_unittest(
f16fmaf_test
SUITE
Expand Down
Loading
Loading