fastfloat / fast_float

Fast and exact implementation of the C++ from_chars functions for number types: 4x to 10x faster than strtod, part of GCC 12, Chromium and WebKit/Safari
Apache License 2.0
1.35k stars 123 forks source link

std::float16_t/std::bfloat16_t support #148

Open jakubjelinek opened 1 year ago

jakubjelinek commented 1 year ago

Just FYI, in GCC we've added support for from_chars for the C++23 std::{,b}float16_t support in https://gcc.gnu.org/r13-3592 . We've done it by using an artificial wrapper class for each of those and by using float as container type on the library boundary so that we don't need to change library ABI whenever some new architecture decides to start supporting those formats. But if you'd want to have direct support for these types in upstream, it could be a matter of:

#if defined(__STDCPP_FLOAT16_T__) || defined(__STDCPP_BFLOAT16_T__)
#include <stdatomic>
#endif
...

#ifdef __STDCPP_FLOAT16_T__
  template<>
  constexpr int
  binary_format<std::float16_t>::mantissa_explicit_bits()
  { return 10; }

  template<>
  constexpr int
  binary_format<std::float16_t>::max_exponent_round_to_even()
  { return 5; }

  template<>
  constexpr int
  binary_format<std::float16_t>::min_exponent_round_to_even()
  { return -22; }

  template<>
  constexpr int
  binary_format<std::float16_t>::minimum_exponent()
  { return -15; }

  template<>
  constexpr int
  binary_format<std::float16_t>::infinite_power()
  { return 0x1F; }

  template<>
  constexpr int
  binary_format<std::float16_t>::sign_index()
  { return 15; }

  template<>
  constexpr int
  binary_format<std::float16_t>::largest_power_of_ten()
  { return 4; }

  template<>
  constexpr int
  binary_format<std::float16_t>::smallest_power_of_ten()
  { return -27; }

  template<>
  constexpr size_t
  binary_format<std::float16_t>::max_digits()
  { return 22; }
#endif

#ifdef __STDCPP_BFLOAT16_T__
  template<>
  constexpr int
  binary_format<std::bfloat16_t>::mantissa_explicit_bits()
  { return 7; }

  template<>
  constexpr int
  binary_format<std::bfloat16_t>::max_exponent_round_to_even()
  { return 3; }

  template<>
  constexpr int
  binary_format<std::bfloat16_t>::min_exponent_round_to_even()
  { return -24; }

  template<>
  constexpr int
  binary_format<std::bfloat16_t>::minimum_exponent()
  { return -127; }

  template<>
  constexpr int
  binary_format<std::bfloat16_t>::infinite_power()
  { return 0xFF; }

  template<>
  constexpr int
  binary_format<std::bfloat16_t>::sign_index()
  { return 15; }

  template<>
  constexpr int
  binary_format<std::bfloat16_t>::largest_power_of_ten()
  { return 38; }

  template<>
  constexpr int
  binary_format<std::bfloat16_t>::smallest_power_of_ten()
  { return -60; }

  template<>
  constexpr size_t
  binary_format<std::bfloat16_t>::max_digits()
  { return 98; }
#endif

And add specializations for {min,max}_exponent_fast_path, max_mantissa_fast_path and exact_power_of_ten (those we didn't need for GCC because we've omitted for these the Clinger's fast path).

lemire commented 1 year ago

This is very reasonable and we will add (and test) such support as soon as mainstream compiler releases support C++23. At the moment, it looks like GCC 13 is not yet released.

jwakely commented 1 year ago

That's right, new GCC versions are usually released around April or May each year.

lemire commented 2 months ago

GCC 13 is now widely available.

lemire commented 2 weeks ago

I have begun the work but I am not yet getting a clean build.