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

Diverging from primitive behavior in overflowing shift #121

Closed
elichai opened this issue Sep 17, 2022 · 2 comments · Fixed by #395
Closed

Diverging from primitive behavior in overflowing shift #121

elichai opened this issue Sep 17, 2022 · 2 comments · Fixed by #395

Comments

@elichai
Copy link

elichai commented Sep 17, 2022

The rust docs say the following about overflowing shift:

If the shift value is too large, then value is masked (N-1) where N is the number of bits, and this value is then used to perform the shift.
(Source: https://doc.rust-lang.org/std/primitive.u64.html#method.overflowing_shl)
And you can see this via the following code:

#[allow(arithmetic_overflow)]
fn main() {
    let a: u64 = 500;
    let b = a << 119;
    println!("{}", b);
}

Which prints 18014398509481984000.

On the other hand in the implementation here zeros will be returned:

return Self { limbs };

@tarcieri
Copy link
Member

cc @mikelodder7

@elichai
Copy link
Author

elichai commented Sep 24, 2022

FYI to achieve the same behavior as libstd we can just add n = n % (Limb::BIT_SIZE * LIMBS) at the start of that function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants