[libc-commits] [libc] [libc][math] implement `signbit` (PR #97791)
via libc-commits
libc-commits at lists.llvm.org
Fri Jul 5 17:49:35 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Akiel Aries (akielaries)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/97791.diff
5 Files Affected:
- (modified) libc/include/llvm-libc-macros/CMakeLists.txt (+6)
- (added) libc/include/llvm-libc-macros/generic-math-macros.h (+20)
- (modified) libc/include/llvm-libc-macros/math-macros.h (-5)
- (modified) libc/test/include/CMakeLists.txt (+10)
- (added) libc/test/include/generic-math-macros_test.cpp (+87)
``````````diff
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/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/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/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 03c31855e352b..7a805fc3a2e66 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -79,3 +79,13 @@ 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
new file mode 100644
index 0000000000000..d7b37c7ba4e8b
--- /dev/null
+++ b/libc/test/include/generic-math-macros_test.cpp
@@ -0,0 +1,87 @@
+//===-- 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 "include/llvm-libc-macros/generic-math-macros.h"
+
+// INF can be defined as a number with zeroed out mantissa and 1s in the
+// exponent
+static uint32_t positive_infinity = 0x7F800000;
+static uint32_t negative_infinity = 0xFF800000;
+static const float pos_inf = *(float *) &positive_infinity;
+static const float neg_inf = *(float *) &negative_infinity;
+
+// NaN can be defined as a number with all 1s in the exponent
+static uint32_t positive_nan = 0x7F800001;
+static uint32_t negative_nan = 0xFF800001;
+static const float pos_nan = *(float *) &positive_nan;
+static const float neg_nan = *(float *) &negative_nan;
+
+#define PI 3.14159265358979323846
+#define CASE_DIV_BY_ZERO PI / 0.0
+#define CASE_DIV_BY_POS_INF PI / pos_inf
+#define CASE_DIV_BY_NEG_INF PI / neg_inf
+#define CASE_MULT_ZERO_BY_POS_INF 0 * pos_inf
+#define CASE_MULT_ZERO_BY_NEG_INF 0 * neg_inf
+
+/*
+ * As with IEEE 754-1985, the biased-exponent field is filled with all 1 bits
+ * to indicate either infinity (trailing significand field = 0) or a NaN
+ * (trailing significand field ≠ 0)
+ */
+
+TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsfinite) {
+ EXPECT_EQ(isfinite(pos_inf), 0);
+ EXPECT_EQ(isfinite(neg_inf), 0);
+ EXPECT_EQ(isfinite(pos_nan), 0);
+ EXPECT_EQ(isfinite(neg_nan), 0);
+ EXPECT_EQ(isfinite(CASE_DIV_BY_ZERO), 0);
+ EXPECT_EQ(isfinite(PI), 1);
+}
+
+TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsinf) {
+ EXPECT_EQ(isinf(PI), 0);
+ EXPECT_EQ(isinf(CASE_DIV_BY_POS_INF), 0);
+ EXPECT_EQ(isinf(CASE_DIV_BY_NEG_INF), 0);
+ EXPECT_EQ(isinf(CASE_MULT_ZERO_BY_POS_INF), 0);
+ EXPECT_EQ(isinf(CASE_MULT_ZERO_BY_NEG_INF), 0);
+ EXPECT_EQ(isinf(pos_inf), 1);
+ EXPECT_EQ(isinf(neg_inf), 1);
+ EXPECT_EQ(isinf(CASE_DIV_BY_ZERO), 1);
+}
+
+TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsnan) {
+ EXPECT_EQ(isnan(PI), 0);
+ EXPECT_EQ(isnan(CASE_DIV_BY_ZERO), 0);
+ EXPECT_EQ(isnan(CASE_DIV_BY_POS_INF), 0);
+ EXPECT_EQ(isnan(CASE_DIV_BY_NEG_INF), 0);
+ EXPECT_EQ(isnan(pos_nan), 1);
+ EXPECT_EQ(isnan(neg_nan), 1);
+ EXPECT_EQ(isnan(CASE_MULT_ZERO_BY_POS_INF), 1);
+ EXPECT_EQ(isnan(CASE_MULT_ZERO_BY_NEG_INF), 1);
+ EXPECT_EQ(isnan(pos_inf / neg_inf), 1);
+}
+
+TEST(LlvmLibcGenericMath, TypeGenericMacroMathSignbit) {
+ EXPECT_EQ(signbit(static_cast<float>(PI)), 0);
+ EXPECT_EQ(signbit(static_cast<double>(PI)), 0);
+ EXPECT_EQ(signbit(static_cast<long double>(PI)), 0);
+ EXPECT_EQ(signbit(pos_inf), 0);
+ EXPECT_EQ(signbit(pos_nan), 0);
+
+ EXPECT_EQ(signbit(static_cast<float>(-PI)), 1);
+ EXPECT_EQ(signbit(static_cast<double>(-PI)), 1);
+ EXPECT_EQ(signbit(static_cast<long double>(-PI)), 1);
+ EXPECT_EQ(signbit(neg_inf), 1);
+ EXPECT_EQ(signbit(neg_nan), 1);
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/97791
More information about the libc-commits
mailing list