Skip to content

Commit ba5e195

Browse files
authored
[libc][math] Implement issubnormal macro. (#109572)
#109201
1 parent 9ed46fb commit ba5e195

File tree

7 files changed

+155
-0
lines changed

7 files changed

+155
-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
@@ -19,5 +19,6 @@
1919
#define fpclassify(x) \
2020
__builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL, FP_SUBNORMAL, FP_ZERO, x)
2121
#define isnormal(x) __builtin_isnormal(x)
22+
#define issubnormal(x) (fpclassify(x) == FP_SUBNORMAL)
2223

2324
#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+
issubnormal_test
86+
SUITE
87+
libc_include_tests
88+
SRCS
89+
issubnormal_test.cpp
90+
DEPENDS
91+
libc.include.llvm-libc-macros.math_function_macros
92+
)
93+
94+
add_libc_test(
95+
issubnormalf_test
96+
SUITE
97+
libc_include_tests
98+
SRCS
99+
issubnormalf_test.cpp
100+
DEPENDS
101+
libc.include.llvm-libc-macros.math_function_macros
102+
)
103+
104+
add_libc_test(
105+
issubnormall_test
106+
SUITE
107+
libc_include_tests
108+
SRCS
109+
issubnormall_test.cpp
110+
DEPENDS
111+
libc.include.llvm-libc-macros.math_function_macros
112+
)
113+
84114
add_libc_test(
85115
isnormal_test
86116
SUITE
@@ -366,6 +396,21 @@ add_libc_test(
366396
libc.include.llvm-libc-macros.math_function_macros
367397
)
368398

399+
add_libc_test(
400+
issubnormal_c_test
401+
C_TEST
402+
UNIT_TEST_ONLY
403+
SUITE
404+
libc_include_tests
405+
SRCS
406+
issubnormal_test.c
407+
COMPILE_OPTIONS
408+
-Wall
409+
-Werror
410+
DEPENDS
411+
libc.include.llvm-libc-macros.math_function_macros
412+
)
413+
369414
add_libc_test(
370415
fpclassify_c_test
371416
C_TEST

libc/test/include/IsSubnormalTest.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 issubnormal 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_ISSUBNORMAL_H
10+
#define LLVM_LIBC_TEST_INCLUDE_MATH_ISSUBNORMAL_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 IsSubnormalTest : public LIBC_NAMESPACE::testing::Test {
19+
DECLARE_SPECIAL_CONSTANTS(T)
20+
21+
public:
22+
typedef bool (*IsSubnormalFunc)(T);
23+
24+
void testSpecialNumbers(IsSubnormalFunc func) {
25+
EXPECT_FALSE(func(aNaN));
26+
EXPECT_FALSE(func(neg_aNaN));
27+
EXPECT_FALSE(func(sNaN));
28+
EXPECT_FALSE(func(neg_sNaN));
29+
EXPECT_FALSE(func(inf));
30+
EXPECT_FALSE(func(neg_inf));
31+
EXPECT_FALSE(func(min_normal));
32+
EXPECT_FALSE(func(max_normal));
33+
EXPECT_FALSE(func(neg_max_normal));
34+
EXPECT_TRUE(func(min_denormal));
35+
EXPECT_TRUE(func(neg_min_denormal));
36+
EXPECT_TRUE(func(max_denormal));
37+
EXPECT_FALSE(func(zero));
38+
EXPECT_FALSE(func(neg_zero));
39+
}
40+
};
41+
42+
#define LIST_ISSUBNORMAL_TESTS(T, func) \
43+
using LlvmLibcIsSubnormalTest = IsSubnormalTest<T>; \
44+
TEST_F(LlvmLibcIsSubnormalTest, SpecialNumbers) { \
45+
auto issubnormal_func = [](T x) { return func(x); }; \
46+
testSpecialNumbers(issubnormal_func); \
47+
}
48+
49+
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISSUBNORMAL_H

libc/test/include/issubnormal_test.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- Unittests for issubnormal 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 issubnormal
14+
#error "issubnormal macro is not defined"
15+
#else
16+
int main(void) {
17+
assert(issubnormal(1.819f) == 0);
18+
assert(issubnormal(-1.726) == 0);
19+
assert(issubnormal(1.426L) == 0);
20+
assert(issubnormal(1e-308) == 1);
21+
assert(issubnormal(-1e-308) == 1);
22+
return 0;
23+
}
24+
#endif
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//===-- Unittest for issubnormal[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 "IsSubnormalTest.h"
10+
#include "include/llvm-libc-macros/math-function-macros.h"
11+
12+
LIST_ISSUBNORMAL_TESTS(double, issubnormal)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//===-- Unittest for issubnormal[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 "IsSubnormalTest.h"
10+
#include "include/llvm-libc-macros/math-function-macros.h"
11+
12+
LIST_ISSUBNORMAL_TESTS(float, issubnormal)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//===-- Unittest for issubnormal[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 "IsSubnormalTest.h"
10+
#include "include/llvm-libc-macros/math-function-macros.h"
11+
12+
LIST_ISSUBNORMAL_TESTS(long double, issubnormal)

0 commit comments

Comments
 (0)