forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc][math][c23] Add nanf16 C23 math function (llvm#94767)
Part of llvm#93566.
- Loading branch information
1 parent
bf0d76d
commit cb1a727
Showing
11 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//===-- Implementation of nanf16 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/nanf16.h" | ||
#include "src/__support/common.h" | ||
#include "src/__support/str_to_float.h" | ||
#include "src/errno/libc_errno.h" | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
LLVM_LIBC_FUNCTION(float16, nanf16, (const char *arg)) { | ||
auto result = internal::strtonan<float16>(arg); | ||
if (result.has_error()) | ||
libc_errno = result.error; | ||
return result.value; | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===-- Implementation header for nanf16 ------------------------*- 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_NANF16_H | ||
#define LLVM_LIBC_SRC_MATH_NANF16_H | ||
|
||
#include "src/__support/macros/properties/types.h" | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
float16 nanf16(const char *arg); | ||
|
||
} // namespace LIBC_NAMESPACE | ||
|
||
#endif // LLVM_LIBC_SRC_MATH_NANF16_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//===-- Unittests for nanf16 ----------------------------------------------===// | ||
// | ||
// 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/__support/FPUtil/FPBits.h" | ||
#include "src/__support/macros/sanitizer.h" | ||
#include "src/math/nanf16.h" | ||
#include "test/UnitTest/FEnvSafeTest.h" | ||
#include "test/UnitTest/FPMatcher.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
#include <signal.h> | ||
|
||
class LlvmLibcNanf16Test : public LIBC_NAMESPACE::testing::FEnvSafeTest { | ||
public: | ||
using StorageType = LIBC_NAMESPACE::fputil::FPBits<float16>::StorageType; | ||
|
||
void run_test(const char *input_str, StorageType bits) { | ||
float16 result = LIBC_NAMESPACE::nanf16(input_str); | ||
auto actual_fp = LIBC_NAMESPACE::fputil::FPBits<float16>(result); | ||
auto expected_fp = LIBC_NAMESPACE::fputil::FPBits<float16>(bits); | ||
EXPECT_EQ(actual_fp.uintval(), expected_fp.uintval()); | ||
}; | ||
}; | ||
|
||
TEST_F(LlvmLibcNanf16Test, NCharSeq) { | ||
run_test("", 0x7e00); | ||
run_test("123", 0x7e7b); | ||
run_test("0x123", 0x7f23); | ||
run_test("1a", 0x7e00); | ||
run_test("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_", | ||
0x7e00); | ||
run_test("10000000000000000000000000000000000000000000000000", 0x7e00); | ||
} | ||
|
||
TEST_F(LlvmLibcNanf16Test, RandomString) { | ||
run_test(" 1234", 0x7e00); | ||
run_test("-1234", 0x7e00); | ||
run_test("asd&f", 0x7e00); | ||
run_test("123 ", 0x7e00); | ||
} | ||
|
||
#ifndef LIBC_HAVE_ADDRESS_SANITIZER | ||
TEST_F(LlvmLibcNanf16Test, InvalidInput) { | ||
EXPECT_DEATH([] { LIBC_NAMESPACE::nanf16(nullptr); }, WITH_SIGNAL(SIGSEGV)); | ||
} | ||
#endif // LIBC_HAVE_ADDRESS_SANITIZER |