Skip to content

Commit 514708b

Browse files
[SYCL] Fix float-to-half conversion for minimum subnormal on host (#7401)
Currently when converting a floating point value representing the minimum subnormal value for half to a half on host will result in 0 rather than the expected minimum value. This is due to a faulty check for the minimum supported single-precision exponent when converting. This commit fixes this check. Signed-off-by: Larsen, Steffen <[email protected]>
1 parent 45d516c commit 514708b

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

sycl/include/sycl/half_type.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ inline __SYCL_CONSTEXPR_HALF uint16_t float2Half(const float &Val) {
7878
// Tie to even.
7979
else if (roundBits == halfway)
8080
Frac16 += Frac16 & 1;
81-
} else if (__builtin_expect(Exp32Diff > -24, 0)) {
81+
} else if (__builtin_expect(Exp32Diff > -25, 0)) {
8282
// subnormals
8383
Frac16 = (Frac32 | (uint32_t(1) << 23)) >> (-Exp32Diff - 1);
8484
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %clangxx -fsycl %s -o %t.out
2+
// RUN: %t.out
3+
//
4+
// Checks that sycl::half on host can correctly cast its minimum subnormal value
5+
// to and from a floating point value.
6+
7+
#include <sycl/sycl.hpp>
8+
9+
int main() {
10+
sycl::half SubnormalMin =
11+
sycl::bit_cast<sycl::half>((uint16_t)0b0000000000000001u);
12+
sycl::half ConvertedSubnormalMin =
13+
static_cast<sycl::half>(static_cast<float>(SubnormalMin));
14+
15+
if (SubnormalMin != ConvertedSubnormalMin) {
16+
std::cout << "Failed! (0x" << std::hex
17+
<< sycl::bit_cast<uint16_t>(SubnormalMin) << " != 0x"
18+
<< sycl::bit_cast<uint16_t>(ConvertedSubnormalMin) << ")"
19+
<< std::endl;
20+
return 1;
21+
}
22+
23+
std::cout << "Passed!" << std::endl;
24+
return 0;
25+
}

0 commit comments

Comments
 (0)