-
-
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
Allow negative bit-shift counts #14791
Conversation
|
Should we even have In machine language, this would be called a "logical shift". |
We could use |
Leaving it out sounds appropriate. |
|
e462f12
to
60d4821
Compare
Removed |
LGTM. @StefanKarpinski what do you think? |
IIUC this is breaking, so it could use a mention in NEWS.md. |
I'm not sure. I'm somewhat more inclined to define |
@JeffBezanson, thoughts? |
Ping |
@StefanKarpinski: It seems @JeffBezanson doesn't have input on this. What's your verdict? |
Rather than introducing a new a << -b == a >> b
a >> -b == a << b
a >>> -b == a << b I suspect that in this case, using a branch will be better than using a select instruction ( @eschnett, do you mind modifying this PR this way (or making a new one)? |
+1 |
as long as constant shifts get folded correctly I think that's fine |
I think might be worth a benchmark. |
60d4821
to
9a0e2b6
Compare
@@ -346,23 +346,27 @@ for (fJ, fC) in ((:-, :neg), (:~, :com)) | |||
end | |||
end | |||
|
|||
function <<(x::BigInt, c::Int) | |||
c < 0 && throw(DomainError()) | |||
function <<(x::BigInt, c::UInt) |
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.
Shouldn't this be Culong
?
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.
Making this Culong
would force every caller to explicitly convert to Culong
.
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.
Ah, I misread it (I thought it was Cuint
).
What if Culong != UInt
(as I think it is on Windows 64-bit): won't the implicit conversion throw an error for typemax(UInt)
?
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.
Possibly; I don't know whether ccall
checks. 64-bit Linux definitely has this problem. That problem was there before, though.
I don't know a good solution -- shifting a BigInt
left by a large number should work "in principle", but will in practice run out of memory. So an error message is probably the best we can do anyway.
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.
That's probably true.
ee99029
to
78a2d09
Compare
Are there any further comments or requests? I'm ready to squash and merge. |
6072d28
to
e3d5fda
Compare
Rebased and squashed, Travis and Appveyor are green. Ready to go? |
where `n >= 0`, filling with `0`s. This is equivalent to `x * 2^n`. | ||
Left bit shift operator, `x << n`. For `n >= 0`, the result is `x` shifted left | ||
by `n` bits, filling with `0`s. This is equivalent to `x * 2^n`. For `n < 0`, | ||
this is equivalent to `x >> -n`. |
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.
needs a make docs
to update the rst contents
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.
Done. Anything else I missed?
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 think we're good.
- Introduce methods that take unsigned shift counts - Handle signed shift counts by dispatching to the respective unsigned shift counts, shifting in the opposite direction for negative counts - Update documentation - Add tests Closes JuliaLang#14516.
e3d5fda
to
5a9717b
Compare
Allow negative bit-shift counts
This seems to have introduced several ambiguity warnings during bootstrap. |
I will check. |
This is now PR #15368. |
This was never addressed here, was it? |
Good catch. |
@tkelman In what sense in this breaking? The new functionality is a strict superset of the old one. Do you refer to how one now defines bit-shift operations for user-defined types? Or do you refer to the performance difference between signed and unsigned shift counts? |
None of those - weren't we previously doing the Perl behavior for #14516 (comment), so after this PR negative shifts now give different results? |
Very good; yes, I understand now. I'll add the |
See ##18188. |
This patch concerts the bit shift operators << >> >>>.
bitshift
andunsigned_bitshift
that (similar to Matlab) shift left or right for positive and negative counts, respectively.Closes #14516.