Skip to content

[libc][math] Implement isnormal macro. #109547

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 2 commits into from
Sep 22, 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
1 change: 1 addition & 0 deletions libc/include/llvm-libc-macros/math-function-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
#define iszero(x) (x == 0)
#define fpclassify(x) \
__builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
#define isnormal(x) __builtin_isnormal(x)

#endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
45 changes: 45 additions & 0 deletions libc/test/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,36 @@ add_libc_test(
libc.include.llvm-libc-macros.stdckdint_macros
)

add_libc_test(
isnormal_test
SUITE
libc_include_tests
SRCS
isnormal_test.cpp
DEPENDS
libc.include.llvm-libc-macros.math_function_macros
)

add_libc_test(
isnormalf_test
SUITE
libc_include_tests
SRCS
isnormalf_test.cpp
DEPENDS
libc.include.llvm-libc-macros.math_function_macros
)

add_libc_test(
isnormall_test
SUITE
libc_include_tests
SRCS
isnormall_test.cpp
DEPENDS
libc.include.llvm-libc-macros.math_function_macros
)

add_libc_test(
fpclassify_test
SUITE
Expand Down Expand Up @@ -291,6 +321,21 @@ add_libc_test(
libc.include.llvm-libc-macros.math_function_macros
)

add_libc_test(
isnormal_c_test
C_TEST
UNIT_TEST_ONLY
SUITE
libc_include_tests
SRCS
isnormal_test.c
COMPILE_OPTIONS
-Wall
-Werror
DEPENDS
libc.include.llvm-libc-macros.math_function_macros
)

add_libc_test(
isinf_c_test
C_TEST
Expand Down
49 changes: 49 additions & 0 deletions libc/test/include/IsNormalTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//===-- Utility class to test the isnormal macro ---------------*- 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_TEST_INCLUDE_MATH_ISNORMAL_H
#define LLVM_LIBC_TEST_INCLUDE_MATH_ISNORMAL_H

#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"

#include "include/llvm-libc-macros/math-function-macros.h"

template <typename T>
class IsNormalTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)

public:
typedef int (*IsNormalFunc)(T);

void testSpecialNumbers(IsNormalFunc func) {
EXPECT_EQ(func(aNaN), 0);
EXPECT_EQ(func(neg_aNaN), 0);
EXPECT_EQ(func(sNaN), 0);
EXPECT_EQ(func(neg_sNaN), 0);
EXPECT_EQ(func(inf), 0);
EXPECT_EQ(func(neg_inf), 0);
EXPECT_EQ(func(min_normal), 1);
EXPECT_EQ(func(max_normal), 1);
EXPECT_EQ(func(neg_max_normal), 1);
EXPECT_EQ(func(min_denormal), 0);
EXPECT_EQ(func(neg_min_denormal), 0);
EXPECT_EQ(func(max_denormal), 0);
EXPECT_EQ(func(zero), 0);
EXPECT_EQ(func(neg_zero), 0);
}
};

#define LIST_ISNORMAL_TESTS(T, func) \
using LlvmLibcIsNormalTest = IsNormalTest<T>; \
TEST_F(LlvmLibcIsNormalTest, SpecialNumbers) { \
auto isnormal_func = [](T x) { return func(x); }; \
testSpecialNumbers(isnormal_func); \
}

#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISNORMAL_H
25 changes: 25 additions & 0 deletions libc/test/include/isnormal_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===-- Unittests for isnormal macro --------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "include/llvm-libc-macros/math-function-macros.h"

#include <assert.h>

// check if macro is defined
#ifndef isnormal
#error "isnormal macro is not defined"
#else
int main(void) {
assert(isnormal(1.819f) == 1);
assert(isnormal(-1.726) == 1);
assert(isnormal(1.426L) == 1);
assert(isnormal(-0.0f) == 0);
assert(isnormal(0.0) == 0);
assert(isnormal(-0.0L) == 0);
return 0;
}
#endif
12 changes: 12 additions & 0 deletions libc/test/include/isnormal_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//===-- Unittest for isnormal[d] macro ------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "IsNormalTest.h"
#include "include/llvm-libc-macros/math-function-macros.h"

LIST_ISNORMAL_TESTS(double, isnormal)
12 changes: 12 additions & 0 deletions libc/test/include/isnormalf_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//===-- Unittest for isnormal[f] macro ------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "IsNormalTest.h"
#include "include/llvm-libc-macros/math-function-macros.h"

LIST_ISNORMAL_TESTS(float, isnormal)
12 changes: 12 additions & 0 deletions libc/test/include/isnormall_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//===-- Unittest for isnormal[l] macro ------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "IsNormalTest.h"
#include "include/llvm-libc-macros/math-function-macros.h"

LIST_ISNORMAL_TESTS(long double, isnormal)
Loading