forked from MoarVM/MoarVM
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Avoid undefined behaviour when overshifting
Shifting by a negative amount or more than the width of the value is undefined behaviour in C. Adopt the semantics from Perl 5: - Negative shift inverts the direction - Left overshift yields zero - Right overshift yields zero for positive numbers, -1 for negative numbers
- Loading branch information
Showing
5 changed files
with
27 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #include "moar.h" | ||
|
|
||
| /* copied from perl5/pp.c:S_iv_shift */ | ||
| static MVMint64 S_int_shift(MVMint64 value, MVMint64 shift, MVMint64 left) { | ||
| if (shift < 0) { | ||
| shift = -shift; | ||
| left = !left; | ||
| } | ||
| if (shift >= sizeof(MVMint64) * CHAR_BIT) { | ||
| return value < 0 && !left ? -1 : 0; | ||
| } | ||
| return left ? value << shift : value >> shift; | ||
| } | ||
|
|
||
| MVMint64 MVM_int_shl(MVMThreadContext *tc, MVMint64 value, MVMint64 shift) { | ||
| return S_int_shift(value, shift, 1); | ||
| } | ||
|
|
||
| MVMint64 MVM_int_shr(MVMThreadContext *tc, MVMint64 value, MVMint64 shift) { | ||
| return S_int_shift(value, shift, 0); | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| MVMint64 MVM_int_shl(MVMThreadContext *tc, MVMint64 value, MVMint64 shift); | ||
| MVMint64 MVM_int_shr(MVMThreadContext *tc, MVMint64 value, MVMint64 shift); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters