mirror of
https://github.com/ziglang/zig.git
synced 2025-12-30 01:53:16 +00:00
Fix std.math.big.int.Managed capacity after mul and sqr
This commit is contained in:
parent
577f9fdbae
commit
d63604b116
@ -2780,7 +2780,7 @@ pub const Managed = struct {
|
||||
if (alias_count == 0) {
|
||||
m.mulNoAlias(a.toConst(), b.toConst(), rma.allocator);
|
||||
} else {
|
||||
const limb_count = calcMulLimbsBufferLen(a.limbs.len, b.limbs.len, alias_count);
|
||||
const limb_count = calcMulLimbsBufferLen(a.toConst().limbs.len, b.toConst().limbs.len, alias_count);
|
||||
const limbs_buffer = try rma.allocator.alloc(Limb, limb_count);
|
||||
defer rma.allocator.free(limbs_buffer);
|
||||
m.mul(a.toConst(), b.toConst(), limbs_buffer, rma.allocator);
|
||||
@ -2960,7 +2960,7 @@ pub const Managed = struct {
|
||||
|
||||
/// r = a * a
|
||||
pub fn sqr(rma: *Managed, a: *const Managed) !void {
|
||||
const needed_limbs = 2 * a.limbs.len + 1;
|
||||
const needed_limbs = 2 * a.toConst().limbs.len + 1;
|
||||
|
||||
if (rma.limbs.ptr == a.limbs.ptr) {
|
||||
var m = try Managed.initCapacity(rma.allocator, needed_limbs);
|
||||
|
||||
@ -2883,3 +2883,35 @@ test "big int byte swap" {
|
||||
try byteSwapTest(i32, 0x123456f8, @bitCast(i32, @as(u32, 0xf8563412)));
|
||||
try byteSwapTest(i48, 0x123456789abc, @bitCast(i48, @as(u48, 0xbc9a78563412)));
|
||||
}
|
||||
|
||||
test "big.int mul multi-multi alias r with a and b" {
|
||||
var a = try Managed.initSet(testing.allocator, 2 * maxInt(Limb));
|
||||
defer a.deinit();
|
||||
|
||||
try a.mul(&a, &a);
|
||||
|
||||
var want = try Managed.initSet(testing.allocator, 4 * maxInt(Limb) * maxInt(Limb));
|
||||
defer want.deinit();
|
||||
|
||||
try testing.expect(a.eq(want));
|
||||
|
||||
if (@typeInfo(Limb).Int.bits == 64) {
|
||||
try testing.expectEqual(@as(usize, 5), a.limbs.len);
|
||||
}
|
||||
}
|
||||
|
||||
test "big.int sqr multi alias r with a" {
|
||||
var a = try Managed.initSet(testing.allocator, 2 * maxInt(Limb));
|
||||
defer a.deinit();
|
||||
|
||||
try a.sqr(&a);
|
||||
|
||||
var want = try Managed.initSet(testing.allocator, 4 * maxInt(Limb) * maxInt(Limb));
|
||||
defer want.deinit();
|
||||
|
||||
try testing.expect(a.eq(want));
|
||||
|
||||
if (@typeInfo(Limb).Int.bits == 64) {
|
||||
try testing.expectEqual(@as(usize, 5), a.limbs.len);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user