Skip to content

[SYCL] Fix float-to-half conversion for minimum subnormal on host #7401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sycl/include/sycl/half_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ inline __SYCL_CONSTEXPR_HALF uint16_t float2Half(const float &Val) {
// Tie to even.
else if (roundBits == halfway)
Frac16 += Frac16 & 1;
} else if (__builtin_expect(Exp32Diff > -24, 0)) {
} else if (__builtin_expect(Exp32Diff > -25, 0)) {
// subnormals
Frac16 = (Frac32 | (uint32_t(1) << 23)) >> (-Exp32Diff - 1);
}
Expand Down
25 changes: 25 additions & 0 deletions sycl/test/regression/half_host_subnormal_min.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %clangxx -fsycl %s -o %t.out
// RUN: %t.out
//
// Checks that sycl::half on host can correctly cast its minimum subnormal value
// to and from a floating point value.

#include <sycl/sycl.hpp>

int main() {
sycl::half SubnormalMin =
sycl::bit_cast<sycl::half>((uint16_t)0b0000000000000001u);
sycl::half ConvertedSubnormalMin =
static_cast<sycl::half>(static_cast<float>(SubnormalMin));

if (SubnormalMin != ConvertedSubnormalMin) {
std::cout << "Failed! (0x" << std::hex
<< sycl::bit_cast<uint16_t>(SubnormalMin) << " != 0x"
<< sycl::bit_cast<uint16_t>(ConvertedSubnormalMin) << ")"
<< std::endl;
return 1;
}

std::cout << "Passed!" << std::endl;
return 0;
}