Skip to content

Commit cb1a727

Browse files
authored
[libc][math][c23] Add nanf16 C23 math function (#94767)
Part of #93566.
1 parent bf0d76d commit cb1a727

File tree

11 files changed

+129
-2
lines changed

11 files changed

+129
-2
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
522522
libc.src.math.llroundf16
523523
libc.src.math.lrintf16
524524
libc.src.math.lroundf16
525+
libc.src.math.nanf16
525526
libc.src.math.nearbyintf16
526527
libc.src.math.nextafterf16
527528
libc.src.math.nextdownf16

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ if(LIBC_TYPES_HAS_FLOAT16)
555555
libc.src.math.llroundf16
556556
libc.src.math.lrintf16
557557
libc.src.math.lroundf16
558+
libc.src.math.nanf16
558559
libc.src.math.nearbyintf16
559560
libc.src.math.nextafterf16
560561
libc.src.math.nextdownf16

libc/docs/math/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ Basic Operations
186186
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
187187
| modf | |check| | |check| | |check| | | |check| | 7.12.6.18 | F.10.3.18 |
188188
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
189-
| nan | |check| | |check| | |check| | | |check| | 7.12.11.2 | F.10.8.2 |
189+
| nan | |check| | |check| | |check| | |check| | |check| | 7.12.11.2 | F.10.8.2 |
190190
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+
191191
| nearbyint | |check| | |check| | |check| | |check| | |check| | 7.12.9.3 | F.10.6.3 |
192192
+------------------+------------------+-----------------+------------------------+----------------------+------------------------+------------------------+----------------------------+

libc/include/llvm-libc-types/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ add_header(pthread_rwlockattr_t HDR pthread_rwlockattr_t.h)
5858
add_header(pthread_t HDR pthread_t.h DEPENDS .__thread_type)
5959
add_header(rlim_t HDR rlim_t.h)
6060
add_header(time_t HDR time_t.h)
61-
add_header(stack_t HDR stack_t.h)
61+
add_header(stack_t HDR stack_t.h DEPENDS .size_t)
6262
add_header(suseconds_t HDR suseconds_t.h)
6363
add_header(struct_flock HDR struct_flock.h DEPENDS .off_t .pid_t)
6464
add_header(struct_flock64 HDR struct_flock64.h DEPENDS .off64_t .pid_t)

libc/spec/stdc.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ def StdC : StandardSpec<"stdc"> {
695695
FunctionSpec<"nanf", RetValSpec<FloatType>, [ArgSpec<ConstCharPtr>]>,
696696
FunctionSpec<"nan", RetValSpec<DoubleType>, [ArgSpec<ConstCharPtr>]>,
697697
FunctionSpec<"nanl", RetValSpec<LongDoubleType>, [ArgSpec<ConstCharPtr>]>,
698+
GuardedFunctionSpec<"nanf16", RetValSpec<Float16Type>, [ArgSpec<ConstCharPtr>], "LIBC_TYPES_HAS_FLOAT16">,
698699
GuardedFunctionSpec<"nanf128", RetValSpec<Float128Type>, [ArgSpec<ConstCharPtr>], "LIBC_TYPES_HAS_FLOAT128">,
699700

700701
FunctionSpec<"canonicalize", RetValSpec<IntType>, [ArgSpec<DoubleType>, ArgSpec<DoubleType>]>,

libc/src/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ add_math_entrypoint_object(modff128)
272272
add_math_entrypoint_object(nan)
273273
add_math_entrypoint_object(nanf)
274274
add_math_entrypoint_object(nanl)
275+
add_math_entrypoint_object(nanf16)
275276
add_math_entrypoint_object(nanf128)
276277

277278
add_math_entrypoint_object(nearbyint)

libc/src/math/generic/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2631,6 +2631,19 @@ add_entrypoint_object(
26312631
-O3
26322632
)
26332633

2634+
add_entrypoint_object(
2635+
nanf16
2636+
SRCS
2637+
nanf16.cpp
2638+
HDRS
2639+
../nanf16.h
2640+
DEPENDS
2641+
libc.src.__support.str_to_float
2642+
libc.src.errno.errno
2643+
COMPILE_OPTIONS
2644+
-O3
2645+
)
2646+
26342647
add_entrypoint_object(
26352648
nanf128
26362649
SRCS

libc/src/math/generic/nanf16.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===-- Implementation of nanf16 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/nanf16.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/str_to_float.h"
12+
#include "src/errno/libc_errno.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
LLVM_LIBC_FUNCTION(float16, nanf16, (const char *arg)) {
17+
auto result = internal::strtonan<float16>(arg);
18+
if (result.has_error())
19+
libc_errno = result.error;
20+
return result.value;
21+
}
22+
23+
} // namespace LIBC_NAMESPACE

libc/src/math/nanf16.h

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

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,6 +2636,22 @@ add_fp_unittest(
26362636
UNIT_TEST_ONLY
26372637
)
26382638

2639+
add_fp_unittest(
2640+
nanf16_test
2641+
SUITE
2642+
libc-math-smoke-tests
2643+
SRCS
2644+
nanf16_test.cpp
2645+
DEPENDS
2646+
libc.include.signal
2647+
libc.src.math.nanf16
2648+
libc.src.__support.FPUtil.fp_bits
2649+
libc.src.__support.macros.sanitizer
2650+
# FIXME: The nan tests currently have death tests, which aren't supported for
2651+
# hermetic tests.
2652+
UNIT_TEST_ONLY
2653+
)
2654+
26392655
add_fp_unittest(
26402656
nanf128_test
26412657
SUITE
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===-- Unittests for nanf16 ----------------------------------------------===//
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/__support/FPUtil/FPBits.h"
10+
#include "src/__support/macros/sanitizer.h"
11+
#include "src/math/nanf16.h"
12+
#include "test/UnitTest/FEnvSafeTest.h"
13+
#include "test/UnitTest/FPMatcher.h"
14+
#include "test/UnitTest/Test.h"
15+
16+
#include <signal.h>
17+
18+
class LlvmLibcNanf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest {
19+
public:
20+
using StorageType = LIBC_NAMESPACE::fputil::FPBits<float16>::StorageType;
21+
22+
void run_test(const char *input_str, StorageType bits) {
23+
float16 result = LIBC_NAMESPACE::nanf16(input_str);
24+
auto actual_fp = LIBC_NAMESPACE::fputil::FPBits<float16>(result);
25+
auto expected_fp = LIBC_NAMESPACE::fputil::FPBits<float16>(bits);
26+
EXPECT_EQ(actual_fp.uintval(), expected_fp.uintval());
27+
};
28+
};
29+
30+
TEST_F(LlvmLibcNanf16Test, NCharSeq) {
31+
run_test("", 0x7e00);
32+
run_test("123", 0x7e7b);
33+
run_test("0x123", 0x7f23);
34+
run_test("1a", 0x7e00);
35+
run_test("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_",
36+
0x7e00);
37+
run_test("10000000000000000000000000000000000000000000000000", 0x7e00);
38+
}
39+
40+
TEST_F(LlvmLibcNanf16Test, RandomString) {
41+
run_test(" 1234", 0x7e00);
42+
run_test("-1234", 0x7e00);
43+
run_test("asd&f", 0x7e00);
44+
run_test("123 ", 0x7e00);
45+
}
46+
47+
#ifndef LIBC_HAVE_ADDRESS_SANITIZER
48+
TEST_F(LlvmLibcNanf16Test, InvalidInput) {
49+
EXPECT_DEATH([] { LIBC_NAMESPACE::nanf16(nullptr); }, WITH_SIGNAL(SIGSEGV));
50+
}
51+
#endif // LIBC_HAVE_ADDRESS_SANITIZER

0 commit comments

Comments
 (0)