Skip to content

Commit 3604c23

Browse files
authored
[libc][math] implement signbit and math macro unit tests (#97791)
This PR resolves #96322 and implements the `signbit` macro under a new header `generic-math-macros.h`. This also removed the `TODO` in `math-macros.h` and moves `isfinite`, `isinf`, and `isnan` to the same generic maths header. Finally, a test file `generic-math-macros_test.cpp` that adds coverage to the above 4 macros. Fixes #96322.
1 parent 8b42517 commit 3604c23

22 files changed

+570
-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
@@ -12,5 +12,6 @@
1212
#define isfinite(x) __builtin_isfinite(x)
1313
#define isinf(x) __builtin_isinf(x)
1414
#define isnan(x) __builtin_isnan(x)
15+
#define signbit(x) __builtin_signbit(x)
1516

1617
#endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H

libc/test/include/CMakeLists.txt

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,183 @@ add_libc_test(
8080
DEPENDS
8181
libc.include.llvm-libc-macros.stdckdint_macros
8282
)
83+
84+
add_libc_test(
85+
signbit_test
86+
SUITE
87+
libc_include_tests
88+
SRCS
89+
signbit_test.cpp
90+
DEPENDS
91+
libc.include.llvm-libc-macros.math_function_macros
92+
)
93+
94+
add_libc_test(
95+
signbitf_test
96+
SUITE
97+
libc_include_tests
98+
SRCS
99+
signbitf_test.cpp
100+
DEPENDS
101+
libc.include.llvm-libc-macros.math_function_macros
102+
)
103+
104+
add_libc_test(
105+
signbitl_test
106+
SUITE
107+
libc_include_tests
108+
SRCS
109+
signbitl_test.cpp
110+
DEPENDS
111+
libc.include.llvm-libc-macros.math_function_macros
112+
)
113+
114+
add_libc_test(
115+
isnan_test
116+
SUITE
117+
libc_include_tests
118+
SRCS
119+
isnan_test.cpp
120+
DEPENDS
121+
libc.include.llvm-libc-macros.math_function_macros
122+
)
123+
124+
add_libc_test(
125+
isnanf_test
126+
SUITE
127+
libc_include_tests
128+
SRCS
129+
isnanf_test.cpp
130+
DEPENDS
131+
libc.include.llvm-libc-macros.math_function_macros
132+
)
133+
134+
add_libc_test(
135+
isnanl_test
136+
SUITE
137+
libc_include_tests
138+
SRCS
139+
isnanl_test.cpp
140+
DEPENDS
141+
libc.include.llvm-libc-macros.math_function_macros
142+
)
143+
144+
add_libc_test(
145+
isinf_test
146+
SUITE
147+
libc_include_tests
148+
SRCS
149+
isinf_test.cpp
150+
DEPENDS
151+
libc.include.llvm-libc-macros.math_function_macros
152+
)
153+
154+
add_libc_test(
155+
isinff_test
156+
SUITE
157+
libc_include_tests
158+
SRCS
159+
isinff_test.cpp
160+
DEPENDS
161+
libc.include.llvm-libc-macros.math_function_macros
162+
)
163+
164+
add_libc_test(
165+
isinfl_test
166+
SUITE
167+
libc_include_tests
168+
SRCS
169+
isinfl_test.cpp
170+
DEPENDS
171+
libc.include.llvm-libc-macros.math_function_macros
172+
)
173+
174+
add_libc_test(
175+
isfinite_test
176+
SUITE
177+
libc_include_tests
178+
SRCS
179+
isfinite_test.cpp
180+
DEPENDS
181+
libc.include.llvm-libc-macros.math_function_macros
182+
)
183+
184+
add_libc_test(
185+
isfinitef_test
186+
SUITE
187+
libc_include_tests
188+
SRCS
189+
isfinitef_test.cpp
190+
DEPENDS
191+
libc.include.llvm-libc-macros.math_function_macros
192+
)
193+
194+
add_libc_test(
195+
isfinitel_test
196+
SUITE
197+
libc_include_tests
198+
SRCS
199+
isfinitel_test.cpp
200+
DEPENDS
201+
libc.include.llvm-libc-macros.math_function_macros
202+
)
203+
204+
add_libc_test(
205+
signbit_c_test
206+
C_TEST
207+
UNIT_TEST_ONLY
208+
SUITE
209+
libc_include_tests
210+
SRCS
211+
signbit_test.c
212+
COMPILE_OPTIONS
213+
-Wall
214+
-Werror
215+
DEPENDS
216+
libc.include.llvm-libc-macros.math_function_macros
217+
)
218+
219+
add_libc_test(
220+
isnan_c_test
221+
C_TEST
222+
UNIT_TEST_ONLY
223+
SUITE
224+
libc_include_tests
225+
SRCS
226+
isnan_test.c
227+
COMPILE_OPTIONS
228+
-Wall
229+
-Werror
230+
DEPENDS
231+
libc.include.llvm-libc-macros.math_function_macros
232+
)
233+
234+
add_libc_test(
235+
isinf_c_test
236+
C_TEST
237+
UNIT_TEST_ONLY
238+
SUITE
239+
libc_include_tests
240+
SRCS
241+
isinf_test.c
242+
COMPILE_OPTIONS
243+
-Wall
244+
-Werror
245+
DEPENDS
246+
libc.include.llvm-libc-macros.math_function_macros
247+
)
248+
249+
add_libc_test(
250+
isfinite_c_test
251+
C_TEST
252+
UNIT_TEST_ONLY
253+
SUITE
254+
libc_include_tests
255+
SRCS
256+
isfinite_test.c
257+
COMPILE_OPTIONS
258+
-Wall
259+
-Werror
260+
DEPENDS
261+
libc.include.llvm-libc-macros.math_function_macros
262+
)

libc/test/include/IsFiniteTest.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===-- Utility class to test the isfinite macro [f|l] ----------*- 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_ISFINITE_H
10+
#define LLVM_LIBC_TEST_INCLUDE_MATH_ISFINITE_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 IsFiniteTest : public LIBC_NAMESPACE::testing::Test {
19+
DECLARE_SPECIAL_CONSTANTS(T)
20+
21+
public:
22+
typedef int (*IsFiniteFunc)(T);
23+
24+
void testSpecialNumbers(IsFiniteFunc func) {
25+
EXPECT_EQ(func(inf), 0);
26+
EXPECT_EQ(func(neg_inf), 0);
27+
EXPECT_EQ(func(zero), 1);
28+
EXPECT_EQ(func(neg_zero), 1);
29+
}
30+
};
31+
32+
#define LIST_ISFINITE_TESTS(T, func) \
33+
using LlvmLibcIsFiniteTest = IsFiniteTest<T>; \
34+
TEST_F(LlvmLibcIsFiniteTest, SpecialNumbers) { \
35+
auto isfinite_func = [](T x) { return func(x); }; \
36+
testSpecialNumbers(isfinite_func); \
37+
}
38+
39+
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISFINITE_H

libc/test/include/IsInfTest.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===-- Utility class to test the isinf macro [f|l] -------------*- 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_ISINF_H
10+
#define LLVM_LIBC_TEST_INCLUDE_MATH_ISINF_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> class IsInfTest : public LIBC_NAMESPACE::testing::Test {
18+
19+
DECLARE_SPECIAL_CONSTANTS(T)
20+
21+
public:
22+
typedef int (*IsInfFunc)(T);
23+
24+
void testSpecialNumbers(IsInfFunc func) {
25+
EXPECT_EQ(func(zero), 0);
26+
EXPECT_EQ(func(neg_zero), 0);
27+
EXPECT_EQ(func(inf), 1);
28+
EXPECT_EQ(func(neg_inf), 1);
29+
}
30+
};
31+
32+
#define LIST_ISINF_TESTS(T, func) \
33+
using LlvmLibcIsInfTest = IsInfTest<T>; \
34+
TEST_F(LlvmLibcIsInfTest, SpecialNumbers) { \
35+
auto isinf_func = [](T x) { return func(x); }; \
36+
testSpecialNumbers(isinf_func); \
37+
}
38+
39+
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISINF_H

libc/test/include/IsNanTest.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===-- Utility class to test the isnan macro [f|l] -------------*- 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 nanormation.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_TEST_INCLUDE_MATH_ISNAN_H
10+
#define LLVM_LIBC_TEST_INCLUDE_MATH_ISNAN_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> class IsNanTest : public LIBC_NAMESPACE::testing::Test {
18+
19+
DECLARE_SPECIAL_CONSTANTS(T)
20+
21+
public:
22+
typedef int (*IsNanFunc)(T);
23+
24+
void testSpecialNumbers(IsNanFunc func) {
25+
EXPECT_EQ(func(zero), 0);
26+
EXPECT_EQ(func(neg_zero), 0);
27+
EXPECT_EQ(func(aNaN), 1);
28+
EXPECT_EQ(func(sNaN), 1);
29+
}
30+
};
31+
32+
#define LIST_ISNAN_TESTS(T, func) \
33+
using LlvmLibcIsNanTest = IsNanTest<T>; \
34+
TEST_F(LlvmLibcIsNanTest, SpecialNumbers) { \
35+
auto isnan_func = [](T x) { return func(x); }; \
36+
testSpecialNumbers(isnan_func); \
37+
}
38+
39+
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISNAN_H

libc/test/include/SignbitTest.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===-- Utility class to test the signbit macro [f|l] -----------*- 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_SIGNBIT_H
10+
#define LLVM_LIBC_TEST_INCLUDE_MATH_SIGNBIT_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> class SignbitTest : public LIBC_NAMESPACE::testing::Test {
18+
19+
DECLARE_SPECIAL_CONSTANTS(T)
20+
21+
public:
22+
typedef int (*SignbitFunc)(T);
23+
24+
void testSpecialNumbers(SignbitFunc func) {
25+
EXPECT_EQ(func(1), 0);
26+
EXPECT_EQ(func(-1), 1);
27+
}
28+
};
29+
30+
#define LIST_SIGNBIT_TESTS(T, func) \
31+
using LlvmLibcSignbitTest = SignbitTest<T>; \
32+
TEST_F(LlvmLibcSignbitTest, SpecialNumbers) { \
33+
auto signbit_func = [](T x) { return func(x); }; \
34+
testSpecialNumbers(signbit_func); \
35+
}
36+
37+
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_SIGNBIT_H

libc/test/include/isfinite_test.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Unittests for isfinite 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 isfinite
14+
#error "isfinite macro is not defined"
15+
#else
16+
int main(void) {
17+
assert(isfinite(1.0f));
18+
assert(isfinite(1.0));
19+
assert(isfinite(1.0L));
20+
return 0;
21+
}
22+
#endif

libc/test/include/isfinite_test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//===-- Unittest for isfinite[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 "IsFiniteTest.h"
10+
#include "include/llvm-libc-macros/math-function-macros.h"
11+
12+
LIST_ISFINITE_TESTS(double, isfinite)

libc/test/include/isfinitef_test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//===-- Unittest for isfinite[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 "IsFiniteTest.h"
10+
#include "include/llvm-libc-macros/math-function-macros.h"
11+
12+
LIST_ISFINITE_TESTS(float, isfinite)

0 commit comments

Comments
 (0)