117 Commits

Author SHA1 Message Date
Maya Warrier
38613a39f9 Fix perf decrease when UC = char 2023-05-17 01:34:33 -04:00
Maya Warrier
6ede038789 Apply changes from benchmarked version
- Move parse_truncated_number_string back inside parse_number_string
2023-05-09 22:19:23 -04:00
Maya Warrier
53b065f38d Avoid redundant load in SSE2 code 2023-05-07 17:38:32 -04:00
Maya Warrier
4cb09b5f59 Automatically detect SSE2 2023-05-02 13:05:57 -04:00
Maya Warrier
c811b027ea Remove testing macro 2023-05-02 01:52:00 -04:00
Maya Warrier
5136b181ba Fixes and cleanup 2023-05-02 01:41:49 -04:00
Maya Warrier
680ccc73ed Merge from upstream/main, fix conflicts 2023-05-01 20:27:29 -04:00
Maya Warrier
e08c55c380 Remove json parse rules/allow inf_nan 2023-05-01 19:45:50 -04:00
Maya Warrier
091458d192 Add basic support for char32_t (unoptimized) 2023-04-30 02:23:33 -04:00
Daniel Lemire
8199baeb70 Slightly less ugly code. 2023-04-26 18:46:19 -04:00
Daniel Lemire
5223d7a460 address issues raised by @mayawarrier 2023-04-26 18:25:27 -04:00
Maya Warrier
65bd922e38 Merge remote-tracking branch 'upstream/main'
- Fix conflicts
2023-04-26 16:47:42 -04:00
Maya Warrier
89fc24007a Clean up 2023-04-26 16:25:41 -04:00
Maya Warrier
c849b7a8ff Option to forbid nan/inf, refactor 2023-04-15 23:16:01 -04:00
Pharago
2bfbe4ca96 cosmetic changes 2023-04-06 00:58:34 +02:00
Pharago
593709f056
Merge branch 'main' into main 2023-04-05 03:31:35 +02:00
Aras Pranckevičius
21fefa5b44
Fix warnings with -Wundef
- FASTFLOAT_ALLOWS_LEADING_PLUS and FASTFLOAT_SKIP_WHITE_SPACE are not defined by default, and compiling with -Wundef is emitting warnigns like "FASTFLOAT_ALLOWS_LEADING_PLUS is not defined, evaluates to 0".
- Likewise for FASTFLOAT_VISUAL_STUDIO, change checks to use #ifdef for that like in other places.
- __cpp_lib_bit_cast and __cpp_lib_is_constant_evaluated are not defined pre-C++20, and are emitting a warning too.
2023-04-04 21:18:57 +03:00
Pharago
bc77f956e2 Initial Unicode release
Added support for the other char types
2023-04-02 22:58:01 +02:00
Maya Warrier
cda25408bc Optimize char16_t parsing for digit_comparison.h 2023-04-02 00:33:52 -04:00
Maya Warrier
2d57c09530 Fixes 2023-04-01 23:29:00 -04:00
Maya Warrier
8a9a9d538a SIMD optimization to parse 8 char16_t at a time 2023-04-01 22:43:00 -04:00
Maya Warrier
f59f73c4da Disable simd-related warnings 2023-04-01 04:09:00 -04:00
Maya Warrier
20f3870361 Fixes 2023-03-30 04:58:22 -04:00
Maya Warrier
2b118c843a Experimental support for char_t types 2023-03-30 04:48:18 -04:00
Maya Warrier
b6acf38a2e Fix bugs 2023-03-29 18:42:24 -04:00
Maya Warrier
3cafcca2ff Add support for json parsing rules and integers 2023-03-29 02:14:12 -04:00
Maya Warrier
8f94758c78 Expose parsed string (before computation) so it can be reused 2023-03-27 22:50:21 -04:00
Lenard Szolnoki
82ee3b1b5f Constexpr parse_number_string 2023-03-04 17:18:25 +00:00
Lenard Szolnoki
be6084863c Low-risk C++14 constexpr functions 2023-02-25 10:50:45 +00:00
Daniel Lemire
3e2da540ef Support rccpfastfloat. 2023-01-19 20:28:10 -05:00
Daniel Lemire
6484c73696 Trimming out one eight-digit optimization. 2022-11-15 11:38:06 -05:00
Daniel Lemire
d148241404 Removing CXX20 support 2021-09-20 09:49:23 -04:00
Alex Huszagh
fc0c8680a5 Implement the big-integer arithmetic algorithm.
Replaces the existing decimal implementation, for substantial
performance improvements with near-halfway cases. This is especially
fast with a large number of digits.

**Big Integer Implementation**

A small subset of big-integer arithmetic has been added, with the
`bigint` struct. It uses a stack-allocated vector with enough bits to
store the float with the large number of significant digits. This is
log2(10^(769 + 342)), to account for the largest possible magnitude
exponent, and number of digits (3600 bits), and then rounded up to 4k bits.

The limb size is determined by the architecture: most 64-bit
architectures have efficient 128-bit multiplication, either by a single
hardware instruction or 2 native multiplications for the high and low
bits. This includes x86_64, mips64, s390x, aarch64, powerpc64, riscv64,
and the only known exception is sparcv8 and sparcv9. Therefore, we
define a limb size of 64-bits on 64-bit architectures except SPARC,
otherwise we fallback to 32-bit limbs.

A simple stackvector is used, which just has operations to add elements,
index, and truncate the vector.

`bigint` is then just a wrapper around this, with methods for
big-integer arithmetic. For our algorithms, we just need multiplication
by a power (x * b^N), multiplication by a bigint or scalar value, and
addition by a bigint or scalar value. Scalar addition and multiplication
uses compiler extensions when possible (__builtin_add_overflow and
__uint128_t), if not, then we implement simple logic shown to optimize
well on MSVC. Big-integer multiplication is done via grade school
multiplication, which is more efficient than any asymptotically faster
algorithms. Multiplication by a power is then done via bitshifts for
powers-of-two, and by iterative multiplications of a large and then
scalar value for powers-of-5.

**compute_float**

Compute float has been slightly modified so if the algorithm cannot
round correctly, it returns a normalized, extended-precision adjusted
mantissa with the power2 shifted by INT16_MIN so the exponent is always
negative. `compute_error` and `compute_error_scaled` have been added.

**Digit Optimiations**

To improve performance for numbers with many digits,
`parse_eight_digits_unrolled` is used for both integers and fractions,
and uses a while loop than two nested if statements. This adds no
noticeable performance cost for common floats, but dramatically improves
performance for numbers with large digits (without these optimizations,
~65% of the total runtime cost is in parse_number_string).

**Parsed Number**

Two fields have been added to `parsed_number_string`, which contains a
slice of the integer and fraction digits. This is extremely cheap, since
the work is already done, and the strings are pre-tokenized during
parsing. This allows us on overflow to re-parse these tokenized strings,
without checking if each character is an integer. Likewise, for the
big-integer algorithms, we can merely re-parse the pre-tokenized
strings.

**Slow Algorithm**

The new algorithm is `digit_comp`, which takes the parsed number string
and the `adjusted_mantissa` from `compute_float`. The significant digits
are parsed into a big integer, and the exponent relative to the
significant digits is calculated. If the exponent is >= 0, we use
`positive_digit_comp`, otherwise, we use `negative_digit_comp`.

`positive_digit_comp` is quite simple: we scale the significant digits
to the exponent, and then we get the high 64-bits for the native float,
determine if any lower bits were truncated, and use that to direct
rounding.

`negative_digit_comp` is a little more complex, but also quite trivial:
we use the parsed significant digits as the real digits, and calculate
the theoretical digits from `b+h`, the halfway point between `b` and
`b+u`, the next-positive float. To get `b`, we round the adjusted
mantissa down, create an extended-precision representation, and
calculate the halfway point. We now have a base-10 exponent for the real
digits, and a base-2 exponent for the theoretical digits. We scale these
two to the same exponent by multiplying the theoretixal digits by
`5**-real_exp`. We then get the base-2 exponent as `theor_exp -
real_exp`, and if this is positive, we multipy the theoretical digits by
it, otherwise, we multiply the real digits by it. Now, both are scaled
to the same magnitude, and we simply compare the digits in the big
integer, and use that to direct rounding.

**Rust-Isms**

A few Rust-isms have been added, since it simplifies logic assertions.
These can be trivially removed or reworked, as needed.

- a `slice` type has been added, which is a pointer and length.
- `FASTFLOAT_ASSERT`, `FASTFLOAT_DEBUG_ASSERT`, and `FASTFLOAT_TRY` have
  been added
  - `FASTFLOAT_ASSERT` aborts, even in release builds, if the condition
    fails.
  - `FASTFLOAT_DEBUG_ASSERT` defaults to `assert`, for logic errors.
  - `FASTFLOAT_TRY` is like a Rust `Option` type, which propagates
    errors.

Specifically, `FASTFLOAT_TRY` is useful in combination with
`FASTFLOAT_ASSERT` to ensure there are no memory corruption errors
possible in the big-integer arithmetic. Although the `bigint` type
ensures we have enough storage for all valid floats, memory issues are
quite a severe class of vulnerabilities, and due to the low performance
cost of checks, we abort if we would have out-of-bounds writes. This can
only occur when we are adding items to the vector, which is a very small
number of steps. Therefore, we abort if our memory safety guarantees
ever fail. lexical has never aborted, so it's unlikely we will ever fail
these guarantees.
2021-09-10 18:53:53 -05:00
Jonas Rahlf
162a37b25a remove cstdio includes, remove cassert include, add asthetic newlines 2021-09-05 23:13:41 +02:00
Jonas Rahlf
e5d5e576a6 use #if defined __has_include properly 2021-09-02 22:22:03 +02:00
Jonas Rahlf
b17eafd06f chnage compiler check for bit_cast so it compiles with older compilers 2021-09-02 22:00:57 +02:00
Jonas Rahlf
d8ee88e7f6 initial version with working constexpr for c++20 compliant compilers 2021-09-01 00:52:25 +02:00
Alex Huszagh
3e74ed313a Fixes #94, with unspecified behavior in pointer comparisons. 2021-08-21 13:07:57 -05:00
Antoine Pitrou
3881ea6937 Issue #90: accept custom decimal point 2021-08-03 10:44:24 +02:00
Alex Huszagh
49ca5d855e Added 8-digit optimizations to big endian. 2021-05-23 21:47:40 -05:00
Eugene Golushkov
87e5a95585 Prevent fast_float::from_chars from parsing whitespaces and leading '+' sign, similar to MSVC and integer LLVM std::from_chars behavior. See C++17 20.19.3.(7.1) and http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0067r5.html 2021-03-04 20:21:45 +02:00
Daniel Lemire
716c87067e Simplifying fix. 2021-01-11 11:24:54 -05:00
Daniel Lemire
c5adf9e4a5 Fix truncate flag. 2021-01-11 11:22:00 -05:00
Daniel Lemire
cf1a4ec2f5 Further tweaking. 2021-01-08 10:09:26 -05:00
Daniel Lemire
a27fcc230d This should be mostly correct. 2021-01-07 17:46:47 -05:00
Daniel Lemire
51b27a3324 Trying alternate handling of overflows. 2020-12-31 11:14:48 -05:00
Daniel Lemire
7ef9d9b7d2 Tweaking cast. 2020-11-23 15:04:48 -05:00
Daniel Lemire
86bc73af9e Need explicit cast. 2020-11-23 13:53:50 -05:00
Daniel Lemire
426dd2a4a6 Merge branch 'main' into dlemire/aqrit_magic 2020-11-23 13:48:06 -05:00
Daniel Lemire
7ff364b59a This might add support for big endian systems (untested). 2020-11-16 12:04:57 -05:00
Daniel Lemire
e79741ede2 Minor cleaning. 2020-11-12 22:35:32 -05:00
Daniel Lemire
1e92d59997 Sign conversion pedantry. 2020-11-11 20:43:36 -05:00
Daniel Lemire
41ee34701b Magical optimizations from @aqrit 2020-11-09 19:06:51 -05:00
Daniel Lemire
cd8f09885b Removing possibly misleading comment. 2020-11-09 18:34:46 -05:00
Daniel Lemire
9a424bde1e Reverting a microoptimization. 2020-11-09 09:51:35 -05:00
Maksim Kita
68633178d5 Fixed odr with inlining and anonymous namespace 2020-11-08 15:20:11 +03:00
Daniel Lemire
693fa66fa4 More testing. 2020-11-06 17:28:55 -05:00
Daniel Lemire
4e076c70f9
Merge pull request #18 from lemire/dlemire/minor_styling
Fixing minor style issues.
2020-11-05 19:04:59 -05:00
Daniel Lemire
741e68ce61 Fixes https://github.com/lemire/fast_float/issues/19 2020-11-05 16:32:19 -05:00
Daniel Lemire
9b102a95ab Actually, 19. 2020-11-02 21:46:31 -05:00
Daniel Lemire
83154a24ad Fixing minor style issues. 2020-11-02 14:55:17 -05:00
Daniel Lemire
022118e5d0 Minor logical fix. 2020-10-30 14:56:15 -04:00
Daniel Lemire
f7d3cdc426 Minor fix. 2020-10-29 16:40:20 -04:00
Daniel Lemire
47d3d443d8 Minor fix. 2020-10-27 21:26:11 -04:00
Daniel Lemire
c53bfc4176 Minor tweak. 2020-10-27 20:10:42 -04:00
Daniel Lemire
fa1242c97e Fixing issue 8 2020-10-27 15:41:31 -04:00
Daniel Lemire
1701be0224 First commit 2020-10-19 12:38:13 -04:00