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

Fast exp2(::Int) (fixs #17412) #17447

Merged
merged 10 commits into from
Aug 1, 2016
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
12 changes: 7 additions & 5 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,17 @@ end
@inline function exp2(x::Base.BitInteger)
if x > 1023
Inf64
Copy link
Contributor

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 a BigFloat answer?

Copy link
Contributor Author

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.

elseif x < -1074
Float64(0.0)
elseif x <= -1023
# Result will be a subnormal number
reinterpret(Float64, Int64(1) << (x + 1074))
if x < -1074
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be x >= -1074?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, you might not need this branch at all (if x < -1074, the bitshift should still give the correct answer).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be x >= -1074?
yes

Actually, you might not need this branch at all (if x < -1074, the bitshift should still give the correct answer).

Yes. Tested and confirmed

# Result will be a subnormal number
reinterpret(Float64, (1 % Int64) << (x + 1074))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could convert the shift count to unsigned via %; otherwise Julia will have to check the sign.

else #-1073 < x <= -1023
0.0
end
else
# If x is a Int128, and is outside the range of Int64, then it is not -1023<x<=1023
# We will cast everything to Int64 to avoid errors in case of Int128
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you cast everything to Int64, you could also restrict the argument type to Int, and have a generic version that accepts Integer and calls the main function with argument converted to Int.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is a bit cleaner not to.
Not all paths have the cast, on x
only line 141

The cast on the 1 in line 134, is to make sure things work on 32 bit machines where 1 is a 32bit.
Perhaps cleaner, I change that one to 0x0000_0000_0000_0001

reinterpret(Float64, (exponent_bias(Float64) + Int64(x)) << significand_bits(Float64))
reinterpret(Float64, (exponent_bias(Float64) + (x % Int64)) << significand_bits(Float64))
end
end

Expand Down