duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

README.md (10823B)


      1 ## fast_float number parsing library: 4x faster than strtod
      2 
      3 The fast_float library provides fast header-only implementations for the C++ from_chars
      4 functions for `float` and `double` types.  These functions convert ASCII strings representing
      5 decimal values (e.g., `1.3e10`) into binary types. We provide exact rounding (including
      6 round to even). In our experience, these `fast_float` functions many times faster than comparable number-parsing functions from existing C++ standard libraries.
      7 
      8 Specifically, `fast_float` provides the following two functions with a C++17-like syntax (the library itself only requires C++11):
      9 
     10 ```C++
     11 from_chars_result from_chars(const char* first, const char* last, float& value, ...);
     12 from_chars_result from_chars(const char* first, const char* last, double& value, ...);
     13 ```
     14 
     15 The return type (`from_chars_result`) is defined as the struct:
     16 ```C++
     17 struct from_chars_result {
     18     const char* ptr;
     19     std::errc ec;
     20 };
     21 ```
     22 
     23 It parses the character sequence [first,last) for a number. It parses floating-point numbers expecting
     24 a locale-independent format equivalent to the C++17 from_chars function.
     25 The resulting floating-point value is the closest floating-point values (using either float or double),
     26 using the "round to even" convention for values that would otherwise fall right in-between two values.
     27 That is, we provide exact parsing according to the IEEE standard.
     28 
     29 
     30 Given a successful parse, the pointer (`ptr`) in the returned value is set to point right after the
     31 parsed number, and the `value` referenced is set to the parsed value. In case of error, the returned
     32 `ec` contains a representative error, otherwise the default (`std::errc()`) value is stored.
     33 
     34 The implementation does not throw and does not allocate memory (e.g., with `new` or `malloc`).
     35 
     36 It will parse infinity and nan values.
     37 
     38 Example:
     39 
     40 ``` C++
     41 #include "fast_float/fast_float.h"
     42 #include <iostream>
     43 
     44 int main() {
     45     const std::string input =  "3.1416 xyz ";
     46     double result;
     47     auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result);
     48     if(answer.ec != std::errc()) { std::cerr << "parsing failure\n"; return EXIT_FAILURE; }
     49     std::cout << "parsed the number " << result << std::endl;
     50     return EXIT_SUCCESS;
     51 }
     52 ```
     53 
     54 
     55 Like the C++17 standard, the `fast_float::from_chars` functions take an optional last argument of
     56 the type `fast_float::chars_format`. It is a bitset value: we check whether
     57 `fmt & fast_float::chars_format::fixed` and `fmt & fast_float::chars_format::scientific` are set
     58 to determine whether we allow the fixed point and scientific notation respectively.
     59 The default is  `fast_float::chars_format::general` which allows both `fixed` and `scientific`.
     60 
     61 The library seeks to follow the C++17 (see [20.19.3](http://eel.is/c++draft/charconv.from.chars).(7.1))  specification.
     62 * The `from_chars` function does not skip leading white-space characters.
     63 * [A leading `+` sign](https://en.cppreference.com/w/cpp/utility/from_chars) is forbidden.
     64 * 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.
     65 
     66 Furthermore, we have the following restrictions:
     67 * We only support `float` and `double` types at this time.
     68 * We only support the decimal format: we do not support hexadecimal strings.
     69 * For values that are either very large or very small (e.g., `1e9999`), we represent it using the infinity or negative infinity value.
     70 
     71 We support Visual Studio, macOS, Linux, freeBSD. We support big and little endian. We support 32-bit and 64-bit systems.
     72 
     73 We assume that the rounding mode is set to nearest (`std::fegetround() == FE_TONEAREST`).
     74 
     75 ## Using commas as decimal separator
     76 
     77 
     78 The C++ standard stipulate that `from_chars` has to be locale-independent. In
     79 particular, the decimal separator has to be the period (`.`). However,
     80 some users still want to use the `fast_float` library with in a locale-dependent
     81 manner. Using a separate function called `from_chars_advanced`, we allow the users
     82 to pass a `parse_options` instance which contains a custom decimal separator (e.g.,
     83 the comma). You may use it as follows.
     84 
     85 ```C++
     86 #include "fast_float/fast_float.h"
     87 #include <iostream>
     88 
     89 int main() {
     90     const std::string input =  "3,1416 xyz ";
     91     double result;
     92     fast_float::parse_options options{fast_float::chars_format::general, ','};
     93     auto answer = fast_float::from_chars_advanced(input.data(), input.data()+input.size(), result, options);
     94     if((answer.ec != std::errc()) || ((result != 3.1416))) { std::cerr << "parsing failure\n"; return EXIT_FAILURE; }
     95     std::cout << "parsed the number " << result << std::endl;
     96     return EXIT_SUCCESS;
     97 }
     98 ```
     99 
    100 You can parse delimited numbers:
    101 ```C++
    102   const std::string input =   "234532.3426362,7869234.9823,324562.645";
    103   double result;
    104   auto answer = fast_float::from_chars(input.data(), input.data()+input.size(), result);
    105   if(answer.ec != std::errc()) {
    106     // check error
    107   }
    108   // we have result == 234532.3426362.
    109   if(answer.ptr[0] != ',') {
    110     // unexpected delimiter
    111   }
    112   answer = fast_float::from_chars(answer.ptr + 1, input.data()+input.size(), result);
    113   if(answer.ec != std::errc()) {
    114     // check error
    115   }
    116   // we have result == 7869234.9823.
    117   if(answer.ptr[0] != ',') {
    118     // unexpected delimiter
    119   }
    120   answer = fast_float::from_chars(answer.ptr + 1, input.data()+input.size(), result);
    121   if(answer.ec != std::errc()) {
    122     // check error
    123   }
    124   // we have result == 324562.645.
    125 ```
    126 
    127 
    128 ## Relation With Other Work
    129 
    130 The fast_float library is part of:
    131 
    132 - GCC (as of version 12): the `from_chars` function in GCC relies on fast_float.
    133 - [WebKit](https://github.com/WebKit/WebKit), the engine behind Safari (Apple's web browser)
    134 
    135 
    136 The fastfloat algorithm is part of the [LLVM standard libraries](https://github.com/llvm/llvm-project/commit/87c016078ad72c46505461e4ff8bfa04819fe7ba).
    137 
    138 There is a [derived implementation part of AdaCore](https://github.com/AdaCore/VSS).
    139 
    140 
    141 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).
    142 
    143 ## Reference
    144 
    145 - Daniel Lemire, [Number Parsing at a Gigabyte per Second](https://arxiv.org/abs/2101.11408), Software: Practice and Experience 51 (8), 2021.
    146 
    147 ## Other programming languages
    148 
    149 - [There is an R binding](https://github.com/eddelbuettel/rcppfastfloat) called `rcppfastfloat`.
    150 - [There is a Rust port of the fast_float library](https://github.com/aldanor/fast-float-rust/) called `fast-float-rust`.
    151 - [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).
    152 - [There is a C# port of the fast_float library](https://github.com/CarlVerret/csFastFloat) called `csFastFloat`.
    153 
    154 
    155 ## Users
    156 
    157 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).
    158 
    159 
    160 ## How fast is it?
    161 
    162 It can parse random floating-point numbers at a speed of 1 GB/s on some systems. We find that it is often twice as fast as the best available competitor, and many times faster than many standard-library implementations.
    163 
    164 <img src="http://lemire.me/blog/wp-content/uploads/2020/11/fastfloat_speed.png" width="400">
    165 
    166 ```
    167 $ ./build/benchmarks/benchmark
    168 # parsing random integers in the range [0,1)
    169 volume = 2.09808 MB
    170 netlib                                  :   271.18 MB/s (+/- 1.2 %)    12.93 Mfloat/s 
    171 doubleconversion                        :   225.35 MB/s (+/- 1.2 %)    10.74 Mfloat/s 
    172 strtod                                  :   190.94 MB/s (+/- 1.6 %)     9.10 Mfloat/s 
    173 abseil                                  :   430.45 MB/s (+/- 2.2 %)    20.52 Mfloat/s 
    174 fastfloat                               :  1042.38 MB/s (+/- 9.9 %)    49.68 Mfloat/s 
    175 ```
    176 
    177 See https://github.com/lemire/simple_fastfloat_benchmark for our benchmarking code.
    178 
    179 
    180 ## Video
    181 
    182 [![Go Systems 2020](http://img.youtube.com/vi/AVXgvlMeIm4/0.jpg)](http://www.youtube.com/watch?v=AVXgvlMeIm4)<br />
    183 
    184 ## Using as a CMake dependency
    185 
    186 This library is header-only by design. The CMake file provides the `fast_float` target
    187 which is merely a pointer to the `include` directory.
    188 
    189 If you drop the `fast_float` repository in your CMake project, you should be able to use
    190 it in this manner:
    191 
    192 ```cmake
    193 add_subdirectory(fast_float)
    194 target_link_libraries(myprogram PUBLIC fast_float)
    195 ```
    196 
    197 Or you may want to retrieve the dependency automatically if you have a sufficiently recent version of CMake (3.11 or better at least):
    198 
    199 ```cmake
    200 FetchContent_Declare(
    201   fast_float
    202   GIT_REPOSITORY https://github.com/lemire/fast_float.git
    203   GIT_TAG tags/v1.1.2
    204   GIT_SHALLOW TRUE)
    205 
    206 FetchContent_MakeAvailable(fast_float)
    207 target_link_libraries(myprogram PUBLIC fast_float)
    208 
    209 ```
    210 
    211 You should change the `GIT_TAG` line so that you recover the version you wish to use.
    212 
    213 ## Using as single header
    214 
    215 The script `script/amalgamate.py` may be used to generate a single header
    216 version of the library if so desired.
    217 Just run the script from the root directory of this repository.
    218 You can customize the license type and output file if desired as described in
    219 the command line help.
    220 
    221 You may directly download automatically generated single-header files:
    222 
    223 https://github.com/fastfloat/fast_float/releases/download/v3.4.0/fast_float.h
    224 
    225 ## Credit
    226 
    227 Though this work is inspired by many different people, this work benefited especially from exchanges with
    228 Michael Eisel, who motivated the original research with his key insights, and with Nigel Tao who provided
    229 invaluable feedback. Rémy Oudompheng first implemented a fast path we use in the case of long digits.
    230 
    231 The library includes code adapted from Google Wuffs (written by Nigel Tao) which was originally published
    232 under the Apache 2.0 license.
    233 
    234 ## License
    235 
    236 <sup>
    237 Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
    238 2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
    239 </sup>
    240 
    241 <br>
    242 
    243 <sub>
    244 Unless you explicitly state otherwise, any contribution intentionally submitted
    245 for inclusion in this repository by you, as defined in the Apache-2.0 license,
    246 shall be dual licensed as above, without any additional terms or conditions.
    247 </sub>