SWDEV-424956 - Fix OpenCL printf bug while printing vectors of half type

OpenCL printf handling did not process vector of half precision floats properly
 (mainly because compiler packs 2 halfs into a dword and runtime failed to extract the
 individual parts).

 This patch fixes the issue.

Change-Id: Ia1f15ccfb5db52b71c43cfd588dd38f551ee5277


[ROCm/clr commit: 6f390f5af9]
Цей коміт міститься в:
Vikram
2024-01-25 12:58:41 +00:00
зафіксовано Vikram Hegde
джерело 609893e98f
коміт 4b5304adc1
3 змінених файлів з 56 додано та 2 видалено
+42
Переглянути файл
@@ -238,6 +238,48 @@ template <typename lambda> class ScopeGuard {
#define MAKE_SCOPE_GUARD(name, ...) \
MAKE_SCOPE_GUARD_HELPER(XCONCAT(scopeGuardLambda, __COUNTER__), name, __VA_ARGS__)
// utility function to convert half precision to float to a
// single precision value.
inline void half2float(uint16_t Val, uint32_t *Res) {
constexpr uint32_t halfExpoentMask = 0x7c00;
constexpr uint32_t halfFractionMask = 0x03ff;
constexpr uint32_t floatExponentBias = 127;
constexpr uint32_t halfExponentBias = 15;
constexpr uint32_t signBitShift = 16;
constexpr uint32_t floatExponentShift = 23;
uint32_t signBit = ((uint32_t)(Val & 0x8000)) << signBitShift;
uint32_t exponent = (Val & halfExpoentMask) >> 10;
uint32_t fraction = ((uint32_t)(Val & halfFractionMask))
<< 13; // Aligning half fraction to float
// Handling special cases
if (exponent == 0x1f) { // NaN or Infinity
// When all exponent bits are 1, the value is either Infinity or NaN
// For NaN, the fraction part should also be non-zero.
*Res = signBit | 0x7f800000 |
fraction; // setting exponent to all 1's and keeping the fraction
return;
} else if (exponent == 0) { // Subnormal numbers or zero
if (fraction == 0) {
*Res = signBit; // Plus or minus zero
return;
} else {
// Normalize subnormal number
while ((fraction & (1 << 23)) == 0) {
fraction <<= 1;
exponent--;
}
exponent++;
fraction &=
~(1 << 23); // Remove leading 1 (implicit for normalized numbers)
}
}
uint32_t floatExponent =
((exponent + floatExponentBias - halfExponentBias) & 0xff)
<< floatExponentShift;
*Res = signBit | floatExponent | fraction;
}
/*@}*/} // namespace amd
#endif /*UTIL_HPP_*/