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 · 9 comments
Open

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

jakubjelinek opened this issue Nov 1, 2022 · 9 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

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.

@lemire
Copy link
Member

lemire commented Jun 27, 2024

GCC 13 is now widely available.

@lemire
Copy link
Member

lemire commented Aug 24, 2024

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

@dalle
Copy link
Collaborator

dalle commented Dec 2, 2024

I have taken your branch and rebased it and it looks good. Just some tests left. Maybe

@lemire
Copy link
Member

lemire commented Dec 2, 2024

@dalle I definitively could use some help with

#264

It would be a big deal to add this support.

@dalle
Copy link
Collaborator

dalle commented Dec 4, 2024

I didn't understand how to incorporate #264 to use a rebased branch with your initial work. Anyway, I pushed and it's looking good haven't checked why though. I built and tested basictest.cpp using g++ -std=c++23 (version 14.2.0 (MSYS2)) locally, there it ran with success:

system info:
FASTFLOAT_CONSTEXPR_TESTS='1'
FASTFLOAT_64BIT_LIMB='1'
little endian
FASTFLOAT_64BIT='1'
FLT_EVAL_METHOD='0'
_WIN32='1'
_WIN64='1'
fegetround() = FE_TONEAREST

...

[doctest] test cases:   27 |   27 passed | 0 failed | 0 skipped
[doctest] assertions: 8993 | 8993 passed | 0 failed |
[doctest] Status: SUCCESS!

The tests in basictest.cpp may have way too high precision.

One test is failing:

Checking data/data/ulfjack-ryu.txt
fesetround to FE_UPWARD
bad 16: 0000 33000000 3E60000000000000 2.98023223876953125E-8
parsed as 0x1p-24 (5.9604644775390625e-08)
as raw uint16_t, parsed = 1, expected = 0

@lemire
Copy link
Member

lemire commented Dec 4, 2024

@dalle I don't trust the testing code I wrote for 16-bit floats. :-) We need to examine these tests manually and make sure it makes sense. It is likely incomplete as well.

(Thankfully, it should not be enormously difficult to get it right since GCC already supports those on top of fast_float.)

@lemire
Copy link
Member

lemire commented Dec 4, 2024

@dalle Feel free to open a new PR even if it is only partial progress.

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

No branches or pull requests

4 participants