[libc-commits] [libc] [libc][math] implement `signbit` (PR #97791)
via libc-commits
libc-commits at lists.llvm.org
Fri Jul 12 21:10:38 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 01/17] [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 02/17] [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 03/17] [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 04/17] [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);
}
/*
>From 665d0df8b61a03c6c4c8d769edca173f93abf44b Mon Sep 17 00:00:00 2001
From: akielaries <akiel at akiel.org>
Date: Fri, 5 Jul 2024 16:15:25 -0700
Subject: [PATCH 05/17] [libc][math] adding valuable test cases. working on
cleanup
---
.../test/include/generic-math-macros_test.cpp | 46 ++++++++++++++++---
1 file changed, 40 insertions(+), 6 deletions(-)
diff --git a/libc/test/include/generic-math-macros_test.cpp b/libc/test/include/generic-math-macros_test.cpp
index 42712f22f335b..9be7f2c3cc40e 100644
--- a/libc/test/include/generic-math-macros_test.cpp
+++ b/libc/test/include/generic-math-macros_test.cpp
@@ -12,26 +12,60 @@
* The intent of this test is validate that the generic math macros operate as
* intended
*/
+#include "include/llvm-libc-macros/generic-math-macros.h"
-#include "stdbit_stub.h"
-#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;
+
+/*
+ * 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(3.14), 1);
EXPECT_EQ(isfinite(3.14 / 0.0), 0);
+ EXPECT_EQ(isfinite(pos_inf), 0);
+ EXPECT_EQ(isfinite(neg_inf), 0);
+ EXPECT_EQ(isfinite(pos_nan), 0);
+ EXPECT_EQ(isfinite(neg_nan), 0);
}
-/*
TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsinf) {
-
+ EXPECT_EQ(isinf(3.14), 0);
+ EXPECT_EQ(isinf(pos_inf), 1);
+ EXPECT_EQ(isinf(neg_inf), 1);
+ EXPECT_EQ(isnan(0.0 * pos_inf), 1); // multiply 0 by infinity
+ EXPECT_EQ(isinf(3.14 / 0.0), 1); // division by 0
+ EXPECT_EQ(isnan(3.14 / pos_inf), 0); // division by infinity
}
TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsnan) {
-
+ EXPECT_EQ(isnan(3.14), 0);
+ EXPECT_EQ(isnan(pos_nan), 1);
+ EXPECT_EQ(isnan(neg_nan), 1);
+ EXPECT_EQ(isnan(0.0 * pos_inf), 1); // multiply 0 by infinity
+ EXPECT_EQ(isnan(3.14 / 0.0), 0); // division by 0
+ EXPECT_EQ(isnan(3.14 / pos_inf), 0); // division by infinity
+ EXPECT_EQ(isnan(pos_inf / neg_inf), 1); // pos infinity / neg infinity
}
-
+/*
TEST(LlvmLibcGenericMath, TypeGenericMacroMathSignbit) {
+ // float
+
+ // double
}*/
>From aa9bdff2be9929e75cd06e6a083064975f26af0e Mon Sep 17 00:00:00 2001
From: akielaries <akiel at akiel.org>
Date: Fri, 5 Jul 2024 17:07:16 -0700
Subject: [PATCH 06/17] [libc][math] editing test cases
---
.../test/include/generic-math-macros_test.cpp | 33 ++++++++++++-------
1 file changed, 21 insertions(+), 12 deletions(-)
diff --git a/libc/test/include/generic-math-macros_test.cpp b/libc/test/include/generic-math-macros_test.cpp
index 9be7f2c3cc40e..1c41bfccc573e 100644
--- a/libc/test/include/generic-math-macros_test.cpp
+++ b/libc/test/include/generic-math-macros_test.cpp
@@ -14,7 +14,6 @@
*/
#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;
@@ -28,6 +27,13 @@ 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
@@ -35,31 +41,34 @@ static const float neg_nan = *(float *) &negative_nan;
*/
TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsfinite) {
- EXPECT_EQ(isfinite(3.14), 1);
- EXPECT_EQ(isfinite(3.14 / 0.0), 0);
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(3.14), 0);
+ 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(isnan(0.0 * pos_inf), 1); // multiply 0 by infinity
- EXPECT_EQ(isinf(3.14 / 0.0), 1); // division by 0
- EXPECT_EQ(isnan(3.14 / pos_inf), 0); // division by infinity
+ EXPECT_EQ(isinf(CASE_DIV_BY_ZERO), 1);
}
TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsnan) {
- EXPECT_EQ(isnan(3.14), 0);
+ EXPECT_EQ(isnan(PI), 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(0.0 * pos_inf), 1); // multiply 0 by infinity
- EXPECT_EQ(isnan(3.14 / 0.0), 0); // division by 0
- EXPECT_EQ(isnan(3.14 / pos_inf), 0); // division by infinity
- EXPECT_EQ(isnan(pos_inf / neg_inf), 1); // pos infinity / neg infinity
+ 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);
}
/*
>From c955ec1743c72c545680c828199199f042d7dbbb Mon Sep 17 00:00:00 2001
From: akielaries <akiel at akiel.org>
Date: Fri, 5 Jul 2024 17:44:17 -0700
Subject: [PATCH 07/17] [libc][math] adding cases for `signbit` for `float`,
`double`, and `long double` types
---
libc/test/include/CMakeLists.txt | 1 -
libc/test/include/generic-math-macros_test.cpp | 17 ++++++++++++-----
2 files changed, 12 insertions(+), 6 deletions(-)
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 52fa9b230f289..7a805fc3a2e66 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -89,4 +89,3 @@ add_libc_test(
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 1c41bfccc573e..d7b37c7ba4e8b 100644
--- a/libc/test/include/generic-math-macros_test.cpp
+++ b/libc/test/include/generic-math-macros_test.cpp
@@ -62,6 +62,7 @@ TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsinf) {
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);
@@ -71,10 +72,16 @@ TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsnan) {
EXPECT_EQ(isnan(pos_inf / neg_inf), 1);
}
-/*
TEST(LlvmLibcGenericMath, TypeGenericMacroMathSignbit) {
- // float
-
- // double
+ 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);
+}
>From 724633d588354b4bc868ab9852467d623c137872 Mon Sep 17 00:00:00 2001
From: akielaries <akiel at akiel.org>
Date: Fri, 5 Jul 2024 17:52:05 -0700
Subject: [PATCH 08/17] [libc][math] fixing typo
---
libc/test/include/generic-math-macros_test.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libc/test/include/generic-math-macros_test.cpp b/libc/test/include/generic-math-macros_test.cpp
index d7b37c7ba4e8b..6adbc029c9f92 100644
--- a/libc/test/include/generic-math-macros_test.cpp
+++ b/libc/test/include/generic-math-macros_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for stdbit ----------------------------------------------===//
+//===-- Unittests for generic-math-macros ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
>From 118feda796b579659ce65c0ed8569bbe50c009cf Mon Sep 17 00:00:00 2001
From: akielaries <akiel at akiel.org>
Date: Fri, 5 Jul 2024 20:43:13 -0700
Subject: [PATCH 09/17] [libc][math] clarity around inf and nan definitions in
`generic-math-macros_test.cpp`
---
libc/test/include/generic-math-macros_test.cpp | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/libc/test/include/generic-math-macros_test.cpp b/libc/test/include/generic-math-macros_test.cpp
index 6adbc029c9f92..651bc8e507e01 100644
--- a/libc/test/include/generic-math-macros_test.cpp
+++ b/libc/test/include/generic-math-macros_test.cpp
@@ -16,16 +16,16 @@
// 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;
+static uint32_t pos_inf_bits = 0x7F800000;
+static uint32_t neg_inf_bits = 0xFF800000;
+static const float pos_inf = *(float *) &pos_inf_bits;
+static const float neg_inf = *(float *) &neg_inf_bits;
// 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;
+static uint32_t pos_nan_bits = 0x7F800001;
+static uint32_t neg_nan_bits = 0xFF800001;
+static const float pos_nan = *(float *) &pos_nan_bits;
+static const float neg_nan = *(float *) &neg_nan_bits;
#define PI 3.14159265358979323846
#define CASE_DIV_BY_ZERO PI / 0.0
>From 3d83dc314eb33cdf443eb4029429e151079bf2c6 Mon Sep 17 00:00:00 2001
From: akielaries <akiel at akiel.org>
Date: Sat, 6 Jul 2024 10:10:46 -0700
Subject: [PATCH 10/17] [libc][math] formatting
---
libc/test/include/generic-math-macros_test.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/libc/test/include/generic-math-macros_test.cpp b/libc/test/include/generic-math-macros_test.cpp
index 651bc8e507e01..809aceb4ba8b8 100644
--- a/libc/test/include/generic-math-macros_test.cpp
+++ b/libc/test/include/generic-math-macros_test.cpp
@@ -18,14 +18,14 @@
// exponent
static uint32_t pos_inf_bits = 0x7F800000;
static uint32_t neg_inf_bits = 0xFF800000;
-static const float pos_inf = *(float *) &pos_inf_bits;
-static const float neg_inf = *(float *) &neg_inf_bits;
+static const float pos_inf = *(float *)&pos_inf_bits;
+static const float neg_inf = *(float *)&neg_inf_bits;
// NaN can be defined as a number with all 1s in the exponent
static uint32_t pos_nan_bits = 0x7F800001;
static uint32_t neg_nan_bits = 0xFF800001;
-static const float pos_nan = *(float *) &pos_nan_bits;
-static const float neg_nan = *(float *) &neg_nan_bits;
+static const float pos_nan = *(float *)&pos_nan_bits;
+static const float neg_nan = *(float *)&neg_nan_bits;
#define PI 3.14159265358979323846
#define CASE_DIV_BY_ZERO PI / 0.0
>From e3259e623e905f69df08e6e54f334c616176b8be Mon Sep 17 00:00:00 2001
From: akielaries <akiel at akiel.org>
Date: Sun, 7 Jul 2024 17:15:40 -0700
Subject: [PATCH 11/17] [libc][math] addressing @lntue's comments. edit naming
conventions and remove type checking in macro
---
libc/include/llvm-libc-macros/CMakeLists.txt | 4 +-
...c-math-macros.h => math-function-macros.h} | 11 ++--
libc/test/include/CMakeLists.txt | 6 +-
libc/test/include/math-function-macros_test.c | 61 +++++++++++++++++++
...test.cpp => math-function-macros_test.cpp} | 10 +--
5 files changed, 75 insertions(+), 17 deletions(-)
rename libc/include/llvm-libc-macros/{generic-math-macros.h => math-function-macros.h} (60%)
create mode 100644 libc/test/include/math-function-macros_test.c
rename libc/test/include/{generic-math-macros_test.cpp => math-function-macros_test.cpp} (90%)
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 99d3048b860e1..783782ec62e80 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -285,7 +285,7 @@ add_macro_header(
)
add_macro_header(
- generic_math_macros
+ math_function_macros
HDR
- generic-math-macros.h
+ math-function-macros.h
)
diff --git a/libc/include/llvm-libc-macros/generic-math-macros.h b/libc/include/llvm-libc-macros/math-function-macros.h
similarity index 60%
rename from libc/include/llvm-libc-macros/generic-math-macros.h
rename to libc/include/llvm-libc-macros/math-function-macros.h
index 8fec2cc6c02ea..8fb1b2324ac99 100644
--- a/libc/include/llvm-libc-macros/generic-math-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -6,15 +6,12 @@
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_LIBC_MACROS_GENERIC_MATH_MACROS_H
-#define LLVM_LIBC_MACROS_GENERIC_MATH_MACROS_H
+#ifndef LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
+#define LLVM_LIBC_MACROS_MATH_FUNCTION_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))
+#define signbit(x) __builtin_signbit(x)
-#endif // LLVM_LIBC_MACROS_GENERIC_MATH_MACROS_H
+#endif // LLVM_LIBC_MACROS_MATH_FUNCTION_MACROS_H
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 7a805fc3a2e66..d4c88b64f5920 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -81,11 +81,11 @@ add_libc_test(
)
add_libc_test(
- generic_math_macros_test
+ math_function_macros_test
SUITE
libc_include_tests
SRCS
- generic-math-macros_test.cpp
+ math-function-macros_test.cpp
DEPENDS
- libc.include.llvm-libc-macros.generic_math_macros
+ libc.include.llvm-libc-macros.math_function_macros
)
diff --git a/libc/test/include/math-function-macros_test.c b/libc/test/include/math-function-macros_test.c
new file mode 100644
index 0000000000000..e278e9a7374e0
--- /dev/null
+++ b/libc/test/include/math-function-macros_test.c
@@ -0,0 +1,61 @@
+//===-- 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
+//
+//===----------------------------------------------------------------------===//
+
+/*
+ * The intent of this test is validate that:
+ * 1. We provide the definition of the various type generic macros of stdbit.h
+ * (the macros are transitively included from stdbit-macros.h by stdbit.h).
+ * 2. It dispatches to the correct underlying function.
+ * Because unit tests build without public packaging, the object files produced
+ * do not contain non-namespaced symbols.
+ */
+
+/*
+ * Declare these BEFORE including stdbit-macros.h so that this test may still be
+ * run even if a given target doesn't yet have these individual entrypoints
+ * enabled.
+ */
+#include "stdbit_stub.h"
+
+#include "include/llvm-libc-macros/stdbit-macros.h"
+
+#include <assert.h>
+
+#define CHECK_FUNCTION(FUNC_NAME, VAL) \
+ do { \
+ assert(FUNC_NAME((unsigned char)0U) == VAL##AU); \
+ assert(FUNC_NAME((unsigned short)0U) == VAL##BU); \
+ assert(FUNC_NAME(0U) == VAL##CU); \
+ assert(FUNC_NAME(0UL) == VAL##DU); \
+ assert(FUNC_NAME(0ULL) == VAL##EU); \
+ } while (0)
+
+int main(void) {
+ CHECK_FUNCTION(stdc_leading_zeros, 0xA);
+ CHECK_FUNCTION(stdc_leading_ones, 0xB);
+ CHECK_FUNCTION(stdc_trailing_zeros, 0xC);
+ CHECK_FUNCTION(stdc_trailing_ones, 0xD);
+ CHECK_FUNCTION(stdc_first_leading_zero, 0xE);
+ CHECK_FUNCTION(stdc_first_leading_one, 0xF);
+ CHECK_FUNCTION(stdc_first_trailing_zero, 0x0);
+ CHECK_FUNCTION(stdc_first_trailing_one, 0x1);
+ CHECK_FUNCTION(stdc_count_zeros, 0x2);
+ CHECK_FUNCTION(stdc_count_ones, 0x3);
+
+ assert(!stdc_has_single_bit((unsigned char)1U));
+ assert(!stdc_has_single_bit((unsigned short)1U));
+ assert(!stdc_has_single_bit(1U));
+ assert(!stdc_has_single_bit(1UL));
+ assert(!stdc_has_single_bit(1ULL));
+
+ CHECK_FUNCTION(stdc_bit_width, 0x4);
+ CHECK_FUNCTION(stdc_bit_floor, 0x5);
+ CHECK_FUNCTION(stdc_bit_ceil, 0x6);
+
+ return 0;
+}
diff --git a/libc/test/include/generic-math-macros_test.cpp b/libc/test/include/math-function-macros_test.cpp
similarity index 90%
rename from libc/test/include/generic-math-macros_test.cpp
rename to libc/test/include/math-function-macros_test.cpp
index 809aceb4ba8b8..ac1b07da16be2 100644
--- a/libc/test/include/generic-math-macros_test.cpp
+++ b/libc/test/include/math-function-macros_test.cpp
@@ -12,7 +12,7 @@
* The intent of this test is validate that the generic math macros operate as
* intended
*/
-#include "include/llvm-libc-macros/generic-math-macros.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
// INF can be defined as a number with zeroed out mantissa and 1s in the
// exponent
@@ -40,7 +40,7 @@ static const float neg_nan = *(float *)&neg_nan_bits;
* (trailing significand field ≠ 0)
*/
-TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsfinite) {
+TEST(LlvmLibcMathFunctionMacros, TypeMathFunctionMacroIsfinite) {
EXPECT_EQ(isfinite(pos_inf), 0);
EXPECT_EQ(isfinite(neg_inf), 0);
EXPECT_EQ(isfinite(pos_nan), 0);
@@ -49,7 +49,7 @@ TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsfinite) {
EXPECT_EQ(isfinite(PI), 1);
}
-TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsinf) {
+TEST(LlvmLibcMathFunctionMacros, TypeMathFunctionMacroIsinf) {
EXPECT_EQ(isinf(PI), 0);
EXPECT_EQ(isinf(CASE_DIV_BY_POS_INF), 0);
EXPECT_EQ(isinf(CASE_DIV_BY_NEG_INF), 0);
@@ -60,7 +60,7 @@ TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsinf) {
EXPECT_EQ(isinf(CASE_DIV_BY_ZERO), 1);
}
-TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsnan) {
+TEST(LlvmLibcMathFunctionMacros, TypeMathFunctionMacroIsnan) {
EXPECT_EQ(isnan(PI), 0);
EXPECT_EQ(isnan(CASE_DIV_BY_ZERO), 0);
EXPECT_EQ(isnan(CASE_DIV_BY_POS_INF), 0);
@@ -72,7 +72,7 @@ TEST(LlvmLibcGenericMath, TypeGenericMacroMathIsnan) {
EXPECT_EQ(isnan(pos_inf / neg_inf), 1);
}
-TEST(LlvmLibcGenericMath, TypeGenericMacroMathSignbit) {
+TEST(LlvmLibcMathFunctionMacros, TypeMathFunctionMacroSignbit) {
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);
>From d9ed1d428ff53dac533973bf6c277f70b9610c23 Mon Sep 17 00:00:00 2001
From: akielaries <akiel at akiel.org>
Date: Sun, 7 Jul 2024 17:22:59 -0700
Subject: [PATCH 12/17] [libc][math] updating top level file with math
function macros header
---
libc/include/CMakeLists.txt | 1 +
1 file changed, 1 insertion(+)
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index f8ef35078a8c4..2cf7206f3a625 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -122,6 +122,7 @@ add_gen_header(
.llvm_libc_common_h
.llvm-libc-macros.float16_macros
.llvm-libc-macros.math_macros
+ .llvm-libc-macros.math_function_macros
.llvm-libc-types.double_t
.llvm-libc-types.float_t
.llvm-libc-types.float128
>From 8e3613b3ee6354d52782860ae87b6adad8ae32e1 Mon Sep 17 00:00:00 2001
From: akielaries <akiel at akiel.org>
Date: Sun, 7 Jul 2024 22:03:41 -0700
Subject: [PATCH 13/17] [libc][math] adding working test cases in a new format
for
---
.../llvm-libc-macros/math-function-macros.h | 2 +-
libc/test/include/CMakeLists.txt | 27 ++++++++++-
libc/test/include/SignbitTest.h | 46 +++++++++++++++++++
libc/test/include/math-function-macros_test.c | 36 ++-------------
.../include/math-function-macros_test.cpp | 12 ++++-
libc/test/include/signbit_test.cpp | 12 +++++
6 files changed, 100 insertions(+), 35 deletions(-)
create mode 100644 libc/test/include/SignbitTest.h
create mode 100644 libc/test/include/signbit_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 8fb1b2324ac99..4487726d22f43 100644
--- a/libc/include/llvm-libc-macros/math-function-macros.h
+++ b/libc/include/llvm-libc-macros/math-function-macros.h
@@ -1,4 +1,4 @@
-//===-- Definition of macros from math.h ----------------------------------===//
+//===-- Definition of math function macros --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index d4c88b64f5920..84ea1981d3024 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -81,11 +81,34 @@ add_libc_test(
)
add_libc_test(
- math_function_macros_test
+ #math_function_macros_test
+ signbit_test
+ NEED_MPFR
SUITE
libc_include_tests
SRCS
- math-function-macros_test.cpp
+ signbit_test.cpp
DEPENDS
libc.include.llvm-libc-macros.math_function_macros
+ #libc.test.UnitTest.LibcFPTestHelpers
+ #libc.src.__support.FPUtil.basic_operations
+ #libc.src.__support.FPUtil.fp_bits
+ LINK_LIBRARIES
+ LibcFPTestHelpers
+
)
+
+#add_libc_test(
+# math_function_macros_c_test
+# C_TEST
+# UNIT_TEST_ONLY
+# SUITE
+# libc_include_tests
+# SRCS
+# math-function-macros_test.c
+# COMPILE_OPTIONS
+# -Wall
+# -Werror
+# DEPENDS
+# libc.include.llvm-libc-macros.math_function_macros
+#)
diff --git a/libc/test/include/SignbitTest.h b/libc/test/include/SignbitTest.h
new file mode 100644
index 0000000000000..00dd374db4b9b
--- /dev/null
+++ b/libc/test/include/SignbitTest.h
@@ -0,0 +1,46 @@
+//===-- Utility class to test math function macros --------------*- 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_SIGNBIT_H
+#define LLVM_LIBC_TEST_INCLUDE_MATH_SIGNBIT_H
+
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+#define PI 3.14159265358979323846
+
+template <typename T>
+class SignbitTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+
+ DECLARE_SPECIAL_CONSTANTS(T)
+
+ public:
+ typedef bool (*SignbitFunc)(T);
+
+ void testSpecialNumbers(SignbitFunc func) {
+ EXPECT_EQ(signbit(zero), 0);
+ EXPECT_EQ(signbit(PI), 0);
+ EXPECT_EQ(signbit(inf), 0);
+ EXPECT_EQ(signbit(aNaN), 0);
+
+ EXPECT_EQ(signbit(neg_zero), 1);
+ EXPECT_EQ(signbit(-PI), 1);
+ EXPECT_EQ(signbit(neg_inf), 1);
+ EXPECT_EQ(signbit(neg_aNaN), 1);
+ }
+};
+
+#define LIST_SIGNBIT_TESTS(T, func) \
+ using LlvmLibcSignbitTest = SignbitTest<T>; \
+ TEST_F(LlvmLibcSignbitTest, SecialNumbers) { testSpecialNumbers(&func); }
+
+
+#endif // LLVM_LIBC_TEST_INCLUDE_MATH_SIGNBIT_H
diff --git a/libc/test/include/math-function-macros_test.c b/libc/test/include/math-function-macros_test.c
index e278e9a7374e0..b913929e7f5c8 100644
--- a/libc/test/include/math-function-macros_test.c
+++ b/libc/test/include/math-function-macros_test.c
@@ -1,4 +1,4 @@
-//===-- Unittests for stdbit ----------------------------------------------===//
+//===-- Unittests for math function macros --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -6,23 +6,7 @@
//
//===----------------------------------------------------------------------===//
-/*
- * The intent of this test is validate that:
- * 1. We provide the definition of the various type generic macros of stdbit.h
- * (the macros are transitively included from stdbit-macros.h by stdbit.h).
- * 2. It dispatches to the correct underlying function.
- * Because unit tests build without public packaging, the object files produced
- * do not contain non-namespaced symbols.
- */
-
-/*
- * Declare these BEFORE including stdbit-macros.h so that this test may still be
- * run even if a given target doesn't yet have these individual entrypoints
- * enabled.
- */
-#include "stdbit_stub.h"
-
-#include "include/llvm-libc-macros/stdbit-macros.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
#include <assert.h>
@@ -36,26 +20,16 @@
} while (0)
int main(void) {
- CHECK_FUNCTION(stdc_leading_zeros, 0xA);
- CHECK_FUNCTION(stdc_leading_ones, 0xB);
- CHECK_FUNCTION(stdc_trailing_zeros, 0xC);
- CHECK_FUNCTION(stdc_trailing_ones, 0xD);
- CHECK_FUNCTION(stdc_first_leading_zero, 0xE);
- CHECK_FUNCTION(stdc_first_leading_one, 0xF);
- CHECK_FUNCTION(stdc_first_trailing_zero, 0x0);
- CHECK_FUNCTION(stdc_first_trailing_one, 0x1);
CHECK_FUNCTION(stdc_count_zeros, 0x2);
- CHECK_FUNCTION(stdc_count_ones, 0x3);
+ //CHECK_FUNCTION(isfinite(pos_inf), 0);
+ /*
assert(!stdc_has_single_bit((unsigned char)1U));
assert(!stdc_has_single_bit((unsigned short)1U));
assert(!stdc_has_single_bit(1U));
assert(!stdc_has_single_bit(1UL));
assert(!stdc_has_single_bit(1ULL));
-
- CHECK_FUNCTION(stdc_bit_width, 0x4);
- CHECK_FUNCTION(stdc_bit_floor, 0x5);
- CHECK_FUNCTION(stdc_bit_ceil, 0x6);
+ */
return 0;
}
diff --git a/libc/test/include/math-function-macros_test.cpp b/libc/test/include/math-function-macros_test.cpp
index ac1b07da16be2..48118d0f2f90a 100644
--- a/libc/test/include/math-function-macros_test.cpp
+++ b/libc/test/include/math-function-macros_test.cpp
@@ -40,6 +40,7 @@ static const float neg_nan = *(float *)&neg_nan_bits;
* (trailing significand field ≠ 0)
*/
+/*
TEST(LlvmLibcMathFunctionMacros, TypeMathFunctionMacroIsfinite) {
EXPECT_EQ(isfinite(pos_inf), 0);
EXPECT_EQ(isfinite(neg_inf), 0);
@@ -61,10 +62,14 @@ TEST(LlvmLibcMathFunctionMacros, TypeMathFunctionMacroIsinf) {
}
TEST(LlvmLibcMathFunctionMacros, TypeMathFunctionMacroIsnan) {
- EXPECT_EQ(isnan(PI), 0);
+ EXPECT_EQ(isnan(static_cast<float>(PI)), 0);
+ EXPECT_EQ(isnan(static_cast<double>(PI)), 0);
+ EXPECT_EQ(isnan(static_cast<long double>(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);
@@ -85,3 +90,8 @@ TEST(LlvmLibcMathFunctionMacros, TypeMathFunctionMacroSignbit) {
EXPECT_EQ(signbit(neg_inf), 1);
EXPECT_EQ(signbit(neg_nan), 1);
}
+*/
+
+LIST_MATH_FUNCTION_MACROS_TEST
+
+
diff --git a/libc/test/include/signbit_test.cpp b/libc/test/include/signbit_test.cpp
new file mode 100644
index 0000000000000..de39fd78ab3ca
--- /dev/null
+++ b/libc/test/include/signbit_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittests for ---------------------------------===//
+//
+// 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 "SignbitTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_SIGNBIT_TESTS(double, signbit)
>From 637bd657e0efba8861aa0cbfabae0b28a844cd5f Mon Sep 17 00:00:00 2001
From: akielaries <akiel at akiel.org>
Date: Sun, 7 Jul 2024 22:15:14 -0700
Subject: [PATCH 14/17] [libc][math] separating tests for float, double, long
double
---
libc/test/include/CMakeLists.txt | 28 +++++++++++++++++++++++-----
libc/test/include/SignbitTest.h | 2 +-
libc/test/include/signbitf_test.cpp | 12 ++++++++++++
libc/test/include/signbitl_test.cpp | 12 ++++++++++++
4 files changed, 48 insertions(+), 6 deletions(-)
create mode 100644 libc/test/include/signbitf_test.cpp
create mode 100644 libc/test/include/signbitl_test.cpp
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 84ea1981d3024..711ca9af53a57 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -81,21 +81,39 @@ add_libc_test(
)
add_libc_test(
- #math_function_macros_test
signbit_test
- NEED_MPFR
SUITE
libc_include_tests
SRCS
signbit_test.cpp
DEPENDS
libc.include.llvm-libc-macros.math_function_macros
- #libc.test.UnitTest.LibcFPTestHelpers
- #libc.src.__support.FPUtil.basic_operations
- #libc.src.__support.FPUtil.fp_bits
LINK_LIBRARIES
LibcFPTestHelpers
+)
+add_libc_test(
+ signbitf_test
+ SUITE
+ libc_include_tests
+ SRCS
+ signbitf_test.cpp
+ DEPENDS
+ libc.include.llvm-libc-macros.math_function_macros
+ LINK_LIBRARIES
+ LibcFPTestHelpers
+)
+
+add_libc_test(
+ signbitl_test
+ SUITE
+ libc_include_tests
+ SRCS
+ signbitl_test.cpp
+ DEPENDS
+ libc.include.llvm-libc-macros.math_function_macros
+ LINK_LIBRARIES
+ LibcFPTestHelpers
)
#add_libc_test(
diff --git a/libc/test/include/SignbitTest.h b/libc/test/include/SignbitTest.h
index 00dd374db4b9b..82eba9ce75fa7 100644
--- a/libc/test/include/SignbitTest.h
+++ b/libc/test/include/SignbitTest.h
@@ -40,7 +40,7 @@ class SignbitTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
#define LIST_SIGNBIT_TESTS(T, func) \
using LlvmLibcSignbitTest = SignbitTest<T>; \
- TEST_F(LlvmLibcSignbitTest, SecialNumbers) { testSpecialNumbers(&func); }
+ TEST_F(LlvmLibcSignbitTest, SpecialNumbers) { testSpecialNumbers(&func); }
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_SIGNBIT_H
diff --git a/libc/test/include/signbitf_test.cpp b/libc/test/include/signbitf_test.cpp
new file mode 100644
index 0000000000000..e33a2417951ff
--- /dev/null
+++ b/libc/test/include/signbitf_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittests for ---------------------------------===//
+//
+// 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 "SignbitTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_SIGNBIT_TESTS(float, signbit)
diff --git a/libc/test/include/signbitl_test.cpp b/libc/test/include/signbitl_test.cpp
new file mode 100644
index 0000000000000..731af8db73724
--- /dev/null
+++ b/libc/test/include/signbitl_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittests for ---------------------------------===//
+//
+// 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 "SignbitTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_SIGNBIT_TESTS(long double, signbit)
>From b5ee920458706908d5569b5f80d32a79f3b59028 Mon Sep 17 00:00:00 2001
From: akielaries <akiel at akiel.org>
Date: Sun, 7 Jul 2024 23:59:55 -0700
Subject: [PATCH 15/17] [libc][math] adding separate tests for float, double,
and long double for macros signbit, isinf, isfinite, and isnan. curious on
replicating this unit test abstraction piece for the C tests and for
converting the PI define into a hex representation
---
libc/test/include/CMakeLists.txt | 109 ++++++++++++++++++
libc/test/include/IsFiniteTest.h | 60 ++++++++++
libc/test/include/IsInfTest.h | 60 ++++++++++
libc/test/include/IsNanTest.h | 60 ++++++++++
libc/test/include/SignbitTest.h | 18 ++-
libc/test/include/isfinite_test.cpp | 12 ++
libc/test/include/isfinitef_test.cpp | 12 ++
libc/test/include/isfinitel_test.cpp | 12 ++
libc/test/include/isinf_test.cpp | 12 ++
libc/test/include/isinff_test.cpp | 12 ++
libc/test/include/isinfl_test.cpp | 12 ++
libc/test/include/isnan_test.cpp | 12 ++
libc/test/include/isnanf_test.cpp | 12 ++
libc/test/include/isnanl_test.cpp | 12 ++
libc/test/include/math-function-macros_test.c | 35 ------
.../include/math-function-macros_test.cpp | 97 ----------------
libc/test/include/signbit_test.cpp | 2 +-
libc/test/include/signbitf_test.cpp | 2 +-
libc/test/include/signbitl_test.cpp | 2 +-
19 files changed, 416 insertions(+), 137 deletions(-)
create mode 100644 libc/test/include/IsFiniteTest.h
create mode 100644 libc/test/include/IsInfTest.h
create mode 100644 libc/test/include/IsNanTest.h
create mode 100644 libc/test/include/isfinite_test.cpp
create mode 100644 libc/test/include/isfinitef_test.cpp
create mode 100644 libc/test/include/isfinitel_test.cpp
create mode 100644 libc/test/include/isinf_test.cpp
create mode 100644 libc/test/include/isinff_test.cpp
create mode 100644 libc/test/include/isinfl_test.cpp
create mode 100644 libc/test/include/isnan_test.cpp
create mode 100644 libc/test/include/isnanf_test.cpp
create mode 100644 libc/test/include/isnanl_test.cpp
delete mode 100644 libc/test/include/math-function-macros_test.c
delete mode 100644 libc/test/include/math-function-macros_test.cpp
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 711ca9af53a57..7688f62210886 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -116,6 +116,115 @@ add_libc_test(
LibcFPTestHelpers
)
+add_libc_test(
+ isnan_test
+ SUITE
+ libc_include_tests
+ SRCS
+ isnan_test.cpp
+ DEPENDS
+ libc.include.llvm-libc-macros.math_function_macros
+ LINK_LIBRARIES
+ LibcFPTestHelpers
+)
+
+add_libc_test(
+ isnanf_test
+ SUITE
+ libc_include_tests
+ SRCS
+ isnanf_test.cpp
+ DEPENDS
+ libc.include.llvm-libc-macros.math_function_macros
+ LINK_LIBRARIES
+ LibcFPTestHelpers
+)
+
+add_libc_test(
+ isnanl_test
+ SUITE
+ libc_include_tests
+ SRCS
+ isnanl_test.cpp
+ DEPENDS
+ libc.include.llvm-libc-macros.math_function_macros
+ LINK_LIBRARIES
+ LibcFPTestHelpers
+)
+
+add_libc_test(
+ isinf_test
+ SUITE
+ libc_include_tests
+ SRCS
+ isinf_test.cpp
+ DEPENDS
+ libc.include.llvm-libc-macros.math_function_macros
+ LINK_LIBRARIES
+ LibcFPTestHelpers
+)
+
+add_libc_test(
+ isinff_test
+ SUITE
+ libc_include_tests
+ SRCS
+ isinff_test.cpp
+ DEPENDS
+ libc.include.llvm-libc-macros.math_function_macros
+ LINK_LIBRARIES
+ LibcFPTestHelpers
+)
+
+add_libc_test(
+ isinfl_test
+ SUITE
+ libc_include_tests
+ SRCS
+ isinfl_test.cpp
+ DEPENDS
+ libc.include.llvm-libc-macros.math_function_macros
+ LINK_LIBRARIES
+ LibcFPTestHelpers
+)
+
+add_libc_test(
+ isfinite_test
+ SUITE
+ libc_include_tests
+ SRCS
+ isfinite_test.cpp
+ DEPENDS
+ libc.include.llvm-libc-macros.math_function_macros
+ LINK_LIBRARIES
+ LibcFPTestHelpers
+)
+
+add_libc_test(
+ isfinitef_test
+ SUITE
+ libc_include_tests
+ SRCS
+ isfinitef_test.cpp
+ DEPENDS
+ libc.include.llvm-libc-macros.math_function_macros
+ LINK_LIBRARIES
+ LibcFPTestHelpers
+)
+
+add_libc_test(
+ isfinitel_test
+ SUITE
+ libc_include_tests
+ SRCS
+ isfinitel_test.cpp
+ DEPENDS
+ libc.include.llvm-libc-macros.math_function_macros
+ LINK_LIBRARIES
+ LibcFPTestHelpers
+)
+
+
#add_libc_test(
# math_function_macros_c_test
# C_TEST
diff --git a/libc/test/include/IsFiniteTest.h b/libc/test/include/IsFiniteTest.h
new file mode 100644
index 0000000000000..80739cf02c825
--- /dev/null
+++ b/libc/test/include/IsFiniteTest.h
@@ -0,0 +1,60 @@
+//===-- Utility class to test the isfinite macro [f|l] ----------*- 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_ISFINITE_H
+#define LLVM_LIBC_TEST_INCLUDE_MATH_ISFINITE_H
+
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+#define PI 3.14159265358979323846
+
+template <typename T>
+class IsFiniteTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+
+ DECLARE_SPECIAL_CONSTANTS(T)
+
+ public:
+ typedef bool (*IsFiniteFunc)(T);
+
+ void testSpecialNumbers(IsFiniteFunc func) {
+ EXPECT_EQ(isfinite(zero), 1);
+ EXPECT_EQ(isfinite(PI), 1);
+ EXPECT_EQ(isfinite(inf), 0);
+ EXPECT_EQ(isfinite(aNaN), 0);
+
+ EXPECT_EQ(isfinite(neg_zero), 1);
+ EXPECT_EQ(isfinite(-PI), 1);
+ EXPECT_EQ(isfinite(neg_inf), 0);
+ EXPECT_EQ(isfinite(neg_aNaN), 0);
+ }
+
+ void testSpecialCases(IsFiniteFunc func) {
+ EXPECT_EQ(isfinite(PI / zero), 0); // division by zero
+ EXPECT_EQ(isfinite(PI / inf), 1); // division by +inf
+ EXPECT_EQ(isfinite(PI / neg_inf), 1); // division by -inf
+ EXPECT_EQ(isfinite(inf / neg_inf), 0); // +inf divided by -inf
+
+ EXPECT_EQ(isfinite(inf * neg_inf), 0); // multiply +inf by -inf
+ EXPECT_EQ(isfinite(inf * zero), 0); // multiply by +inf
+ EXPECT_EQ(isfinite(neg_inf * zero), 0); // multiply by -inf
+
+ EXPECT_EQ(isfinite(inf + neg_inf), 0); // +inf + -inf
+ }
+};
+
+#define LIST_ISFINITE_TESTS(T, func) \
+ using LlvmLibcIsFiniteTest = IsFiniteTest<T>; \
+ TEST_F(LlvmLibcIsFiniteTest, SpecialNumbers) { testSpecialNumbers(&func); } \
+ TEST_F(LlvmLibcIsFiniteTest, SpecialCases) { testSpecialCases(&func); }
+
+
+#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISFINITE_H
diff --git a/libc/test/include/IsInfTest.h b/libc/test/include/IsInfTest.h
new file mode 100644
index 0000000000000..bcbd79b717dd6
--- /dev/null
+++ b/libc/test/include/IsInfTest.h
@@ -0,0 +1,60 @@
+//===-- Utility class to test the isinf macro [f|l] -------------*- 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_ISINF_H
+#define LLVM_LIBC_TEST_INCLUDE_MATH_ISINF_H
+
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+#define PI 3.14159265358979323846
+
+template <typename T>
+class IsInfTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+
+ DECLARE_SPECIAL_CONSTANTS(T)
+
+ public:
+ typedef bool (*IsInfFunc)(T);
+
+ void testSpecialNumbers(IsInfFunc func) {
+ EXPECT_EQ(isinf(zero), 0);
+ EXPECT_EQ(isinf(PI), 0);
+ EXPECT_EQ(isinf(inf), 1);
+ EXPECT_EQ(isinf(aNaN), 0);
+
+ EXPECT_EQ(isinf(neg_zero), 0);
+ EXPECT_EQ(isinf(-PI), 0);
+ EXPECT_EQ(isinf(neg_inf), 1);
+ EXPECT_EQ(isinf(neg_aNaN), 0);
+ }
+
+ void testSpecialCases(IsInfFunc func) {
+ EXPECT_EQ(isinf(PI / zero), 1); // division by zero
+ EXPECT_EQ(isinf(PI / inf), 0); // division by +inf
+ EXPECT_EQ(isinf(PI / neg_inf), 0); // division by -inf
+ EXPECT_EQ(isinf(inf / neg_inf), 0); // +inf divided by -inf
+
+ EXPECT_EQ(isinf(inf * neg_inf), 1); // multiply +inf by -inf
+ EXPECT_EQ(isinf(inf * zero), 0); // multiply by +inf
+ EXPECT_EQ(isinf(neg_inf * zero), 0); // multiply by -inf
+
+ EXPECT_EQ(isinf(inf + neg_inf), 0); // +inf + -inf
+ }
+};
+
+#define LIST_ISINF_TESTS(T, func) \
+ using LlvmLibcIsInfTest = IsInfTest<T>; \
+ TEST_F(LlvmLibcIsInfTest, SpecialNumbers) { testSpecialNumbers(&func); } \
+ TEST_F(LlvmLibcIsInfTest, SpecialCases) { testSpecialCases(&func); }
+
+
+#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISINF_H
diff --git a/libc/test/include/IsNanTest.h b/libc/test/include/IsNanTest.h
new file mode 100644
index 0000000000000..519dabe894093
--- /dev/null
+++ b/libc/test/include/IsNanTest.h
@@ -0,0 +1,60 @@
+//===-- Utility class to test the isnan macro [f|l] -------------*- 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_ISNAN_H
+#define LLVM_LIBC_TEST_INCLUDE_MATH_ISNAN_H
+
+#include "test/UnitTest/FEnvSafeTest.h"
+#include "test/UnitTest/FPMatcher.h"
+#include "test/UnitTest/Test.h"
+
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+#define PI 3.14159265358979323846
+
+template <typename T>
+class IsNanTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+
+ DECLARE_SPECIAL_CONSTANTS(T)
+
+ public:
+ typedef bool (*IsNanFunc)(T);
+
+ void testSpecialNumbers(IsNanFunc func) {
+ EXPECT_EQ(isnan(zero), 0);
+ EXPECT_EQ(isnan(PI), 0);
+ EXPECT_EQ(isnan(inf), 0);
+ EXPECT_EQ(isnan(aNaN), 1);
+
+ EXPECT_EQ(isnan(neg_zero), 0);
+ EXPECT_EQ(isnan(-PI), 0);
+ EXPECT_EQ(isnan(neg_inf), 0);
+ EXPECT_EQ(isnan(neg_aNaN), 1);
+ }
+
+ void testSpecialCases(IsNanFunc func) {
+ EXPECT_EQ(isnan(PI / zero), 0); // division by zero
+ EXPECT_EQ(isnan(PI / inf), 0); // division by +inf
+ EXPECT_EQ(isnan(PI / neg_inf), 0); // division by -inf
+ EXPECT_EQ(isnan(inf / neg_inf), 1); // +inf divided by -inf
+
+ EXPECT_EQ(isnan(inf * neg_inf), 0); // multiply +inf by -inf
+ EXPECT_EQ(isnan(inf * zero), 1); // multiply by +inf
+ EXPECT_EQ(isnan(neg_inf * zero), 1); // multiply by -inf
+
+ EXPECT_EQ(isnan(inf + neg_inf), 1); // +inf + -inf
+ }
+};
+
+#define LIST_ISNAN_TESTS(T, func) \
+ using LlvmLibcIsNanTest = IsNanTest<T>; \
+ TEST_F(LlvmLibcIsNanTest, SpecialNumbers) { testSpecialNumbers(&func); } \
+ TEST_F(LlvmLibcIsNanTest, SpecialCases) { testSpecialCases(&func); }
+
+
+#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISNAN_H
diff --git a/libc/test/include/SignbitTest.h b/libc/test/include/SignbitTest.h
index 82eba9ce75fa7..6dacb6e69c274 100644
--- a/libc/test/include/SignbitTest.h
+++ b/libc/test/include/SignbitTest.h
@@ -1,4 +1,4 @@
-//===-- Utility class to test math function macros --------------*- C++ -*-===//
+//===-- Utility class to test the signbit macro [f|l] -----------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
@@ -36,11 +36,25 @@ class SignbitTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
EXPECT_EQ(signbit(neg_inf), 1);
EXPECT_EQ(signbit(neg_aNaN), 1);
}
+
+ void testSpecialCases(SignbitFunc func) {
+ EXPECT_EQ(signbit(PI / zero), 0); // division by zero
+ EXPECT_EQ(signbit(PI / inf), 0); // division by +inf
+ EXPECT_EQ(signbit(PI / neg_inf), 1); // division by -inf
+ EXPECT_EQ(signbit(inf / neg_inf), 1); // +inf divided by -inf
+
+ EXPECT_EQ(signbit(inf * neg_inf), 1); // multiply +inf by -inf
+ EXPECT_EQ(signbit(inf * zero), 1); // multiply by +inf
+ EXPECT_EQ(signbit(neg_inf * zero), 1); // multiply by -inf
+
+ EXPECT_EQ(signbit(inf + neg_inf), 1); // +inf + -inf
+ }
};
#define LIST_SIGNBIT_TESTS(T, func) \
using LlvmLibcSignbitTest = SignbitTest<T>; \
- TEST_F(LlvmLibcSignbitTest, SpecialNumbers) { testSpecialNumbers(&func); }
+ TEST_F(LlvmLibcSignbitTest, SpecialNumbers) { testSpecialNumbers(&func); } \
+ TEST_F(LlvmLibcSignbitTest, SpecialCases) { testSpecialCases(&func); }
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_SIGNBIT_H
diff --git a/libc/test/include/isfinite_test.cpp b/libc/test/include/isfinite_test.cpp
new file mode 100644
index 0000000000000..79ac44206e634
--- /dev/null
+++ b/libc/test/include/isfinite_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittest for isfinite[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 "IsFiniteTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_ISFINITE_TESTS(double, isfinite)
diff --git a/libc/test/include/isfinitef_test.cpp b/libc/test/include/isfinitef_test.cpp
new file mode 100644
index 0000000000000..b1f66cd040e16
--- /dev/null
+++ b/libc/test/include/isfinitef_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittest for isfinite[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 "IsFiniteTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_ISFINITE_TESTS(float, isfinite)
diff --git a/libc/test/include/isfinitel_test.cpp b/libc/test/include/isfinitel_test.cpp
new file mode 100644
index 0000000000000..9087cd69ba014
--- /dev/null
+++ b/libc/test/include/isfinitel_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittest for isfinite[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 "IsFiniteTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_ISFINITE_TESTS(long double, isfinite)
diff --git a/libc/test/include/isinf_test.cpp b/libc/test/include/isinf_test.cpp
new file mode 100644
index 0000000000000..ecf19d078489a
--- /dev/null
+++ b/libc/test/include/isinf_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittest for isinf[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 "IsInfTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_ISINF_TESTS(double, isinf)
diff --git a/libc/test/include/isinff_test.cpp b/libc/test/include/isinff_test.cpp
new file mode 100644
index 0000000000000..a2170c766bf6e
--- /dev/null
+++ b/libc/test/include/isinff_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittest for isinf[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 "IsInfTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_ISINF_TESTS(float, isinf)
diff --git a/libc/test/include/isinfl_test.cpp b/libc/test/include/isinfl_test.cpp
new file mode 100644
index 0000000000000..e4fb91d9608fb
--- /dev/null
+++ b/libc/test/include/isinfl_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittest for isinf[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 "IsInfTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_ISINF_TESTS(long double, isinf)
diff --git a/libc/test/include/isnan_test.cpp b/libc/test/include/isnan_test.cpp
new file mode 100644
index 0000000000000..07dfab740724b
--- /dev/null
+++ b/libc/test/include/isnan_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittest for isnan[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 "IsNanTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_ISNAN_TESTS(double, isnan)
diff --git a/libc/test/include/isnanf_test.cpp b/libc/test/include/isnanf_test.cpp
new file mode 100644
index 0000000000000..e78a8e45e0233
--- /dev/null
+++ b/libc/test/include/isnanf_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittest for isnan[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 "IsNanTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_ISNAN_TESTS(float, isnan)
diff --git a/libc/test/include/isnanl_test.cpp b/libc/test/include/isnanl_test.cpp
new file mode 100644
index 0000000000000..84759a3ab28bc
--- /dev/null
+++ b/libc/test/include/isnanl_test.cpp
@@ -0,0 +1,12 @@
+//===-- Unittest for isnan[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 "IsNanTest.h"
+#include "include/llvm-libc-macros/math-function-macros.h"
+
+LIST_ISNAN_TESTS(long double, isnan)
diff --git a/libc/test/include/math-function-macros_test.c b/libc/test/include/math-function-macros_test.c
deleted file mode 100644
index b913929e7f5c8..0000000000000
--- a/libc/test/include/math-function-macros_test.c
+++ /dev/null
@@ -1,35 +0,0 @@
-//===-- Unittests for math function macros --------------------------------===//
-//
-// 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>
-
-#define CHECK_FUNCTION(FUNC_NAME, VAL) \
- do { \
- assert(FUNC_NAME((unsigned char)0U) == VAL##AU); \
- assert(FUNC_NAME((unsigned short)0U) == VAL##BU); \
- assert(FUNC_NAME(0U) == VAL##CU); \
- assert(FUNC_NAME(0UL) == VAL##DU); \
- assert(FUNC_NAME(0ULL) == VAL##EU); \
- } while (0)
-
-int main(void) {
- CHECK_FUNCTION(stdc_count_zeros, 0x2);
- //CHECK_FUNCTION(isfinite(pos_inf), 0);
-
- /*
- assert(!stdc_has_single_bit((unsigned char)1U));
- assert(!stdc_has_single_bit((unsigned short)1U));
- assert(!stdc_has_single_bit(1U));
- assert(!stdc_has_single_bit(1UL));
- assert(!stdc_has_single_bit(1ULL));
- */
-
- return 0;
-}
diff --git a/libc/test/include/math-function-macros_test.cpp b/libc/test/include/math-function-macros_test.cpp
deleted file mode 100644
index 48118d0f2f90a..0000000000000
--- a/libc/test/include/math-function-macros_test.cpp
+++ /dev/null
@@ -1,97 +0,0 @@
-//===-- Unittests for generic-math-macros ---------------------------------===//
-//
-// 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/math-function-macros.h"
-
-// INF can be defined as a number with zeroed out mantissa and 1s in the
-// exponent
-static uint32_t pos_inf_bits = 0x7F800000;
-static uint32_t neg_inf_bits = 0xFF800000;
-static const float pos_inf = *(float *)&pos_inf_bits;
-static const float neg_inf = *(float *)&neg_inf_bits;
-
-// NaN can be defined as a number with all 1s in the exponent
-static uint32_t pos_nan_bits = 0x7F800001;
-static uint32_t neg_nan_bits = 0xFF800001;
-static const float pos_nan = *(float *)&pos_nan_bits;
-static const float neg_nan = *(float *)&neg_nan_bits;
-
-#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(LlvmLibcMathFunctionMacros, TypeMathFunctionMacroIsfinite) {
- 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(LlvmLibcMathFunctionMacros, TypeMathFunctionMacroIsinf) {
- 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(LlvmLibcMathFunctionMacros, TypeMathFunctionMacroIsnan) {
- EXPECT_EQ(isnan(static_cast<float>(PI)), 0);
- EXPECT_EQ(isnan(static_cast<double>(PI)), 0);
- EXPECT_EQ(isnan(static_cast<long double>(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(LlvmLibcMathFunctionMacros, TypeMathFunctionMacroSignbit) {
- 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);
-}
-*/
-
-LIST_MATH_FUNCTION_MACROS_TEST
-
-
diff --git a/libc/test/include/signbit_test.cpp b/libc/test/include/signbit_test.cpp
index de39fd78ab3ca..d97ab0b1e0a89 100644
--- a/libc/test/include/signbit_test.cpp
+++ b/libc/test/include/signbit_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for ---------------------------------===//
+//===-- Unittest for signbit [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.
diff --git a/libc/test/include/signbitf_test.cpp b/libc/test/include/signbitf_test.cpp
index e33a2417951ff..3a4bf933ec81e 100644
--- a/libc/test/include/signbitf_test.cpp
+++ b/libc/test/include/signbitf_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for ---------------------------------===//
+//===-- Unittests for signbit [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.
diff --git a/libc/test/include/signbitl_test.cpp b/libc/test/include/signbitl_test.cpp
index 731af8db73724..5859840b4562d 100644
--- a/libc/test/include/signbitl_test.cpp
+++ b/libc/test/include/signbitl_test.cpp
@@ -1,4 +1,4 @@
-//===-- Unittests for ---------------------------------===//
+//===-- Unittests for signbit[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.
>From 67164db0c3cc554b3d7a26995fa6d85e18d17903 Mon Sep 17 00:00:00 2001
From: akielaries <akiel at akiel.org>
Date: Mon, 8 Jul 2024 00:02:21 -0700
Subject: [PATCH 16/17] [libc][math] formatting changes
---
libc/test/include/IsFiniteTest.h | 59 ++++++++++++++---------------
libc/test/include/IsInfTest.h | 59 ++++++++++++++---------------
libc/test/include/IsNanTest.h | 59 ++++++++++++++---------------
libc/test/include/SignbitTest.h | 65 ++++++++++++++++----------------
4 files changed, 119 insertions(+), 123 deletions(-)
diff --git a/libc/test/include/IsFiniteTest.h b/libc/test/include/IsFiniteTest.h
index 80739cf02c825..a608ded062b9f 100644
--- a/libc/test/include/IsFiniteTest.h
+++ b/libc/test/include/IsFiniteTest.h
@@ -20,35 +20,35 @@
template <typename T>
class IsFiniteTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
- DECLARE_SPECIAL_CONSTANTS(T)
-
- public:
- typedef bool (*IsFiniteFunc)(T);
-
- void testSpecialNumbers(IsFiniteFunc func) {
- EXPECT_EQ(isfinite(zero), 1);
- EXPECT_EQ(isfinite(PI), 1);
- EXPECT_EQ(isfinite(inf), 0);
- EXPECT_EQ(isfinite(aNaN), 0);
-
- EXPECT_EQ(isfinite(neg_zero), 1);
- EXPECT_EQ(isfinite(-PI), 1);
- EXPECT_EQ(isfinite(neg_inf), 0);
- EXPECT_EQ(isfinite(neg_aNaN), 0);
- }
-
- void testSpecialCases(IsFiniteFunc func) {
- EXPECT_EQ(isfinite(PI / zero), 0); // division by zero
- EXPECT_EQ(isfinite(PI / inf), 1); // division by +inf
- EXPECT_EQ(isfinite(PI / neg_inf), 1); // division by -inf
- EXPECT_EQ(isfinite(inf / neg_inf), 0); // +inf divided by -inf
-
- EXPECT_EQ(isfinite(inf * neg_inf), 0); // multiply +inf by -inf
- EXPECT_EQ(isfinite(inf * zero), 0); // multiply by +inf
- EXPECT_EQ(isfinite(neg_inf * zero), 0); // multiply by -inf
-
- EXPECT_EQ(isfinite(inf + neg_inf), 0); // +inf + -inf
- }
+ DECLARE_SPECIAL_CONSTANTS(T)
+
+public:
+ typedef bool (*IsFiniteFunc)(T);
+
+ void testSpecialNumbers(IsFiniteFunc func) {
+ EXPECT_EQ(isfinite(zero), 1);
+ EXPECT_EQ(isfinite(PI), 1);
+ EXPECT_EQ(isfinite(inf), 0);
+ EXPECT_EQ(isfinite(aNaN), 0);
+
+ EXPECT_EQ(isfinite(neg_zero), 1);
+ EXPECT_EQ(isfinite(-PI), 1);
+ EXPECT_EQ(isfinite(neg_inf), 0);
+ EXPECT_EQ(isfinite(neg_aNaN), 0);
+ }
+
+ void testSpecialCases(IsFiniteFunc func) {
+ EXPECT_EQ(isfinite(PI / zero), 0); // division by zero
+ EXPECT_EQ(isfinite(PI / inf), 1); // division by +inf
+ EXPECT_EQ(isfinite(PI / neg_inf), 1); // division by -inf
+ EXPECT_EQ(isfinite(inf / neg_inf), 0); // +inf divided by -inf
+
+ EXPECT_EQ(isfinite(inf * neg_inf), 0); // multiply +inf by -inf
+ EXPECT_EQ(isfinite(inf * zero), 0); // multiply by +inf
+ EXPECT_EQ(isfinite(neg_inf * zero), 0); // multiply by -inf
+
+ EXPECT_EQ(isfinite(inf + neg_inf), 0); // +inf + -inf
+ }
};
#define LIST_ISFINITE_TESTS(T, func) \
@@ -56,5 +56,4 @@ class IsFiniteTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
TEST_F(LlvmLibcIsFiniteTest, SpecialNumbers) { testSpecialNumbers(&func); } \
TEST_F(LlvmLibcIsFiniteTest, SpecialCases) { testSpecialCases(&func); }
-
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISFINITE_H
diff --git a/libc/test/include/IsInfTest.h b/libc/test/include/IsInfTest.h
index bcbd79b717dd6..91f5e721e4c63 100644
--- a/libc/test/include/IsInfTest.h
+++ b/libc/test/include/IsInfTest.h
@@ -20,35 +20,35 @@
template <typename T>
class IsInfTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
- DECLARE_SPECIAL_CONSTANTS(T)
-
- public:
- typedef bool (*IsInfFunc)(T);
-
- void testSpecialNumbers(IsInfFunc func) {
- EXPECT_EQ(isinf(zero), 0);
- EXPECT_EQ(isinf(PI), 0);
- EXPECT_EQ(isinf(inf), 1);
- EXPECT_EQ(isinf(aNaN), 0);
-
- EXPECT_EQ(isinf(neg_zero), 0);
- EXPECT_EQ(isinf(-PI), 0);
- EXPECT_EQ(isinf(neg_inf), 1);
- EXPECT_EQ(isinf(neg_aNaN), 0);
- }
-
- void testSpecialCases(IsInfFunc func) {
- EXPECT_EQ(isinf(PI / zero), 1); // division by zero
- EXPECT_EQ(isinf(PI / inf), 0); // division by +inf
- EXPECT_EQ(isinf(PI / neg_inf), 0); // division by -inf
- EXPECT_EQ(isinf(inf / neg_inf), 0); // +inf divided by -inf
-
- EXPECT_EQ(isinf(inf * neg_inf), 1); // multiply +inf by -inf
- EXPECT_EQ(isinf(inf * zero), 0); // multiply by +inf
- EXPECT_EQ(isinf(neg_inf * zero), 0); // multiply by -inf
-
- EXPECT_EQ(isinf(inf + neg_inf), 0); // +inf + -inf
- }
+ DECLARE_SPECIAL_CONSTANTS(T)
+
+public:
+ typedef bool (*IsInfFunc)(T);
+
+ void testSpecialNumbers(IsInfFunc func) {
+ EXPECT_EQ(isinf(zero), 0);
+ EXPECT_EQ(isinf(PI), 0);
+ EXPECT_EQ(isinf(inf), 1);
+ EXPECT_EQ(isinf(aNaN), 0);
+
+ EXPECT_EQ(isinf(neg_zero), 0);
+ EXPECT_EQ(isinf(-PI), 0);
+ EXPECT_EQ(isinf(neg_inf), 1);
+ EXPECT_EQ(isinf(neg_aNaN), 0);
+ }
+
+ void testSpecialCases(IsInfFunc func) {
+ EXPECT_EQ(isinf(PI / zero), 1); // division by zero
+ EXPECT_EQ(isinf(PI / inf), 0); // division by +inf
+ EXPECT_EQ(isinf(PI / neg_inf), 0); // division by -inf
+ EXPECT_EQ(isinf(inf / neg_inf), 0); // +inf divided by -inf
+
+ EXPECT_EQ(isinf(inf * neg_inf), 1); // multiply +inf by -inf
+ EXPECT_EQ(isinf(inf * zero), 0); // multiply by +inf
+ EXPECT_EQ(isinf(neg_inf * zero), 0); // multiply by -inf
+
+ EXPECT_EQ(isinf(inf + neg_inf), 0); // +inf + -inf
+ }
};
#define LIST_ISINF_TESTS(T, func) \
@@ -56,5 +56,4 @@ class IsInfTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
TEST_F(LlvmLibcIsInfTest, SpecialNumbers) { testSpecialNumbers(&func); } \
TEST_F(LlvmLibcIsInfTest, SpecialCases) { testSpecialCases(&func); }
-
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISINF_H
diff --git a/libc/test/include/IsNanTest.h b/libc/test/include/IsNanTest.h
index 519dabe894093..16eccc46760eb 100644
--- a/libc/test/include/IsNanTest.h
+++ b/libc/test/include/IsNanTest.h
@@ -20,35 +20,35 @@
template <typename T>
class IsNanTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
- DECLARE_SPECIAL_CONSTANTS(T)
-
- public:
- typedef bool (*IsNanFunc)(T);
-
- void testSpecialNumbers(IsNanFunc func) {
- EXPECT_EQ(isnan(zero), 0);
- EXPECT_EQ(isnan(PI), 0);
- EXPECT_EQ(isnan(inf), 0);
- EXPECT_EQ(isnan(aNaN), 1);
-
- EXPECT_EQ(isnan(neg_zero), 0);
- EXPECT_EQ(isnan(-PI), 0);
- EXPECT_EQ(isnan(neg_inf), 0);
- EXPECT_EQ(isnan(neg_aNaN), 1);
- }
-
- void testSpecialCases(IsNanFunc func) {
- EXPECT_EQ(isnan(PI / zero), 0); // division by zero
- EXPECT_EQ(isnan(PI / inf), 0); // division by +inf
- EXPECT_EQ(isnan(PI / neg_inf), 0); // division by -inf
- EXPECT_EQ(isnan(inf / neg_inf), 1); // +inf divided by -inf
-
- EXPECT_EQ(isnan(inf * neg_inf), 0); // multiply +inf by -inf
- EXPECT_EQ(isnan(inf * zero), 1); // multiply by +inf
- EXPECT_EQ(isnan(neg_inf * zero), 1); // multiply by -inf
-
- EXPECT_EQ(isnan(inf + neg_inf), 1); // +inf + -inf
- }
+ DECLARE_SPECIAL_CONSTANTS(T)
+
+public:
+ typedef bool (*IsNanFunc)(T);
+
+ void testSpecialNumbers(IsNanFunc func) {
+ EXPECT_EQ(isnan(zero), 0);
+ EXPECT_EQ(isnan(PI), 0);
+ EXPECT_EQ(isnan(inf), 0);
+ EXPECT_EQ(isnan(aNaN), 1);
+
+ EXPECT_EQ(isnan(neg_zero), 0);
+ EXPECT_EQ(isnan(-PI), 0);
+ EXPECT_EQ(isnan(neg_inf), 0);
+ EXPECT_EQ(isnan(neg_aNaN), 1);
+ }
+
+ void testSpecialCases(IsNanFunc func) {
+ EXPECT_EQ(isnan(PI / zero), 0); // division by zero
+ EXPECT_EQ(isnan(PI / inf), 0); // division by +inf
+ EXPECT_EQ(isnan(PI / neg_inf), 0); // division by -inf
+ EXPECT_EQ(isnan(inf / neg_inf), 1); // +inf divided by -inf
+
+ EXPECT_EQ(isnan(inf * neg_inf), 0); // multiply +inf by -inf
+ EXPECT_EQ(isnan(inf * zero), 1); // multiply by +inf
+ EXPECT_EQ(isnan(neg_inf * zero), 1); // multiply by -inf
+
+ EXPECT_EQ(isnan(inf + neg_inf), 1); // +inf + -inf
+ }
};
#define LIST_ISNAN_TESTS(T, func) \
@@ -56,5 +56,4 @@ class IsNanTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
TEST_F(LlvmLibcIsNanTest, SpecialNumbers) { testSpecialNumbers(&func); } \
TEST_F(LlvmLibcIsNanTest, SpecialCases) { testSpecialCases(&func); }
-
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISNAN_H
diff --git a/libc/test/include/SignbitTest.h b/libc/test/include/SignbitTest.h
index 6dacb6e69c274..fe8e13caf7a17 100644
--- a/libc/test/include/SignbitTest.h
+++ b/libc/test/include/SignbitTest.h
@@ -20,41 +20,40 @@
template <typename T>
class SignbitTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
- DECLARE_SPECIAL_CONSTANTS(T)
-
- public:
- typedef bool (*SignbitFunc)(T);
-
- void testSpecialNumbers(SignbitFunc func) {
- EXPECT_EQ(signbit(zero), 0);
- EXPECT_EQ(signbit(PI), 0);
- EXPECT_EQ(signbit(inf), 0);
- EXPECT_EQ(signbit(aNaN), 0);
-
- EXPECT_EQ(signbit(neg_zero), 1);
- EXPECT_EQ(signbit(-PI), 1);
- EXPECT_EQ(signbit(neg_inf), 1);
- EXPECT_EQ(signbit(neg_aNaN), 1);
- }
-
- void testSpecialCases(SignbitFunc func) {
- EXPECT_EQ(signbit(PI / zero), 0); // division by zero
- EXPECT_EQ(signbit(PI / inf), 0); // division by +inf
- EXPECT_EQ(signbit(PI / neg_inf), 1); // division by -inf
- EXPECT_EQ(signbit(inf / neg_inf), 1); // +inf divided by -inf
-
- EXPECT_EQ(signbit(inf * neg_inf), 1); // multiply +inf by -inf
- EXPECT_EQ(signbit(inf * zero), 1); // multiply by +inf
- EXPECT_EQ(signbit(neg_inf * zero), 1); // multiply by -inf
-
- EXPECT_EQ(signbit(inf + neg_inf), 1); // +inf + -inf
- }
+ DECLARE_SPECIAL_CONSTANTS(T)
+
+public:
+ typedef bool (*SignbitFunc)(T);
+
+ void testSpecialNumbers(SignbitFunc func) {
+ EXPECT_EQ(signbit(zero), 0);
+ EXPECT_EQ(signbit(PI), 0);
+ EXPECT_EQ(signbit(inf), 0);
+ EXPECT_EQ(signbit(aNaN), 0);
+
+ EXPECT_EQ(signbit(neg_zero), 1);
+ EXPECT_EQ(signbit(-PI), 1);
+ EXPECT_EQ(signbit(neg_inf), 1);
+ EXPECT_EQ(signbit(neg_aNaN), 1);
+ }
+
+ void testSpecialCases(SignbitFunc func) {
+ EXPECT_EQ(signbit(PI / zero), 0); // division by zero
+ EXPECT_EQ(signbit(PI / inf), 0); // division by +inf
+ EXPECT_EQ(signbit(PI / neg_inf), 1); // division by -inf
+ EXPECT_EQ(signbit(inf / neg_inf), 1); // +inf divided by -inf
+
+ EXPECT_EQ(signbit(inf * neg_inf), 1); // multiply +inf by -inf
+ EXPECT_EQ(signbit(inf * zero), 1); // multiply by +inf
+ EXPECT_EQ(signbit(neg_inf * zero), 1); // multiply by -inf
+
+ EXPECT_EQ(signbit(inf + neg_inf), 1); // +inf + -inf
+ }
};
#define LIST_SIGNBIT_TESTS(T, func) \
- using LlvmLibcSignbitTest = SignbitTest<T>; \
- TEST_F(LlvmLibcSignbitTest, SpecialNumbers) { testSpecialNumbers(&func); } \
- TEST_F(LlvmLibcSignbitTest, SpecialCases) { testSpecialCases(&func); }
-
+ using LlvmLibcSignbitTest = SignbitTest<T>; \
+ TEST_F(LlvmLibcSignbitTest, SpecialNumbers) { testSpecialNumbers(&func); } \
+ TEST_F(LlvmLibcSignbitTest, SpecialCases) { testSpecialCases(&func); }
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_SIGNBIT_H
>From 31d2eef80ace095d10ccbb612cc214dcbbee7598 Mon Sep 17 00:00:00 2001
From: akielaries <akiel at akiel.org>
Date: Fri, 12 Jul 2024 21:10:16 -0700
Subject: [PATCH 17/17] [libc][math] restructuring and simplifying math macro
unit tests
---
libc/test/include/IsFiniteTest.h | 40 ++++++++----------------------
libc/test/include/IsInfTest.h | 40 ++++++++----------------------
libc/test/include/IsNanTest.h | 42 +++++++++-----------------------
libc/test/include/SignbitTest.h | 40 +++++++-----------------------
4 files changed, 40 insertions(+), 122 deletions(-)
diff --git a/libc/test/include/IsFiniteTest.h b/libc/test/include/IsFiniteTest.h
index a608ded062b9f..3d2fc0daae4ca 100644
--- a/libc/test/include/IsFiniteTest.h
+++ b/libc/test/include/IsFiniteTest.h
@@ -9,51 +9,31 @@
#ifndef LLVM_LIBC_TEST_INCLUDE_MATH_ISFINITE_H
#define LLVM_LIBC_TEST_INCLUDE_MATH_ISFINITE_H
-#include "test/UnitTest/FEnvSafeTest.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
#include "include/llvm-libc-macros/math-function-macros.h"
-#define PI 3.14159265358979323846
-
template <typename T>
-class IsFiniteTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
-
+class IsFiniteTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)
public:
- typedef bool (*IsFiniteFunc)(T);
+ typedef int (*IsFiniteFunc)(T);
void testSpecialNumbers(IsFiniteFunc func) {
- EXPECT_EQ(isfinite(zero), 1);
- EXPECT_EQ(isfinite(PI), 1);
- EXPECT_EQ(isfinite(inf), 0);
- EXPECT_EQ(isfinite(aNaN), 0);
-
- EXPECT_EQ(isfinite(neg_zero), 1);
- EXPECT_EQ(isfinite(-PI), 1);
- EXPECT_EQ(isfinite(neg_inf), 0);
- EXPECT_EQ(isfinite(neg_aNaN), 0);
- }
-
- void testSpecialCases(IsFiniteFunc func) {
- EXPECT_EQ(isfinite(PI / zero), 0); // division by zero
- EXPECT_EQ(isfinite(PI / inf), 1); // division by +inf
- EXPECT_EQ(isfinite(PI / neg_inf), 1); // division by -inf
- EXPECT_EQ(isfinite(inf / neg_inf), 0); // +inf divided by -inf
-
- EXPECT_EQ(isfinite(inf * neg_inf), 0); // multiply +inf by -inf
- EXPECT_EQ(isfinite(inf * zero), 0); // multiply by +inf
- EXPECT_EQ(isfinite(neg_inf * zero), 0); // multiply by -inf
-
- EXPECT_EQ(isfinite(inf + neg_inf), 0); // +inf + -inf
+ EXPECT_EQ(func(inf), 0);
+ EXPECT_EQ(func(neg_inf), 0);
+ EXPECT_EQ(func(zero), 1);
+ EXPECT_EQ(func(neg_zero), 1);
}
};
#define LIST_ISFINITE_TESTS(T, func) \
using LlvmLibcIsFiniteTest = IsFiniteTest<T>; \
- TEST_F(LlvmLibcIsFiniteTest, SpecialNumbers) { testSpecialNumbers(&func); } \
- TEST_F(LlvmLibcIsFiniteTest, SpecialCases) { testSpecialCases(&func); }
+ TEST_F(LlvmLibcIsFiniteTest, SpecialNumbers) { \
+ auto isfinite_func = [](T x) { return func(x); }; \
+ testSpecialNumbers(isfinite_func); \
+ }
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISFINITE_H
diff --git a/libc/test/include/IsInfTest.h b/libc/test/include/IsInfTest.h
index 91f5e721e4c63..7993a49c15fef 100644
--- a/libc/test/include/IsInfTest.h
+++ b/libc/test/include/IsInfTest.h
@@ -9,51 +9,31 @@
#ifndef LLVM_LIBC_TEST_INCLUDE_MATH_ISINF_H
#define LLVM_LIBC_TEST_INCLUDE_MATH_ISINF_H
-#include "test/UnitTest/FEnvSafeTest.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
#include "include/llvm-libc-macros/math-function-macros.h"
-#define PI 3.14159265358979323846
-
-template <typename T>
-class IsInfTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+template <typename T> class IsInfTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)
public:
- typedef bool (*IsInfFunc)(T);
+ typedef int (*IsInfFunc)(T);
void testSpecialNumbers(IsInfFunc func) {
- EXPECT_EQ(isinf(zero), 0);
- EXPECT_EQ(isinf(PI), 0);
- EXPECT_EQ(isinf(inf), 1);
- EXPECT_EQ(isinf(aNaN), 0);
-
- EXPECT_EQ(isinf(neg_zero), 0);
- EXPECT_EQ(isinf(-PI), 0);
- EXPECT_EQ(isinf(neg_inf), 1);
- EXPECT_EQ(isinf(neg_aNaN), 0);
- }
-
- void testSpecialCases(IsInfFunc func) {
- EXPECT_EQ(isinf(PI / zero), 1); // division by zero
- EXPECT_EQ(isinf(PI / inf), 0); // division by +inf
- EXPECT_EQ(isinf(PI / neg_inf), 0); // division by -inf
- EXPECT_EQ(isinf(inf / neg_inf), 0); // +inf divided by -inf
-
- EXPECT_EQ(isinf(inf * neg_inf), 1); // multiply +inf by -inf
- EXPECT_EQ(isinf(inf * zero), 0); // multiply by +inf
- EXPECT_EQ(isinf(neg_inf * zero), 0); // multiply by -inf
-
- EXPECT_EQ(isinf(inf + neg_inf), 0); // +inf + -inf
+ EXPECT_EQ(func(zero), 0);
+ EXPECT_EQ(func(neg_zero), 0);
+ EXPECT_EQ(func(inf), 1);
+ EXPECT_EQ(func(neg_inf), 1);
}
};
#define LIST_ISINF_TESTS(T, func) \
using LlvmLibcIsInfTest = IsInfTest<T>; \
- TEST_F(LlvmLibcIsInfTest, SpecialNumbers) { testSpecialNumbers(&func); } \
- TEST_F(LlvmLibcIsInfTest, SpecialCases) { testSpecialCases(&func); }
+ TEST_F(LlvmLibcIsInfTest, SpecialNumbers) { \
+ auto isinf_func = [](T x) { return func(x); }; \
+ testSpecialNumbers(isinf_func); \
+ }
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISINF_H
diff --git a/libc/test/include/IsNanTest.h b/libc/test/include/IsNanTest.h
index 16eccc46760eb..362699a18dfc1 100644
--- a/libc/test/include/IsNanTest.h
+++ b/libc/test/include/IsNanTest.h
@@ -1,7 +1,7 @@
//===-- Utility class to test the isnan macro [f|l] -------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
+// See https://llvm.org/LICENSE.txt for license nanormation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
@@ -9,51 +9,31 @@
#ifndef LLVM_LIBC_TEST_INCLUDE_MATH_ISNAN_H
#define LLVM_LIBC_TEST_INCLUDE_MATH_ISNAN_H
-#include "test/UnitTest/FEnvSafeTest.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
#include "include/llvm-libc-macros/math-function-macros.h"
-#define PI 3.14159265358979323846
-
-template <typename T>
-class IsNanTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+template <typename T> class IsNanTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)
public:
- typedef bool (*IsNanFunc)(T);
+ typedef int (*IsNanFunc)(T);
void testSpecialNumbers(IsNanFunc func) {
- EXPECT_EQ(isnan(zero), 0);
- EXPECT_EQ(isnan(PI), 0);
- EXPECT_EQ(isnan(inf), 0);
- EXPECT_EQ(isnan(aNaN), 1);
-
- EXPECT_EQ(isnan(neg_zero), 0);
- EXPECT_EQ(isnan(-PI), 0);
- EXPECT_EQ(isnan(neg_inf), 0);
- EXPECT_EQ(isnan(neg_aNaN), 1);
- }
-
- void testSpecialCases(IsNanFunc func) {
- EXPECT_EQ(isnan(PI / zero), 0); // division by zero
- EXPECT_EQ(isnan(PI / inf), 0); // division by +inf
- EXPECT_EQ(isnan(PI / neg_inf), 0); // division by -inf
- EXPECT_EQ(isnan(inf / neg_inf), 1); // +inf divided by -inf
-
- EXPECT_EQ(isnan(inf * neg_inf), 0); // multiply +inf by -inf
- EXPECT_EQ(isnan(inf * zero), 1); // multiply by +inf
- EXPECT_EQ(isnan(neg_inf * zero), 1); // multiply by -inf
-
- EXPECT_EQ(isnan(inf + neg_inf), 1); // +inf + -inf
+ EXPECT_EQ(func(zero), 0);
+ EXPECT_EQ(func(neg_zero), 0);
+ EXPECT_EQ(func(aNaN), 1);
+ EXPECT_EQ(func(sNaN), 1);
}
};
#define LIST_ISNAN_TESTS(T, func) \
using LlvmLibcIsNanTest = IsNanTest<T>; \
- TEST_F(LlvmLibcIsNanTest, SpecialNumbers) { testSpecialNumbers(&func); } \
- TEST_F(LlvmLibcIsNanTest, SpecialCases) { testSpecialCases(&func); }
+ TEST_F(LlvmLibcIsNanTest, SpecialNumbers) { \
+ auto isnan_func = [](T x) { return func(x); }; \
+ testSpecialNumbers(isnan_func); \
+ }
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_ISNAN_H
diff --git a/libc/test/include/SignbitTest.h b/libc/test/include/SignbitTest.h
index fe8e13caf7a17..85d499823088c 100644
--- a/libc/test/include/SignbitTest.h
+++ b/libc/test/include/SignbitTest.h
@@ -9,51 +9,29 @@
#ifndef LLVM_LIBC_TEST_INCLUDE_MATH_SIGNBIT_H
#define LLVM_LIBC_TEST_INCLUDE_MATH_SIGNBIT_H
-#include "test/UnitTest/FEnvSafeTest.h"
#include "test/UnitTest/FPMatcher.h"
#include "test/UnitTest/Test.h"
#include "include/llvm-libc-macros/math-function-macros.h"
-#define PI 3.14159265358979323846
-
-template <typename T>
-class SignbitTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
+template <typename T> class SignbitTest : public LIBC_NAMESPACE::testing::Test {
DECLARE_SPECIAL_CONSTANTS(T)
public:
- typedef bool (*SignbitFunc)(T);
+ typedef int (*SignbitFunc)(T);
void testSpecialNumbers(SignbitFunc func) {
- EXPECT_EQ(signbit(zero), 0);
- EXPECT_EQ(signbit(PI), 0);
- EXPECT_EQ(signbit(inf), 0);
- EXPECT_EQ(signbit(aNaN), 0);
-
- EXPECT_EQ(signbit(neg_zero), 1);
- EXPECT_EQ(signbit(-PI), 1);
- EXPECT_EQ(signbit(neg_inf), 1);
- EXPECT_EQ(signbit(neg_aNaN), 1);
- }
-
- void testSpecialCases(SignbitFunc func) {
- EXPECT_EQ(signbit(PI / zero), 0); // division by zero
- EXPECT_EQ(signbit(PI / inf), 0); // division by +inf
- EXPECT_EQ(signbit(PI / neg_inf), 1); // division by -inf
- EXPECT_EQ(signbit(inf / neg_inf), 1); // +inf divided by -inf
-
- EXPECT_EQ(signbit(inf * neg_inf), 1); // multiply +inf by -inf
- EXPECT_EQ(signbit(inf * zero), 1); // multiply by +inf
- EXPECT_EQ(signbit(neg_inf * zero), 1); // multiply by -inf
-
- EXPECT_EQ(signbit(inf + neg_inf), 1); // +inf + -inf
+ EXPECT_EQ(func(1), 0);
+ EXPECT_EQ(func(-1), 1);
}
};
#define LIST_SIGNBIT_TESTS(T, func) \
- using LlvmLibcSignbitTest = SignbitTest<T>; \
- TEST_F(LlvmLibcSignbitTest, SpecialNumbers) { testSpecialNumbers(&func); } \
- TEST_F(LlvmLibcSignbitTest, SpecialCases) { testSpecialCases(&func); }
+ using LlvmLibcSignbitTest = SignbitTest<T>; \
+ TEST_F(LlvmLibcSignbitTest, SpecialNumbers) { \
+ auto signbit_func = [](T x) { return func(x); }; \
+ testSpecialNumbers(signbit_func); \
+ }
#endif // LLVM_LIBC_TEST_INCLUDE_MATH_SIGNBIT_H
More information about the libc-commits
mailing list