authorgravatar for 12179851+leesongun@users.noreply.github.comleesongun <12179851+leesongun@users.noreply.github.com> 2021-07-06 18:42:18+09:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-06 21:30:58-07:00
logceebcfa317874ea6cf0cab55cfc12ec60078273d
treef3d85b05474a426a41ff3bd385d299102843d359
parentb26ab39836ea07cdc92af430f12f0d4732433b53

Fix unexpected truncation behavior with comptime_int larger than u64 range (#9303)

Closes #9299

2 files changed, 6 insertions(+), 1 deletions(-)

src/stage1/bigint.cpp+4-1
......@@ -88,8 +88,11 @@ static void to_twos_complement(BigInt *dest, const BigInt *op, size_t bit_count)
8888 size_t digits_to_copy = bit_count / 64;
8989 size_t leftover_bits = bit_count % 64;
9090 dest->digit_count = digits_to_copy + ((leftover_bits == 0) ? 0 : 1);
91 if (dest->digit_count == 1 && leftover_bits == 0) {
91 if (dest->digit_count == 1) {
9292 dest->data.digit = op_digits[0];
93 if (leftover_bits != 0) {
94 dest->data.digit &= (1ULL << leftover_bits) - 1;
95 }
9396 if (dest->data.digit == 0) dest->digit_count = 0;
9497 return;
9598 }
test/behavior/truncate.zig+2
......@@ -54,4 +54,6 @@ test "truncate on comptime integer" {
5454 try expect(y == 0xabcd);
5555 var z = @truncate(i16, -65537);
5656 try expect(z == -1);
57 var w = @truncate(u1, 1 << 100);
58 try expect(w == 0);
5759}