[libc-commits] [libc] 04c681d - [libc] Specify rounding mode for strto[f|d] tests

Alex Brachet via libc-commits libc-commits at lists.llvm.org
Wed Jul 13 13:21:22 PDT 2022


Author: Alex Brachet
Date: 2022-07-13T20:20:30Z
New Revision: 04c681d195647bc111e067726b0b1bf6025971ce

URL: https://github.com/llvm/llvm-project/commit/04c681d195647bc111e067726b0b1bf6025971ce
DIFF: https://github.com/llvm/llvm-project/commit/04c681d195647bc111e067726b0b1bf6025971ce.diff

LOG: [libc] Specify rounding mode for strto[f|d] tests

The specified rounding mode will be used and restored
to what it was before the test ran.

Additionally, it moves ForceRoundingMode and RoundingMode
out of MPFRUtils to be used in more places.

Differential Revision: https://reviews.llvm.org/D129685

Added: 
    libc/utils/testutils/RoundingModeUtils.cpp
    libc/utils/testutils/RoundingModeUtils.h

Modified: 
    libc/test/src/stdlib/strtod_test.cpp
    libc/test/src/stdlib/strtof_test.cpp
    libc/utils/MPFRWrapper/CMakeLists.txt
    libc/utils/MPFRWrapper/MPFRUtils.cpp
    libc/utils/MPFRWrapper/MPFRUtils.h
    libc/utils/testutils/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/test/src/stdlib/strtod_test.cpp b/libc/test/src/stdlib/strtod_test.cpp
index eee11231c0442..1a778fe05002a 100644
--- a/libc/test/src/stdlib/strtod_test.cpp
+++ b/libc/test/src/stdlib/strtod_test.cpp
@@ -10,12 +10,17 @@
 #include "src/stdlib/strtod.h"
 
 #include "utils/UnitTest/Test.h"
+#include "utils/testutils/RoundingModeUtils.h"
 
 #include <errno.h>
 #include <limits.h>
 #include <stddef.h>
 
-class LlvmLibcStrToDTest : public __llvm_libc::testing::Test {
+using __llvm_libc::testutils::ForceRoundingModeTest;
+using __llvm_libc::testutils::RoundingMode;
+
+class LlvmLibcStrToDTest : public __llvm_libc::testing::Test,
+                           ForceRoundingModeTest<RoundingMode::Nearest> {
 public:
   void run_test(const char *inputString, const ptr
diff _t expectedStrLen,
                 const uint64_t expectedRawData, const int expectedErrno = 0) {

diff  --git a/libc/test/src/stdlib/strtof_test.cpp b/libc/test/src/stdlib/strtof_test.cpp
index e47541da47ccb..09f13f97ec556 100644
--- a/libc/test/src/stdlib/strtof_test.cpp
+++ b/libc/test/src/stdlib/strtof_test.cpp
@@ -10,12 +10,17 @@
 #include "src/stdlib/strtof.h"
 
 #include "utils/UnitTest/Test.h"
+#include "utils/testutils/RoundingModeUtils.h"
 
 #include <errno.h>
 #include <limits.h>
 #include <stddef.h>
 
-class LlvmLibcStrToFTest : public __llvm_libc::testing::Test {
+using __llvm_libc::testutils::ForceRoundingModeTest;
+using __llvm_libc::testutils::RoundingMode;
+
+class LlvmLibcStrToFTest : public __llvm_libc::testing::Test,
+                           ForceRoundingModeTest<RoundingMode::Nearest> {
 public:
   void run_test(const char *inputString, const ptr
diff _t expectedStrLen,
                 const uint32_t expectedRawData, const int expectedErrno = 0) {

diff  --git a/libc/utils/MPFRWrapper/CMakeLists.txt b/libc/utils/MPFRWrapper/CMakeLists.txt
index 70d598d3c5a98..763a3d563e9d1 100644
--- a/libc/utils/MPFRWrapper/CMakeLists.txt
+++ b/libc/utils/MPFRWrapper/CMakeLists.txt
@@ -12,12 +12,13 @@ if(LIBC_TESTS_CAN_USE_MPFR)
     libc.src.__support.CPP.type_traits 
     libc.src.__support.FPUtil.fputil 
     LibcUnitTest
+    libc_test_utils
   )
   if(EXISTS ${LLVM_LIBC_MPFR_INSTALL_PATH})
     target_include_directories(libcMPFRWrapper PUBLIC ${LLVM_LIBC_MPFR_INSTALL_PATH}/include)
     target_link_directories(libcMPFRWrapper PUBLIC ${LLVM_LIBC_MPFR_INSTALL_PATH}/lib)
   endif()
-  target_link_libraries(libcMPFRWrapper LibcFPTestHelpers LibcUnitTest mpfr gmp)
+  target_link_libraries(libcMPFRWrapper LibcFPTestHelpers LibcUnitTest mpfr gmp libc_test_utils)
 else()
   message(WARNING "Math tests using MPFR will be skipped.")
 endif()

diff  --git a/libc/utils/MPFRWrapper/MPFRUtils.cpp b/libc/utils/MPFRWrapper/MPFRUtils.cpp
index 61052b99dfadc..695aed8f36198 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.cpp
+++ b/libc/utils/MPFRWrapper/MPFRUtils.cpp
@@ -106,35 +106,6 @@ static inline mpfr_rnd_t get_mpfr_rounding_mode(RoundingMode mode) {
   }
 }
 
-int get_fe_rounding(RoundingMode mode) {
-  switch (mode) {
-  case RoundingMode::Upward:
-    return FE_UPWARD;
-    break;
-  case RoundingMode::Downward:
-    return FE_DOWNWARD;
-    break;
-  case RoundingMode::TowardZero:
-    return FE_TOWARDZERO;
-    break;
-  case RoundingMode::Nearest:
-    return FE_TONEAREST;
-    break;
-  }
-}
-
-ForceRoundingMode::ForceRoundingMode(RoundingMode mode) {
-  old_rounding_mode = fegetround();
-  rounding_mode = get_fe_rounding(mode);
-  if (old_rounding_mode != rounding_mode)
-    fesetround(rounding_mode);
-}
-
-ForceRoundingMode::~ForceRoundingMode() {
-  if (old_rounding_mode != rounding_mode)
-    fesetround(old_rounding_mode);
-}
-
 class MPFRNumber {
   unsigned int mpfr_precision;
   mpfr_rnd_t mpfr_rounding;

diff  --git a/libc/utils/MPFRWrapper/MPFRUtils.h b/libc/utils/MPFRWrapper/MPFRUtils.h
index f1caa41ca1d01..1c80ea9a5c23a 100644
--- a/libc/utils/MPFRWrapper/MPFRUtils.h
+++ b/libc/utils/MPFRWrapper/MPFRUtils.h
@@ -11,6 +11,7 @@
 
 #include "src/__support/CPP/TypeTraits.h"
 #include "utils/UnitTest/Test.h"
+#include "utils/testutils/RoundingModeUtils.h"
 
 #include <stdint.h>
 
@@ -75,17 +76,8 @@ enum class Operation : int {
   EndTernaryOperationsSingleOutput,
 };
 
-enum class RoundingMode : uint8_t { Upward, Downward, TowardZero, Nearest };
-
-int get_fe_rounding(RoundingMode mode);
-
-struct ForceRoundingMode {
-  ForceRoundingMode(RoundingMode);
-  ~ForceRoundingMode();
-
-  int old_rounding_mode;
-  int rounding_mode;
-};
+using __llvm_libc::testutils::ForceRoundingMode;
+using __llvm_libc::testutils::RoundingMode;
 
 template <typename T> struct BinaryInput {
   static_assert(

diff  --git a/libc/utils/testutils/CMakeLists.txt b/libc/utils/testutils/CMakeLists.txt
index eaa7ad0d69d26..616a01f44de2a 100644
--- a/libc/utils/testutils/CMakeLists.txt
+++ b/libc/utils/testutils/CMakeLists.txt
@@ -15,4 +15,5 @@ add_library(
   FDReader.h
   Timer.h
   Timer.cpp
+  RoundingModeUtils.cpp
 )

diff  --git a/libc/utils/testutils/RoundingModeUtils.cpp b/libc/utils/testutils/RoundingModeUtils.cpp
new file mode 100644
index 0000000000000..1f071083489fd
--- /dev/null
+++ b/libc/utils/testutils/RoundingModeUtils.cpp
@@ -0,0 +1,46 @@
+//===-- RoundingModeUtils.cpp ---------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "RoundingModeUtils.h"
+
+#include <fenv.h>
+
+namespace __llvm_libc {
+namespace testutils {
+
+int get_fe_rounding(RoundingMode mode) {
+  switch (mode) {
+  case RoundingMode::Upward:
+    return FE_UPWARD;
+    break;
+  case RoundingMode::Downward:
+    return FE_DOWNWARD;
+    break;
+  case RoundingMode::TowardZero:
+    return FE_TOWARDZERO;
+    break;
+  case RoundingMode::Nearest:
+    return FE_TONEAREST;
+    break;
+  }
+}
+
+ForceRoundingMode::ForceRoundingMode(RoundingMode mode) {
+  old_rounding_mode = fegetround();
+  rounding_mode = get_fe_rounding(mode);
+  if (old_rounding_mode != rounding_mode)
+    fesetround(rounding_mode);
+}
+
+ForceRoundingMode::~ForceRoundingMode() {
+  if (old_rounding_mode != rounding_mode)
+    fesetround(old_rounding_mode);
+}
+
+} // namespace testutils
+} // namespace __llvm_libc

diff  --git a/libc/utils/testutils/RoundingModeUtils.h b/libc/utils/testutils/RoundingModeUtils.h
new file mode 100644
index 0000000000000..49c81ec4a8cea
--- /dev/null
+++ b/libc/utils/testutils/RoundingModeUtils.h
@@ -0,0 +1,34 @@
+//===-- RoundingModeUtils.h -------------------------------------*- 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_UTILS_TESTUTILS_ROUNDINGMODEUTILS_H
+#define LLVM_LIBC_UTILS_TESTUTILS_ROUNDINGMODEUTILS_H
+
+#include <stdint.h>
+
+namespace __llvm_libc {
+namespace testutils {
+
+enum class RoundingMode : uint8_t { Upward, Downward, TowardZero, Nearest };
+
+struct ForceRoundingMode {
+  ForceRoundingMode(RoundingMode);
+  ~ForceRoundingMode();
+
+  int old_rounding_mode;
+  int rounding_mode;
+};
+
+template <RoundingMode R> struct ForceRoundingModeTest : ForceRoundingMode {
+  ForceRoundingModeTest() : ForceRoundingMode(R) {}
+};
+
+} // namespace testutils
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_UTILS_TESTUTILS_ROUNDINGMODEUTILS_H


        


More information about the libc-commits mailing list