authorgravatar for git.litmus427@passinbox.comunplanned <git.litmus427@passinbox.com> 2025-12-04 01:05:17+01:00
committergravatar for git.litmus427@passinbox.comunplanned <git.litmus427@passinbox.com> 2025-12-04 10:28:42+01:00
log688af047255790d9aec4a1152eaa525f7dd5b46d
tree11c42abfc8f133dd6072e0ab084d9a5e0dd8c233
parent73e82332d02e42a8b95f5c30652d4b7d0db7bea6

math.big: stronger asserts to reduce risks of aliasing


1 files changed, 7 insertions(+), 0 deletions(-)

lib/std/math/big/int.zig+7
...@@ -3630,6 +3630,7 @@ fn llmulaccKaratsuba(...@@ -3630,6 +3630,7 @@ fn llmulaccKaratsuba(
3630/// r = r (op) a.3630/// r = r (op) a.
3631/// The result is computed modulo `r.len`.3631/// The result is computed modulo `r.len`.
3632fn llaccum(comptime op: AccOp, r: []Limb, a: []const Limb) void {3632fn llaccum(comptime op: AccOp, r: []Limb, a: []const Limb) void {
3633 assert(!slicesOverlap(r, a) or @intFromPtr(r.ptr) <= @intFromPtr(a.ptr));
3633 if (op == .sub) {3634 if (op == .sub) {
3634 _ = llsubcarry(r, r, a);3635 _ = llsubcarry(r, r, a);
3635 return;3636 return;
...@@ -3699,6 +3700,8 @@ fn llmulaccLong(comptime op: AccOp, r: []Limb, a: []const Limb, b: []const Limb)...@@ -3699,6 +3700,8 @@ fn llmulaccLong(comptime op: AccOp, r: []Limb, a: []const Limb, b: []const Limb)
3699/// The result is computed modulo `r.len`.3700/// The result is computed modulo `r.len`.
3700/// Returns whether the operation overflowed.3701/// Returns whether the operation overflowed.
3701fn llmulLimb(comptime op: AccOp, acc: []Limb, y: []const Limb, xi: Limb) bool {3702fn llmulLimb(comptime op: AccOp, acc: []Limb, y: []const Limb, xi: Limb) bool {
3703 assert(!slicesOverlap(acc, y) or @intFromPtr(acc.ptr) <= @intFromPtr(y.ptr));
3704
3702 if (xi == 0) {3705 if (xi == 0) {
3703 return false;3706 return false;
3704 }3707 }
...@@ -3761,6 +3764,8 @@ fn llsubcarry(r: []Limb, a: []const Limb, b: []const Limb) Limb {...@@ -3761,6 +3764,8 @@ fn llsubcarry(r: []Limb, a: []const Limb, b: []const Limb) Limb {
3761 assert(a.len != 0 and b.len != 0);3764 assert(a.len != 0 and b.len != 0);
3762 assert(a.len >= b.len);3765 assert(a.len >= b.len);
3763 assert(r.len >= a.len);3766 assert(r.len >= a.len);
3767 assert(!slicesOverlap(r, a) or @intFromPtr(r.ptr) <= @intFromPtr(a.ptr));
3768 assert(!slicesOverlap(r, b) or @intFromPtr(r.ptr) <= @intFromPtr(b.ptr));
37643769
3765 var i: usize = 0;3770 var i: usize = 0;
3766 var borrow: Limb = 0;3771 var borrow: Limb = 0;
...@@ -3792,6 +3797,8 @@ fn lladdcarry(r: []Limb, a: []const Limb, b: []const Limb) Limb {...@@ -3792,6 +3797,8 @@ fn lladdcarry(r: []Limb, a: []const Limb, b: []const Limb) Limb {
3792 assert(a.len != 0 and b.len != 0);3797 assert(a.len != 0 and b.len != 0);
3793 assert(a.len >= b.len);3798 assert(a.len >= b.len);
3794 assert(r.len >= a.len);3799 assert(r.len >= a.len);
3800 assert(!slicesOverlap(r, a) or @intFromPtr(r.ptr) <= @intFromPtr(a.ptr));
3801 assert(!slicesOverlap(r, b) or @intFromPtr(r.ptr) <= @intFromPtr(b.ptr));
37953802
3796 var i: usize = 0;3803 var i: usize = 0;
3797 var carry: Limb = 0;3804 var carry: Limb = 0;