zig

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

commit 2fe5bdb9ed4e17c94055a9539092bdf35a55f752 (tree)
parent 5c49341f097dc666eb4ad8c26c6b00fd2189a396
Author: Jacob Young <jacobly0@users.noreply.github.com>
Date:   Tue, 11 Oct 2022 19:57:13 -0400

big.int: rewrite confusing code in an equivalent but less confusing way

Diffstat:
Mlib/std/math/big/int.zig | 24++++++++----------------
1 file changed, 8 insertions(+), 16 deletions(-)

diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig @@ -391,16 +391,12 @@ pub const Mutable = struct { /// Asserts the result fits in `r`. An upper bound on the number of limbs needed by /// r is `math.max(a.limbs.len, calcLimbLen(scalar)) + 1`. pub fn addScalar(r: *Mutable, a: Const, scalar: anytype) void { - const Scalar = @TypeOf(scalar); - const magnitude = switch (@typeInfo(Scalar)) { - .ComptimeInt => scalar, - .Int => |int| switch (int.signedness) { - .signed => minInt(Scalar), - .unsigned => maxInt(Scalar), - }, + const limb_len = comptime switch (@typeInfo(@TypeOf(scalar))) { + .ComptimeInt => calcLimbLen(scalar), + .Int => |info| calcTwosCompLimbCount(info.bits), else => @compileError("expected scalar to be an int"), }; - var limbs: [calcLimbLen(magnitude)]Limb = undefined; + var limbs: [limb_len]Limb = undefined; const operand = init(&limbs, scalar).toConst(); return add(r, a, operand); } @@ -2312,16 +2308,12 @@ pub const Const = struct { /// Same as `order` but the right-hand operand is a primitive integer. pub fn orderAgainstScalar(lhs: Const, scalar: anytype) math.Order { - const Scalar = @TypeOf(scalar); - const magnitude = switch (@typeInfo(Scalar)) { - .ComptimeInt => scalar, - .Int => |int| switch (int.signedness) { - .signed => minInt(Scalar), - .unsigned => maxInt(Scalar), - }, + const limb_len = comptime switch (@typeInfo(@TypeOf(scalar))) { + .ComptimeInt => calcLimbLen(scalar), + .Int => |info| calcTwosCompLimbCount(info.bits), else => @compileError("expected scalar to be an int"), }; - var limbs: [calcLimbLen(magnitude)]Limb = undefined; + var limbs: [limb_len]Limb = undefined; const rhs = Mutable.init(&limbs, scalar); return order(lhs, rhs.toConst()); }