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