authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-03-25 18:44:35+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-03-25 18:44:35+01:00
logcb8d7a811003b08a7e16c533577fee92113585b1
tree5d7199b3ed8237a23f2dcf8e61bcaaff402181b9
parent6b6dc1cd3ac2690490628e0fee276e143e431b39
parent22013f1b39434e496acc540fb91c293202540553
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #23310 from Rexicon226/fix-23309

big.int: return normalized results from `{add,sub}Carry`

4 files changed, 53 insertions(+), 5 deletions(-)

lib/std/math/big/int.zig+15-4
......@@ -76,9 +76,18 @@ pub fn calcSqrtLimbsBufferLen(a_bit_count: usize) usize {
7676 return a_limb_count + 3 * u_s_rem_limb_count + calcDivLimbsBufferLen(a_limb_count, u_s_rem_limb_count);
7777}
7878
79// Compute the number of limbs required to store a 2s-complement number of `bit_count` bits.
79/// Compute the number of limbs required to store a 2s-complement number of `bit_count` bits.
80pub fn calcNonZeroTwosCompLimbCount(bit_count: usize) usize {
81 assert(bit_count != 0);
82 return calcTwosCompLimbCount(bit_count);
83}
84
85/// Compute the number of limbs required to store a 2s-complement number of `bit_count` bits.
86///
87/// Special cases `bit_count == 0` to return 1. Zero-bit integers can only store the value zero
88/// and this big integer implementation stores zero using one limb.
8089pub fn calcTwosCompLimbCount(bit_count: usize) usize {
81 return std.math.divCeil(usize, bit_count, @bitSizeOf(Limb)) catch unreachable;
90 return @max(std.math.divCeil(usize, bit_count, @bitSizeOf(Limb)) catch unreachable, 1);
8291}
8392
8493/// a + b * c + *carry, sets carry to the overflow bits
......@@ -188,8 +197,10 @@ pub const Mutable = struct {
188197 if (self.limbs.ptr != other.limbs.ptr) {
189198 @memcpy(self.limbs[0..other.limbs.len], other.limbs[0..other.limbs.len]);
190199 }
191 self.positive = other.positive;
192 self.len = other.limbs.len;
200 // Normalize before setting `positive` so the `eqlZero` doesn't need to iterate
201 // over the extra zero limbs.
202 self.normalize(other.limbs.len);
203 self.positive = other.positive or other.eqlZero();
193204 }
194205
195206 /// Efficiently swap an Mutable with another. This swaps the limb pointers and a full copy is not
lib/std/math/big/int_test.zig+28
......@@ -726,6 +726,34 @@ test "subWrap single-multi, signed, limb aligned" {
726726 try testing.expect((try a.toInt(SignedDoubleLimb)) == maxInt(SignedDoubleLimb));
727727}
728728
729test "addWrap returns normalized result" {
730 var x = try Managed.initSet(testing.allocator, 0);
731 defer x.deinit();
732 var y = try Managed.initSet(testing.allocator, 0);
733 defer y.deinit();
734
735 // make them both non normalized "-0"
736 x.setMetadata(false, 1);
737 y.setMetadata(false, 1);
738
739 var r = try Managed.init(testing.allocator);
740 defer r.deinit();
741 try testing.expect(!(try r.addWrap(&x, &y, .unsigned, 64)));
742 try testing.expect(r.isPositive() and r.len() == 1 and r.limbs[0] == 0);
743}
744
745test "subWrap returns normalized result" {
746 var x = try Managed.initSet(testing.allocator, 0);
747 defer x.deinit();
748 var y = try Managed.initSet(testing.allocator, 0);
749 defer y.deinit();
750
751 var r = try Managed.init(testing.allocator);
752 defer r.deinit();
753 try testing.expect(!(try r.subWrap(&x, &y, .unsigned, 64)));
754 try testing.expect(r.isPositive() and r.len() == 1 and r.limbs[0] == 0);
755}
756
729757test "addSat single-single, unsigned" {
730758 var a = try Managed.initSet(testing.allocator, maxInt(u17) - 5);
731759 defer a.deinit();
src/Value.zig+1-1
......@@ -2223,7 +2223,7 @@ pub fn shlSatScalar(
22232223 const shift: usize = @intCast(rhs.toUnsignedInt(zcu));
22242224 const limbs = try arena.alloc(
22252225 std.math.big.Limb,
2226 std.math.big.int.calcTwosCompLimbCount(info.bits) + 1,
2226 std.math.big.int.calcTwosCompLimbCount(info.bits),
22272227 );
22282228 var result_bigint = BigIntMutable{
22292229 .limbs = limbs,
test/behavior/for.zig+9
......@@ -535,3 +535,12 @@ test "return from inline for" {
535535 };
536536 try std.testing.expect(!S.do());
537537}
538
539test "for loop 0 length range" {
540 const map: []const u8 = &.{};
541 for (map, 0..map.len) |i, j| {
542 _ = i;
543 _ = j;
544 comptime unreachable;
545 }
546}