[libc-commits] [libc] [libc][math] implement `signbit` (PR #97791)

Akiel Aries via libc-commits libc-commits at lists.llvm.org
Fri Jul 5 11:09:07 PDT 2024


https://github.com/akielaries updated https://github.com/llvm/llvm-project/pull/97791

>From 88aa30aaa0c94cbbbd729d641299d4711ac1b3d6 Mon Sep 17 00:00:00 2001
From: akielaries <akiel at akiel.org>
Date: Thu, 4 Jul 2024 21:12:58 -0700
Subject: [PATCH 1/4] [libc][math] adding scaffolding for solving #96322

---
 .../llvm-libc-macros/generic-math-macros.h    | 20 +++++++++++
 libc/include/llvm-libc-macros/genmv           |  0
 libc/include/llvm-libc-macros/math-macros.h   |  5 ---
 .../test/include/generic-math-macros_test.cpp | 34 +++++++++++++++++++
 4 files changed, 54 insertions(+), 5 deletions(-)
 create mode 100644 libc/include/llvm-libc-macros/generic-math-macros.h
 create mode 100644 libc/include/llvm-libc-macros/genmv
 create mode 100644 libc/test/include/generic-math-macros_test.cpp

diff --git a/libc/include/llvm-libc-macros/generic-math-macros.h b/libc/include/llvm-libc-macros/generic-math-macros.h
new file mode 100644
index 0000000000000..8fec2cc6c02ea
--- /dev/null
+++ b/libc/include/llvm-libc-macros/generic-math-macros.h
@@ -0,0 +1,20 @@
+//===-- Definition of macros from math.h ----------------------------------===//
+//
+// 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_MACROS_GENERIC_MATH_MACROS_H
+#define LLVM_LIBC_MACROS_GENERIC_MATH_MACROS_H
+
+#define isfinite(x) __builtin_isfinite(x)
+#define isinf(x) __builtin_isinf(x)
+#define isnan(x) __builtin_isnan(x)
+#define signbit(x) \
+    ((sizeof(x) == sizeof(float)) ? __builtin_signbitf(x) \
+    : (sizeof(x) == sizeof(double)) ? __builtin_signbit(x) \
+    : __builtin_signbitl(x))
+
+#endif // LLVM_LIBC_MACROS_GENERIC_MATH_MACROS_H
diff --git a/libc/include/llvm-libc-macros/genmv b/libc/include/llvm-libc-macros/genmv
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/libc/include/llvm-libc-macros/math-macros.h b/libc/include/llvm-libc-macros/math-macros.h
index 47838969d59ae..bcda32a615b62 100644
--- a/libc/include/llvm-libc-macros/math-macros.h
+++ b/libc/include/llvm-libc-macros/math-macros.h
@@ -51,9 +51,4 @@
 #define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
 #endif
 
-// TODO: Move generic functional math macros to a separate header file.
-#define isfinite(x) __builtin_isfinite(x)
-#define isinf(x) __builtin_isinf(x)
-#define isnan(x) __builtin_isnan(x)
-
 #endif // LLVM_LIBC_MACROS_MATH_MACROS_H
diff --git a/libc/test/include/generic-math-macros_test.cpp b/libc/test/include/generic-math-macros_test.cpp
new file mode 100644
index 0000000000000..d988ec3110fba
--- /dev/null
+++ b/libc/test/include/generic-math-macros_test.cpp
@@ -0,0 +1,34 @@
+//===-- Unittests for stdbit ----------------------------------------------===//
+//
+// 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 "test/UnitTest/Test.h"
+
+/*
+ * The intent of this test is validate that the generic math macros operate as
+ * intended
+ */
+
+#include "stdbit_stub.h"
+
+#include "include/llvm-libc-macros/generic-math-macros.h"
+
+TEST(LlvmLibcIsfinite, TypeGenericMacroMathIsfinite) {
+
+}
+
+TEST(LlvmLibcIsinf, TypeGenericMacroMathIsinf) {
+
+}
+
+TEST(LlvmLibcIsnan, TypeGenericMacroMathIsnan) {
+
+}
+
+TEST(LlvmLibcSignbit, TypeGenericMacroMathSignbit) {
+
+}

>From f3c51abcb303baa3daa1f91055b9fe39eb8f75d9 Mon Sep 17 00:00:00 2001
From: akielaries <akiel at akiel.org>
Date: Thu, 4 Jul 2024 21:13:21 -0700
Subject: [PATCH 2/4] [libc][math] adding scaffolding for solving #96322

---
 libc/include/llvm-libc-macros/genmv | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 libc/include/llvm-libc-macros/genmv

diff --git a/libc/include/llvm-libc-macros/genmv b/libc/include/llvm-libc-macros/genmv
deleted file mode 100644
index e69de29bb2d1d..0000000000000

>From 06eb1e293e5e74294aaef6498f9bb46e2d13267a Mon Sep 17 00:00:00 2001
From: akielaries <akiel at akiel.org>
Date: Thu, 4 Jul 2024 23:00:20 -0700
Subject: [PATCH 3/4] [libc][math] adding additional scaffolding while
 compiling source

---
 libc/test/include/CMakeLists.txt               | 12 ++++++++++++
 libc/test/include/generic-math-macros_test.cpp | 17 ++++++++++-------
 2 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 03c31855e352b..6cae50eb0e82c 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -68,6 +68,18 @@ if(LLVM_LIBC_FULL_BUILD AND libc.include.stdbit IN_LIST TARGET_PUBLIC_HEADERS)
       # of the underlying functions which the type generic macros may dispatch
       # to.
   )
+  add_libc_test(
+    generic_math_test
+    SUITE
+      libc_include_tests
+    # HRDS
+    SRCS
+      generic-math-macros_test.cpp
+    DEPENDS
+      libc.include.llvm-libc-macros.generic_math_macros
+      libc.include.llvm-libc-macros.stdbit_macros
+      libc.include.llvm_libc_common_h
+  )
 endif()
 
 add_libc_test(
diff --git a/libc/test/include/generic-math-macros_test.cpp b/libc/test/include/generic-math-macros_test.cpp
index d988ec3110fba..cd31aa64d0436 100644
--- a/libc/test/include/generic-math-macros_test.cpp
+++ b/libc/test/include/generic-math-macros_test.cpp
@@ -13,22 +13,25 @@
  * intended
  */
 
-#include "stdbit_stub.h"
+// #include "stdbit_stub.h"
 
 #include "include/llvm-libc-macros/generic-math-macros.h"
 
-TEST(LlvmLibcIsfinite, TypeGenericMacroMathIsfinite) {
-
+TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsfinite) {
+  EXPECT_EQ(isfinite(3.14), 0);
+  EXPECT_EQ(isfinite(3.14 / 0.0), 1);
 }
 
-TEST(LlvmLibcIsinf, TypeGenericMacroMathIsinf) {
+/*
+TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsinf) {
 
 }
 
-TEST(LlvmLibcIsnan, TypeGenericMacroMathIsnan) {
+TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsnan) {
 
 }
 
-TEST(LlvmLibcSignbit, TypeGenericMacroMathSignbit) {
 
-}
+TEST(LlvmLibcGenericMath, TypeGenericMacroMathSignbit) {
+
+}*/

>From da15670fa70d08a9626c122bb31a1d0bd206941b Mon Sep 17 00:00:00 2001
From: akielaries <akiel at akiel.org>
Date: Fri, 5 Jul 2024 11:08:54 -0700
Subject: [PATCH 4/4] [libc][math] adding generic math macros header and test
 to CMake build system. verifying 1 case for now

---
 libc/include/llvm-libc-macros/CMakeLists.txt  |  6 +++++
 libc/test/include/CMakeLists.txt              | 23 +++++++++----------
 .../test/include/generic-math-macros_test.cpp |  6 ++---
 3 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index f6af11abd4dd7..666b6337ddf37 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -277,3 +277,9 @@ add_macro_header(
   HDR
     stdckdint-macros.h
 )
+
+add_macro_header(
+  generic_math_macros
+  HDR
+    generic-math-macros.h
+)
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 6cae50eb0e82c..52fa9b230f289 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -68,18 +68,6 @@ if(LLVM_LIBC_FULL_BUILD AND libc.include.stdbit IN_LIST TARGET_PUBLIC_HEADERS)
       # of the underlying functions which the type generic macros may dispatch
       # to.
   )
-  add_libc_test(
-    generic_math_test
-    SUITE
-      libc_include_tests
-    # HRDS
-    SRCS
-      generic-math-macros_test.cpp
-    DEPENDS
-      libc.include.llvm-libc-macros.generic_math_macros
-      libc.include.llvm-libc-macros.stdbit_macros
-      libc.include.llvm_libc_common_h
-  )
 endif()
 
 add_libc_test(
@@ -91,3 +79,14 @@ add_libc_test(
   DEPENDS
     libc.include.llvm-libc-macros.stdckdint_macros
 )
+
+add_libc_test(
+  generic_math_macros_test
+  SUITE
+    libc_include_tests
+  SRCS
+    generic-math-macros_test.cpp
+  DEPENDS
+    libc.include.llvm-libc-macros.generic_math_macros
+)
+
diff --git a/libc/test/include/generic-math-macros_test.cpp b/libc/test/include/generic-math-macros_test.cpp
index cd31aa64d0436..42712f22f335b 100644
--- a/libc/test/include/generic-math-macros_test.cpp
+++ b/libc/test/include/generic-math-macros_test.cpp
@@ -13,13 +13,13 @@
  * intended
  */
 
-// #include "stdbit_stub.h"
+#include "stdbit_stub.h"
 
 #include "include/llvm-libc-macros/generic-math-macros.h"
 
 TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsfinite) {
-  EXPECT_EQ(isfinite(3.14), 0);
-  EXPECT_EQ(isfinite(3.14 / 0.0), 1);
+  EXPECT_EQ(isfinite(3.14), 1);
+  EXPECT_EQ(isfinite(3.14 / 0.0), 0);
 }
 
 /*



More information about the libc-commits mailing list