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

Akiel Aries via libc-commits libc-commits at lists.llvm.org
Thu Jul 4 23:22:32 PDT 2024


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

This PR addresses #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.

>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/3] [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 00000000000000..8fec2cc6c02ea7
--- /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 00000000000000..e69de29bb2d1d6
diff --git a/libc/include/llvm-libc-macros/math-macros.h b/libc/include/llvm-libc-macros/math-macros.h
index 47838969d59aed..bcda32a615b628 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 00000000000000..d988ec3110fbac
--- /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/3] [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 e69de29bb2d1d6..00000000000000

>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/3] [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 03c31855e352ba..6cae50eb0e82ce 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 d988ec3110fbac..cd31aa64d04360 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) {
+
+}*/



More information about the libc-commits mailing list