Provide fast bfloat16 conversion routines as user-registerable examples via H5Tregister
- Dominant language
- C
- Stars
- 988
- Forks
- 355
- Avg merge
- 4d 2h
- Merged PRs (30d)
- 12
Description
HDF5's general floating-point conversion loop (`H5T__conv_f_f_loop`) handles arbitrary format pairs, byte order, and per-element exception callbacks. That generality is valuable, but it prevents auto-vectorization: the loop processes one element at a time regardless of target ISA.
For bfloat16, which is now a first-class storage type in HDF5, this is a significant performance problem. Workloads that stage data between HDF5 and AI/ML frameworks must convert large arrays on every read or write, and at current general-loop throughput the conversion becomes the dominant cost.
**Approach**
Rather than baking fast-path converters into the library core, provide reference implementations that applications can register via `H5Tregister`. bfloat16 is simply the top 16 bits of an IEEE-754 float32, so the four conversions (double/float to/from bfloat16) can each be written as a short loop over standard integer types (`uint32_t`, `uint16_t`) with no compiler-native bfloat16 type required and no SIMD library needed. GCC and Clang both auto-vectorize this to wide SIMD instructions, producing large speedups over the general loop in practice.
This approach lets the compiler and FPU handle the conversion details as compiler bfloat16 type support matures (e.g. `__bf16` in GCC 13+ and Clang 17+), while still giving users access to fast conversions today without waiting for the ecosystem to settle.
**Design concern: double rounding**
When converting double to bfloat16 via an intermediate float32 cast, two correctly-rounded steps are not always equivalent to one. The intermediate cast may round at the float32 boundary, and the subsequent narrowing may round again at the bfloat16 boundary, occasionally producing a result that differs by 1 ULP from a single-step rounding. Any reference implementation must document this trade-off and decide whether to accept it, preserving vectorization, or avoid it with a more complex single-step bit-level path.
Contributor guide
Assessment
This issue has not been assessed yet.