authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-05-31 00:42:24-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:47:57-07:00
log26fac15f485e89dc7106256e1aa79184c1761efd
tree59f0c3a453e0789f989bf096fe6fe235a6865b14
parentd019229c2c3432c9053594eb140b255c3be9ebeb

math.big.int: fix ctz of zero


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

lib/std/math/big/int.zig+2-2
...@@ -2512,7 +2512,7 @@ pub const Const = struct {...@@ -2512,7 +2512,7 @@ pub const Const = struct {
2512 return total_limb_lz + bits - total_limb_bits;2512 return total_limb_lz + bits - total_limb_bits;
2513 }2513 }
25142514
2515 pub fn ctz(a: Const) Limb {2515 pub fn ctz(a: Const, bits: Limb) Limb {
2516 // Limbs are stored in little-endian order.2516 // Limbs are stored in little-endian order.
2517 var result: Limb = 0;2517 var result: Limb = 0;
2518 for (a.limbs) |limb| {2518 for (a.limbs) |limb| {
...@@ -2520,7 +2520,7 @@ pub const Const = struct {...@@ -2520,7 +2520,7 @@ pub const Const = struct {
2520 result += limb_tz;2520 result += limb_tz;
2521 if (limb_tz != @sizeOf(Limb) * 8) break;2521 if (limb_tz != @sizeOf(Limb) * 8) break;
2522 }2522 }
2523 return result;2523 return @min(result, bits);
2524 }2524 }
2525};2525};
25262526
src/value.zig+2-2
...@@ -1216,10 +1216,10 @@ pub const Value = struct {...@@ -1216,10 +1216,10 @@ pub const Value = struct {
1216 return bigint.clz(ty.intInfo(mod).bits);1216 return bigint.clz(ty.intInfo(mod).bits);
1217 }1217 }
12181218
1219 pub fn ctz(val: Value, _: Type, mod: *Module) u64 {1219 pub fn ctz(val: Value, ty: Type, mod: *Module) u64 {
1220 var bigint_buf: BigIntSpace = undefined;1220 var bigint_buf: BigIntSpace = undefined;
1221 const bigint = val.toBigInt(&bigint_buf, mod);1221 const bigint = val.toBigInt(&bigint_buf, mod);
1222 return bigint.ctz();1222 return bigint.ctz(ty.intInfo(mod).bits);
1223 }1223 }
12241224
1225 pub fn popCount(val: Value, ty: Type, mod: *Module) u64 {1225 pub fn popCount(val: Value, ty: Type, mod: *Module) u64 {