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 {...@@ -391,16 +391,12 @@ pub const Mutable = struct {
391 /// Asserts the result fits in `r`. An upper bound on the number of limbs needed by391 /// Asserts the result fits in `r`. An upper bound on the number of limbs needed by
392 /// r is `math.max(a.limbs.len, calcLimbLen(scalar)) + 1`.392 /// r is `math.max(a.limbs.len, calcLimbLen(scalar)) + 1`.
393 pub fn addScalar(r: *Mutable, a: Const, scalar: anytype) void {393 pub fn addScalar(r: *Mutable, a: Const, scalar: anytype) void {
394 const Scalar = @TypeOf(scalar);394 const limb_len = comptime switch (@typeInfo(@TypeOf(scalar))) {
395 const magnitude = switch (@typeInfo(Scalar)) {395 .ComptimeInt => calcLimbLen(scalar),
396 .ComptimeInt => scalar,396 .Int => |info| calcTwosCompLimbCount(info.bits),
397 .Int => |int| switch (int.signedness) {
398 .signed => minInt(Scalar),
399 .unsigned => maxInt(Scalar),
400 },
401 else => @compileError("expected scalar to be an int"),397 else => @compileError("expected scalar to be an int"),
402 };398 };
403 var limbs: [calcLimbLen(magnitude)]Limb = undefined;399 var limbs: [limb_len]Limb = undefined;
404 const operand = init(&limbs, scalar).toConst();400 const operand = init(&limbs, scalar).toConst();
405 return add(r, a, operand);401 return add(r, a, operand);
406 }402 }
...@@ -2312,16 +2308,12 @@ pub const Const = struct {...@@ -2312,16 +2308,12 @@ pub const Const = struct {
23122308
2313 /// Same as `order` but the right-hand operand is a primitive integer.2309 /// Same as `order` but the right-hand operand is a primitive integer.
2314 pub fn orderAgainstScalar(lhs: Const, scalar: anytype) math.Order {2310 pub fn orderAgainstScalar(lhs: Const, scalar: anytype) math.Order {
2315 const Scalar = @TypeOf(scalar);2311 const limb_len = comptime switch (@typeInfo(@TypeOf(scalar))) {
2316 const magnitude = switch (@typeInfo(Scalar)) {2312 .ComptimeInt => calcLimbLen(scalar),
2317 .ComptimeInt => scalar,2313 .Int => |info| calcTwosCompLimbCount(info.bits),
2318 .Int => |int| switch (int.signedness) {
2319 .signed => minInt(Scalar),
2320 .unsigned => maxInt(Scalar),
2321 },
2322 else => @compileError("expected scalar to be an int"),2314 else => @compileError("expected scalar to be an int"),
2323 };2315 };
2324 var limbs: [calcLimbLen(magnitude)]Limb = undefined;2316 var limbs: [limb_len]Limb = undefined;
2325 const rhs = Mutable.init(&limbs, scalar);2317 const rhs = Mutable.init(&limbs, scalar);
2326 return order(lhs, rhs.toConst());2318 return order(lhs, rhs.toConst());
2327 }2319 }