authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-21 13:05:14-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-01-21 13:05:14-05:00
log18fcb3b5e82d84e50b951373203f48325b2feada
treecc9aa163cfdbeb3b03d7d4266b492e7880e5b1fc
parentd652dd065858c754f6d744beec3e7e5bc4ec058b
parentb7a887f0fb7d166ad93eae62ecd93d1ed173297e
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #18912 from dweiller/memcpy-opt

optimized memcpy

3 files changed, 347 insertions(+), 112 deletions(-)

lib/compiler_rt.zig+1
......@@ -233,6 +233,7 @@ comptime {
233233
234234 _ = @import("compiler_rt/memcpy.zig");
235235 _ = @import("compiler_rt/memset.zig");
236 _ = @import("compiler_rt/memmove.zig");
236237 _ = @import("compiler_rt/memcmp.zig");
237238 _ = @import("compiler_rt/bcmp.zig");
238239 _ = @import("compiler_rt/ssp.zig");
lib/compiler_rt/memcpy.zig+176-112
......@@ -1,149 +1,213 @@
11const std = @import("std");
2const assert = std.debug.assert;
23const common = @import("./common.zig");
34const builtin = @import("builtin");
45
56comptime {
67 if (builtin.object_format != .c) {
7 @export(&memcpy, .{ .name = "memcpy", .linkage = common.linkage, .visibility = common.visibility });
8 @export(&memmove, .{ .name = "memmove", .linkage = common.linkage, .visibility = common.visibility });
8 const export_options: std.builtin.ExportOptions = .{
9 .name = "memcpy",
10 .linkage = common.linkage,
11 .visibility = common.visibility,
12 };
13
14 if (builtin.mode == .ReleaseSmall)
15 @export(&memcpySmall, export_options)
16 else
17 @export(&memcpyFast, export_options);
918 }
1019}
1120
12fn memcpy(noalias opt_dest: ?[*]u8, noalias opt_src: ?[*]const u8, len: usize) callconv(.C) ?[*]u8 {
13 return memmove(opt_dest, opt_src, len);
21const Element = if (std.simd.suggestVectorLength(u8)) |vec_size|
22 @Type(.{ .vector = .{
23 .child = u8,
24 .len = vec_size,
25 } })
26else
27 usize;
28
29comptime {
30 assert(@sizeOf(Element) >= @alignOf(Element));
31 assert(std.math.isPowerOfTwo(@sizeOf(Element)));
1432}
1533
16fn memmove(opt_dest: ?[*]u8, opt_src: ?[*]const u8, len: usize) callconv(.C) ?[*]u8 {
17 // a port of https://github.com/facebook/folly/blob/1c8bc50e88804e2a7361a57cd9b551dd10f6c5fd/folly/memcpy.S
18 if (len == 0) {
19 @branchHint(.unlikely);
20 return opt_dest;
21 }
34fn memcpySmall(noalias dest: ?[*]u8, noalias src: ?[*]const u8, len: usize) callconv(.C) ?[*]u8 {
35 @setRuntimeSafety(builtin.is_test);
2236
23 const dest = opt_dest.?;
24 const src = opt_src.?;
25
26 if (len < 8) {
27 @branchHint(.unlikely);
28 if (len == 1) {
29 @branchHint(.unlikely);
30 dest[0] = src[0];
31 } else if (len >= 4) {
32 @branchHint(.unlikely);
33 blockCopy(dest, src, 4, len);
34 } else {
35 blockCopy(dest, src, 2, len);
36 }
37 return dest;
37 for (0..len) |i| {
38 dest.?[i] = src.?[i];
3839 }
3940
40 if (len > 32) {
41 @branchHint(.unlikely);
42 if (len > 256) {
43 @branchHint(.unlikely);
44 copyMove(dest, src, len);
45 return dest;
46 }
47 copyLong(dest, src, len);
48 return dest;
49 }
41 return dest;
42}
5043
51 if (len > 16) {
52 @branchHint(.unlikely);
53 blockCopy(dest, src, 16, len);
54 return dest;
55 }
44fn memcpyFast(noalias dest: ?[*]u8, noalias src: ?[*]const u8, len: usize) callconv(.C) ?[*]u8 {
45 @setRuntimeSafety(builtin.is_test);
46
47 const small_limit = 2 * @sizeOf(Element);
5648
57 blockCopy(dest, src, 8, len);
49 if (copySmallLength(small_limit, dest.?, src.?, len)) return dest;
50
51 copyForwards(dest.?, src.?, len);
5852
5953 return dest;
6054}
6155
62inline fn blockCopy(dest: [*]u8, src: [*]const u8, block_size: comptime_int, len: usize) void {
63 const first = @as(*align(1) const @Vector(block_size, u8), src[0..block_size]).*;
64 const second = @as(*align(1) const @Vector(block_size, u8), src[len - block_size ..][0..block_size]).*;
65 dest[0..block_size].* = first;
66 dest[len - block_size ..][0..block_size].* = second;
67}
56inline fn copySmallLength(
57 comptime small_limit: comptime_int,
58 dest: [*]u8,
59 src: [*]const u8,
60 len: usize,
61) bool {
62 if (len < 16) {
63 copyLessThan16(dest, src, len);
64 return true;
65 }
6866
69inline fn copyLong(dest: [*]u8, src: [*]const u8, len: usize) void {
70 var array: [8]@Vector(32, u8) = undefined;
67 if (comptime 2 < (std.math.log2(small_limit) + 1) / 2) {
68 if (copy16ToSmallLimit(small_limit, dest, src, len)) return true;
69 }
7170
72 inline for (.{ 64, 128, 192, 256 }, 0..) |N, i| {
73 array[i * 2] = src[(N / 2) - 32 ..][0..32].*;
74 array[(i * 2) + 1] = src[len - N / 2 ..][0..32].*;
71 return false;
72}
7573
76 if (len <= N) {
77 @branchHint(.unlikely);
78 for (0..i + 1) |j| {
79 dest[j * 32 ..][0..32].* = array[j * 2];
80 dest[len - ((j * 32) + 32) ..][0..32].* = array[(j * 2) + 1];
81 }
82 return;
83 }
74inline fn copyLessThan16(
75 dest: [*]u8,
76 src: [*]const u8,
77 len: usize,
78) void {
79 @setRuntimeSafety(builtin.is_test);
80 if (len < 4) {
81 if (len == 0) return;
82 dest[0] = src[0];
83 dest[len / 2] = src[len / 2];
84 dest[len - 1] = src[len - 1];
85 return;
8486 }
87 copyRange4(4, dest, src, len);
8588}
8689
87inline fn copyMove(dest: [*]u8, src: [*]const u8, len: usize) void {
88 if (@intFromPtr(src) >= @intFromPtr(dest)) {
89 @branchHint(.unlikely);
90 copyForward(dest, src, len);
91 } else if (@intFromPtr(src) + len > @intFromPtr(dest)) {
92 @branchHint(.unlikely);
93 overlapBwd(dest, src, len);
94 } else {
95 copyForward(dest, src, len);
90inline fn copy16ToSmallLimit(
91 comptime small_limit: comptime_int,
92 dest: [*]u8,
93 src: [*]const u8,
94 len: usize,
95) bool {
96 @setRuntimeSafety(builtin.is_test);
97 inline for (2..(std.math.log2(small_limit) + 1) / 2 + 1) |p| {
98 const limit = 1 << (2 * p);
99 if (len < limit) {
100 copyRange4(limit / 4, dest, src, len);
101 return true;
102 }
96103 }
104 return false;
97105}
98106
99inline fn copyForward(dest: [*]u8, src: [*]const u8, len: usize) void {
100 const tail: @Vector(32, u8) = src[len - 32 ..][0..32].*;
107inline fn copyForwards(
108 noalias dest: [*]u8,
109 noalias src: [*]const u8,
110 len: usize,
111) void {
112 @setRuntimeSafety(builtin.is_test);
113 assert(len >= 2 * @sizeOf(Element));
114
115 dest[0..@sizeOf(Element)].* = src[0..@sizeOf(Element)].*;
116 const alignment_offset = @alignOf(Element) - @intFromPtr(src) % @alignOf(Element);
117 const n = len - alignment_offset;
118 const d = dest + alignment_offset;
119 const s = src + alignment_offset;
120
121 copyBlocksAlignedSource(@ptrCast(d), @alignCast(@ptrCast(s)), n);
122
123 // copy last `@sizeOf(Element)` bytes unconditionally, since block copy
124 // methods only copy a multiple of `@sizeOf(Element)` bytes.
125 const offset = len - @sizeOf(Element);
126 dest[offset..][0..@sizeOf(Element)].* = src[offset..][0..@sizeOf(Element)].*;
127}
101128
102 const N: usize = len & ~@as(usize, 127);
103 var i: usize = 0;
129inline fn copyBlocksAlignedSource(
130 noalias dest: [*]align(1) Element,
131 noalias src: [*]const Element,
132 max_bytes: usize,
133) void {
134 copyBlocks(dest, src, max_bytes);
135}
104136
105 while (i < N) : (i += 128) {
106 dest[i..][0..32].* = src[i..][0..32].*;
107 dest[i + 32 ..][0..32].* = src[i + 32 ..][0..32].*;
108 dest[i + 64 ..][0..32].* = src[i + 64 ..][0..32].*;
109 dest[i + 96 ..][0..32].* = src[i + 96 ..][0..32].*;
110 }
137/// Copies the largest multiple of `@sizeOf(T)` bytes from `src` to `dest`,
138/// that is less than `max_bytes` where `T` is the child type of `src` and
139/// `dest`.
140inline fn copyBlocks(
141 noalias dest: anytype,
142 noalias src: anytype,
143 max_bytes: usize,
144) void {
145 @setRuntimeSafety(builtin.is_test);
146
147 const T = @typeInfo(@TypeOf(dest)).pointer.child;
148 comptime assert(T == @typeInfo(@TypeOf(src)).pointer.child);
111149
112 if (len - i <= 32) {
113 @branchHint(.unlikely);
114 dest[len - 32 ..][0..32].* = tail;
115 } else {
116 copyLong(dest[i..], src[i..], len - i);
150 const loop_count = max_bytes / @sizeOf(T);
151
152 for (dest[0..loop_count], src[0..loop_count]) |*d, s| {
153 d.* = s;
117154 }
118155}
119156
120inline fn overlapBwd(dest: [*]u8, src: [*]const u8, len: usize) void {
121 var array: [5]@Vector(32, u8) = undefined;
122 array[0] = src[len - 32 ..][0..32].*;
123 inline for (1..5) |i| array[i] = src[(i - 1) << 5 ..][0..32].*;
124
125 const end: usize = (@intFromPtr(dest) + len - 32) & 31;
126 const range = len - end;
127 var s = src + range;
128 var d = dest + range;
129
130 while (@intFromPtr(s) > @intFromPtr(src + 128)) {
131 // zig fmt: off
132 const first = @as(*align(1) const @Vector(32, u8), @ptrCast(s - 32)).*;
133 const second = @as(*align(1) const @Vector(32, u8), @ptrCast(s - 64)).*;
134 const third = @as(*align(1) const @Vector(32, u8), @ptrCast(s - 96)).*;
135 const fourth = @as(*align(1) const @Vector(32, u8), @ptrCast(s - 128)).*;
136
137 @as(*align(32) @Vector(32, u8), @alignCast(@ptrCast(d - 32))).* = first;
138 @as(*align(32) @Vector(32, u8), @alignCast(@ptrCast(d - 64))).* = second;
139 @as(*align(32) @Vector(32, u8), @alignCast(@ptrCast(d - 96))).* = third;
140 @as(*align(32) @Vector(32, u8), @alignCast(@ptrCast(d - 128))).* = fourth;
141 // zig fmt: on
142
143 s -= 128;
144 d -= 128;
145 }
157/// copy `len` bytes from `src` to `dest`; `len` must be in the range
158/// `[copy_len, 4 * copy_len)`.
159inline fn copyRange4(
160 comptime copy_len: comptime_int,
161 noalias dest: [*]u8,
162 noalias src: [*]const u8,
163 len: usize,
164) void {
165 @setRuntimeSafety(builtin.is_test);
166 comptime assert(std.math.isPowerOfTwo(copy_len));
167 assert(len >= copy_len);
168 assert(len < 4 * copy_len);
169
170 const a = len & (copy_len * 2);
171 const b = a / 2;
172
173 const last = len - copy_len;
174 const pen = last - b;
175
176 dest[0..copy_len].* = src[0..copy_len].*;
177 dest[b..][0..copy_len].* = src[b..][0..copy_len].*;
178 dest[pen..][0..copy_len].* = src[pen..][0..copy_len].*;
179 dest[last..][0..copy_len].* = src[last..][0..copy_len].*;
180}
181
182test {
183 const S = struct {
184 fn testFunc(comptime copy_func: anytype) !void {
185 const max_len = 1024;
186 var buffer: [max_len + @alignOf(Element) - 1]u8 align(@alignOf(Element)) = undefined;
187 for (&buffer, 0..) |*b, i| {
188 b.* = @intCast(i % 97);
189 }
190 var dest: [max_len + @alignOf(Element) - 1]u8 align(@alignOf(Element)) = undefined;
191
192 for (0..max_len) |copy_len| {
193 for (0..@alignOf(Element)) |s_offset| {
194 for (0..@alignOf(Element)) |d_offset| {
195 @memset(&dest, 0xff);
196 const s = buffer[s_offset..][0..copy_len];
197 const d = dest[d_offset..][0..copy_len];
198 _ = copy_func(@ptrCast(d.ptr), @ptrCast(s.ptr), s.len);
199 std.testing.expectEqualSlices(u8, s, d) catch |e| {
200 std.debug.print("error encountered for length={d}, s_offset={d}, d_offset={d}\n", .{
201 copy_len, s_offset, d_offset,
202 });
203 return e;
204 };
205 }
206 }
207 }
208 }
209 };
146210
147 inline for (array[1..], 0..) |vec, i| dest[i * 32 ..][0..32].* = vec;
148 dest[len - 32 ..][0..32].* = array[0];
211 try S.testFunc(memcpySmall);
212 try S.testFunc(memcpyFast);
149213}
lib/compiler_rt/memmove.zig created+170
......@@ -0,0 +1,170 @@
1const std = @import("std");
2const common = @import("./common.zig");
3const builtin = @import("builtin");
4
5comptime {
6 if (builtin.object_format != .c) {
7 const export_options: std.builtin.ExportOptions = .{
8 .name = "memmove",
9 .linkage = common.linkage,
10 .visibility = common.visibility,
11 };
12
13 if (builtin.mode == .ReleaseSmall)
14 @export(&memmoveSmall, export_options)
15 else
16 @export(&memmoveFast, export_options);
17 }
18}
19
20fn memmoveSmall(opt_dest: ?[*]u8, opt_src: ?[*]const u8, len: usize) callconv(.C) ?[*]u8 {
21 const dest = opt_dest.?;
22 const src = opt_src.?;
23
24 if (@intFromPtr(dest) < @intFromPtr(src)) {
25 for (0..len) |i| {
26 dest[i] = src[i];
27 }
28 } else {
29 for (0..len) |i| {
30 dest[len - 1 - i] = src[len - 1 - i];
31 }
32 }
33
34 return dest;
35}
36
37pub fn memmoveFast(opt_dest: ?[*]u8, opt_src: ?[*]const u8, len: usize) callconv(.C) ?[*]u8 {
38 // a port of https://github.com/facebook/folly/blob/1c8bc50e88804e2a7361a57cd9b551dd10f6c5fd/folly/memcpy.S
39 if (len == 0) {
40 @branchHint(.unlikely);
41 return opt_dest;
42 }
43
44 const dest = opt_dest.?;
45 const src = opt_src.?;
46
47 if (len < 8) {
48 @branchHint(.unlikely);
49 if (len == 1) {
50 @branchHint(.unlikely);
51 dest[0] = src[0];
52 } else if (len >= 4) {
53 @branchHint(.unlikely);
54 blockCopy(dest, src, 4, len);
55 } else {
56 blockCopy(dest, src, 2, len);
57 }
58 return dest;
59 }
60
61 if (len > 32) {
62 @branchHint(.unlikely);
63 if (len > 256) {
64 @branchHint(.unlikely);
65 copyMove(dest, src, len);
66 return dest;
67 }
68 copyLong(dest, src, len);
69 return dest;
70 }
71
72 if (len > 16) {
73 @branchHint(.unlikely);
74 blockCopy(dest, src, 16, len);
75 return dest;
76 }
77
78 blockCopy(dest, src, 8, len);
79
80 return dest;
81}
82
83inline fn blockCopy(dest: [*]u8, src: [*]const u8, block_size: comptime_int, len: usize) void {
84 const first = @as(*align(1) const @Vector(block_size, u8), src[0..block_size]).*;
85 const second = @as(*align(1) const @Vector(block_size, u8), src[len - block_size ..][0..block_size]).*;
86 dest[0..block_size].* = first;
87 dest[len - block_size ..][0..block_size].* = second;
88}
89
90inline fn copyLong(dest: [*]u8, src: [*]const u8, len: usize) void {
91 var array: [8]@Vector(32, u8) = undefined;
92
93 inline for (.{ 64, 128, 192, 256 }, 0..) |N, i| {
94 array[i * 2] = src[(N / 2) - 32 ..][0..32].*;
95 array[(i * 2) + 1] = src[len - N / 2 ..][0..32].*;
96
97 if (len <= N) {
98 @branchHint(.unlikely);
99 for (0..i + 1) |j| {
100 dest[j * 32 ..][0..32].* = array[j * 2];
101 dest[len - ((j * 32) + 32) ..][0..32].* = array[(j * 2) + 1];
102 }
103 return;
104 }
105 }
106}
107
108inline fn copyMove(dest: [*]u8, src: [*]const u8, len: usize) void {
109 if (@intFromPtr(src) >= @intFromPtr(dest)) {
110 @branchHint(.unlikely);
111 copyForward(dest, src, len);
112 } else if (@intFromPtr(src) + len > @intFromPtr(dest)) {
113 @branchHint(.unlikely);
114 overlapBwd(dest, src, len);
115 } else {
116 copyForward(dest, src, len);
117 }
118}
119
120inline fn copyForward(dest: [*]u8, src: [*]const u8, len: usize) void {
121 const tail: @Vector(32, u8) = src[len - 32 ..][0..32].*;
122
123 const N: usize = len & ~@as(usize, 127);
124 var i: usize = 0;
125
126 while (i < N) : (i += 128) {
127 dest[i..][0..32].* = src[i..][0..32].*;
128 dest[i + 32 ..][0..32].* = src[i + 32 ..][0..32].*;
129 dest[i + 64 ..][0..32].* = src[i + 64 ..][0..32].*;
130 dest[i + 96 ..][0..32].* = src[i + 96 ..][0..32].*;
131 }
132
133 if (len - i <= 32) {
134 @branchHint(.unlikely);
135 dest[len - 32 ..][0..32].* = tail;
136 } else {
137 copyLong(dest[i..], src[i..], len - i);
138 }
139}
140
141inline fn overlapBwd(dest: [*]u8, src: [*]const u8, len: usize) void {
142 var array: [5]@Vector(32, u8) = undefined;
143 array[0] = src[len - 32 ..][0..32].*;
144 inline for (1..5) |i| array[i] = src[(i - 1) << 5 ..][0..32].*;
145
146 const end: usize = (@intFromPtr(dest) + len - 32) & 31;
147 const range = len - end;
148 var s = src + range;
149 var d = dest + range;
150
151 while (@intFromPtr(s) > @intFromPtr(src + 128)) {
152 // zig fmt: off
153 const first = @as(*align(1) const @Vector(32, u8), @ptrCast(s - 32)).*;
154 const second = @as(*align(1) const @Vector(32, u8), @ptrCast(s - 64)).*;
155 const third = @as(*align(1) const @Vector(32, u8), @ptrCast(s - 96)).*;
156 const fourth = @as(*align(1) const @Vector(32, u8), @ptrCast(s - 128)).*;
157
158 @as(*align(32) @Vector(32, u8), @alignCast(@ptrCast(d - 32))).* = first;
159 @as(*align(32) @Vector(32, u8), @alignCast(@ptrCast(d - 64))).* = second;
160 @as(*align(32) @Vector(32, u8), @alignCast(@ptrCast(d - 96))).* = third;
161 @as(*align(32) @Vector(32, u8), @alignCast(@ptrCast(d - 128))).* = fourth;
162 // zig fmt: on
163
164 s -= 128;
165 d -= 128;
166 }
167
168 inline for (array[1..], 0..) |vec, i| dest[i * 32 ..][0..32].* = vec;
169 dest[len - 32 ..][0..32].* = array[0];
170}