zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 26fac15f485e89dc7106256e1aa79184c1761efd (tree)
parent d019229c2c3432c9053594eb140b255c3be9ebeb
Author: Jacob Young <jacobly0@users.noreply.github.com>
Date:   Wed, 31 May 2023 00:42:24 -0400

math.big.int: fix ctz of zero

Diffstat:
Mlib/std/math/big/int.zig | 4++--
Msrc/value.zig | 4++--
2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig @@ -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); } }; diff --git a/src/value.zig b/src/value.zig @@ -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 {