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

#109201
This commit is contained in:
Shourya Goel
2024-09-22 07:56:56 +05:30
committed by GitHub
parent 2c770675ce
commit aaa637d8d0
7 changed files with 156 additions and 0 deletions

View File

@@ -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

View File

@@ -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
@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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)

View File

@@ -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)