libstdc++: Check FE_TONEAREST is defined before using it
authorPatrick Palka <ppalka@redhat.com>
Fri, 18 Dec 2020 16:52:14 +0000 (11:52 -0500)
committerPatrick Palka <ppalka@redhat.com>
Fri, 18 Dec 2020 16:52:14 +0000 (11:52 -0500)
We need to test that FE_TONEAREST is defined before we may use it along
with fegetround/fesetround to adjust the floating-point rounding mode.
This fixes a build failure with older versions of newlib.

libstdc++-v3/ChangeLog:

* src/c++17/floating_from_chars.cc (from_chars_impl)
[!defined(FE_TONEAREST)]: Don't adjust the rounding mode.
* src/c++17/floating_to_chars.cc (__floating_to_chars_precision):
Likewise.

libstdc++-v3/src/c++17/floating_from_chars.cc
libstdc++-v3/src/c++17/floating_to_chars.cc

index a1943351493760e1596cf3244ef57c060b9e84b6..86d6d0645a98851d03013c161208ae0eaf390726 100644 (file)
@@ -307,7 +307,7 @@ namespace
       {
        locale_t orig = ::uselocale(loc);
 
-#if _GLIBCXX_USE_C99_FENV_TR1
+#if _GLIBCXX_USE_C99_FENV_TR1 && defined(FE_TONEAREST)
        const int rounding = std::fegetround();
        if (rounding != FE_TONEAREST)
          std::fesetround(FE_TONEAREST);
@@ -333,7 +333,7 @@ namespace
 #endif
        const int conv_errno = std::__exchange(errno, save_errno);
 
-#if _GLIBCXX_USE_C99_FENV_TR1
+#if _GLIBCXX_USE_C99_FENV_TR1 && defined(FE_TONEAREST)
        if (rounding != FE_TONEAREST)
          std::fesetround(rounding);
 #endif
index dd83f5eea93fdc609e7516c03fb47bebb6ee9662..474e791e717602e00dcaada18945b1bbbe07bad1 100644 (file)
@@ -946,13 +946,13 @@ template<typename T>
            // digit, and carefully compute and write the last digit
            // ourselves.
            char buffer[expected_output_length+1];
-#if _GLIBCXX_USE_C99_FENV_TR1
+#if _GLIBCXX_USE_C99_FENV_TR1 && defined(FE_TONEAREST)
            const int saved_rounding_mode = fegetround();
            if (saved_rounding_mode != FE_TONEAREST)
              fesetround(FE_TONEAREST); // We want round-to-nearest behavior.
 #endif
            const int output_length = sprintf(buffer, "%.0Lf", value);
-#if _GLIBCXX_USE_C99_FENV_TR1
+#if _GLIBCXX_USE_C99_FENV_TR1 && defined(FE_TONEAREST)
            if (saved_rounding_mode != FE_TONEAREST)
              fesetround(saved_rounding_mode);
 #endif
@@ -1139,14 +1139,14 @@ template<typename T>
 
        // Do the sprintf into the local buffer.
        char buffer[output_length_upper_bound+1];
-#if _GLIBCXX_USE_C99_FENV_TR1
+#if _GLIBCXX_USE_C99_FENV_TR1 && defined(FE_TONEAREST)
        const int saved_rounding_mode = fegetround();
        if (saved_rounding_mode != FE_TONEAREST)
          fesetround(FE_TONEAREST); // We want round-to-nearest behavior.
 #endif
        int output_length
          = sprintf(buffer, output_specifier, effective_precision, value);
-#if _GLIBCXX_USE_C99_FENV_TR1
+#if _GLIBCXX_USE_C99_FENV_TR1 && defined(FE_TONEAREST)
        if (saved_rounding_mode != FE_TONEAREST)
          fesetround(saved_rounding_mode);
 #endif