Browse Source

Safer type-punning

Michael Hansen 11 years ago
parent
commit
fced942020
  1. 22
      Sources/Plasma/CoreLib/HeadSpin.h

22
Sources/Plasma/CoreLib/HeadSpin.h

@ -177,15 +177,25 @@ inline uint64_t hsSwapEndian64(uint64_t value)
} }
inline float hsSwapEndianFloat(float fvalue) inline float hsSwapEndianFloat(float fvalue)
{ {
uint32_t value = *(uint32_t*)&fvalue; union {
value = hsSwapEndian32(value); uint32_t i;
return *(float*)&value; float f;
} value;
value.f = fvalue;
value.i = hsSwapEndian32(value.i);
return value.f;
} }
inline double hsSwapEndianDouble(double dvalue) inline double hsSwapEndianDouble(double dvalue)
{ {
uint64_t value = *(uint64_t*)&dvalue; union {
value = hsSwapEndian64(value); uint64_t i;
return *(double*)&value; double f;
} value;
value.f = dvalue;
value.i = hsSwapEndian64(value.i);
return value.f;
} }
#if LITTLE_ENDIAN #if LITTLE_ENDIAN

Loading…
Cancel
Save