Skip to content
Merged
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
16 changes: 9 additions & 7 deletions sycl/include/sycl/ext/oneapi/bfloat16.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,25 +407,25 @@ inline bfloat16 getBFloat16FromDoubleWithRTE(const double &d) {
// handling +/-infinity and NAN for double input
if (fp64_exp == 0x7FF) {
if (!fp64_mant)
return bf16_sign ? 0xFF80 : 0x7F80;
return bit_cast<bfloat16, uint16_t>(bf16_sign ? 0xFF80 : 0x7F80);

// returns a quiet NaN
return 0x7FC0;
return bit_cast<bfloat16, uint16_t>(0x7FC0);
}

// Subnormal double precision is converted to 0
if (fp64_exp == 0)
return bf16_sign ? 0x8000 : 0x0;
return bit_cast<bfloat16, uint16_t>(bf16_sign ? 0x8000 : 0x0);

fp64_exp -= 1023;

// handling overflow, convert to +/-infinity
if (static_cast<int16_t>(fp64_exp) > 127)
return bf16_sign ? 0xFF80 : 0x7F80;
return bit_cast<bfloat16, uint16_t>(bf16_sign ? 0xFF80 : 0x7F80);

// handling underflow
if (static_cast<int16_t>(fp64_exp) < -133)
return bf16_sign ? 0x8000 : 0x0;
return bit_cast<bfloat16, uint16_t>(bf16_sign ? 0x8000 : 0x0);

//-133 <= fp64_exp <= 127, 1.signicand * 2^fp64_exp
// For these numbers, they are NOT subnormal double-precision numbers but
Expand All @@ -444,7 +444,8 @@ inline bfloat16 getBFloat16FromDoubleWithRTE(const double &d) {
bf16_mant = 0;
fp64_exp = 1;
}
return (bf16_sign << 15) | (fp64_exp << 7) | bf16_mant;
return bit_cast<bfloat16, uint16_t>((bf16_sign << 15) | (fp64_exp << 7) |
bf16_mant);
}

// For normal value, discard 45 bits from mantissa
Expand All @@ -462,7 +463,8 @@ inline bfloat16 getBFloat16FromDoubleWithRTE(const double &d) {
}
fp64_exp += 127;

return (bf16_sign << 15) | (fp64_exp << 7) | bf16_mant;
return bit_cast<bfloat16, uint16_t>((bf16_sign << 15) | (fp64_exp << 7) |
bf16_mant);
}

// Function to get the most significant bit position of a number.
Expand Down