Unverified Commit 901e5f15 authored by Juan Manuel Martinez Caamaño's avatar Juan Manuel Martinez Caamaño Committed by GitHub
Browse files

Remove reinterpret_cast uses that result in undefined behaviour. (#1445)

* Remove reinterpret_cast uses that result in undefined behaviour. Use a bitcast instead.

See https://en.cppreference.com/w/cpp/language/reinterpret_cast#Type_accessibility



Closes #1439

* fix clang format

---------
Co-authored-by: default avatarillsilin <Illia.Silin@amd.com>
parent 5df10432
...@@ -44,7 +44,7 @@ __host__ __device__ Y run_cast_to_f8(X x, uint32_t rng) ...@@ -44,7 +44,7 @@ __host__ __device__ Y run_cast_to_f8(X x, uint32_t rng)
// convert to bitwise // convert to bitwise
using T_bitwise = typename NumericUtils<X>::bitwise_type; using T_bitwise = typename NumericUtils<X>::bitwise_type;
T_bitwise x_bitwise = *(reinterpret_cast<T_bitwise*>(&x)); T_bitwise x_bitwise = bit_cast<T_bitwise>(x);
// unpack the input, depends on datatype // unpack the input, depends on datatype
head = x_bitwise & NumericUtils<X>::head_mask; head = x_bitwise & NumericUtils<X>::head_mask;
...@@ -196,7 +196,6 @@ __host__ __device__ Y run_cast_from_f8(X x) ...@@ -196,7 +196,6 @@ __host__ __device__ Y run_cast_from_f8(X x)
// prepare the codes // prepare the codes
constexpr X nan_code = 0x80; constexpr X nan_code = 0x80;
Y Inf, NegInf, NaN, Neg0;
using T_bitwise = typename NumericUtils<Y>::bitwise_type; using T_bitwise = typename NumericUtils<Y>::bitwise_type;
constexpr T_bitwise Inf_bitwise = NumericUtils<Y>::Inf; constexpr T_bitwise Inf_bitwise = NumericUtils<Y>::Inf;
...@@ -204,10 +203,10 @@ __host__ __device__ Y run_cast_from_f8(X x) ...@@ -204,10 +203,10 @@ __host__ __device__ Y run_cast_from_f8(X x)
constexpr T_bitwise NaN_bitwise = NumericUtils<Y>::NaN; constexpr T_bitwise NaN_bitwise = NumericUtils<Y>::NaN;
constexpr T_bitwise Neg0_bitwise = NumericUtils<Y>::Neg0; constexpr T_bitwise Neg0_bitwise = NumericUtils<Y>::Neg0;
Inf = *(reinterpret_cast<const Y*>(&Inf_bitwise)); constexpr Y Inf = bit_cast<Y>(Inf_bitwise);
NegInf = *(reinterpret_cast<const Y*>(&NegInf_bitwise)); constexpr Y NegInf = bit_cast<Y>(NegInf_bitwise);
NaN = *(reinterpret_cast<const Y*>(&NaN_bitwise)); constexpr Y NaN = bit_cast<Y>(NaN_bitwise);
Neg0 = *(reinterpret_cast<const Y*>(&Neg0_bitwise)); constexpr Y Neg0 = bit_cast<Y>(Neg0_bitwise);
// check if x is 0.0 // check if x is 0.0
if(x == 0) if(x == 0)
...@@ -240,7 +239,7 @@ __host__ __device__ Y run_cast_from_f8(X x) ...@@ -240,7 +239,7 @@ __host__ __device__ Y run_cast_from_f8(X x)
{ {
retval = x; retval = x;
retval <<= 8; retval <<= 8;
return *(reinterpret_cast<const Y*>(&retval)); return bit_cast<Y>(retval);
} }
// subnormal input // subnormal input
...@@ -264,7 +263,7 @@ __host__ __device__ Y run_cast_from_f8(X x) ...@@ -264,7 +263,7 @@ __host__ __device__ Y run_cast_from_f8(X x)
} }
retval = (sign << (out_exp + out_mant)) | (exponent << out_mant) | mantissa; retval = (sign << (out_exp + out_mant)) | (exponent << out_mant) | mantissa;
return *(reinterpret_cast<const Y*>(&retval)); return bit_cast<Y>(retval);
} }
} // namespace } // namespace
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment