Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions libc/config/baremetal/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
libc.src.math.nanbf16
libc.src.math.nextafterbf16
libc.src.math.nextdownbf16
libc.src.math.nexttowardbf16
Expand Down
1 change: 1 addition & 0 deletions libc/config/baremetal/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
libc.src.math.nanbf16
libc.src.math.nextafterbf16
libc.src.math.nextdownbf16
libc.src.math.nexttowardbf16
Expand Down
1 change: 1 addition & 0 deletions libc/config/baremetal/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
libc.src.math.nanbf16
libc.src.math.nextafterbf16
libc.src.math.nextdownbf16
libc.src.math.nexttowardbf16
Expand Down
1 change: 1 addition & 0 deletions libc/config/darwin/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
libc.src.math.nanbf16
libc.src.math.nextafterbf16
libc.src.math.nextdownbf16
libc.src.math.nexttowardbf16
Expand Down
1 change: 1 addition & 0 deletions libc/config/darwin/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
libc.src.math.nanbf16
libc.src.math.nextafterbf16
libc.src.math.nextdownbf16
libc.src.math.nexttowardbf16
Expand Down
1 change: 1 addition & 0 deletions libc/config/gpu/amdgpu/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
libc.src.math.nanbf16
libc.src.math.nextafterbf16
libc.src.math.nextdownbf16
libc.src.math.nexttowardbf16
Expand Down
1 change: 1 addition & 0 deletions libc/config/gpu/nvptx/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
libc.src.math.nanbf16
libc.src.math.nextafterbf16
libc.src.math.nextdownbf16
libc.src.math.nexttowardbf16
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
libc.src.math.nanbf16
libc.src.math.nextafterbf16
libc.src.math.nextdownbf16
libc.src.math.nexttowardbf16
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
libc.src.math.nanbf16
libc.src.math.nextafterbf16
libc.src.math.nextdownbf16
libc.src.math.nexttowardbf16
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
libc.src.math.nanbf16
libc.src.math.nextafterbf16
libc.src.math.nextdownbf16
libc.src.math.nexttowardbf16
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
libc.src.math.nanbf16
libc.src.math.nextafterbf16
libc.src.math.nextdownbf16
libc.src.math.nexttowardbf16
Expand Down
1 change: 1 addition & 0 deletions libc/config/windows/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ list(APPEND TARGET_LIBM_ENTRYPOINTS
libc.src.math.fromfpbf16
libc.src.math.fromfpxbf16
libc.src.math.getpayloadbf16
libc.src.math.nanbf16
libc.src.math.nextafterbf16
libc.src.math.nextdownbf16
libc.src.math.nexttowardbf16
Expand Down
2 changes: 1 addition & 1 deletion libc/docs/headers/math/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ Basic Operations
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| modf | |check| | |check| | |check| | |check| | |check| | | 7.12.6.18 | F.10.3.18 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| nan | |check| | |check| | |check| | |check| | |check| | | 7.12.11.2 | F.10.8.2 |
| nan | |check| | |check| | |check| | |check| | |check| | |check| | 7.12.11.2 | F.10.8.2 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
| nearbyint | |check| | |check| | |check| | |check| | |check| | | 7.12.9.3 | F.10.6.3 |
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+------------------------+----------------------------+
Expand Down
5 changes: 4 additions & 1 deletion libc/src/__support/FPUtil/BasicOperations.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,10 @@ LIBC_INLINE cpp::enable_if_t<cpp::is_floating_point_v<T>, T> getpayload(T x) {

return static_cast<T>(payload_dfloat);
} else {
return static_cast<T>(payload);
if constexpr (cpp::is_same_v<T, bfloat16>)
return T(static_cast<int>(payload));
else
return static_cast<T>(payload);
}
}

Expand Down
1 change: 1 addition & 0 deletions libc/src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ add_math_entrypoint_object(nanf)
add_math_entrypoint_object(nanl)
add_math_entrypoint_object(nanf16)
add_math_entrypoint_object(nanf128)
add_math_entrypoint_object(nanbf16)

add_math_entrypoint_object(nearbyint)
add_math_entrypoint_object(nearbyintf)
Expand Down
16 changes: 16 additions & 0 deletions libc/src/math/generic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3432,6 +3432,22 @@ add_entrypoint_object(
libc.src.errno.errno
)

add_entrypoint_object(
nanbf16
SRCS
nanbf16.cpp
HDRS
../nanbf16.h
DEPENDS
libc.src.errno.errno
libc.src.__support.common
libc.src.__support.FPUtil.bfloat16
libc.src.__support.libc_errno
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
libc.src.__support.str_to_float
)

add_entrypoint_object(
nextafter
SRCS
Expand Down
25 changes: 25 additions & 0 deletions libc/src/math/generic/nanbf16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===-- Implementation of nanbf16 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/nanbf16.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
#include "src/__support/str_to_float.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(bfloat16, nanbf16, (const char *arg)) {
auto result = internal::strtonan<bfloat16>(arg);
if (result.has_error())
libc_errno = result.error;
return result.value;
}

} // namespace LIBC_NAMESPACE_DECL
21 changes: 21 additions & 0 deletions libc/src/math/nanbf16.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation header for nanbf16 -----------------------*- 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_NANBF16_H
#define LLVM_LIBC_SRC_MATH_NANBF16_H

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

namespace LIBC_NAMESPACE_DECL {

bfloat16 nanbf16(const char *arg);

} // namespace LIBC_NAMESPACE_DECL

#endif // LLVM_LIBC_SRC_MATH_NANBF16_H
16 changes: 16 additions & 0 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3515,6 +3515,22 @@ add_fp_unittest(
UNIT_TEST_ONLY
)

add_fp_unittest(
nanbf16_test
SUITE
libc-math-smoke-tests
SRCS
nanbf16_test.cpp
DEPENDS
libc.hdr.signal_macros
libc.src.math.nanbf16
libc.src.__support.FPUtil.bfloat16
libc.src.__support.FPUtil.fp_bits
# FIXME: The nan tests currently have death tests, which aren't supported for
# hermetic tests.
UNIT_TEST_ONLY
)

add_fp_unittest(
nearbyint_test
SUITE
Expand Down
55 changes: 55 additions & 0 deletions libc/test/src/math/smoke/nanbf16_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//===-- Unittests for nanbf16 ---------------------------------------------===//
//
// 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 "hdr/signal_macros.h"
#include "src/__support/FPUtil/FPBits.h"
#include "src/__support/FPUtil/bfloat16.h"
#include "src/math/nanbf16.h"
#include "test/UnitTest/FEnvSafeTest.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"

class LlvmLibcNanf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
public:
using StorageType = LIBC_NAMESPACE::fputil::FPBits<bfloat16>::StorageType;

void run_test(const char *input_str, StorageType bits) {
bfloat16 result = LIBC_NAMESPACE::nanbf16(input_str);
auto actual_fp = LIBC_NAMESPACE::fputil::FPBits<bfloat16>(result);
auto expected_fp = LIBC_NAMESPACE::fputil::FPBits<bfloat16>(bits);
EXPECT_EQ(actual_fp.uintval(), expected_fp.uintval());
}
};

TEST_F(LlvmLibcNanf16Test, NCharSeq) {
run_test("", 0x7fc0);

// 0x7fc0 + 0x1f (31) = 0x7cdf
run_test("31", 0x7fdf);

// 0x7fc0 + 0x15 = 0x7fd5
run_test("0x15", 0x7fd5);

run_test("1a", 0x7fc0);
run_test("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_",
0x7fc0);
run_test("10000000000000000000000000000", 0x7fc0);
}

TEST_F(LlvmLibcNanf16Test, RandomString) {
run_test(" 1234", 0x7fc0);
run_test("-1234", 0x7fc0);
run_test("asd&f", 0x7fc0);
run_test("123 ", 0x7fc0);
}

#if defined(LIBC_ADD_NULL_CHECKS)
TEST_F(LlvmLibcNanf16Test, InvalidInput) {
EXPECT_DEATH([] { LIBC_NAMESPACE::nanbf16(nullptr); }, WITH_SIGNAL(-1));
}
#endif // LIBC_ADD_NULL_CHECKS
Loading