@@ -5384,19 +5384,27 @@ int GetPixelDataSize(int width, int height, int format)
5384
5384
//----------------------------------------------------------------------------------
5385
5385
// Module specific Functions Definition
5386
5386
//----------------------------------------------------------------------------------
5387
+ union floatUnsignedUnion {
5388
+ float fm ;
5389
+ unsigned int ui ;
5390
+ };
5391
+
5387
5392
// Convert half-float (stored as unsigned short) to float
5388
5393
// REF: https://stackoverflow.com/questions/1659440/32-bit-to-16-bit-floating-point-conversion/60047308#60047308
5389
5394
static float HalfToFloat (unsigned short x )
5390
5395
{
5391
5396
float result = 0.0f ;
5392
5397
5398
+ union floatUnsignedUnion uni ;
5399
+ uni .fm = 0.0f ;
5400
+
5393
5401
const unsigned int e = (x & 0x7C00 ) >> 10 ; // Exponent
5394
5402
const unsigned int m = (x & 0x03FF ) << 13 ; // Mantissa
5395
- const float fm = (float )m ;
5396
- const unsigned int v = ( * ( unsigned int * ) & fm ) >> 23 ; // Evil log2 bit hack to count leading zeros in denormalized format
5397
- const unsigned int r = (x & 0x8000 ) << 16 | (e != 0 )* ((e + 112 ) << 23 | m ) | ((e == 0 )& (m != 0 ))* ((v - 37 ) << 23 | ((m << (150 - v )) & 0x007FE000 )); // sign : normalized : denormalized
5403
+ uni . fm = (float )m ;
5404
+ const unsigned int v = uni . ui >> 23 ; // Evil log2 bit hack to count leading zeros in denormalized format
5405
+ uni . ui = (x & 0x8000 ) << 16 | (e != 0 )* ((e + 112 ) << 23 | m ) | ((e == 0 )& (m != 0 ))* ((v - 37 ) << 23 | ((m << (150 - v )) & 0x007FE000 )); // sign : normalized : denormalized
5398
5406
5399
- result = * ( float * ) & r ;
5407
+ result = uni . fm ;
5400
5408
5401
5409
return result ;
5402
5410
}
@@ -5406,7 +5414,10 @@ static unsigned short FloatToHalf(float x)
5406
5414
{
5407
5415
unsigned short result = 0 ;
5408
5416
5409
- const unsigned int b = (* (unsigned int * ) & x ) + 0x00001000 ; // Round-to-nearest-even: add last bit after truncated mantissa
5417
+ union floatUnsignedUnion uni ;
5418
+ uni .fm = x ;
5419
+
5420
+ const unsigned int b = uni .ui + 0x00001000 ; // Round-to-nearest-even: add last bit after truncated mantissa
5410
5421
const unsigned int e = (b & 0x7F800000 ) >> 23 ; // Exponent
5411
5422
const unsigned int m = b & 0x007FFFFF ; // Mantissa; in line below: 0x007FF000 = 0x00800000-0x00001000 = decimal indicator flag - initial rounding
5412
5423
0 commit comments