Skip to content

Commit 739ede4

Browse files
authored
[libc][[math] Implement IsZero Macro (#109336)
#109201
1 parent c472c9f commit 739ede4

File tree

7 files changed

+145
-0
lines changed

7 files changed

+145
-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
@@ -13,5 +13,6 @@
1313
#define isinf(x) __builtin_isinf(x)
1414
#define isnan(x) __builtin_isnan(x)
1515
#define signbit(x) __builtin_signbit(x)
16+
#define iszero(x) (x == 0)
1617

1718
#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+
iszero_test
86+
SUITE
87+
libc_include_tests
88+
SRCS
89+
iszero_test.cpp
90+
DEPENDS
91+
libc.include.llvm-libc-macros.math_function_macros
92+
)
93+
94+
add_libc_test(
95+
iszerof_test
96+
SUITE
97+
libc_include_tests
98+
SRCS
99+
iszerof_test.cpp
100+
DEPENDS
101+
libc.include.llvm-libc-macros.math_function_macros
102+
)
103+
104+
add_libc_test(
105+
iszerol_test
106+
SUITE
107+
libc_include_tests
108+
SRCS
109+
iszerol_test.cpp
110+
DEPENDS
111+
libc.include.llvm-libc-macros.math_function_macros
112+
)
113+
84114
add_libc_test(
85115
signbit_test
86116
SUITE
@@ -260,3 +290,18 @@ add_libc_test(
260290
DEPENDS
261291
libc.include.llvm-libc-macros.math_function_macros
262292
)
293+
294+
add_libc_test(
295+
iszero_c_test
296+
C_TEST
297+
UNIT_TEST_ONLY
298+
SUITE
299+
libc_include_tests
300+
SRCS
301+
iszero_test.c
302+
COMPILE_OPTIONS
303+
-Wall
304+
-Werror
305+
DEPENDS
306+
libc.include.llvm-libc-macros.math_function_macros
307+
)

libc/test/include/IsZeroTest.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===-- Utility class to test the iszero 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_ISZERO_H
10+
#define LLVM_LIBC_TEST_INCLUDE_MATH_ISZERO_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 IsZeroTest : public LIBC_NAMESPACE::testing::Test {
18+
DECLARE_SPECIAL_CONSTANTS(T)
19+
20+
public:
21+
typedef bool (*IsZeroFunc)(T);
22+
23+
void testSpecialNumbers(IsZeroFunc func) {
24+
EXPECT_FALSE(func(inf));
25+
EXPECT_FALSE(func(neg_inf));
26+
EXPECT_TRUE(func(zero));
27+
EXPECT_TRUE(func(neg_zero));
28+
}
29+
};
30+
31+
#define LIST_ISZERO_TESTS(T, func) \
32+
using LlvmLibcIsZeroTest = IsZeroTest<T>; \
33+
TEST_F(LlvmLibcIsZeroTest, SpecialNumbers) { \
34+
auto iszero_func = [](T x) { return func(x); }; \
35+
testSpecialNumbers(iszero_func); \
36+
}
37+
38+
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISZERO_H

libc/test/include/iszero_test.c

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

libc/test/include/iszero_test.cpp

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

libc/test/include/iszerof_test.cpp

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

libc/test/include/iszerol_test.cpp

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

0 commit comments

Comments
 (0)