29 Commits

Author SHA1 Message Date
Daniel Lemire
ef9a60d516 update CI to ubuntu 24 + safe a shift value to a variable (for elegance) 2024-07-22 09:36:14 -04:00
Daniel Lemire
ca43e6722e We need to update some of our exhaustive tests to the new API 2023-04-02 17:43:17 -04:00
Lenard Szolnoki
e4d4e43b21 Constexpr from_chars 2023-03-04 20:53:43 +00:00
Daniel Lemire
b1d27734c5
Merge pull request #173 from fastfloat/nofallbacknecessary
We do not need a fallback.
2023-02-28 17:19:57 -05:00
Lenard Szolnoki
be6084863c Low-risk C++14 constexpr functions 2023-02-25 10:50:45 +00:00
Daniel Lemire
39e04ea766 We do not need a fallback. 2023-01-27 20:59:56 -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
d8ee88e7f6 initial version with working constexpr for c++20 compliant compilers 2021-09-01 00:52:25 +02:00
Daniel Lemire
a8d49f40f0 This uses the template trick to ensure we get only one definition 2021-04-07 13:34:53 -04:00
Marcin Wojdyr
f21b2f24cb change anonymous namespace to namespace detail (#54) 2021-04-07 15:17:37 +02:00
Joao Paulo Magalhaes
e65f977135 fix: never include iostream unless it's absolutely necessary 2020-11-24 00:24:17 +00:00
Daniel Lemire
7bf5db7216 Tuning. 2020-11-20 17:05:06 -05:00
Daniel Lemire
8fde4bad4e Adding a guard. 2020-11-20 16:09:53 -05:00
Daniel Lemire
d521ddf7f7 Let us adjust the powers instead. 2020-11-19 18:15:42 -05:00
Daniel Lemire
1afba556e3 Extending the fast path. 2020-11-17 21:55:01 -05:00
Daniel Lemire
1e92d59997 Sign conversion pedantry. 2020-11-11 20:43:36 -05:00
Daniel Lemire
9c5dac3705 Build with pedantic flags under GNU GCC. 2020-11-11 20:17:18 -05:00
Daniel Lemire
689fba95b5 Minor simplification. 2020-11-10 20:38:23 -05:00
Daniel Lemire
51f813a987 Tweaking. 2020-11-07 16:28:49 -05:00
Daniel Lemire
e3fd438c64 Tweaking. 2020-11-07 16:27:53 -05:00
Daniel Lemire
741e68ce61 Fixes https://github.com/lemire/fast_float/issues/19 2020-11-05 16:32:19 -05:00
Daniel Lemire
288efd35eb Minor cleaning. 2020-11-02 21:42:01 -05:00
Daniel Lemire
71e0355de8 fixing a comment. 2020-10-29 16:54:35 -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
05ad45dfb5 Let us try the long path. 2020-10-27 18:26:16 -04:00
Daniel Lemire
1701be0224 First commit 2020-10-19 12:38:13 -04:00