-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add pure Julia ldexp function #19491
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -349,8 +349,49 @@ julia> ldexp(5., 2) | |
20.0 | ||
``` | ||
""" | ||
ldexp(x::Float64,e::Integer) = ccall((:scalbn,libm), Float64, (Float64,Int32), x, Int32(e)) | ||
ldexp(x::Float32,e::Integer) = ccall((:scalbnf,libm), Float32, (Float32,Int32), x, Int32(e)) | ||
function ldexp{T<:AbstractFloat}(x::T, e::Integer) | ||
xu = reinterpret(Unsigned, x) | ||
xs = xu & ~sign_mask(T) | ||
xs >= exponent_mask(T) && return x # NaN or Inf | ||
k = Int(xs >> significand_bits(T)) | ||
if k == 0 # x is subnormal | ||
xs == 0 && return x # +-0 | ||
m = leading_zeros(xs) - exponent_bits(T) | ||
ys = xs << unsigned(m) | ||
xu = ys | (xu & sign_mask(T)) | ||
k = 1 - m | ||
# underflow, otherwise may have integer underflow in the following n + k | ||
e < -50000 && return flipsign(T(0.0), x) | ||
end | ||
# For cases where e of an Integer larger than Int make sure we properly | ||
# overlfow/underflow; this is optimized away otherwise. | ||
if e > typemax(Int) | ||
return flipsign(T(Inf), x) | ||
elseif e < typemin(Int) | ||
return flipsign(T(0.0), x) | ||
end | ||
n = e % Int | ||
k += n | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if this overflows (e.g. if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could handle if q > 50000
return flipsign(T(Inf), x)
elseif q < -50000
return flipsign(T(0), x)
end
n = q % Int There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would add another branch, is it worth it, because you still have to check that Currently we do a lot worse and cast to Int32. I don't necessarily have a problem with this, but question if this is the best way to do it. The quest for the most generic function is satisfying, but it typically entails performance penalties. Internally for my codes that need this function, I don't use this since it's a bit too slow in practice but instead use the following: Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In that case, another option would be if q > typemax(Int)
return flipsign(T(Inf), x)
elseif q < typemin(Int)
return flipsign(T(0), x)
end
n = q % Int which should be able to optimise away the check if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. very nice! |
||
# overflow, if k is larger than maximum posible exponent | ||
if k >= Int(exponent_mask(T) >> significand_bits(T)) | ||
return flipsign(T(Inf), x) | ||
end | ||
if k > 0 # normal case | ||
xu = (xu & ~exponent_mask(T)) | (k % typeof(xu) << significand_bits(T)) | ||
return reinterpret(T, xu) | ||
else # subnormal case | ||
if k <= -significand_bits(T) # underflow | ||
# overflow, for the case of integer overflow in n + k | ||
e > 50000 && return flipsign(T(Inf), x) | ||
return flipsign(T(0.0), x) | ||
end | ||
k += significand_bits(T) | ||
z = T(2.0)^-significand_bits(T) | ||
xu = (xu & ~exponent_mask(T)) | (k % typeof(xu) << significand_bits(T)) | ||
return z*reinterpret(T, xu) | ||
end | ||
end | ||
ldexp(x::Float16, q::Integer) = Float16(ldexp(Float32(x), q)) | ||
|
||
""" | ||
exponent(x) -> Int | ||
|
@@ -604,7 +645,6 @@ for func in (:atan2,:hypot) | |
end | ||
end | ||
|
||
ldexp(a::Float16, b::Integer) = Float16(ldexp(Float32(a), b)) | ||
cbrt(a::Float16) = Float16(cbrt(Float32(a))) | ||
|
||
# More special functions | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this isn't really valid for every single AbstractFloat subtype, is it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably not: realistically it's probably only valid for
Float16
,Float32
andFloat64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it assumes:
bitstype
.So it could be valid for, e.g. an x87
Float80
or an IEEEFloat128
, but notBigFloat
, IEEE decimal (as in DecFP.jl), DoubleDoubles, IBM floats or formats which do funny things like two's complement exponents.