Open
Description
Description
When compiling with clang++ at -O0, a float -> __bf16 -> float conversion using a templated static_cast chain produces an incorrect result (0.0f instead of the expected value). This issue disappears when optimization is enabled (-O1 or higher).
Reproduction:
Compile the following code with: clang++ filename.cpp
#include <iostream>
template<typename D, typename S> D fp_cast(S x) {
return static_cast<D>(x);
}
float do_cast(float x) {
return fp_cast<float>(fp_cast<__bf16>(x));
}
int main() {
float t0 = 3.f;
float t2 = do_cast(t0);
std::cout << t2 << std::endl;
return 0;
}
Expected behavior: do_cast(3.f)
should return 3.0f
.
Actual behavior: It returns 0.0f
.
Environment
Ubuntu clang version 20.1.0 (++20250221063304+dc1bd6a8fa6a-1~exp1~20250221183420.63)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-20/bin