Skip to content

Add host-side bfloat16 helpers for platforms without std::bfloat16_t#3110

Open
thomthehound wants to merge 7 commits into
Xilinx:mainfrom
thomthehound:bfloat16-helpers
Open

Add host-side bfloat16 helpers for platforms without std::bfloat16_t#3110
thomthehound wants to merge 7 commits into
Xilinx:mainfrom
thomthehound:bfloat16-helpers

Conversation

@thomthehound
Copy link
Copy Markdown
Contributor

@thomthehound thomthehound commented May 23, 2026

Under the C++23 standard, std::bfloat16_t support is optional. The current MSVC toolchain has chosen not to exercise that option, while the Linux toolchain does. Therefore, host-side example code that manipulates bfloat16 data cannot use std::bfloat16_t safely when compiled with MSVC, as is the intended route on native Windows.

This patch adds test_utils::bfloat16_t (and related helpers) to test_utils.h. When the standard type is available, test_utils::bfloat16_t resolves directly to std::bfloat16_t, preserving native execution. Otherwise, it uses std::uint16_t storage and performs host-side conversion/math through float (rounding back to 16 bits by the nearest-even method).

In the trivial case, the type selection works as so:

#if defined(__STDCPP_BFLOAT16_T__)
using bfloat16_t = std::bfloat16_t;
#else
using bfloat16_t = std::uint16_t;
#endif

These changes are host-side only; device code is unaffected. The examples have been updated to use test_utils::bfloat16_t consistently for host-side bfloat16 data (including those that are not obviously Windows-portable) so there are not two competing vocabularies.

This workaround is not intended to permanently solve host-side bfloat16 portability for all user code. It is intended to draw attention to this limitation, and to prepare the programming examples for cross-platform conversion. In the future, it may be reasonable to provide a better API boundary for this, but doing so would create a new public convention and is therefore a separate consideration.

Signed-off-by: thomthehound <thomthehound@gmail.com>
Signed-off-by: thomthehound <thomthehound@gmail.com>
Signed-off-by: thomthehound <thomthehound@gmail.com>
Signed-off-by: thomthehound <thomthehound@gmail.com>
Signed-off-by: thomthehound <thomthehound@gmail.com>
Signed-off-by: thomthehound <thomthehound@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant