Blame SOURCES/gcc12-pr107468.patch

5ae6c5
libstdc++: Update from latest fast_float [PR107468]
5ae6c5
5ae6c5
The following patch is a cherry-pick from
5ae6c5
https://github.com/fastfloat/fast_float/pull/153
5ae6c5
to restrict fast_float Clinger's fast path to when rounding mode
5ae6c5
is FE_TONEAREST.
5ae6c5
Using std::fegetround showed in benchmarks too slow, so instead
5ae6c5
it uses a check with 2 float additions and comparison to verify
5ae6c5
if rounding is FE_TONEAREST.
5ae6c5
5ae6c5
2022-11-20  Jakub Jelinek  <jakub@redhat.com>
5ae6c5
5ae6c5
	PR libstdc++/107468
5ae6c5
	* src/c++17/fast_float/fast_float.h (detail::rounds_to_nearest): New
5ae6c5
	function, taken from https://github.com/fastfloat/fast_float/pull/153.
5ae6c5
	(from_chars_advanced): Only use Clinger's fast path if
5ae6c5
	detail::rounds_to_nearest().
5ae6c5
	* testsuite/20_util/from_chars/pr107468.cc: New test.
5ae6c5
5ae6c5
--- libstdc++-v3/src/c++17/fast_float/fast_float.h.jj	2022-04-28 15:56:18.315632888 +0200
5ae6c5
+++ libstdc++-v3/src/c++17/fast_float/fast_float.h	2022-11-20 18:53:49.570830249 +0100
5ae6c5
@@ -2842,6 +2842,48 @@ from_chars_result parse_infnan(const cha
5ae6c5
   return answer;
5ae6c5
 }
5ae6c5
 
5ae6c5
+/**
5ae6c5
+ * Returns true if the floating-pointing rounding mode is to 'nearest'.
5ae6c5
+ * It is the default on most system. This function is meant to be inexpensive.
5ae6c5
+ * Credit : @mwalcott3
5ae6c5
+ */
5ae6c5
+fastfloat_really_inline bool rounds_to_nearest() noexcept {
5ae6c5
+  // See
5ae6c5
+  // A fast function to check your floating-point rounding mode
5ae6c5
+  // https://lemire.me/blog/2022/11/16/a-fast-function-to-check-your-floating-point-rounding-mode/
5ae6c5
+  //
5ae6c5
+  // This function is meant to be equivalent to :
5ae6c5
+  // prior: #include <cfenv>
5ae6c5
+  //  return fegetround() == FE_TONEAREST;
5ae6c5
+  // However, it is expected to be much faster than the fegetround()
5ae6c5
+  // function call.
5ae6c5
+  //
5ae6c5
+  // The volatile keywoard prevents the compiler from computing the function
5ae6c5
+  // at compile-time.
5ae6c5
+  // There might be other ways to prevent compile-time optimizations (e.g., asm).
5ae6c5
+  // The value does not need to be std::numeric_limits<float>::min(), any small
5ae6c5
+  // value so that 1 + x should round to 1 would do (after accounting for excess
5ae6c5
+  // precision, as in 387 instructions).
5ae6c5
+  static volatile float fmin = std::numeric_limits<float>::min();
5ae6c5
+  float fmini = fmin; // we copy it so that it gets loaded at most once.
5ae6c5
+  //
5ae6c5
+  // Explanation:
5ae6c5
+  // Only when fegetround() == FE_TONEAREST do we have that
5ae6c5
+  // fmin + 1.0f == 1.0f - fmin.
5ae6c5
+  //
5ae6c5
+  // FE_UPWARD:
5ae6c5
+  //  fmin + 1.0f > 1
5ae6c5
+  //  1.0f - fmin == 1
5ae6c5
+  //
5ae6c5
+  // FE_DOWNWARD or  FE_TOWARDZERO:
5ae6c5
+  //  fmin + 1.0f == 1
5ae6c5
+  //  1.0f - fmin < 1
5ae6c5
+  //
5ae6c5
+  // Note: This may fail to be accurate if fast-math has been
5ae6c5
+  // enabled, as rounding conventions may not apply.
5ae6c5
+  return (fmini + 1.0f == 1.0f - fmini);
5ae6c5
+}
5ae6c5
+
5ae6c5
 } // namespace detail
5ae6c5
 
5ae6c5
 template<typename T>
5ae6c5
@@ -2870,7 +2912,7 @@ from_chars_result from_chars_advanced(co
5ae6c5
   answer.ec = std::errc(); // be optimistic
5ae6c5
   answer.ptr = pns.lastmatch;
5ae6c5
   // Next is Clinger's fast path.
5ae6c5
-  if (binary_format<T>::min_exponent_fast_path() <= pns.exponent && pns.exponent <= binary_format<T>::max_exponent_fast_path() && pns.mantissa <=binary_format<T>::max_mantissa_fast_path() && !pns.too_many_digits) {
5ae6c5
+  if (binary_format<T>::min_exponent_fast_path() <= pns.exponent && pns.exponent <= binary_format<T>::max_exponent_fast_path() && pns.mantissa <=binary_format<T>::max_mantissa_fast_path() && !pns.too_many_digits && detail::rounds_to_nearest()) {
5ae6c5
     value = T(pns.mantissa);
5ae6c5
     if (pns.exponent < 0) { value = value / binary_format<T>::exact_power_of_ten(-pns.exponent); }
5ae6c5
     else { value = value * binary_format<T>::exact_power_of_ten(pns.exponent); }
5ae6c5
--- libstdc++-v3/testsuite/20_util/from_chars/pr107468.cc.jj
5ae6c5
+++ libstdc++-v3/testsuite/20_util/from_chars/pr107468.cc
5ae6c5
@@ -0,0 +1,42 @@
5ae6c5
+// Copyright (C) 2022 Free Software Foundation, Inc.
5ae6c5
+//
5ae6c5
+// This file is part of the GNU ISO C++ Library.  This library is free
5ae6c5
+// software; you can redistribute it and/or modify it under the
5ae6c5
+// terms of the GNU General Public License as published by the
5ae6c5
+// Free Software Foundation; either version 3, or (at your option)
5ae6c5
+// any later version.
5ae6c5
+
5ae6c5
+// This library is distributed in the hope that it will be useful,
5ae6c5
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
5ae6c5
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5ae6c5
+// GNU General Public License for more details.
5ae6c5
+
5ae6c5
+// You should have received a copy of the GNU General Public License along
5ae6c5
+// with this library; see the file COPYING3.  If not see
5ae6c5
+// <http://www.gnu.org/licenses/>.
5ae6c5
+
5ae6c5
+// { dg-do run { target c++17 } }
5ae6c5
+// { dg-add-options ieee }
5ae6c5
+
5ae6c5
+#include <charconv>
5ae6c5
+#include <string>
5ae6c5
+#include <cfenv>
5ae6c5
+#include <testsuite_hooks.h>
5ae6c5
+
5ae6c5
+int
5ae6c5
+main()
5ae6c5
+{
5ae6c5
+  // FP from_char not available otherwise.
5ae6c5
+#if __cpp_lib_to_chars >= 201611L \
5ae6c5
+    && _GLIBCXX_USE_C99_FENV_TR1 \
5ae6c5
+    && defined(FE_DOWNWARD) \
5ae6c5
+    && defined(_GLIBCXX_FLOAT_IS_IEEE_BINARY32)
5ae6c5
+  // PR libstdc++/107468
5ae6c5
+  float f;
5ae6c5
+  char buf[] = "3.355447e+07";
5ae6c5
+  std::fesetround(FE_DOWNWARD);
5ae6c5
+  auto [ptr, ec] = std::from_chars(buf, buf + sizeof(buf) - 1, f, std::chars_format::scientific);
5ae6c5
+  VERIFY( ec == std::errc() && ptr == buf + sizeof(buf) - 1 );
5ae6c5
+  VERIFY( f == 33554472.0f );
5ae6c5
+#endif
5ae6c5
+}