Open johnplatts opened 1 year ago
Interesting, thanks for proposing this. I agree it would be useful to have support for Set() of bfloat16_t. Currently tests use float constants that we then DemoteTo, which is not very convenient.
It's great that your proposal still works in C++11, that is important for us.
Should we change hwy::bfloat16_t to a typedef to std::bfloat16_t (where available) instead of using a wrapper? That is also the approach we take with float16_t, though last I checked that only worked reliably on Arm and RVV.
If you'd like to start preparing a pull request, let's first change the constexpr+#if
to a HWY_CONSTEXPR_BIT_CAST
and HWY_CONSTEXPR2
(or some better name), which either expands to constexpr or nothing depending on compiler capabilities. This would shorten the spots where it is used, and also help with code folding in IDEs because they might not be able to fold past an #if
.
For the record, my suggestion to change hwy::bfloat16_t to a typedef will break ABI compatibility, so let's not do that :) But I like your idea of adding a ctor that takes std::bfloat16_t.
The upcoming C++23 standard adds support for the std::float16_t and std::bfloat16_t types, and the upcoming GCC 13 release will have support for the std::float16_t and std::bfloat16_t types with the
-std=c++23
option.The hwy::bfloat16_t type should also be updated to allow for implicit conversions to/from std::bfloat16_t in C++23 or later mode on platforms that support the std::bfloat16_t type.
Here is how support for conversions between std::bfloat16_t and hwy::bfloat16_t could be implemented:
The updated implementation of hwy::bfloat16_t above will compile in C++11 or later mode, including with older C++11 compilers such as g++ 4.7.1 or clang 3.4.1.
The updated implementation of hwy::bfloat16_t also adds a
hwy::bfloat16_t(SpecialFloatFromBitsTag, uint16_t)
constructor to distinguish between constructing a hwy::bfloat16_t from its bit representation and a conversion to hwy::bfloat16_t from floating-point types.The behavior of code such as the function below will change with the updated hwy::bfloat16 implementation above:
SomeFuncThatReturnsABF16 will return a hwy::bfloat16_t with the value of 2.75 with the current implementation (which initializes a hwy::bfloat16_t from the BF16 bitwise representation of 2.75), whereas SomeFuncThatReturnsABF16 will return a hwy::bfloat16_t with the value of 16432.0 with the updated implementation of hwy::bfloat16_t (which converts the integer value of 16432.0 to a floating point).
The updated hwy::bfloat16_t implementation above will allow code such as the following in C++23 mode with GCC 13 or later (with updates to the Set function):