authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-11 19:57:13-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-11 19:57:13-04:00
log2fe5bdb9ed4e17c94055a9539092bdf35a55f752
treed2d35c92701916c26371c8f1383a74c6fa95d8fa
parent5c49341f097dc666eb4ad8c26c6b00fd2189a396

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


1 files changed, 8 insertions(+), 16 deletions(-)

lib/std/math/big/int.zig+8-16
......@@ -391,16 +391,12 @@ pub const Mutable = struct {
391391 /// Asserts the result fits in `r`. An upper bound on the number of limbs needed by
392392 /// r is `math.max(a.limbs.len, calcLimbLen(scalar)) + 1`.
393393 pub fn addScalar(r: *Mutable, a: Const, scalar: anytype) void {
394 const Scalar = @TypeOf(scalar);
395 const magnitude = switch (@typeInfo(Scalar)) {
396 .ComptimeInt => scalar,
397 .Int => |int| switch (int.signedness) {
398 .signed => minInt(Scalar),
399 .unsigned => maxInt(Scalar),
400 },
394 const limb_len = comptime switch (@typeInfo(@TypeOf(scalar))) {
395 .ComptimeInt => calcLimbLen(scalar),
396 .Int => |info| calcTwosCompLimbCount(info.bits),
401397 else => @compileError("expected scalar to be an int"),
402398 };
403 var limbs: [calcLimbLen(magnitude)]Limb = undefined;
399 var limbs: [limb_len]Limb = undefined;
404400 const operand = init(&limbs, scalar).toConst();
405401 return add(r, a, operand);
406402 }
......@@ -2312,16 +2308,12 @@ pub const Const = struct {
23122308
23132309 /// Same as `order` but the right-hand operand is a primitive integer.
23142310 pub fn orderAgainstScalar(lhs: Const, scalar: anytype) math.Order {
2315 const Scalar = @TypeOf(scalar);
2316 const magnitude = switch (@typeInfo(Scalar)) {
2317 .ComptimeInt => scalar,
2318 .Int => |int| switch (int.signedness) {
2319 .signed => minInt(Scalar),
2320 .unsigned => maxInt(Scalar),
2321 },
2311 const limb_len = comptime switch (@typeInfo(@TypeOf(scalar))) {
2312 .ComptimeInt => calcLimbLen(scalar),
2313 .Int => |info| calcTwosCompLimbCount(info.bits),
23222314 else => @compileError("expected scalar to be an int"),
23232315 };
2324 var limbs: [calcLimbLen(magnitude)]Limb = undefined;
2316 var limbs: [limb_len]Limb = undefined;
23252317 const rhs = Mutable.init(&limbs, scalar);
23262318 return order(lhs, rhs.toConst());
23272319 }