Skip to content

Conversation

tisonkun
Copy link
Contributor

@tisonkun tisonkun commented Nov 7, 2023

This fixes #17858.

Offset by one issue.

Signed-off-by: tison <wander4096@gmail.com>
@@ -1380,7 +1380,7 @@ pub const Mutable = struct {
var u = b: {
const start = buf_index;
const shift = (a.bitCountAbs() + 1) / 2;
buf_index += 1 + ((shift - 1) / limb_bits + 1);
buf_index += 1 + ((shift / limb_bits) + 1);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. 1 - The initial value of m is 1, it takes at lest one limb_bit.
  2. shift / limb_bits - For each the shift bits == limb_bits, it takes one more limb_bit.
  3. 1 - The sentinel.

Copy link
Contributor Author

@tisonkun tisonkun Nov 7, 2023

Choose a reason for hiding this comment

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

and this is how we calculate in the next shiftLeft:

r.normalize(a.limbs.len + (shift / limb_bits) + 1);

notice that we don't shift - 1 here.

Copy link
Contributor Author

@tisonkun tisonkun Nov 8, 2023

Choose a reason for hiding this comment

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

Actually I'm thinking of that we may not need the so-called sentinel + 1 in both code place. But this is safe while may be waste one usize space.

Signed-off-by: tison <wander4096@gmail.com>
@Vexu Vexu merged commit ee47643 into ziglang:master Nov 8, 2023
@tisonkun tisonkun deleted the fix-sqrt-oversize branch November 8, 2023 06:45
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 this pull request may close these issues.

std.math.big.int sqrt panic
2 participants