diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig index 30b1530932ca..181615ec0cee 100644 --- a/lib/std/math/big/int.zig +++ b/lib/std/math/big/int.zig @@ -1358,7 +1358,7 @@ pub const Mutable = struct { var tmp_x = try Managed.init(limbs_buffer.allocator); defer tmp_x.deinit(); - while (y.len() > 1) { + while (y.len() > 1 and !y.eqZero()) { assert(x.isPositive() and y.isPositive()); assert(x.len() >= y.len()); @@ -1576,6 +1576,10 @@ pub const Mutable = struct { // for i from n down to t + 1, do var i = n; while (i >= t + 1) : (i -= 1) { + if (x.eqZero()) { + break; + } + const k = i - t - 1; // 3.1. // if x_i == y_t: @@ -3676,8 +3680,8 @@ fn llsignedor(r: []Limb, a: []const Limb, a_positive: bool, b: []const Limb, b_p fn llsignedand(r: []Limb, a: []const Limb, a_positive: bool, b: []const Limb, b_positive: bool) bool { @setRuntimeSafety(debug_safety); assert(a.len != 0 and b.len != 0); - assert(r.len >= a.len); assert(a.len >= b.len); + assert(r.len >= if (!a_positive and !b_positive) a.len + 1 else b.len); if (a_positive and b_positive) { // Trivial case, result is positive. diff --git a/lib/std/math/big/int_test.zig b/lib/std/math/big/int_test.zig index 71c80d5a837e..73b549d20494 100644 --- a/lib/std/math/big/int_test.zig +++ b/lib/std/math/big/int_test.zig @@ -1345,6 +1345,68 @@ test "big.int div trunc single-single -/-" { try testing.expect((try r.to(i32)) == er); } +test "big.int divFloor #10932" { + var a = try Managed.init(testing.allocator); + defer a.deinit(); + + var b = try Managed.init(testing.allocator); + defer b.deinit(); + + var res = try Managed.init(testing.allocator); + defer res.deinit(); + + try a.setString(10, "40000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); + try b.setString(10, "8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); + + var mod = try Managed.init(testing.allocator); + defer mod.deinit(); + + try res.divFloor(&mod, a.toConst(), b.toConst()); + + const ress = try res.toString(testing.allocator, 16, .lower); + defer testing.allocator.free(ress); + try testing.expect(std.mem.eql(u8, ress, "194bd136316c046d070b763396297bf8869a605030216b52597015902a172b2a752f62af1568dcd431602f03725bfa62b0be71ae86616210972c0126e173503011ca48c5747ff066d159c95e46b69cbb14c8fc0bd2bf0919f921be96463200000000000000000000000000000000000000000000000000000000000000000000000000000000")); + try testing.expect((try mod.to(i32)) == 0); +} + +test "big.int gcd #10932" { + var a = try Managed.init(testing.allocator); + defer a.deinit(); + + var b = try Managed.init(testing.allocator); + defer b.deinit(); + + var res = try Managed.init(testing.allocator); + defer res.deinit(); + + try a.setString(10, "3000000000000000000000000000000000000000000000000000000000000000000000001461501637330902918203684832716283019655932542975000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); + try b.setString(10, "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200001001500000000000000000100000000040000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000003000000000000000000000000000000000000000000000000000058715661000000000000000000000000000000000000023553252000000000180000000000000000000000000000000000000000000000000250000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001005000002000000000000000000000000000000000000000021000000001000000000000000000000000100000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000200000000000000000000004000000000000000000000000000000000000000000000301000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); + + try res.gcd(a, b); + + const ress = try res.toString(testing.allocator, 16, .lower); + defer testing.allocator.free(ress); + try testing.expect(std.mem.eql(u8, ress, "1a974a5c9734476ff5a3604bcc678a756beacfc21b4427d1f2c1f56f5d4e411a162c56136e20000000000000000000000000000000")); +} + +test "big.int bitAnd #10932" { + var a = try Managed.init(testing.allocator); + defer a.deinit(); + + var b = try Managed.init(testing.allocator); + defer b.deinit(); + + var res = try Managed.init(testing.allocator); + defer res.deinit(); + + try a.setString(10, "154954885951624787839743960731760616696"); + try b.setString(10, "55000000000915215865915724129619485917228346934191537590366734850266784978214506142389798064826139649163838075568111457203909393174933092857416500785632012953993352521899237655507306575657169267399324107627651067352600878339870446048204062696260567762088867991835386857942106708741836433444432529637331429212430394179472179237695833247299409249810963487516399177133175950185719220422442438098353430605822151595560743492661038899294517012784306863064670126197566982968906306814338148792888550378533207318063660581924736840687332023636827401670268933229183389040490792300121030647791095178823932734160000000000000000000000000000000000000555555550000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); + + try res.bitAnd(a, b); + + try testing.expect((try res.to(i32)) == 0); +} + test "big.int div floor single-single +/+" { const u: i32 = 5; const v: i32 = 3;