From a68e6cd3d73680282d06e2d7676f0bca3056c71b Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Fri, 7 Jan 2022 18:57:39 +0100 Subject: [PATCH] Return Value.zero when the result of shr requires zero bits --- src/value.zig | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/value.zig b/src/value.zig index 9da9fa6307..c4d35ad006 100644 --- a/src/value.zig +++ b/src/value.zig @@ -2635,9 +2635,17 @@ pub const Value = extern union { var lhs_space: Value.BigIntSpace = undefined; const lhs_bigint = lhs.toBigInt(&lhs_space); const shift = @intCast(usize, rhs.toUnsignedInt()); + + const result_limbs = lhs_bigint.limbs.len -| (shift / (@sizeOf(std.math.big.Limb) * 8)); + if (result_limbs == 0) { + // The shift is enough to remove all the bits from the number, which means the + // result is zero. + return Value.zero; + } + const limbs = try allocator.alloc( std.math.big.Limb, - lhs_bigint.limbs.len - (shift / (@sizeOf(std.math.big.Limb) * 8)), + result_limbs, ); var result_bigint = BigIntMutable{ .limbs = limbs,