Skip to content

Commit 4cbd459

Browse files
authored
[SYCL] Change sycl host-side float2Half mantissa rounding (#7029)
Change host-side mantissa rounding to make host and device consistent. Otherwise, there will be result mismatch errors in half calculation of host and device.
1 parent 7ed3995 commit 4cbd459

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

sycl/include/sycl/half_type.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,14 @@ inline __SYCL_CONSTEXPR_HALF uint16_t float2Half(const float &Val) {
7070
Frac16 = Frac32 >> 13;
7171
// Round the mantissa as given in OpenCL spec section : 6.1.1.1 The half
7272
// data type.
73-
if (Frac32 >> 12 & 0x01)
73+
// Round to nearest.
74+
uint32_t roundBits = Frac32 & 0x1fff;
75+
uint32_t halfway = 0x1000;
76+
if (roundBits > halfway)
7477
Frac16 += 1;
78+
// Tie to even.
79+
else if (roundBits == halfway)
80+
Frac16 += Frac16 & 1;
7581
} else if (__builtin_expect(Exp32Diff > -24, 0)) {
7682
// subnormals
7783
Frac16 = (Frac32 | (uint32_t(1) << 23)) >> (-Exp32Diff - 1);

0 commit comments

Comments
 (0)