authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-04-10 10:57:43+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-11 03:49:15-04:00
log8f5753ba9f1fd43f14628e143d33d6e8a64847f0
tree7d6832dc1415e294b5adca202af8c95e036a1ba1
parente309ad884a5d2fc8b78325199cd6e81d2efa220d

Fix normalization of right-shifted BigInt at CT

The pointer value for the `digits` field was being treated as if it were a limb. Fixes #2225

2 files changed, 17 insertions(+), 2 deletions(-)

src/bigint.cpp+9-2
......@@ -1410,12 +1410,19 @@ void bigint_shr(BigInt *dest, const BigInt *op1, const BigInt *op2) {
14101410 }
14111411
14121412 dest->digit_count = op1->digit_count - digit_shift_count;
1413 dest->data.digits = allocate<uint64_t>(dest->digit_count);
1413 uint64_t *digits;
1414 if (dest->digit_count == 1) {
1415 digits = &dest->data.digit;
1416 } else {
1417 digits = allocate<uint64_t>(dest->digit_count);
1418 dest->data.digits = digits;
1419 }
1420
14141421 uint64_t carry = 0;
14151422 for (size_t op_digit_index = op1->digit_count - 1;;) {
14161423 uint64_t digit = op1_digits[op_digit_index];
14171424 size_t dest_digit_index = op_digit_index - digit_shift_count;
1418 dest->data.digits[dest_digit_index] = carry | (digit >> leftover_shift_count);
1425 digits[dest_digit_index] = carry | (digit >> leftover_shift_count);
14191426 carry = digit << (64 - leftover_shift_count);
14201427
14211428 if (dest_digit_index == 0) { break; }
test/stage1/behavior/bit_shifting.zig+8
......@@ -86,3 +86,11 @@ fn testShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, c
8686 expect(table.get(@intCast(Key, i)) == node);
8787 }
8888}
89
90// #2225
91test "comptime shr of BigInt" {
92 comptime {
93 var n = 0xdeadbeef0000000000000000;
94 std.debug.assert(n >> 64 == 0xdeadbeef);
95 }
96}