Blame SOURCES/gcc12-pr107468.patch

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