Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] basic float16 support (closes #3467) #3980

Merged
merged 4 commits into from
Aug 7, 2013
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add convert(Float32, Float16)
  • Loading branch information
rwgardner2 authored and nolta committed Aug 7, 2013
commit 13456a425cbdb7681ce8d2bb89f0cf99df071f5a
43 changes: 43 additions & 0 deletions base/float16.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

function convert(::Type{Float32}, val::Float16)
val = uint32(reinterpret(Uint16, val))
sign = (val & 0x8000) >> 15
exp = (val & 0x7c00) >> 10
sig = (val & 0x3ff) >> 0
ret::Uint32

if exp == 0
if sig == 0
sign = sign << 31
ret = sign | exp | sig
else
n_bit = 1
bit = 0x0200
while (bit & sig) == 0
n_bit = n_bit + 1
bit = bit >> 1
end
sign = sign << 31
exp = (-14 - n_bit + 127) << 23
sig = ((sig & (~bit)) << n_bit) << (23 - 10)
ret = sign | exp | sig
end
elseif exp == 0x1f
if sig == 0
if sign == 0
ret = 0x7f800000
else
ret = 0xff800000
end
else
ret = 0xffffffff
end
else
sign = sign << 31
exp = (exp - 15 + 127) << 23
sig = sig << (23 - 10)
ret = sign | exp | sig
end
return reinterpret(Float32, ret)
end