Notifications
Clear all
[Closed] Float 32bit to half 16bit
Aug 06, 2011 3:04 am
Hi I was wondering if anyone could help me convert this function to max script.
I am trying but i am having a little trouble.
What i am looking to do is read a 32 bit float in the scene say a vertex or uv point.
Then write that value out as its half float form.
http://galfar.vevb.net/wp/2011/16bit-half-float-in-pascaldelphi/
1 Reply
Aug 06, 2011 3:04 am
Ok well i got this working to the best of my knowledge idf anyone knows a better way let me know.
HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP = 0x38000000
HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP = 0x47800000
FLOAT_MAX_BIASED_EXP = (bit.shift 0xFF 23)
HALF_FLOAT_MAX_BIASED_EXP = (bit.shift 0x1F 10)
sign = (bit.shift fli -31)
mantissa = (bit.and fli ((bit.shift 1 23) - 1))
exponent = bit.and fli FLOAT_MAX_BIASED_EXP
case of (
(fli == 0):(
hf = 0
)
(exponent >= HALF_FLOAT_MAX_BIASED_EXP_AS_SINGLE_FP_EXP):(
--// check if the original single precision float number is a NaN
if ((mantissa != 0) and (exponent == FLOAT_MAX_BIASED_EXP)) then (
--// we have a single precision NaN
mantissa = (bit.shift 1 23) - 1;
)
else
(
--// 16-bit half-float representation stores number as Inf
mantissa = 0
)
hf1 = (bit.shift sign 15)
hf2 = HALF_FLOAT_MAX_BIASED_EXP
hf3 = (bit.shift mantissa -13)
hf4 = bit.or hf1 hf2
hf = bit.or hf4 hf3
)
--// check if exponent is <= -15
(exponent <= HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP):(
--// store a denorm half-float value or zero
exponent = bit.shift (HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP - exponent) -23
mantissa = bit.shift mantissa ((14 + exponent) * -1)
hf = bit.or (bit.shift sign 15) mantissa
)
default:(
hf1 = (bit.shift sign 15)
hf2 = bit.shift (exponent - HALF_FLOAT_MIN_BIASED_EXP_AS_SINGLE_FP_EXP) -13
hf3 = (bit.shift mantissa -13)
hf4 = bit.or hf1 hf2
hf = bit.or hf4 hf3
)
)