Make faster half float conversion

This commit is contained in:
Ishotihadus
2017-07-16 16:23:32 +09:00
parent 1c6a2099ab
commit 0007810984
+20 -12
View File
@@ -326,20 +326,28 @@ module Mikunyan
# convert 16bit float # convert 16bit float
def self.n2f(n) def self.n2f(n)
s = n & 0x8000 != 0 case n
e = (n & 0x7c00) >> 10 when 0x0000
f = n & 0x03ff 0.0
when 0x8000
r = 0 -0.0
if e == 0 when 0x7c00
r = (f / 1024r) * (2**-14) if f != 0 Float::INFINITY
elsif e == 0x1f when 0xfc00
r = f == 0 ? Float::INFINITY : Float::NAN -Float::INFINITY
else else
r = (f / 1024r + 1) * (2**(e-15)) s = n & 0x8000 != 0
e = n & 0x7c00
f = n & 0x03ff
case e
when 0x7c00
Float::NAN
when 0
(s ? -f : f) * 2.0**-24
else
(s ? -1 : 1) * (f / 1024.0 + 1) * (2.0 ** ((e >> 10)-15))
end
end end
r *= -1 if s
r.to_f
end end
# [0.0,1.0] -> [0,255] # [0.0,1.0] -> [0,255]