Skip to content

Commit aaa637d

Browse files
authored
[libc][math] Implement isnormal macro. (#109547)
#109201
1 parent 2c77067 commit aaa637d

File tree

7 files changed

+156
-0
lines changed

7 files changed

+156
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
#define iszero(x) (x == 0)
1919
#define fpclassify(x) \
2020
__builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
21+
#define isnormal(x) __builtin_isnormal(x)
2122

2223
#endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H

libc/test/include/CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,36 @@ add_libc_test(
8181
libc.include.llvm-libc-macros.stdckdint_macros
8282
)
8383

84+
add_libc_test(
85+
isnormal_test
86+
SUITE
87+
libc_include_tests
88+
SRCS
89+
isnormal_test.cpp
90+
DEPENDS
91+
libc.include.llvm-libc-macros.math_function_macros
92+
)
93+
94+
add_libc_test(
95+
isnormalf_test
96+
SUITE
97+
libc_include_tests
98+
SRCS
99+
isnormalf_test.cpp
100+
DEPENDS
101+
libc.include.llvm-libc-macros.math_function_macros
102+
)
103+
104+
add_libc_test(
105+
isnormall_test
106+
SUITE
107+
libc_include_tests
108+
SRCS
109+
isnormall_test.cpp
110+
DEPENDS
111+
libc.include.llvm-libc-macros.math_function_macros
112+
)
113+
84114
add_libc_test(
85115
fpclassify_test
86116
SUITE
@@ -291,6 +321,21 @@ add_libc_test(
291321
libc.include.llvm-libc-macros.math_function_macros
292322
)
293323

324+
add_libc_test(
325+
isnormal_c_test
326+
C_TEST
327+
UNIT_TEST_ONLY
328+
SUITE
329+
libc_include_tests
330+
SRCS
331+
isnormal_test.c
332+
COMPILE_OPTIONS
333+
-Wall
334+
-Werror
335+
DEPENDS
336+
libc.include.llvm-libc-macros.math_function_macros
337+
)
338+
294339
add_libc_test(
295340
isinf_c_test
296341
C_TEST

libc/test/include/IsNormalTest.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===-- Utility class to test the isnormal macro ---------------*- 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_TEST_INCLUDE_MATH_ISNORMAL_H
10+
#define LLVM_LIBC_TEST_INCLUDE_MATH_ISNORMAL_H
11+
12+
#include "test/UnitTest/FPMatcher.h"
13+
#include "test/UnitTest/Test.h"
14+
15+
#include "include/llvm-libc-macros/math-function-macros.h"
16+
17+
template <typename T>
18+
class IsNormalTest : public LIBC_NAMESPACE::testing::Test {
19+
DECLARE_SPECIAL_CONSTANTS(T)
20+
21+
public:
22+
typedef int (*IsNormalFunc)(T);
23+
24+
void testSpecialNumbers(IsNormalFunc func) {
25+
EXPECT_EQ(func(aNaN), 0);
26+
EXPECT_EQ(func(neg_aNaN), 0);
27+
EXPECT_EQ(func(sNaN), 0);
28+
EXPECT_EQ(func(neg_sNaN), 0);
29+
EXPECT_EQ(func(inf), 0);
30+
EXPECT_EQ(func(neg_inf), 0);
31+
EXPECT_EQ(func(min_normal), 1);
32+
EXPECT_EQ(func(max_normal), 1);
33+
EXPECT_EQ(func(neg_max_normal), 1);
34+
EXPECT_EQ(func(min_denormal), 0);
35+
EXPECT_EQ(func(neg_min_denormal), 0);
36+
EXPECT_EQ(func(max_denormal), 0);
37+
EXPECT_EQ(func(zero), 0);
38+
EXPECT_EQ(func(neg_zero), 0);
39+
}
40+
};
41+
42+
#define LIST_ISNORMAL_TESTS(T, func) \
43+
using LlvmLibcIsNormalTest = IsNormalTest<T>; \
44+
TEST_F(LlvmLibcIsNormalTest, SpecialNumbers) { \
45+
auto isnormal_func = [](T x) { return func(x); }; \
46+
testSpecialNumbers(isnormal_func); \
47+
}
48+
49+
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISNORMAL_H

libc/test/include/isnormal_test.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===-- Unittests for isnormal macro --------------------------------------===//
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+
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
#include "include/llvm-libc-macros/math-function-macros.h"
9+
10+
#include <assert.h>
11+
12+
// check if macro is defined
13+
#ifndef isnormal
14+
#error "isnormal macro is not defined"
15+
#else
16+
int main(void) {
17+
assert(isnormal(1.819f) == 1);
18+
assert(isnormal(-1.726) == 1);
19+
assert(isnormal(1.426L) == 1);
20+
assert(isnormal(-0.0f) == 0);
21+
assert(isnormal(0.0) == 0);
22+
assert(isnormal(-0.0L) == 0);
23+
return 0;
24+
}
25+
#endif

libc/test/include/isnormal_test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//===-- Unittest for isnormal[d] macro ------------------------------------===//
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+
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "IsNormalTest.h"
10+
#include "include/llvm-libc-macros/math-function-macros.h"
11+
12+
LIST_ISNORMAL_TESTS(double, isnormal)

libc/test/include/isnormalf_test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//===-- Unittest for isnormal[f] macro ------------------------------------===//
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+
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "IsNormalTest.h"
10+
#include "include/llvm-libc-macros/math-function-macros.h"
11+
12+
LIST_ISNORMAL_TESTS(float, isnormal)

libc/test/include/isnormall_test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//===-- Unittest for isnormal[l] macro ------------------------------------===//
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+
// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "IsNormalTest.h"
10+
#include "include/llvm-libc-macros/math-function-macros.h"
11+
12+
LIST_ISNORMAL_TESTS(long double, isnormal)

0 commit comments

Comments
 (0)