summaryrefslogtreecommitdiff
path: root/libstdc++-v3/src/c++17
diff options
context:
space:
mode:
Diffstat (limited to 'libstdc++-v3/src/c++17')
-rw-r--r--libstdc++-v3/src/c++17/fast_float/LOCAL_PATCHES2
-rw-r--r--libstdc++-v3/src/c++17/fast_float/MERGE2
-rw-r--r--libstdc++-v3/src/c++17/fast_float/README.md91
-rw-r--r--libstdc++-v3/src/c++17/fast_float/fast_float.h173
-rw-r--r--libstdc++-v3/src/c++17/floating_from_chars.cc27
-rw-r--r--libstdc++-v3/src/c++17/floating_to_chars.cc73
6 files changed, 260 insertions, 108 deletions
diff --git a/libstdc++-v3/src/c++17/fast_float/LOCAL_PATCHES b/libstdc++-v3/src/c++17/fast_float/LOCAL_PATCHES
index 5bb42933398..71495d6728b 100644
--- a/libstdc++-v3/src/c++17/fast_float/LOCAL_PATCHES
+++ b/libstdc++-v3/src/c++17/fast_float/LOCAL_PATCHES
@@ -1,4 +1,2 @@
r12-6647
r12-6648
-r12-6664
-r12-6665
diff --git a/libstdc++-v3/src/c++17/fast_float/MERGE b/libstdc++-v3/src/c++17/fast_float/MERGE
index 43bdc3981c8..20eae9d710f 100644
--- a/libstdc++-v3/src/c++17/fast_float/MERGE
+++ b/libstdc++-v3/src/c++17/fast_float/MERGE
@@ -1,4 +1,4 @@
-d35368cae610b4edeec61cd41e4d2367a4d33f58
+662497742fea7055f0e0ee27e5a7ddc382c2c38e
The first line of this file holds the git revision number of the
last merge done from the master library sources.
diff --git a/libstdc++-v3/src/c++17/fast_float/README.md b/libstdc++-v3/src/c++17/fast_float/README.md
index 1e1c06d0a3e..d6ae279527c 100644
--- a/libstdc++-v3/src/c++17/fast_float/README.md
+++ b/libstdc++-v3/src/c++17/fast_float/README.md
@@ -1,12 +1,5 @@
## fast_float number parsing library: 4x faster than strtod
-![Ubuntu 20.04 CI (GCC 9)](https://github.com/lemire/fast_float/workflows/Ubuntu%2020.04%20CI%20(GCC%209)/badge.svg)
-![Ubuntu 18.04 CI (GCC 7)](https://github.com/lemire/fast_float/workflows/Ubuntu%2018.04%20CI%20(GCC%207)/badge.svg)
-![Alpine Linux](https://github.com/lemire/fast_float/workflows/Alpine%20Linux/badge.svg)
-![MSYS2-CI](https://github.com/lemire/fast_float/workflows/MSYS2-CI/badge.svg)
-![VS16-CLANG-CI](https://github.com/lemire/fast_float/workflows/VS16-CLANG-CI/badge.svg)
-[![VS16-CI](https://github.com/fastfloat/fast_float/actions/workflows/vs16-ci.yml/badge.svg)](https://github.com/fastfloat/fast_float/actions/workflows/vs16-ci.yml)
-
The fast_float library provides fast header-only implementations for the C++ from_chars
functions for `float` and `double` types. These functions convert ASCII strings representing
decimal values (e.g., `1.3e10`) into binary types. We provide exact rounding (including
@@ -28,8 +21,8 @@ struct from_chars_result {
```
It parses the character sequence [first,last) for a number. It parses floating-point numbers expecting
-a locale-independent format equivalent to the C++17 from_chars function.
-The resulting floating-point value is the closest floating-point values (using either float or double),
+a locale-independent format equivalent to the C++17 from_chars function.
+The resulting floating-point value is the closest floating-point values (using either float or double),
using the "round to even" convention for values that would otherwise fall right in-between two values.
That is, we provide exact parsing according to the IEEE standard.
@@ -47,7 +40,7 @@ Example:
``` C++
#include "fast_float/fast_float.h"
#include <iostream>
-
+
int main() {
const std::string input = "3.1416 xyz ";
double result;
@@ -60,15 +53,15 @@ int main() {
Like the C++17 standard, the `fast_float::from_chars` functions take an optional last argument of
-the type `fast_float::chars_format`. It is a bitset value: we check whether
+the type `fast_float::chars_format`. It is a bitset value: we check whether
`fmt & fast_float::chars_format::fixed` and `fmt & fast_float::chars_format::scientific` are set
to determine whether we allow the fixed point and scientific notation respectively.
The default is `fast_float::chars_format::general` which allows both `fixed` and `scientific`.
-The library seeks to follow the C++17 (see [20.19.3](http://eel.is/c++draft/charconv.from.chars).(7.1)) specification.
+The library seeks to follow the C++17 (see [20.19.3](http://eel.is/c++draft/charconv.from.chars).(7.1)) specification.
* The `from_chars` function does not skip leading white-space characters.
* [A leading `+` sign](https://en.cppreference.com/w/cpp/utility/from_chars) is forbidden.
-* It is generally impossible to represent a decimal value exactly as binary floating-point number (`float` and `double` types). We seek the nearest value. We round to an even mantissa when we are in-between two binary floating-point numbers.
+* It is generally impossible to represent a decimal value exactly as binary floating-point number (`float` and `double` types). We seek the nearest value. We round to an even mantissa when we are in-between two binary floating-point numbers.
Furthermore, we have the following restrictions:
* We only support `float` and `double` types at this time.
@@ -77,22 +70,22 @@ Furthermore, we have the following restrictions:
We support Visual Studio, macOS, Linux, freeBSD. We support big and little endian. We support 32-bit and 64-bit systems.
-
+We assume that the rounding mode is set to nearest (`std::fegetround() == FE_TONEAREST`).
## Using commas as decimal separator
The C++ standard stipulate that `from_chars` has to be locale-independent. In
-particular, the decimal separator has to be the period (`.`). However,
-some users still want to use the `fast_float` library with in a locale-dependent
+particular, the decimal separator has to be the period (`.`). However,
+some users still want to use the `fast_float` library with in a locale-dependent
manner. Using a separate function called `from_chars_advanced`, we allow the users
-to pass a `parse_options` instance which contains a custom decimal separator (e.g.,
+to pass a `parse_options` instance which contains a custom decimal separator (e.g.,
the comma). You may use it as follows.
```C++
#include "fast_float/fast_float.h"
#include <iostream>
-
+
int main() {
const std::string input = "3,1416 xyz ";
double result;
@@ -104,25 +97,55 @@ int main() {
}
```
+You can parse delimited numbers:
+```C++
+ const std::string input = "234532.3426362,7869234.9823,324562.645";
+ double result;
+ auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result);
+ if(answer.ec != std::errc()) {
+ // check error
+ }
+ // we have result == 234532.3426362.
+ if(answer.ptr[0] != ',') {
+ // unexpected delimiter
+ }
+ answer = fast_float::from_chars(answer.ptr + 1, input.data()+input.size(), result);
+ if(answer.ec != std::errc()) {
+ // check error
+ }
+ // we have result == 7869234.9823.
+ if(answer.ptr[0] != ',') {
+ // unexpected delimiter
+ }
+ answer = fast_float::from_chars(answer.ptr + 1, input.data()+input.size(), result);
+ if(answer.ec != std::errc()) {
+ // check error
+ }
+ // we have result == 324562.645.
+```
## Reference
-- Daniel Lemire, [Number Parsing at a Gigabyte per Second](https://arxiv.org/abs/2101.11408), Software: Pratice and Experience 51 (8), 2021.
+- Daniel Lemire, [Number Parsing at a Gigabyte per Second](https://arxiv.org/abs/2101.11408), Software: Practice and Experience 51 (8), 2021.
## Other programming languages
- [There is an R binding](https://github.com/eddelbuettel/rcppfastfloat) called `rcppfastfloat`.
- [There is a Rust port of the fast_float library](https://github.com/aldanor/fast-float-rust/) called `fast-float-rust`.
-- [There is a Java port of the fast_float library](https://github.com/wrandelshofer/FastDoubleParser) called `FastDoubleParser`.
+- [There is a Java port of the fast_float library](https://github.com/wrandelshofer/FastDoubleParser) called `FastDoubleParser`. It used for important systems such as [Jackson](https://github.com/FasterXML/jackson-core).
- [There is a C# port of the fast_float library](https://github.com/CarlVerret/csFastFloat) called `csFastFloat`.
## Relation With Other Work
-The fastfloat algorithm is part of the [LLVM standard libraries](https://github.com/llvm/llvm-project/commit/87c016078ad72c46505461e4ff8bfa04819fe7ba).
+The fast_float library is part of GCC (as of version 12): the `from_chars` function in GCC relies on fast_float.
+
+The fastfloat algorithm is part of the [LLVM standard libraries](https://github.com/llvm/llvm-project/commit/87c016078ad72c46505461e4ff8bfa04819fe7ba).
The fast_float library provides a performance similar to that of the [fast_double_parser](https://github.com/lemire/fast_double_parser) library but using an updated algorithm reworked from the ground up, and while offering an API more in line with the expectations of C++ programmers. The fast_double_parser library is part of the [Microsoft LightGBM machine-learning framework](https://github.com/microsoft/LightGBM).
+There is a [derived implementation part of AdaCore](https://github.com/AdaCore/VSS).
+
## Users
The fast_float library is used by [Apache Arrow](https://github.com/apache/arrow/pull/8494) where it multiplied the number parsing speed by two or three times. It is also used by [Yandex ClickHouse](https://github.com/ClickHouse/ClickHouse) and by [Google Jsonnet](https://github.com/google/jsonnet).
@@ -135,14 +158,14 @@ It can parse random floating-point numbers at a speed of 1 GB/s on some systems.
<img src="http://lemire.me/blog/wp-content/uploads/2020/11/fastfloat_speed.png" width="400">
```
-$ ./build/benchmarks/benchmark
+$ ./build/benchmarks/benchmark
# parsing random integers in the range [0,1)
-volume = 2.09808 MB
-netlib : 271.18 MB/s (+/- 1.2 %) 12.93 Mfloat/s
-doubleconversion : 225.35 MB/s (+/- 1.2 %) 10.74 Mfloat/s
-strtod : 190.94 MB/s (+/- 1.6 %) 9.10 Mfloat/s
-abseil : 430.45 MB/s (+/- 2.2 %) 20.52 Mfloat/s
-fastfloat : 1042.38 MB/s (+/- 9.9 %) 49.68 Mfloat/s
+volume = 2.09808 MB
+netlib : 271.18 MB/s (+/- 1.2 %) 12.93 Mfloat/s
+doubleconversion : 225.35 MB/s (+/- 1.2 %) 10.74 Mfloat/s
+strtod : 190.94 MB/s (+/- 1.6 %) 9.10 Mfloat/s
+abseil : 430.45 MB/s (+/- 2.2 %) 20.52 Mfloat/s
+fastfloat : 1042.38 MB/s (+/- 9.9 %) 49.68 Mfloat/s
```
See https://github.com/lemire/simple_fastfloat_benchmark for our benchmarking code.
@@ -183,23 +206,23 @@ You should change the `GIT_TAG` line so that you recover the version you wish to
## Using as single header
-The script `script/amalgamate.py` may be used to generate a single header
+The script `script/amalgamate.py` may be used to generate a single header
version of the library if so desired.
-Just run the script from the root directory of this repository.
+Just run the script from the root directory of this repository.
You can customize the license type and output file if desired as described in
the command line help.
You may directly download automatically generated single-header files:
-https://github.com/fastfloat/fast_float/releases/download/v1.1.2/fast_float.h
+https://github.com/fastfloat/fast_float/releases/download/v3.4.0/fast_float.h
## Credit
-Though this work is inspired by many different people, this work benefited especially from exchanges with
-Michael Eisel, who motivated the original research with his key insights, and with Nigel Tao who provided
+Though this work is inspired by many different people, this work benefited especially from exchanges with
+Michael Eisel, who motivated the original research with his key insights, and with Nigel Tao who provided
invaluable feedback. Rémy Oudompheng first implemented a fast path we use in the case of long digits.
-The library includes code adapted from Google Wuffs (written by Nigel Tao) which was originally published
+The library includes code adapted from Google Wuffs (written by Nigel Tao) which was originally published
under the Apache 2.0 license.
## License
diff --git a/libstdc++-v3/src/c++17/fast_float/fast_float.h b/libstdc++-v3/src/c++17/fast_float/fast_float.h
index 31fb88b8aba..5da55e2fe0a 100644
--- a/libstdc++-v3/src/c++17/fast_float/fast_float.h
+++ b/libstdc++-v3/src/c++17/fast_float/fast_float.h
@@ -74,7 +74,7 @@ struct parse_options {
* Like the C++17 standard, the `fast_float::from_chars` functions take an optional last argument of
* the type `fast_float::chars_format`. It is a bitset value: we check whether
* `fmt & fast_float::chars_format::fixed` and `fmt & fast_float::chars_format::scientific` are set
- * to determine whether we allowe the fixed point and scientific notation respectively.
+ * to determine whether we allow the fixed point and scientific notation respectively.
* The default is `fast_float::chars_format::general` which allows both `fixed` and `scientific`.
*/
template<typename T>
@@ -98,12 +98,11 @@ from_chars_result from_chars_advanced(const char *first, const char *last,
|| defined(__amd64) || defined(__aarch64__) || defined(_M_ARM64) \
|| defined(__MINGW64__) \
|| defined(__s390x__) \
- || (defined(__ppc64__) || defined(__PPC64__) || defined(__ppc64le__) || defined(__PPC64LE__)) \
- || defined(__EMSCRIPTEN__))
+ || (defined(__ppc64__) || defined(__PPC64__) || defined(__ppc64le__) || defined(__PPC64LE__)) )
#define FASTFLOAT_64BIT
#elif (defined(__i386) || defined(__i386__) || defined(_M_IX86) \
|| defined(__arm__) || defined(_M_ARM) \
- || defined(__MINGW32__))
+ || defined(__MINGW32__) || defined(__EMSCRIPTEN__))
#define FASTFLOAT_32BIT
#else
// Need to check incrementally, since SIZE_MAX is a size_t, avoid overflow.
@@ -128,7 +127,7 @@ from_chars_result from_chars_advanced(const char *first, const char *last,
#define FASTFLOAT_VISUAL_STUDIO 1
#endif
-#ifdef __BYTE_ORDER__
+#if defined __BYTE_ORDER__ && defined __ORDER_BIG_ENDIAN__
#define FASTFLOAT_IS_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
#elif defined _WIN32
#define FASTFLOAT_IS_BIG_ENDIAN 0
@@ -271,8 +270,9 @@ fastfloat_really_inline uint64_t _umul128(uint64_t ab, uint64_t cd,
fastfloat_really_inline value128 full_multiplication(uint64_t a,
uint64_t b) {
value128 answer;
-#ifdef _M_ARM64
+#if defined(_M_ARM64) && !defined(__MINGW32__)
// ARM64 has native support for 64-bit multiplications, no need to emulate
+ // But MinGW on ARM64 doesn't have native support for 64-bit multiplications
answer.high = __umulh(a, b);
answer.low = a * b;
#elif defined(FASTFLOAT_32BIT) || (defined(_WIN64) && !defined(__clang__))
@@ -307,21 +307,69 @@ constexpr static double powers_of_ten_double[] = {
1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22};
constexpr static float powers_of_ten_float[] = {1e0, 1e1, 1e2, 1e3, 1e4, 1e5,
1e6, 1e7, 1e8, 1e9, 1e10};
+// used for max_mantissa_double and max_mantissa_float
+constexpr uint64_t constant_55555 = 5 * 5 * 5 * 5 * 5;
+// Largest integer value v so that (5**index * v) <= 1<<53.
+// 0x10000000000000 == 1 << 53
+constexpr static uint64_t max_mantissa_double[] = {
+ 0x10000000000000,
+ 0x10000000000000 / 5,
+ 0x10000000000000 / (5 * 5),
+ 0x10000000000000 / (5 * 5 * 5),
+ 0x10000000000000 / (5 * 5 * 5 * 5),
+ 0x10000000000000 / (constant_55555),
+ 0x10000000000000 / (constant_55555 * 5),
+ 0x10000000000000 / (constant_55555 * 5 * 5),
+ 0x10000000000000 / (constant_55555 * 5 * 5 * 5),
+ 0x10000000000000 / (constant_55555 * 5 * 5 * 5 * 5),
+ 0x10000000000000 / (constant_55555 * constant_55555),
+ 0x10000000000000 / (constant_55555 * constant_55555 * 5),
+ 0x10000000000000 / (constant_55555 * constant_55555 * 5 * 5),
+ 0x10000000000000 / (constant_55555 * constant_55555 * 5 * 5 * 5),
+ 0x10000000000000 / (constant_55555 * constant_55555 * constant_55555),
+ 0x10000000000000 / (constant_55555 * constant_55555 * constant_55555 * 5),
+ 0x10000000000000 / (constant_55555 * constant_55555 * constant_55555 * 5 * 5),
+ 0x10000000000000 / (constant_55555 * constant_55555 * constant_55555 * 5 * 5 * 5),
+ 0x10000000000000 / (constant_55555 * constant_55555 * constant_55555 * 5 * 5 * 5 * 5),
+ 0x10000000000000 / (constant_55555 * constant_55555 * constant_55555 * constant_55555),
+ 0x10000000000000 / (constant_55555 * constant_55555 * constant_55555 * constant_55555 * 5),
+ 0x10000000000000 / (constant_55555 * constant_55555 * constant_55555 * constant_55555 * 5 * 5),
+ 0x10000000000000 / (constant_55555 * constant_55555 * constant_55555 * constant_55555 * 5 * 5 * 5),
+ 0x10000000000000 / (constant_55555 * constant_55555 * constant_55555 * constant_55555 * 5 * 5 * 5 * 5)};
+ // Largest integer value v so that (5**index * v) <= 1<<24.
+ // 0x1000000 == 1<<24
+ constexpr static uint64_t max_mantissa_float[] = {
+ 0x1000000,
+ 0x1000000 / 5,
+ 0x1000000 / (5 * 5),
+ 0x1000000 / (5 * 5 * 5),
+ 0x1000000 / (5 * 5 * 5 * 5),
+ 0x1000000 / (constant_55555),
+ 0x1000000 / (constant_55555 * 5),
+ 0x1000000 / (constant_55555 * 5 * 5),
+ 0x1000000 / (constant_55555 * 5 * 5 * 5),
+ 0x1000000 / (constant_55555 * 5 * 5 * 5 * 5),
+ 0x1000000 / (constant_55555 * constant_55555),
+ 0x1000000 / (constant_55555 * constant_55555 * 5)};
template <typename T> struct binary_format {
+ using equiv_uint = typename std::conditional<sizeof(T) == 4, uint32_t, uint64_t>::type;
+
static inline constexpr int mantissa_explicit_bits();
static inline constexpr int minimum_exponent();
static inline constexpr int infinite_power();
static inline constexpr int sign_index();
- static inline constexpr int min_exponent_fast_path();
static inline constexpr int max_exponent_fast_path();
static inline constexpr int max_exponent_round_to_even();
static inline constexpr int min_exponent_round_to_even();
- static inline constexpr uint64_t max_mantissa_fast_path();
+ static inline constexpr uint64_t max_mantissa_fast_path(int64_t power);
static inline constexpr int largest_power_of_ten();
static inline constexpr int smallest_power_of_ten();
static inline constexpr T exact_power_of_ten(int64_t power);
static inline constexpr size_t max_digits();
+ static inline constexpr equiv_uint exponent_mask();
+ static inline constexpr equiv_uint mantissa_mask();
+ static inline constexpr equiv_uint hidden_bit_mask();
};
template <> inline constexpr int binary_format<double>::mantissa_explicit_bits() {
@@ -364,21 +412,6 @@ template <> inline constexpr int binary_format<float>::infinite_power() {
template <> inline constexpr int binary_format<double>::sign_index() { return 63; }
template <> inline constexpr int binary_format<float>::sign_index() { return 31; }
-template <> inline constexpr int binary_format<double>::min_exponent_fast_path() {
-#if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0)
- return 0;
-#else
- return -22;
-#endif
-}
-template <> inline constexpr int binary_format<float>::min_exponent_fast_path() {
-#if (FLT_EVAL_METHOD != 1) && (FLT_EVAL_METHOD != 0)
- return 0;
-#else
- return -10;
-#endif
-}
-
template <> inline constexpr int binary_format<double>::max_exponent_fast_path() {
return 22;
}
@@ -386,11 +419,17 @@ template <> inline constexpr int binary_format<float>::max_exponent_fast_path()
return 10;
}
-template <> inline constexpr uint64_t binary_format<double>::max_mantissa_fast_path() {
- return uint64_t(2) << mantissa_explicit_bits();
+template <> inline constexpr uint64_t binary_format<double>::max_mantissa_fast_path(int64_t power) {
+ // caller is responsible to ensure that
+ // power >= 0 && power <= 22
+ //
+ return max_mantissa_double[power];
}
-template <> inline constexpr uint64_t binary_format<float>::max_mantissa_fast_path() {
- return uint64_t(2) << mantissa_explicit_bits();
+template <> inline constexpr uint64_t binary_format<float>::max_mantissa_fast_path(int64_t power) {
+ // caller is responsible to ensure that
+ // power >= 0 && power <= 10
+ //
+ return max_mantissa_float[power];
}
template <>
@@ -429,6 +468,33 @@ template <> inline constexpr size_t binary_format<float>::max_digits() {
return 114;
}
+template <> inline constexpr binary_format<float>::equiv_uint
+ binary_format<float>::exponent_mask() {
+ return 0x7F800000;
+}
+template <> inline constexpr binary_format<double>::equiv_uint
+ binary_format<double>::exponent_mask() {
+ return 0x7FF0000000000000;
+}
+
+template <> inline constexpr binary_format<float>::equiv_uint
+ binary_format<float>::mantissa_mask() {
+ return 0x007FFFFF;
+}
+template <> inline constexpr binary_format<double>::equiv_uint
+ binary_format<double>::mantissa_mask() {
+ return 0x000FFFFFFFFFFFFF;
+}
+
+template <> inline constexpr binary_format<float>::equiv_uint
+ binary_format<float>::hidden_bit_mask() {
+ return 0x00800000;
+}
+template <> inline constexpr binary_format<double>::equiv_uint
+ binary_format<double>::hidden_bit_mask() {
+ return 0x0010000000000000;
+}
+
template<typename T>
fastfloat_really_inline void to_float(bool negative, adjusted_mantissa am, T &value) {
uint64_t word = am.mantissa;
@@ -2410,40 +2476,24 @@ fastfloat_really_inline int32_t scientific_exponent(parsed_number_string& num) n
// this converts a native floating-point number to an extended-precision float.
template <typename T>
fastfloat_really_inline adjusted_mantissa to_extended(T value) noexcept {
+ using equiv_uint = typename binary_format<T>::equiv_uint;
+ constexpr equiv_uint exponent_mask = binary_format<T>::exponent_mask();
+ constexpr equiv_uint mantissa_mask = binary_format<T>::mantissa_mask();
+ constexpr equiv_uint hidden_bit_mask = binary_format<T>::hidden_bit_mask();
+
adjusted_mantissa am;
int32_t bias = binary_format<T>::mantissa_explicit_bits() - binary_format<T>::minimum_exponent();
- if (std::is_same<T, float>::value) {
- constexpr uint32_t exponent_mask = 0x7F800000;
- constexpr uint32_t mantissa_mask = 0x007FFFFF;
- constexpr uint64_t hidden_bit_mask = 0x00800000;
- uint32_t bits;
- ::memcpy(&bits, &value, sizeof(T));
- if ((bits & exponent_mask) == 0) {
- // denormal
- am.power2 = 1 - bias;
- am.mantissa = bits & mantissa_mask;
- } else {
- // normal
- am.power2 = int32_t((bits & exponent_mask) >> binary_format<T>::mantissa_explicit_bits());
- am.power2 -= bias;
- am.mantissa = (bits & mantissa_mask) | hidden_bit_mask;
- }
+ equiv_uint bits;
+ ::memcpy(&bits, &value, sizeof(T));
+ if ((bits & exponent_mask) == 0) {
+ // denormal
+ am.power2 = 1 - bias;
+ am.mantissa = bits & mantissa_mask;
} else {
- constexpr uint64_t exponent_mask = 0x7FF0000000000000;
- constexpr uint64_t mantissa_mask = 0x000FFFFFFFFFFFFF;
- constexpr uint64_t hidden_bit_mask = 0x0010000000000000;
- uint64_t bits;
- ::memcpy(&bits, &value, sizeof(T));
- if ((bits & exponent_mask) == 0) {
- // denormal
- am.power2 = 1 - bias;
- am.mantissa = bits & mantissa_mask;
- } else {
- // normal
- am.power2 = int32_t((bits & exponent_mask) >> binary_format<T>::mantissa_explicit_bits());
- am.power2 -= bias;
- am.mantissa = (bits & mantissa_mask) | hidden_bit_mask;
- }
+ // normal
+ am.power2 = int32_t((bits & exponent_mask) >> binary_format<T>::mantissa_explicit_bits());
+ am.power2 -= bias;
+ am.mantissa = (bits & mantissa_mask) | hidden_bit_mask;
}
return am;
@@ -2869,11 +2919,10 @@ from_chars_result from_chars_advanced(const char *first, const char *last,
}
answer.ec = std::errc(); // be optimistic
answer.ptr = pns.lastmatch;
- // Next is Clinger's fast path.
- 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) {
+ // Next is a modified Clinger's fast path, inspired by Jakub Jelínek's proposal
+ if (pns.exponent >= 0 && pns.exponent <= binary_format<T>::max_exponent_fast_path() && pns.mantissa <=binary_format<T>::max_mantissa_fast_path(pns.exponent) && !pns.too_many_digits) {
value = T(pns.mantissa);
- if (pns.exponent < 0) { value = value / binary_format<T>::exact_power_of_ten(-pns.exponent); }
- else { value = value * binary_format<T>::exact_power_of_ten(pns.exponent); }
+ value = value * binary_format<T>::exact_power_of_ten(pns.exponent);
if (pns.negative) { value = -value; }
return answer;
}
diff --git a/libstdc++-v3/src/c++17/floating_from_chars.cc b/libstdc++-v3/src/c++17/floating_from_chars.cc
index 939c751f861..be1e1051b5c 100644
--- a/libstdc++-v3/src/c++17/floating_from_chars.cc
+++ b/libstdc++-v3/src/c++17/floating_from_chars.cc
@@ -59,6 +59,15 @@
#endif
// strtold for __ieee128
extern "C" __ieee128 __strtoieee128(const char*, char**);
+#elif __FLT128_MANT_DIG__ == 113 && __LDBL_MANT_DIG__ != 113 \
+ && defined(__GLIBC_PREREQ)
+#define USE_STRTOF128_FOR_FROM_CHARS 1
+extern "C" _Float128 __strtof128(const char*, char**)
+ __asm ("strtof128")
+#ifndef _GLIBCXX_HAVE_FLOAT128_MATH
+ __attribute__((__weak__))
+#endif
+ ;
#endif
#if _GLIBCXX_FLOAT_IS_IEEE_BINARY32 && _GLIBCXX_DOUBLE_IS_IEEE_BINARY64 \
@@ -618,6 +627,16 @@ namespace
# ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
else if constexpr (is_same_v<T, __ieee128>)
tmpval = __strtoieee128(str, &endptr);
+# elif defined(USE_STRTOF128_FOR_FROM_CHARS)
+ else if constexpr (is_same_v<T, _Float128>)
+ {
+#ifndef _GLIBCXX_HAVE_FLOAT128_MATH
+ if (&__strtof128 == nullptr)
+ tmpval = _Float128(std::strtold(str, &endptr));
+ else
+#endif
+ tmpval = __strtof128(str, &endptr);
+ }
# endif
#else
tmpval = std::strtod(str, &endptr);
@@ -1239,6 +1258,14 @@ from_chars(const char* first, const char* last, __ieee128& value,
// fast_float doesn't support IEEE binary128 format, but we can use strtold.
return from_chars_strtod(first, last, value, fmt);
}
+#elif defined(USE_STRTOF128_FOR_FROM_CHARS)
+from_chars_result
+from_chars(const char* first, const char* last, _Float128& value,
+ chars_format fmt) noexcept
+{
+ // fast_float doesn't support IEEE binary128 format, but we can use strtold.
+ return from_chars_strtod(first, last, value, fmt);
+}
#endif
#endif // USE_LIB_FAST_FLOAT || USE_STRTOD_FOR_FROM_CHARS
diff --git a/libstdc++-v3/src/c++17/floating_to_chars.cc b/libstdc++-v3/src/c++17/floating_to_chars.cc
index a253ee42416..afce8d74c12 100644
--- a/libstdc++-v3/src/c++17/floating_to_chars.cc
+++ b/libstdc++-v3/src/c++17/floating_to_chars.cc
@@ -43,6 +43,14 @@
#endif
// sprintf for __ieee128
extern "C" int __sprintfieee128(char*, const char*, ...);
+#elif __FLT128_MANT_DIG__ == 113 && __LDBL_MANT_DIG__ != 113 \
+ && defined(__GLIBC_PREREQ)
+extern "C" int __strfromf128(char*, size_t, const char*, _Float128)
+ __asm ("strfromf128")
+#ifndef _GLIBCXX_HAVE_FLOAT128_MATH
+ __attribute__((__weak__))
+#endif
+ ;
#endif
// This implementation crucially assumes float/double have the
@@ -77,10 +85,11 @@ extern "C" int __sprintfieee128(char*, const char*, ...);
#if defined _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT && __FLT128_MANT_DIG__ == 113
// Define overloads of std::to_chars for __float128.
# define FLOAT128_TO_CHARS 1
-#endif
-
-#ifdef FLOAT128_TO_CHARS
using F128_type = __float128;
+#elif __FLT128_MANT_DIG__ == 113 && __LDBL_MANT_DIG__ != 113 \
+ && defined(__GLIBC_PREREQ)
+# define FLOAT128_TO_CHARS 1
+using F128_type = _Float128;
#else
using F128_type = void;
#endif
@@ -252,7 +261,7 @@ namespace
# ifdef FLOAT128_TO_CHARS
template<>
- struct floating_type_traits<__float128> : floating_type_traits_binary128
+ struct floating_type_traits<F128_type> : floating_type_traits_binary128
{ };
# endif
#endif
@@ -1034,7 +1043,8 @@ namespace
#pragma GCC diagnostic ignored "-Wabi"
template<typename T, typename... Extra>
inline int
- sprintf_ld(char* buffer, const char* format_string, T value, Extra... args)
+ sprintf_ld(char* buffer, size_t length __attribute__((unused)),
+ const char* format_string, T value, Extra... args)
{
int len;
@@ -1044,10 +1054,31 @@ namespace
fesetround(FE_TONEAREST); // We want round-to-nearest behavior.
#endif
+#ifdef FLOAT128_TO_CHARS
#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
if constexpr (is_same_v<T, __ieee128>)
len = __sprintfieee128(buffer, format_string, args..., value);
else
+#else
+ if constexpr (is_same_v<T, _Float128>)
+ {
+#ifndef _GLIBCXX_HAVE_FLOAT128_MATH
+ if (&__strfromf128 == nullptr)
+ len = sprintf(buffer, format_string, args..., (long double)value);
+ else
+#endif
+ if constexpr (sizeof...(args) == 0)
+ len = __strfromf128(buffer, length, "%.0f", value);
+ else
+ {
+ // strfromf128 unfortunately doesn't allow .*
+ char fmt[3 * sizeof(int) + 6];
+ sprintf(fmt, "%%.%d%c", args..., int(format_string[4]));
+ len = __strfromf128(buffer, length, fmt, value);
+ }
+ }
+ else
+#endif
#endif
len = sprintf(buffer, format_string, args..., value);
@@ -1205,8 +1236,10 @@ template<typename T>
// can avoid this if we use sprintf to write all but the last
// digit, and carefully compute and write the last digit
// ourselves.
- char buffer[expected_output_length+1];
- const int output_length = sprintf_ld(buffer, "%.0Lf", value);
+ char buffer[expected_output_length + 1];
+ const int output_length = sprintf_ld(buffer,
+ expected_output_length + 1,
+ "%.0Lf", value);
__glibcxx_assert(output_length == expected_output_length);
memcpy(first, buffer, output_length);
return {first + output_length, errc{}};
@@ -1396,9 +1429,10 @@ template<typename T>
__builtin_unreachable();
// Do the sprintf into the local buffer.
- char buffer[output_length_upper_bound+1];
+ char buffer[output_length_upper_bound + 1];
int output_length
- = sprintf_ld(buffer, output_specifier, value, effective_precision);
+ = sprintf_ld(buffer, output_length_upper_bound + 1, output_specifier,
+ value, effective_precision);
__glibcxx_assert(output_length <= output_length_upper_bound);
if (effective_precision > 0)
@@ -1798,6 +1832,7 @@ to_chars(char* first, char* last, long double value, chars_format fmt,
}
#ifdef FLOAT128_TO_CHARS
+#ifdef _GLIBCXX_LONG_DOUBLE_ALT128_COMPAT
to_chars_result
to_chars(char* first, char* last, __float128 value) noexcept
{
@@ -1816,6 +1851,26 @@ to_chars(char* first, char* last, __float128 value, chars_format fmt,
{
return __floating_to_chars_precision(first, last, value, fmt, precision);
}
+#else
+to_chars_result
+to_chars(char* first, char* last, _Float128 value) noexcept
+{
+ return __floating_to_chars_shortest(first, last, value, chars_format{});
+}
+
+to_chars_result
+to_chars(char* first, char* last, _Float128 value, chars_format fmt) noexcept
+{
+ return __floating_to_chars_shortest(first, last, value, fmt);
+}
+
+to_chars_result
+to_chars(char* first, char* last, _Float128 value, chars_format fmt,
+ int precision) noexcept
+{
+ return __floating_to_chars_precision(first, last, value, fmt, precision);
+}
+#endif
#endif
// Entrypoints for 16-bit floats.