| author | |
| committer | |
| log | d63604b11681ab196fb65e856bb6b12ebe8930af |
| tree | 7754de11927bf58f2dd758ff2437e1421812be2d |
| parent | 577f9fdbae12eabbbf87bd2cc36a1565df3153b2 |
2 files changed, 34 insertions(+), 2 deletions(-)
lib/std/math/big/int.zig+2-2| ... | @@ -2780,7 +2780,7 @@ pub const Managed = struct { | ... | @@ -2780,7 +2780,7 @@ pub const Managed = struct { |
| 2780 | if (alias_count == 0) { | 2780 | if (alias_count == 0) { |
| 2781 | m.mulNoAlias(a.toConst(), b.toConst(), rma.allocator); | 2781 | m.mulNoAlias(a.toConst(), b.toConst(), rma.allocator); |
| 2782 | } else { | 2782 | } else { |
| 2783 | const limb_count = calcMulLimbsBufferLen(a.limbs.len, b.limbs.len, alias_count); | 2783 | const limb_count = calcMulLimbsBufferLen(a.toConst().limbs.len, b.toConst().limbs.len, alias_count); |
| 2784 | const limbs_buffer = try rma.allocator.alloc(Limb, limb_count); | 2784 | const limbs_buffer = try rma.allocator.alloc(Limb, limb_count); |
| 2785 | defer rma.allocator.free(limbs_buffer); | 2785 | defer rma.allocator.free(limbs_buffer); |
| 2786 | m.mul(a.toConst(), b.toConst(), limbs_buffer, rma.allocator); | 2786 | m.mul(a.toConst(), b.toConst(), limbs_buffer, rma.allocator); |
| ... | @@ -2960,7 +2960,7 @@ pub const Managed = struct { | ... | @@ -2960,7 +2960,7 @@ pub const Managed = struct { |
| 2960 | 2960 | ||
| 2961 | /// r = a * a | 2961 | /// r = a * a |
| 2962 | pub fn sqr(rma: *Managed, a: *const Managed) !void { | 2962 | pub fn sqr(rma: *Managed, a: *const Managed) !void { |
| 2963 | const needed_limbs = 2 * a.limbs.len + 1; | 2963 | const needed_limbs = 2 * a.toConst().limbs.len + 1; |
| 2964 | 2964 | ||
| 2965 | if (rma.limbs.ptr == a.limbs.ptr) { | 2965 | if (rma.limbs.ptr == a.limbs.ptr) { |
| 2966 | var m = try Managed.initCapacity(rma.allocator, needed_limbs); | 2966 | var m = try Managed.initCapacity(rma.allocator, needed_limbs); |
lib/std/math/big/int_test.zig+32| ... | @@ -2883,3 +2883,35 @@ test "big int byte swap" { | ... | @@ -2883,3 +2883,35 @@ test "big int byte swap" { |
| 2883 | try byteSwapTest(i32, 0x123456f8, @bitCast(i32, @as(u32, 0xf8563412))); | 2883 | try byteSwapTest(i32, 0x123456f8, @bitCast(i32, @as(u32, 0xf8563412))); |
| 2884 | try byteSwapTest(i48, 0x123456789abc, @bitCast(i48, @as(u48, 0xbc9a78563412))); | 2884 | try byteSwapTest(i48, 0x123456789abc, @bitCast(i48, @as(u48, 0xbc9a78563412))); |
| 2885 | } | 2885 | } |
| 2886 | |||
| 2887 | test "big.int mul multi-multi alias r with a and b" { | ||
| 2888 | var a = try Managed.initSet(testing.allocator, 2 * maxInt(Limb)); | ||
| 2889 | defer a.deinit(); | ||
| 2890 | |||
| 2891 | try a.mul(&a, &a); | ||
| 2892 | |||
| 2893 | var want = try Managed.initSet(testing.allocator, 4 * maxInt(Limb) * maxInt(Limb)); | ||
| 2894 | defer want.deinit(); | ||
| 2895 | |||
| 2896 | try testing.expect(a.eq(want)); | ||
| 2897 | |||
| 2898 | if (@typeInfo(Limb).Int.bits == 64) { | ||
| 2899 | try testing.expectEqual(@as(usize, 5), a.limbs.len); | ||
| 2900 | } | ||
| 2901 | } | ||
| 2902 | |||
| 2903 | test "big.int sqr multi alias r with a" { | ||
| 2904 | var a = try Managed.initSet(testing.allocator, 2 * maxInt(Limb)); | ||
| 2905 | defer a.deinit(); | ||
| 2906 | |||
| 2907 | try a.sqr(&a); | ||
| 2908 | |||
| 2909 | var want = try Managed.initSet(testing.allocator, 4 * maxInt(Limb) * maxInt(Limb)); | ||
| 2910 | defer want.deinit(); | ||
| 2911 | |||
| 2912 | try testing.expect(a.eq(want)); | ||
| 2913 | |||
| 2914 | if (@typeInfo(Limb).Int.bits == 64) { | ||
| 2915 | try testing.expectEqual(@as(usize, 5), a.limbs.len); | ||
| 2916 | } | ||
| 2917 | } |