diff options
Diffstat (limited to 'src/renderergl2/tr_extramath.c')
-rw-r--r-- | src/renderergl2/tr_extramath.c | 61 |
1 files changed, 27 insertions, 34 deletions
diff --git a/src/renderergl2/tr_extramath.c b/src/renderergl2/tr_extramath.c index 84816a61..552406bb 100644 --- a/src/renderergl2/tr_extramath.c +++ b/src/renderergl2/tr_extramath.c @@ -199,42 +199,35 @@ int NextPowerOfTwo(int in) return out; } -unsigned short FloatToHalf(float in) -{ - unsigned short out; - - union - { - float f; - unsigned int i; - } f32; - - int sign, inExponent, inFraction; - int outExponent, outFraction; +union f32_u { + float f; + uint32_t i; + struct { + unsigned int fraction:23; + unsigned int exponent:8; + unsigned int sign:1; + } pack; +}; + +union f16_u { + uint16_t i; + struct { + unsigned int fraction:10; + unsigned int exponent:5; + unsigned int sign:1; + } pack; +}; + +uint16_t FloatToHalf(float in) +{ + union f32_u f32; + union f16_u f16; f32.f = in; - sign = (f32.i & 0x80000000) >> 31; - inExponent = (f32.i & 0x7F800000) >> 23; - inFraction = f32.i & 0x007FFFFF; - - outExponent = CLAMP(inExponent - 127, -15, 16) + 15; - - outFraction = 0; - if (outExponent == 0x1F) - { - if (inExponent == 0xFF && inFraction != 0) - outFraction = 0x3FF; - } - else if (outExponent == 0x00) - { - if (inExponent == 0x00 && inFraction != 0) - outFraction = 0x3FF; - } - else - outFraction = inFraction >> 13; - - out = (sign << 15) | (outExponent << 10) | outFraction; + f16.pack.exponent = CLAMP(f32.pack.exponent - 112, 0, 31); + f16.pack.fraction = f32.pack.fraction >> 13; + f16.pack.sign = f32.pack.sign; - return out; + return f16.i; } |