math.big.int: fix ctz of zero

This commit is contained in:
Jacob Young 2023-05-31 00:42:24 -04:00 committed by Andrew Kelley
parent d019229c2c
commit 26fac15f48
2 changed files with 4 additions and 4 deletions

View File

@ -2512,7 +2512,7 @@ pub const Const = struct {
return total_limb_lz + bits - total_limb_bits;
}
pub fn ctz(a: Const) Limb {
pub fn ctz(a: Const, bits: Limb) Limb {
// Limbs are stored in little-endian order.
var result: Limb = 0;
for (a.limbs) |limb| {
@ -2520,7 +2520,7 @@ pub const Const = struct {
result += limb_tz;
if (limb_tz != @sizeOf(Limb) * 8) break;
}
return result;
return @min(result, bits);
}
};

View File

@ -1216,10 +1216,10 @@ pub const Value = struct {
return bigint.clz(ty.intInfo(mod).bits);
}
pub fn ctz(val: Value, _: Type, mod: *Module) u64 {
pub fn ctz(val: Value, ty: Type, mod: *Module) u64 {
var bigint_buf: BigIntSpace = undefined;
const bigint = val.toBigInt(&bigint_buf, mod);
return bigint.ctz();
return bigint.ctz(ty.intInfo(mod).bits);
}
pub fn popCount(val: Value, ty: Type, mod: *Module) u64 {