-
-
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 5 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,23 @@ 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 arguements | ||
@inline function exp2(x::Integer) | ||
if x > 1023 | ||
Inf64 | ||
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. should 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. Nice catch. |
||
elseif x < -1074 | ||
Float64(0.0) | ||
elseif x <= -1023 | ||
#Result will be a subnormal number | ||
reinterpret(Float64, Int64(1) << (x + 1074)) | ||
else | ||
#If x is a Int128, and is outside the range of Int64, then it is not -123<x<=1023 | ||
#We will cast everything to Int64 to avoid errors incase of Int128 | ||
reinterpret(Float64, (exponent_bias(Float64) + Int64(x)) << significand_bits(Float64)) | ||
end | ||
end | ||
|
||
|
||
# TODO: GNU libc has exp10 as an extension; should openlibm? | ||
exp10(x::Float64) = 10.0^x | ||
exp10(x::Float32) = 10.0f0^x | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -192,6 +192,24 @@ end | |
@test exp2(Float16(2.)) ≈ exp2(2.) | ||
@test log(e) == 1 | ||
|
||
# check exp2(::Integer) matches exp2(::Float)" | ||
for ii in -2048:2048 | ||
expected = exp2(float(ii)) | ||
@test(exp2(Int16(ii)) == expected) | ||
@test(exp2(Int32(ii)) == expected) | ||
@test(exp2(Int64(ii)) == expected) | ||
@test(exp2(Int128(ii)) == expected) | ||
if ii>=0 | ||
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. spaces around the |
||
@test(exp2(UInt16(ii)) == expected) | ||
@test(exp2(UInt32(ii)) == expected) | ||
@test(exp2(UInt64(ii)) == expected) | ||
@test(exp2(UInt128(ii)) == expected) | ||
end | ||
end | ||
|
||
@test(exp2(false) == exp2(float(false))) | ||
@test(exp2(true) == exp2(float(true))) | ||
|
||
for T in (Int, Float64, BigFloat) | ||
@test deg2rad(T(180)) ≈ 1pi | ||
@test deg2rad(T[45, 60]) ≈ [pi/T(4), pi/T(3)] | ||
|
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.
no tabs here either
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'm sorry. I did check that file. But apparently, I fail at using the
retab
command in vim.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.
no big deal, ci is relatively quiet atm