Skip to content

Commit

Permalink
add methods for Float16
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbyrne committed Feb 12, 2015
1 parent d2b13fb commit 75b22e2
Showing 1 changed file with 49 additions and 1 deletion.
50 changes: 49 additions & 1 deletion base/float16.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,55 @@ for func in (:div,:fld,:cld,:rem,:mod,:atan2,:hypot)
end

ldexp(a::Float16, b::Integer) = float16(ldexp(float32(a), b))
exponent(x::Float16) = exponent(float32(x))

function exponent(x::Float16)
xu = reinterpret(UInt16,x)
k = int(xu >> 10) & 0x001f
if k == 0 # x is subnormal
xu == 0 && return x,0
axu = xu & 0x7fff
s = leading_zeros(axu)-4
axu <<= s
xu = (xu & 0x8000) | axu
k -= s
elseif k == 0x001f # NaN or Inf
return x,0
end
k - 15
end
function significand(x::Float16)
xu = reinterpret(UInt16,x)
k = int(xu >> 10) & 0x001f
if k == 0 # x is subnormal
xu == 0 && return x
axu = xu & 0x7fff
s = leading_zeros(axu)-4
axu <<= s
xu = (xu & 0x8000) | axu
elseif k == 0x001f # NaN or Inf
return x
end
xu = (xu & 0x83ff) | 0x3c00
reinterpret(Float16,xu)
end
function frexp(x::Float16)
xu = reinterpret(UInt16,x)
k = int(xu >> 10) & 0x001f
if k == 0 # x is subnormal
xu == 0 && return x,0
axu = xu & 0x7fff
s = leading_zeros(axu)-4
axu <<= s
xu = (xu & 0x8000) | axu
k -= s
elseif k == 0x001f # NaN or Inf
return x,0
end
k -= 14
xu = (xu & 0x83ff) | 0x3800
reinterpret(Float16,xu), k
end

^(x::Float16, y::Integer) = float16(float32(x)^y)

rationalize{T<:Integer}(::Type{T}, x::Float16; tol::Real=eps(x)) = rationalize(T, float32(x); tol=tol)

0 comments on commit 75b22e2

Please sign in to comment.