Skip to content

Commit

Permalink
speed up Float16 conversions a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Nov 1, 2018
1 parent 13694f4 commit 99e58b2
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,17 @@ function Float16(val::Float32)
return reinterpret(Float16, t ((f >> 0xd) % UInt16))
end
i = (f >> 23) & 0x1ff + 1
sh = shifttable[i]
@inbounds sh = shifttable[i]
f &= 0x007fffff
h::UInt16 = basetable[i] + (f >> sh)
@inbounds h = (basetable[i] + (f >> sh)) % UInt16
# round
# NOTE: we maybe should ignore NaNs here, but the payload is
# getting truncated anyway so "rounding" it might not matter
nextbit = (f >> (sh-1)) & 1
if nextbit != 0
# Round halfway to even or check lower bits
if h&1 == 1 || (f & ((1<<(sh-1))-1)) != 0
h += 1
h += UInt16(1)
end
end
reinterpret(Float16, h)
Expand All @@ -179,7 +179,7 @@ function Float32(val::Float16)
bit = bit >> 1
end
sign = sign << 31
exp = (-14 - n_bit + 127) << 23
exp = ((-14 - n_bit + 127) << 23) % UInt32
sig = ((sig & (~bit)) << n_bit) << (23 - 10)
ret = sign | exp | sig
end
Expand All @@ -195,7 +195,7 @@ function Float32(val::Float16)
end
else
sign = sign << 31
exp = (exp - 15 + 127) << 23
exp = ((exp - 15 + 127) << 23) % UInt32
sig = sig << (23 - 10)
ret = sign | exp | sig
end
Expand Down

0 comments on commit 99e58b2

Please sign in to comment.