| ... | ... | @@ -1,6 +1,10 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | 2 | const common = @import("./common.zig"); |
| 3 | 3 | const builtin = @import("builtin"); |
| 4 | const assert = std.debug.assert; |
| 5 | const memcpy = @import("memcpy.zig"); |
| 6 | |
| 7 | const Element = common.PreferredLoadStoreElement; |
| 4 | 8 | |
| 5 | 9 | comptime { |
| 6 | 10 | if (builtin.object_format != .c) { |
| ... | ... | @@ -34,137 +38,250 @@ fn memmoveSmall(opt_dest: ?[*]u8, opt_src: ?[*]const u8, len: usize) callconv(.C |
| 34 | 38 | return dest; |
| 35 | 39 | } |
| 36 | 40 | |
| 37 | | pub 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; |
| 41 | fn memmoveFast(dest: ?[*]u8, src: ?[*]u8, len: usize) callconv(.C) ?[*]u8 { |
| 42 | @setRuntimeSafety(builtin.is_test); |
| 43 | const unroll_count = 1; |
| 44 | comptime assert(std.math.isPowerOfTwo(unroll_count)); |
| 45 | const small_limit = @max(2 * @sizeOf(Element), unroll_count * @sizeOf(Element)); |
| 46 | |
| 47 | if (copySmallLength(small_limit, dest.?, src.?, len)) return dest; |
| 48 | |
| 49 | const dest_address = @intFromPtr(dest); |
| 50 | const src_address = @intFromPtr(src); |
| 51 | |
| 52 | if (src_address < dest_address and src_address + len > dest_address) { |
| 53 | copyBackwards(unroll_count, dest.?, src.?, len); |
| 54 | } else { |
| 55 | copyForwards(unroll_count, dest.?, src.?, len); |
| 42 | 56 | } |
| 43 | 57 | |
| 44 | | const dest = opt_dest.?; |
| 45 | | const src = opt_src.?; |
| 58 | return dest; |
| 59 | } |
| 46 | 60 | |
| 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; |
| 61 | inline fn copySmallLength( |
| 62 | comptime small_limit: comptime_int, |
| 63 | dest: [*]u8, |
| 64 | src: [*]const u8, |
| 65 | len: usize, |
| 66 | ) bool { |
| 67 | if (len < 16) { |
| 68 | copyLessThan16(dest, src, len); |
| 69 | return true; |
| 59 | 70 | } |
| 60 | 71 | |
| 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; |
| 72 | if (comptime 2 < (std.math.log2(small_limit) + 1) / 2) { |
| 73 | if (copy16ToSmallLimit(small_limit, dest, src, len)) return true; |
| 70 | 74 | } |
| 71 | 75 | |
| 72 | | if (len > 16) { |
| 73 | | @branchHint(.unlikely); |
| 74 | | blockCopy(dest, src, 16, len); |
| 75 | | return dest; |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | inline fn copyLessThan16( |
| 80 | dest: [*]u8, |
| 81 | src: [*]const u8, |
| 82 | len: usize, |
| 83 | ) void { |
| 84 | @setRuntimeSafety(builtin.is_test); |
| 85 | if (len < 4) { |
| 86 | if (len == 0) return; |
| 87 | const b = len / 2; |
| 88 | const d0 = src[0]; |
| 89 | const db = src[b]; |
| 90 | const de = src[len - 1]; |
| 91 | dest[0] = d0; |
| 92 | dest[b] = db; |
| 93 | dest[len - 1] = de; |
| 94 | return; |
| 95 | } |
| 96 | copyRange4(4, dest, src, len); |
| 97 | } |
| 98 | |
| 99 | inline fn copy16ToSmallLimit( |
| 100 | comptime small_limit: comptime_int, |
| 101 | dest: [*]u8, |
| 102 | src: [*]const u8, |
| 103 | len: usize, |
| 104 | ) bool { |
| 105 | @setRuntimeSafety(builtin.is_test); |
| 106 | inline for (2..(std.math.log2(small_limit) + 1) / 2 + 1) |p| { |
| 107 | const limit = 1 << (2 * p); |
| 108 | if (len < limit) { |
| 109 | copyRange4(limit / 4, dest, src, len); |
| 110 | return true; |
| 111 | } |
| 76 | 112 | } |
| 113 | return false; |
| 114 | } |
| 77 | 115 | |
| 78 | | blockCopy(dest, src, 8, len); |
| 116 | /// copy `len` bytes from `src` to `dest`; `len` must be in the range |
| 117 | /// `[copy_len, 4 * copy_len)`. |
| 118 | inline fn copyRange4( |
| 119 | comptime copy_len: comptime_int, |
| 120 | dest: [*]u8, |
| 121 | src: [*]const u8, |
| 122 | len: usize, |
| 123 | ) void { |
| 124 | @setRuntimeSafety(builtin.is_test); |
| 125 | comptime assert(std.math.isPowerOfTwo(copy_len)); |
| 126 | assert(len >= copy_len); |
| 127 | assert(len < 4 * copy_len); |
| 79 | 128 | |
| 80 | | return dest; |
| 129 | const a = len & (copy_len * 2); |
| 130 | const b = a / 2; |
| 131 | |
| 132 | const last = len - copy_len; |
| 133 | const pen = last - b; |
| 134 | |
| 135 | const d0 = src[0..copy_len].*; |
| 136 | const d1 = src[b..][0..copy_len].*; |
| 137 | const d2 = src[pen..][0..copy_len].*; |
| 138 | const d3 = src[last..][0..copy_len].*; |
| 139 | |
| 140 | dest[0..copy_len].* = d0; |
| 141 | dest[b..][0..copy_len].* = d1; |
| 142 | dest[pen..][0..copy_len].* = d2; |
| 143 | dest[last..][0..copy_len].* = d3; |
| 144 | } |
| 145 | |
| 146 | inline fn copyForwards( |
| 147 | comptime unroll_count: comptime_int, |
| 148 | dest: [*]u8, |
| 149 | src: [*]const u8, |
| 150 | len: usize, |
| 151 | ) void { |
| 152 | @setRuntimeSafety(builtin.is_test); |
| 153 | assert(len >= 2 * @sizeOf(Element)); |
| 154 | assert(len >= unroll_count * @sizeOf(Element)); |
| 155 | |
| 156 | const head = src[0..@sizeOf(Element)].*; |
| 157 | const tail = src[len - @sizeOf(Element) ..][0..@sizeOf(Element)].*; |
| 158 | const alignment_offset = @alignOf(Element) - @intFromPtr(src) % @alignOf(Element); |
| 159 | const n = len - alignment_offset; |
| 160 | const d = dest + alignment_offset; |
| 161 | const s = src + alignment_offset; |
| 162 | |
| 163 | copyBlocksAlignedSource(@ptrCast(d), @alignCast(@ptrCast(s)), n, unroll_count); |
| 164 | |
| 165 | // copy last `copy_size` bytes unconditionally, since block copy |
| 166 | // methods only copy a multiple of `copy_size` bytes. |
| 167 | dest[len - @sizeOf(Element) ..][0..@sizeOf(Element)].* = tail; |
| 168 | dest[0..@sizeOf(Element)].* = head; |
| 81 | 169 | } |
| 82 | 170 | |
| 83 | | inline 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; |
| 171 | inline fn copyBlocksAlignedSource( |
| 172 | dest: [*]align(1) Element, |
| 173 | src: [*]const Element, |
| 174 | max_bytes: usize, |
| 175 | comptime unroll_count: comptime_int, |
| 176 | ) void { |
| 177 | copyBlocks(dest, src, max_bytes, unroll_count); |
| 88 | 178 | } |
| 89 | 179 | |
| 90 | | inline fn copyLong(dest: [*]u8, src: [*]const u8, len: usize) void { |
| 91 | | var array: [8]@Vector(32, u8) = undefined; |
| 180 | /// Copies the largest multiple of `@sizeOf(T)` bytes from `src` to `dest`, |
| 181 | /// that is less than `max_bytes` where `T` is the child type of `src` and |
| 182 | /// `dest`; `max_bytes` must be at least `@sizeOf(T)`. The primary copy loop |
| 183 | /// will be unrolled to perform `unroll_count` copies per iteration. |
| 184 | inline fn copyBlocks( |
| 185 | dest: anytype, |
| 186 | src: anytype, |
| 187 | max_bytes: usize, |
| 188 | comptime unroll_count: comptime_int, |
| 189 | ) void { |
| 190 | @setRuntimeSafety(builtin.is_test); |
| 191 | comptime assert(unroll_count > 0); |
| 92 | 192 | |
| 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].*; |
| 193 | const T = @typeInfo(@TypeOf(dest)).pointer.child; |
| 194 | comptime assert(T == @typeInfo(@TypeOf(src)).pointer.child); |
| 96 | 195 | |
| 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; |
| 196 | const loop_count = max_bytes / (@sizeOf(T) * unroll_count); |
| 197 | |
| 198 | // save tail since it can overlap with `dest `in main copy loop |
| 199 | const tail_start = (max_bytes / @sizeOf(T)) - (unroll_count - 1); |
| 200 | const st = src[tail_start..][0 .. unroll_count - 1]; |
| 201 | var tail_data: [unroll_count - 1]Element = undefined; |
| 202 | inline for (&tail_data, st) |*d, s| { |
| 203 | d.* = s; |
| 204 | } |
| 205 | |
| 206 | for (0..loop_count) |i| { |
| 207 | const du = dest[i * unroll_count ..][0..unroll_count]; |
| 208 | const su = src[i * unroll_count ..][0..unroll_count]; |
| 209 | inline for (du, su) |*d, s| { |
| 210 | d.* = s; |
| 104 | 211 | } |
| 105 | 212 | } |
| 106 | | } |
| 107 | 213 | |
| 108 | | inline 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); |
| 214 | const dt = dest[tail_start..][0 .. unroll_count - 1]; |
| 215 | inline for (dt, tail_data) |*d, s| { |
| 216 | d.* = s; |
| 117 | 217 | } |
| 118 | 218 | } |
| 119 | 219 | |
| 120 | | inline fn copyForward(dest: [*]u8, src: [*]const u8, len: usize) void { |
| 121 | | const tail: @Vector(32, u8) = src[len - 32 ..][0..32].*; |
| 220 | inline fn copyBackwards( |
| 221 | comptime unroll_count: comptime_int, |
| 222 | dest: [*]u8, |
| 223 | src: [*]const u8, |
| 224 | len: usize, |
| 225 | ) void { |
| 226 | const end_bytes = src[len - @sizeOf(Element) ..][0..@sizeOf(Element)].*; |
| 227 | const start_bytes = src[0..@sizeOf(Element)].*; |
| 228 | |
| 229 | const tail_dest: [*]Element = @ptrFromInt(std.mem.alignForward(usize, @intFromPtr(dest), @alignOf(Element))); |
| 230 | const tail_src: [*]align(1) const Element = @ptrCast(src + (@intFromPtr(tail_dest) - @intFromPtr(dest))); |
| 231 | const tail_bytes: [unroll_count - 1]Element = tail_src[0 .. unroll_count - 1].*; |
| 122 | 232 | |
| 123 | | const N: usize = len & ~@as(usize, 127); |
| 124 | | var i: usize = 0; |
| 233 | const d_addr: usize = std.mem.alignBackward(usize, @intFromPtr(dest) + len, @alignOf(Element)); |
| 234 | const d: [*]Element = @ptrFromInt(d_addr); |
| 235 | const n = d_addr - @intFromPtr(dest); |
| 236 | const s: [*]align(1) const Element = @ptrCast(src + n); |
| 125 | 237 | |
| 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].*; |
| 238 | const loop_count = n / (unroll_count * @sizeOf(Element)); |
| 239 | var i: usize = 1; |
| 240 | while (i < loop_count + 1) : (i += 1) { |
| 241 | const du = d - (i * unroll_count); |
| 242 | const su = s - (i * unroll_count); |
| 243 | inline for (0..unroll_count) |j| { |
| 244 | du[unroll_count - 1 - j] = su[unroll_count - 1 - j]; |
| 245 | } |
| 131 | 246 | } |
| 132 | 247 | |
| 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); |
| 248 | inline for (tail_dest[0 .. unroll_count - 1], tail_bytes) |*dt, st| { |
| 249 | dt.* = st; |
| 138 | 250 | } |
| 251 | dest[0..@sizeOf(Element)].* = start_bytes; |
| 252 | |
| 253 | dest[len - @sizeOf(Element) ..][0..@sizeOf(Element)].* = end_bytes; |
| 139 | 254 | } |
| 140 | 255 | |
| 141 | | inline 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; |
| 256 | test memmoveFast { |
| 257 | const max_len = 1024; |
| 258 | var buffer: [max_len + @alignOf(Element) - 1]u8 = undefined; |
| 259 | for (&buffer, 0..) |*b, i| { |
| 260 | b.* = @intCast(i % 97); |
| 166 | 261 | } |
| 167 | 262 | |
| 168 | | inline for (array[1..], 0..) |vec, i| dest[i * 32 ..][0..32].* = vec; |
| 169 | | dest[len - 32 ..][0..32].* = array[0]; |
| 263 | var move_buffer: [max_len + @alignOf(Element) - 1]u8 align(@alignOf(Element)) = undefined; |
| 264 | |
| 265 | for (0..max_len) |copy_len| { |
| 266 | for (0..@alignOf(Element)) |s_offset| { |
| 267 | for (0..@alignOf(Element)) |d_offset| { |
| 268 | for (&move_buffer, buffer) |*d, s| { |
| 269 | d.* = s; |
| 270 | } |
| 271 | const dest = move_buffer[d_offset..][0..copy_len]; |
| 272 | const src = move_buffer[s_offset..][0..copy_len]; |
| 273 | _ = memmoveFast(dest.ptr, src.ptr, copy_len); |
| 274 | std.testing.expectEqualSlices(u8, buffer[s_offset..][0..copy_len], dest) catch |e| { |
| 275 | std.debug.print( |
| 276 | "error occured with source offset {d} and destination offset {d}\n", |
| 277 | .{ |
| 278 | s_offset, |
| 279 | d_offset, |
| 280 | }, |
| 281 | ); |
| 282 | return e; |
| 283 | }; |
| 284 | } |
| 285 | } |
| 286 | } |
| 170 | 287 | } |