diff options
author | Shourya Goel <shouryagoel10000@gmail.com> | 2024-09-20 22:30:01 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-20 13:00:01 -0400 |
commit | 739ede400b7aa7dfbec771b0d5e9c47f9da5d6cb (patch) | |
tree | d83e9020fb1b0954a03c1f03a5789071dd64a7cc | |
parent | c472c9f367e8376fb5cb9bcc954d9c434ecd242a (diff) | |
download | llvm-739ede400b7aa7dfbec771b0d5e9c47f9da5d6cb.zip llvm-739ede400b7aa7dfbec771b0d5e9c47f9da5d6cb.tar.gz llvm-739ede400b7aa7dfbec771b0d5e9c47f9da5d6cb.tar.bz2 |
[libc][[math] Implement IsZero Macro (#109336)
#109201
-rw-r--r-- | libc/include/llvm-libc-macros/math-function-macros.h | 1 | ||||
-rw-r--r-- | libc/test/include/CMakeLists.txt | 45 | ||||
-rw-r--r-- | libc/test/include/IsZeroTest.h | 38 | ||||
-rw-r--r-- | libc/test/include/iszero_test.c | 25 | ||||
-rw-r--r-- | libc/test/include/iszero_test.cpp | 12 | ||||
-rw-r--r-- | libc/test/include/iszerof_test.cpp | 12 | ||||
-rw-r--r-- | libc/test/include/iszerol_test.cpp | 12 |
7 files changed, 145 insertions, 0 deletions
diff --git a/libc/include/llvm-libc-macros/math-function-macros.h b/libc/include/llvm-libc-macros/math-function-macros.h index 8d18997..afbabd0 100644 --- a/libc/include/llvm-libc-macros/math-function-macros.h +++ b/libc/include/llvm-libc-macros/math-function-macros.h @@ -13,5 +13,6 @@ #define isinf(x) __builtin_isinf(x) #define isnan(x) __builtin_isnan(x) #define signbit(x) __builtin_signbit(x) +#define iszero(x) (x == 0) #endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt index 32513f2..1a2f187 100644 --- a/libc/test/include/CMakeLists.txt +++ b/libc/test/include/CMakeLists.txt @@ -82,6 +82,36 @@ add_libc_test( ) add_libc_test( + iszero_test + SUITE + libc_include_tests + SRCS + iszero_test.cpp + DEPENDS + libc.include.llvm-libc-macros.math_function_macros +) + +add_libc_test( + iszerof_test + SUITE + libc_include_tests + SRCS + iszerof_test.cpp + DEPENDS + libc.include.llvm-libc-macros.math_function_macros +) + +add_libc_test( + iszerol_test + SUITE + libc_include_tests + SRCS + iszerol_test.cpp + DEPENDS + libc.include.llvm-libc-macros.math_function_macros +) + +add_libc_test( signbit_test SUITE libc_include_tests @@ -260,3 +290,18 @@ add_libc_test( DEPENDS libc.include.llvm-libc-macros.math_function_macros ) + +add_libc_test( + iszero_c_test + C_TEST + UNIT_TEST_ONLY + SUITE + libc_include_tests + SRCS + iszero_test.c + COMPILE_OPTIONS + -Wall + -Werror + DEPENDS + libc.include.llvm-libc-macros.math_function_macros +) diff --git a/libc/test/include/IsZeroTest.h b/libc/test/include/IsZeroTest.h new file mode 100644 index 0000000..8387196 --- /dev/null +++ b/libc/test/include/IsZeroTest.h @@ -0,0 +1,38 @@ +//===-- Utility class to test the iszero 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_ISZERO_H +#define LLVM_LIBC_TEST_INCLUDE_MATH_ISZERO_H + +#include "test/UnitTest/FPMatcher.h" +#include "test/UnitTest/Test.h" + +#include "include/llvm-libc-macros/math-function-macros.h" + +template <typename T> class IsZeroTest : public LIBC_NAMESPACE::testing::Test { + DECLARE_SPECIAL_CONSTANTS(T) + +public: + typedef bool (*IsZeroFunc)(T); + + void testSpecialNumbers(IsZeroFunc func) { + EXPECT_FALSE(func(inf)); + EXPECT_FALSE(func(neg_inf)); + EXPECT_TRUE(func(zero)); + EXPECT_TRUE(func(neg_zero)); + } +}; + +#define LIST_ISZERO_TESTS(T, func) \ + using LlvmLibcIsZeroTest = IsZeroTest<T>; \ + TEST_F(LlvmLibcIsZeroTest, SpecialNumbers) { \ + auto iszero_func = [](T x) { return func(x); }; \ + testSpecialNumbers(iszero_func); \ + } + +#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISZERO_H diff --git a/libc/test/include/iszero_test.c b/libc/test/include/iszero_test.c new file mode 100644 index 0000000..be2d34a --- /dev/null +++ b/libc/test/include/iszero_test.c @@ -0,0 +1,25 @@ +//===-- Unittests for iszero 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 iszero +#error "iszero macro is not defined" +#else +int main(void) { + assert(iszero(1.0f) == 0); + assert(iszero(1.0) == 0); + assert(iszero(1.0L) == 0); + assert(iszero(0.0f) == 1); + assert(iszero(0.0) == 1); + assert(iszero(0.0L) == 1); + return 0; +} +#endif diff --git a/libc/test/include/iszero_test.cpp b/libc/test/include/iszero_test.cpp new file mode 100644 index 0000000..c478097 --- /dev/null +++ b/libc/test/include/iszero_test.cpp @@ -0,0 +1,12 @@ +//===-- Unittest for iszero[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 "IsZeroTest.h" +#include "include/llvm-libc-macros/math-function-macros.h" + +LIST_ISZERO_TESTS(double, iszero) diff --git a/libc/test/include/iszerof_test.cpp b/libc/test/include/iszerof_test.cpp new file mode 100644 index 0000000..8bf5319 --- /dev/null +++ b/libc/test/include/iszerof_test.cpp @@ -0,0 +1,12 @@ +//===-- Unittest for iszero[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 "IsZeroTest.h" +#include "include/llvm-libc-macros/math-function-macros.h" + +LIST_ISZERO_TESTS(float, iszero) diff --git a/libc/test/include/iszerol_test.cpp b/libc/test/include/iszerol_test.cpp new file mode 100644 index 0000000..1b1249f --- /dev/null +++ b/libc/test/include/iszerol_test.cpp @@ -0,0 +1,12 @@ +//===-- Unittest for iszero[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 "IsZeroTest.h" +#include "include/llvm-libc-macros/math-function-macros.h" + +LIST_ISZERO_TESTS(long double, iszero) |