[libc-commits] [libc] [libc][[math] Implement IsZero Macro (PR #109336)

Shourya Goel via libc-commits libc-commits at lists.llvm.org
Fri Sep 20 07:47:47 PDT 2024


https://github.com/Sh0g0-1758 updated https://github.com/llvm/llvm-project/pull/109336

>From d9b0d830f0ebc026ad81daa40cbd9d56f358bcc0 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 20 Sep 2024 02:43:39 +0530
Subject: [PATCH 1/7] Implement IsZero

---
 .../llvm-libc-macros/math-function-macros.h   |  1 +
 libc/test/include/CMakeLists.txt              | 10 +++++
 libc/test/include/IsZeroTest.h                | 39 +++++++++++++++++++
 libc/test/include/iszero_test.c               | 25 ++++++++++++
 libc/test/include/iszero_test.cpp             | 12 ++++++
 libc/test/include/iszerof_test.cpp            | 12 ++++++
 libc/test/include/iszerol_test.cpp            | 12 ++++++
 7 files changed, 111 insertions(+)
 create mode 100644 libc/test/include/IsZeroTest.h
 create mode 100644 libc/test/include/iszero_test.c
 create mode 100644 libc/test/include/iszero_test.cpp
 create mode 100644 libc/test/include/iszerof_test.cpp
 create mode 100644 libc/test/include/iszerol_test.cpp

diff --git a/libc/include/llvm-libc-macros/math-function-macros.h b/libc/include/llvm-libc-macros/math-function-macros.h
index 8d18997bed8d26..eee7f969dfb4c2 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) __builtin_iszero(x)
 
 #endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 32513f234b9b99..d60466c6608df3 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -81,6 +81,16 @@ add_libc_test(
     libc.include.llvm-libc-macros.stdckdint_macros
 )
 
+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(
   signbit_test
   SUITE
diff --git a/libc/test/include/IsZeroTest.h b/libc/test/include/IsZeroTest.h
new file mode 100644
index 00000000000000..3e9788d1ce0aab
--- /dev/null
+++ b/libc/test/include/IsZeroTest.h
@@ -0,0 +1,39 @@
+//===-- 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 int (*IsZeroFunc)(T);
+
+  void testSpecialNumbers(IsZeroFunc func) {
+    EXPECT_EQ(func(inf), 0);
+    EXPECT_EQ(func(neg_inf), 0);
+    EXPECT_EQ(func(zero), 1);
+    EXPECT_EQ(func(neg_zero), 1);
+  }
+};
+
+#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 00000000000000..be2d34ae3e395b
--- /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 00000000000000..c47809708e0bc5
--- /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 00000000000000..8bf5319f6e9be5
--- /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 00000000000000..1b1249f9f0552a
--- /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)

>From 2450df501c825a7793a08df5323f39062ba7a14a Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 20 Sep 2024 02:45:45 +0530
Subject: [PATCH 2/7] Ran formatter

---
 libc/test/include/IsZeroTest.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libc/test/include/IsZeroTest.h b/libc/test/include/IsZeroTest.h
index 3e9788d1ce0aab..f15bca9e7d487d 100644
--- a/libc/test/include/IsZeroTest.h
+++ b/libc/test/include/IsZeroTest.h
@@ -14,8 +14,7 @@
 
 #include "include/llvm-libc-macros/math-function-macros.h"
 
-template <typename T>
-class IsZeroTest : public LIBC_NAMESPACE::testing::Test {
+template <typename T> class IsZeroTest : public LIBC_NAMESPACE::testing::Test {
   DECLARE_SPECIAL_CONSTANTS(T)
 
 public:

>From 4421fddc983ec5122343b331032d3973f8de0412 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 20 Sep 2024 03:10:27 +0530
Subject: [PATCH 3/7] add remaining tests in CMakeLists

---
 libc/test/include/CMakeLists.txt | 35 ++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index d60466c6608df3..1a2f18731565c2 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -91,6 +91,26 @@ add_libc_test(
     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
@@ -270,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
+)

>From 9ba3d2b8446c386c2c68436f96b68c22165ec55a Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 20 Sep 2024 19:00:13 +0530
Subject: [PATCH 4/7] Refactor iszero(x)

---
 libc/include/llvm-libc-macros/math-function-macros.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/include/llvm-libc-macros/math-function-macros.h b/libc/include/llvm-libc-macros/math-function-macros.h
index eee7f969dfb4c2..07d49a2a5860b9 100644
--- a/libc/include/llvm-libc-macros/math-function-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -13,6 +13,6 @@
 #define isinf(x) __builtin_isinf(x)
 #define isnan(x) __builtin_isnan(x)
 #define signbit(x) __builtin_signbit(x)
-#define iszero(x) __builtin_iszero(x)
+#define iszero(x) ((x) == 0.0)
 
 #endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H

>From f0b5a825b9e219e2b7fecdb2dbed513cd13fe5de Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 20 Sep 2024 19:22:53 +0530
Subject: [PATCH 5/7] change 0.0 to 0

---
 libc/include/llvm-libc-macros/math-function-macros.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/include/llvm-libc-macros/math-function-macros.h b/libc/include/llvm-libc-macros/math-function-macros.h
index 07d49a2a5860b9..b7d69b876afcb9 100644
--- a/libc/include/llvm-libc-macros/math-function-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -13,6 +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.0)
+#define iszero(x) ((x) == 0)
 
 #endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H

>From 3b50494d43c1bb44e234a18043f7c48988184576 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 20 Sep 2024 19:51:21 +0530
Subject: [PATCH 6/7] Typecase to int

---
 libc/include/llvm-libc-macros/math-function-macros.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/include/llvm-libc-macros/math-function-macros.h b/libc/include/llvm-libc-macros/math-function-macros.h
index b7d69b876afcb9..00fcdbc9440af3 100644
--- a/libc/include/llvm-libc-macros/math-function-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -13,6 +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)
+#define iszero(x) int(x == 0)
 
 #endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H

>From 473dc31e505a1658c8fa8ca5ee7a734a8bb40497 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Fri, 20 Sep 2024 20:16:40 +0530
Subject: [PATCH 7/7] refactor tests

---
 libc/include/llvm-libc-macros/math-function-macros.h |  2 +-
 libc/test/include/IsZeroTest.h                       | 10 +++++-----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/libc/include/llvm-libc-macros/math-function-macros.h b/libc/include/llvm-libc-macros/math-function-macros.h
index 00fcdbc9440af3..afbabd023203a2 100644
--- a/libc/include/llvm-libc-macros/math-function-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -13,6 +13,6 @@
 #define isinf(x) __builtin_isinf(x)
 #define isnan(x) __builtin_isnan(x)
 #define signbit(x) __builtin_signbit(x)
-#define iszero(x) int(x == 0)
+#define iszero(x) (x == 0)
 
 #endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
diff --git a/libc/test/include/IsZeroTest.h b/libc/test/include/IsZeroTest.h
index f15bca9e7d487d..83871963cd2125 100644
--- a/libc/test/include/IsZeroTest.h
+++ b/libc/test/include/IsZeroTest.h
@@ -18,13 +18,13 @@ template <typename T> class IsZeroTest : public LIBC_NAMESPACE::testing::Test {
   DECLARE_SPECIAL_CONSTANTS(T)
 
 public:
-  typedef int (*IsZeroFunc)(T);
+  typedef bool (*IsZeroFunc)(T);
 
   void testSpecialNumbers(IsZeroFunc func) {
-    EXPECT_EQ(func(inf), 0);
-    EXPECT_EQ(func(neg_inf), 0);
-    EXPECT_EQ(func(zero), 1);
-    EXPECT_EQ(func(neg_zero), 1);
+    EXPECT_FALSE(func(inf));
+    EXPECT_FALSE(func(neg_inf));
+    EXPECT_TRUE(func(zero));
+    EXPECT_TRUE(func(neg_zero));
   }
 };
 



More information about the libc-commits mailing list