-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:JuliaLang/julia
- Loading branch information
Showing
11 changed files
with
150 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
|
||
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 | ||
|
||
# Float32 -> Float16 algorithm from: | ||
# "Fast Half Float Conversion" by Jeroen van der Zijp | ||
# ftp://ftp.fox-toolkit.org/pub/fasthalffloatconversion.pdf | ||
|
||
basetable = Array(Uint16, 512) | ||
shifttable = Array(Uint8, 512) | ||
|
||
for i = 0:255 | ||
e = i - 127 | ||
if e < -24 # Very small numbers map to zero | ||
basetable[i|0x000+1]=0x0000; | ||
basetable[i|0x100+1]=0x8000; | ||
shifttable[i|0x000+1]=24; | ||
shifttable[i|0x100+1]=24; | ||
elseif e < -14 # Small numbers map to denorms | ||
basetable[i|0x000+1]=(0x0400>>(-e-14)); | ||
basetable[i|0x100+1]=(0x0400>>(-e-14)) | 0x8000; | ||
shifttable[i|0x000+1]=-e-1; | ||
shifttable[i|0x100+1]=-e-1; | ||
elseif e <= 15 # Normal numbers just lose precision | ||
basetable[i|0x000+1]=((e+15)<<10); | ||
basetable[i|0x100+1]=((e+15)<<10) | 0x8000; | ||
shifttable[i|0x000+1]=13; | ||
shifttable[i|0x100+1]=13; | ||
elseif e < 128 # Large numbers map to Infinity | ||
basetable[i|0x000+1]=0x7C00; | ||
basetable[i|0x100+1]=0xFC00; | ||
shifttable[i|0x000+1]=24; | ||
shifttable[i|0x100+1]=24; | ||
else # Infinity and NaN's stay Infinity and NaN's | ||
basetable[i|0x000+1]=0x7C00; | ||
basetable[i|0x100+1]=0xFC00; | ||
shifttable[i|0x000+1]=13; | ||
shifttable[i|0x100+1]=13; | ||
end | ||
end | ||
|
||
function convert(::Type{Float16}, val::Float32) | ||
f = reinterpret(Uint32, val) | ||
i = (f >> 23) & 0x1ff + 1 | ||
h = basetable[i] + ((f & 0x007fffff) >> shifttable[i]) | ||
reinterpret(Float16, uint16(h)) | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
f = float16(2.) | ||
g = float16(1.) | ||
|
||
@test -f === float16(-2.) | ||
|
||
@test f+g === 3f0 | ||
@test f-g === 1f0 | ||
@test f*g === 2f0 | ||
@test f/g === 2f0 | ||
|
||
@test f + 2 === 4f0 | ||
@test f - 2 === 0f0 | ||
@test f*2 === 4f0 | ||
@test f/2 === 1f0 | ||
@test f + 2. === 4. | ||
@test f - 2. === 0. | ||
@test f*2. === 4. | ||
@test f/2. === 1. | ||
|
||
@test_approx_eq sin(f) sin(2f0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters