Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jakubjelinek opened this issue Nov 1, 2022 · 2 comments
Open

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

jakubjelinek opened this issue Nov 1, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@jakubjelinek
Copy link

jakubjelinek commented Nov 1, 2022

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
Copy link
Member

lemire commented Dec 1, 2022

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.

@lemire lemire added the enhancement New feature or request label Dec 1, 2022
@jwakely
Copy link
Contributor

jwakely commented Dec 1, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants