39 changes: 39 additions & 0 deletions libc/test/src/math/smoke/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,45 @@ add_fp_unittest(
libc.src.__support.FPUtil.fp_bits
)

add_fp_unittest(
nanf_test
SUITE
libc-math-smoke-tests
SRCS
nanf_test.cpp
DEPENDS
libc.include.math
libc.include.signal
libc.src.math.nanf
libc.src.__support.FPUtil.fp_bits
)

add_fp_unittest(
nan_test
SUITE
libc-math-smoke-tests
SRCS
nan_test.cpp
DEPENDS
libc.include.math
libc.include.signal
libc.src.math.nan
libc.src.__support.FPUtil.fp_bits
)

add_fp_unittest(
nanl_test
SUITE
libc-math-smoke-tests
SRCS
nanl_test.cpp
DEPENDS
libc.include.math
libc.include.signal
libc.src.math.nanl
libc.src.__support.FPUtil.fp_bits
)

# FIXME: These tests are currently spurious for NVPTX.
if(NOT LIBC_GPU_TARGET_ARCHITECTURE_IS_NVPTX)
add_fp_unittest(
Expand Down
47 changes: 47 additions & 0 deletions libc/test/src/math/smoke/nan_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//===-- Unittests for nan -------------------------------------------------===//
//
// 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/math/nan.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
#include <signal.h>

class LlvmLibcNanTest : public LIBC_NAMESPACE::testing::Test {
public:
using StorageType = LIBC_NAMESPACE::fputil::FPBits<double>::StorageType;

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

TEST_F(LlvmLibcNanTest, NCharSeq) {
run_test("", 0x7ff8000000000000);
run_test("1234", 0x7ff80000000004d2);
run_test("0x1234", 0x7ff8000000001234);
run_test("1a", 0x7ff8000000000000);
run_test("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_",
0x7ff8000000000000);
run_test("10000000000000000000000000000000000000000000000000",
0x7ff8000000000000);
}

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

TEST_F(LlvmLibcNanTest, InvalidInput) {
EXPECT_DEATH([] { LIBC_NAMESPACE::nan(nullptr); }, WITH_SIGNAL(SIGSEGV));
}
46 changes: 46 additions & 0 deletions libc/test/src/math/smoke/nanf_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//===-- Unittests for nanf ------------------------------------------------===//
//
// 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/math/nanf.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
#include <signal.h>

class LlvmLibcNanfTest : public LIBC_NAMESPACE::testing::Test {
public:
using StorageType = LIBC_NAMESPACE::fputil::FPBits<float>::StorageType;

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

TEST_F(LlvmLibcNanfTest, NCharSeq) {
run_test("", 0x7fc00000);
run_test("1234", 0x7fc004d2);
run_test("0x1234", 0x7fc01234);
run_test("1a", 0x7fc00000);
run_test("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_",
0x7fc00000);
run_test("10000000000000000000000000000000000000000000000000", 0x7fc00000);
}

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

TEST_F(LlvmLibcNanfTest, InvalidInput) {
EXPECT_DEATH([] { LIBC_NAMESPACE::nanf(nullptr); }, WITH_SIGNAL(SIGSEGV));
}
72 changes: 72 additions & 0 deletions libc/test/src/math/smoke/nanl_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//===-- Unittests for nanl ------------------------------------------------===//
//
// 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/math/nanl.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
#include <signal.h>

#if defined(LIBC_LONG_DOUBLE_IS_FLOAT64)
#define SELECT_LONG_DOUBLE(val, _, __) val
#elif defined(LIBC_LONG_DOUBLE_IS_X86_FLOAT80)
#define SELECT_LONG_DOUBLE(_, val, __) val
#else
#define SELECT_LONG_DOUBLE(_, __, val) val
#endif

class LlvmLibcNanlTest : public LIBC_NAMESPACE::testing::Test {
public:
using StorageType = LIBC_NAMESPACE::fputil::FPBits<long double>::StorageType;

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

TEST_F(LlvmLibcNanlTest, NCharSeq) {
run_test("",
SELECT_LONG_DOUBLE(0x7ff8000000000000, (UInt128(0x7fffc00000) << 40),
(UInt128(0x7fff800000000000) << 64)));
run_test("1234", SELECT_LONG_DOUBLE(
0x7ff80000000004d2,
(UInt128(0x7fffc00000) << 40) + UInt128(0x4d2),
(UInt128(0x7fff800000000000) << 64) + UInt128(0x4d2)));
run_test("0x1234",
SELECT_LONG_DOUBLE(0x7ff8000000001234,
(UInt128(0x7fffc00000) << 40) + UInt128(0x1234),
(UInt128(0x7fff800000000000) << 64) +
UInt128(0x1234)));
run_test("1a",
SELECT_LONG_DOUBLE(0x7ff8000000000000, (UInt128(0x7fffc00000) << 40),
(UInt128(0x7fff800000000000) << 64)));
run_test("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM_",
SELECT_LONG_DOUBLE(0x7ff8000000000000, (UInt128(0x7fffc00000) << 40),
(UInt128(0x7fff800000000000) << 64)));
run_test("10000000000000000000000000000000000000000000000000",
SELECT_LONG_DOUBLE(0x7ff8000000000000, (UInt128(0x7fffc00000) << 40),
(UInt128(0x7fff800000000000) << 64)));
}

TEST_F(LlvmLibcNanlTest, RandomString) {
StorageType expected =
SELECT_LONG_DOUBLE(0x7ff8000000000000, (UInt128(0x7fffc00000) << 40),
(UInt128(0x7fff800000000000) << 64));

run_test(" 1234", expected);
run_test("-1234", expected);
run_test("asd&f", expected);
run_test("123 ", expected);
}

TEST_F(LlvmLibcNanlTest, InvalidInput) {
EXPECT_DEATH([] { LIBC_NAMESPACE::nanl(nullptr); }, WITH_SIGNAL(SIGSEGV));
}
6 changes: 6 additions & 0 deletions utils/bazel/llvm-project-overlay/libc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,12 @@ libc_math_function(name = "llroundf")

libc_math_function(name = "llroundl")

libc_math_function(name = "nan")

libc_math_function(name = "nanf")

libc_math_function(name = "nanl")

libc_math_function(name = "nearbyint")

libc_math_function(name = "nearbyintf")
Expand Down