-
-
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
Fast exp2(::Int) (fixs #17412) #17447
Changes from all commits
c202864
96be3e7
7abae25
101e56c
02198e3
7ab57f5
278403a
920c4b9
d729f94
b5a8a3d
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 |
---|---|---|
|
@@ -124,6 +124,21 @@ for f in (:sinh, :cosh, :tanh, :atan, :asinh, :exp, :erf, :erfc, :expm1) | |
@eval ($f)(x::AbstractFloat) = error("not implemented for ", typeof(x)) | ||
end | ||
|
||
# functions with special cases for integer arguments | ||
@inline function exp2(x::Base.BitInteger) | ||
if x > 1023 | ||
Inf64 | ||
elseif x <= -1023 | ||
# if -1073 < x <= -1023 then Result will be a subnormal number | ||
# Hex literal with padding must be use to work on 32bit machine | ||
reinterpret(Float64, 0x0000_0000_0000_0001 << ((x + 1074)) % UInt) | ||
else | ||
# We will cast everything to Int64 to avoid errors in case of Int128 | ||
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. Since you cast everything to 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. I think it is a bit cleaner not to. The cast on the 1 in line 134, is to make sure things work on 32 bit machines where 1 is a 32bit. |
||
# If x is a Int128, and is outside the range of Int64, then it is not -1023<x<=1023 | ||
reinterpret(Float64, (exponent_bias(Float64) + (x % Int64)) << (significand_bits(Float64)) % UInt) | ||
end | ||
end | ||
|
||
# TODO: GNU libc has exp10 as an extension; should openlibm? | ||
exp10(x::Float64) = 10.0^x | ||
exp10(x::Float32) = 10.0f0^x | ||
|
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.
should
exp2(x::BigInt)
return aBigFloat
answer?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.
Nice catch.
I've added some test cases to
test/bigint.jl
to ensure this.