aboutsummaryrefslogtreecommitdiff
path: root/lib/std/math/big
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-04-29 00:19:55 -0700
committerGitHub <noreply@github.com>2023-04-29 00:19:55 -0700
commitd65b42e07caa00dfe2f2fbf221c593ce57882784 (patch)
tree7926cbea1499e0affe930bf6d7455dc24adf014e /lib/std/math/big
parentfd6200eda6d4fe19c34a59430a88a9ce38d6d7a4 (diff)
parentfa200ca0cad2705bad40eb723dedf4e3bf11f2ff (diff)
downloadzig-d65b42e07caa00dfe2f2fbf221c593ce57882784.tar.gz
zig-d65b42e07caa00dfe2f2fbf221c593ce57882784.zip
Merge pull request #15481 from ziglang/use-mem-intrinsics
actually use the new memory intrinsics
Diffstat (limited to 'lib/std/math/big')
-rw-r--r--lib/std/math/big/int.zig64
1 files changed, 32 insertions, 32 deletions
diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig
index f97a763d95..2406d669ec 100644
--- a/lib/std/math/big/int.zig
+++ b/lib/std/math/big/int.zig
@@ -176,7 +176,7 @@ pub const Mutable = struct {
/// Asserts the value fits in the limbs buffer.
pub fn copy(self: *Mutable, other: Const) void {
if (self.limbs.ptr != other.limbs.ptr) {
- mem.copy(Limb, self.limbs[0..], other.limbs[0..other.limbs.len]);
+ @memcpy(self.limbs[0..other.limbs.len], other.limbs[0..other.limbs.len]);
}
self.positive = other.positive;
self.len = other.limbs.len;
@@ -199,7 +199,7 @@ pub const Mutable = struct {
/// can be modified separately from the original.
/// Asserts that limbs is big enough to store the value.
pub fn clone(other: Mutable, limbs: []Limb) Mutable {
- mem.copy(Limb, limbs, other.limbs[0..other.len]);
+ @memcpy(limbs[0..other.len], other.limbs[0..other.len]);
return .{
.limbs = limbs,
.len = other.len,
@@ -344,7 +344,7 @@ pub const Mutable = struct {
.min => {
// Negative bound, signed = -0x80.
r.len = req_limbs;
- mem.set(Limb, r.limbs[0 .. r.len - 1], 0);
+ @memset(r.limbs[0 .. r.len - 1], 0);
r.limbs[r.len - 1] = signmask;
r.positive = false;
},
@@ -363,7 +363,7 @@ pub const Mutable = struct {
const new_mask = (new_signmask << 1) -% 1; // 0b0..001..1 where the rightmost 0 is the sign bit.
r.len = new_req_limbs;
- std.mem.set(Limb, r.limbs[0 .. r.len - 1], maxInt(Limb));
+ @memset(r.limbs[0 .. r.len - 1], maxInt(Limb));
r.limbs[r.len - 1] = new_mask;
}
},
@@ -376,7 +376,7 @@ pub const Mutable = struct {
.max => {
// Max bound, unsigned = 0xFF
r.len = req_limbs;
- std.mem.set(Limb, r.limbs[0 .. r.len - 1], maxInt(Limb));
+ @memset(r.limbs[0 .. r.len - 1], maxInt(Limb));
r.limbs[r.len - 1] = mask;
},
},
@@ -489,7 +489,7 @@ pub const Mutable = struct {
if (msl < req_limbs) {
r.limbs[msl] = 1;
r.len = req_limbs;
- mem.set(Limb, r.limbs[msl + 1 .. req_limbs], 0);
+ @memset(r.limbs[msl + 1 .. req_limbs], 0);
} else {
carry_truncated = true;
}
@@ -637,14 +637,14 @@ pub const Mutable = struct {
const a_copy = if (rma.limbs.ptr == a.limbs.ptr) blk: {
const start = buf_index;
- mem.copy(Limb, limbs_buffer[buf_index..], a.limbs);
+ @memcpy(limbs_buffer[buf_index..][0..a.limbs.len], a.limbs);
buf_index += a.limbs.len;
break :blk a.toMutable(limbs_buffer[start..buf_index]).toConst();
} else a;
const b_copy = if (rma.limbs.ptr == b.limbs.ptr) blk: {
const start = buf_index;
- mem.copy(Limb, limbs_buffer[buf_index..], b.limbs);
+ @memcpy(limbs_buffer[buf_index..][0..b.limbs.len], b.limbs);
buf_index += b.limbs.len;
break :blk b.toMutable(limbs_buffer[start..buf_index]).toConst();
} else b;
@@ -676,7 +676,7 @@ pub const Mutable = struct {
}
}
- mem.set(Limb, rma.limbs[0 .. a.limbs.len + b.limbs.len], 0);
+ @memset(rma.limbs[0 .. a.limbs.len + b.limbs.len], 0);
llmulacc(.add, allocator, rma.limbs, a.limbs, b.limbs);
@@ -708,7 +708,7 @@ pub const Mutable = struct {
const a_copy = if (rma.limbs.ptr == a.limbs.ptr) blk: {
const start = buf_index;
const a_len = math.min(req_limbs, a.limbs.len);
- mem.copy(Limb, limbs_buffer[buf_index..], a.limbs[0..a_len]);
+ @memcpy(limbs_buffer[buf_index..][0..a_len], a.limbs[0..a_len]);
buf_index += a_len;
break :blk a.toMutable(limbs_buffer[start..buf_index]).toConst();
} else a;
@@ -716,7 +716,7 @@ pub const Mutable = struct {
const b_copy = if (rma.limbs.ptr == b.limbs.ptr) blk: {
const start = buf_index;
const b_len = math.min(req_limbs, b.limbs.len);
- mem.copy(Limb, limbs_buffer[buf_index..], b.limbs[0..b_len]);
+ @memcpy(limbs_buffer[buf_index..][0..b_len], b.limbs[0..b_len]);
buf_index += b_len;
break :blk a.toMutable(limbs_buffer[start..buf_index]).toConst();
} else b;
@@ -751,7 +751,7 @@ pub const Mutable = struct {
const a_limbs = a.limbs[0..math.min(req_limbs, a.limbs.len)];
const b_limbs = b.limbs[0..math.min(req_limbs, b.limbs.len)];
- mem.set(Limb, rma.limbs[0..req_limbs], 0);
+ @memset(rma.limbs[0..req_limbs], 0);
llmulacc(.add, allocator, rma.limbs, a_limbs, b_limbs);
rma.normalize(math.min(req_limbs, a.limbs.len + b.limbs.len));
@@ -919,7 +919,7 @@ pub const Mutable = struct {
_ = opt_allocator;
assert(rma.limbs.ptr != a.limbs.ptr); // illegal aliasing
- mem.set(Limb, rma.limbs, 0);
+ @memset(rma.limbs, 0);
llsquareBasecase(rma.limbs, a.limbs);
@@ -1522,7 +1522,7 @@ pub const Mutable = struct {
if (xy_trailing != 0) {
// Manually shift here since we know its limb aligned.
mem.copyBackwards(Limb, r.limbs[xy_trailing..], r.limbs[0..r.len]);
- mem.set(Limb, r.limbs[0..xy_trailing], 0);
+ @memset(r.limbs[0..xy_trailing], 0);
r.len += xy_trailing;
}
}
@@ -1556,7 +1556,7 @@ pub const Mutable = struct {
// for 0 <= j <= n - t, set q[j] to 0
q.len = shift + 1;
q.positive = true;
- mem.set(Limb, q.limbs[0..q.len], 0);
+ @memset(q.limbs[0..q.len], 0);
// 2.
// while x >= y * b^(n - t):
@@ -1691,7 +1691,7 @@ pub const Mutable = struct {
r.addScalar(a.abs(), -1);
if (req_limbs > r.len) {
- mem.set(Limb, r.limbs[r.len..req_limbs], 0);
+ @memset(r.limbs[r.len..req_limbs], 0);
}
assert(r.limbs.len >= req_limbs);
@@ -1730,7 +1730,7 @@ pub const Mutable = struct {
// Zero-extend the result
if (req_limbs > r.len) {
- mem.set(Limb, r.limbs[r.len..req_limbs], 0);
+ @memset(r.limbs[r.len..req_limbs], 0);
}
// Truncate to required number of limbs.
@@ -1921,8 +1921,8 @@ pub const Const = struct {
/// The result is an independent resource which is managed by the caller.
pub fn toManaged(self: Const, allocator: Allocator) Allocator.Error!Managed {
- const limbs = try allocator.alloc(Limb, math.max(Managed.default_capacity, self.limbs.len));
- mem.copy(Limb, limbs, self.limbs);
+ const limbs = try allocator.alloc(Limb, @max(Managed.default_capacity, self.limbs.len));
+ @memcpy(limbs[0..self.limbs.len], self.limbs);
return Managed{
.allocator = allocator,
.limbs = limbs,
@@ -1935,7 +1935,7 @@ pub const Const = struct {
/// Asserts `limbs` is big enough to store the value.
pub fn toMutable(self: Const, limbs: []Limb) Mutable {
- mem.copy(Limb, limbs, self.limbs[0..self.limbs.len]);
+ @memcpy(limbs[0..self.limbs.len], self.limbs[0..self.limbs.len]);
return .{
.limbs = limbs,
.positive = self.positive,
@@ -2253,7 +2253,7 @@ pub const Const = struct {
.positive = true, // Make absolute by ignoring self.positive.
.len = self.limbs.len,
};
- mem.copy(Limb, q.limbs, self.limbs);
+ @memcpy(q.limbs[0..self.limbs.len], self.limbs);
var r: Mutable = .{
.limbs = limbs_buffer[q.limbs.len..][0..self.limbs.len],
@@ -2587,8 +2587,8 @@ pub const Managed = struct {
.allocator = allocator,
.metadata = other.metadata,
.limbs = block: {
- var limbs = try allocator.alloc(Limb, other.len());
- mem.copy(Limb, limbs[0..], other.limbs[0..other.len()]);
+ const limbs = try allocator.alloc(Limb, other.len());
+ @memcpy(limbs, other.limbs[0..other.len()]);
break :block limbs;
},
};
@@ -2600,7 +2600,7 @@ pub const Managed = struct {
if (self.limbs.ptr == other.limbs.ptr) return;
try self.ensureCapacity(other.limbs.len);
- mem.copy(Limb, self.limbs[0..], other.limbs[0..other.limbs.len]);
+ @memcpy(self.limbs[0..other.limbs.len], other.limbs[0..other.limbs.len]);
self.setMetadata(other.positive, other.limbs.len);
}
@@ -3302,7 +3302,7 @@ fn llmulaccKaratsuba(
// Note, we don't need to compute all of p2, just enough limbs to satisfy r.
const p2_limbs = math.min(limbs_after_split, a1.len + b1.len);
- mem.set(Limb, tmp[0..p2_limbs], 0);
+ @memset(tmp[0..p2_limbs], 0);
llmulacc(.add, allocator, tmp[0..p2_limbs], a1[0..math.min(a1.len, p2_limbs)], b1[0..math.min(b1.len, p2_limbs)]);
const p2 = tmp[0..llnormalize(tmp[0..p2_limbs])];
@@ -3317,7 +3317,7 @@ fn llmulaccKaratsuba(
// Compute p0.
// Since a0.len, b0.len <= split and r.len >= split * 2, the full width of p0 needs to be computed.
const p0_limbs = a0.len + b0.len;
- mem.set(Limb, tmp[0..p0_limbs], 0);
+ @memset(tmp[0..p0_limbs], 0);
llmulacc(.add, allocator, tmp[0..p0_limbs], a0, b0);
const p0 = tmp[0..llnormalize(tmp[0..p0_limbs])];
@@ -3341,7 +3341,7 @@ fn llmulaccKaratsuba(
return;
}
- mem.set(Limb, tmp, 0);
+ @memset(tmp, 0);
// p1 is nonzero, so compute the intermediary terms j0 = a0 - a1 and j1 = b1 - b0.
// Note that in this case, we again need some storage for intermediary results
@@ -3666,7 +3666,7 @@ fn llshl(r: []Limb, a: []const Limb, shift: usize) void {
}
r[limb_shift - 1] = carry;
- mem.set(Limb, r[0 .. limb_shift - 1], 0);
+ @memset(r[0 .. limb_shift - 1], 0);
}
fn llshr(r: []Limb, a: []const Limb, shift: usize) void {
@@ -4061,8 +4061,8 @@ fn llpow(r: []Limb, a: []const Limb, b: u32, tmp_limbs: []Limb) void {
tmp2 = tmp_limbs;
}
- mem.copy(Limb, tmp1, a);
- mem.set(Limb, tmp1[a.len..], 0);
+ @memcpy(tmp1[0..a.len], a);
+ @memset(tmp1[a.len..], 0);
// Scan the exponent as a binary number, from left to right, dropping the
// most significant bit set.
@@ -4074,14 +4074,14 @@ fn llpow(r: []Limb, a: []const Limb, b: u32, tmp_limbs: []Limb) void {
var i: usize = 0;
while (i < exp_bits) : (i += 1) {
// Square
- mem.set(Limb, tmp2, 0);
+ @memset(tmp2, 0);
llsquareBasecase(tmp2, tmp1[0..llnormalize(tmp1)]);
mem.swap([]Limb, &tmp1, &tmp2);
// Multiply by a
const ov = @shlWithOverflow(exp, 1);
exp = ov[0];
if (ov[1] != 0) {
- mem.set(Limb, tmp2, 0);
+ @memset(tmp2, 0);
llmulacc(.add, null, tmp2, tmp1[0..llnormalize(tmp1)], a);
mem.swap([]Limb, &tmp1, &tmp2);
}