Skip to content

Commit e67b525

Browse files
authored
Merge pull request pytorch#911 from gchanan/convWarning
Avoid strict aliasing warning in float/half conversions.
2 parents b4bb4b6 + f467848 commit e67b525

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

THHalf.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,17 @@ float TH_half2float(THHalf h)
7777
}
7878

7979
int temp = ((sign << 31) | (exponent << 23) | mantissa);
80-
81-
return *((float*)((void*)&temp));
80+
float x;
81+
memcpy(&x,&temp,sizeof(float));
82+
return x;
8283
}
8384

8485
THHalf TH_float2half(float f)
8586
{
8687
THHalf ret;
8788

88-
unsigned x = *((int*)(void*)(&f));
89+
unsigned x;
90+
memcpy(&x,&f,sizeof(f));
8991
unsigned u = (x & 0x7fffffff), remainder, shift, lsb, lsb_s1, lsb_m1;
9092
unsigned sign, exponent, mantissa;
9193

0 commit comments

Comments
 (0)