Skip to content

Commit 02c5049

Browse files
committed
copy_misaligned_words: reduce codesize
1 parent 4df7a8d commit 02c5049

File tree

1 file changed

+10
-38
lines changed

1 file changed

+10
-38
lines changed

Diff for: compiler-builtins/src/mem/impls.rs

+10-38
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,13 @@ unsafe fn read_usize_unaligned(x: *const usize) -> usize {
4141
core::mem::transmute(x_read)
4242
}
4343

44-
/// Loads a `T`-sized chunk from `src` into `dst` at offset `offset`, if that does not exceed
45-
/// `load_sz`. The offset pointers must both be `T`-aligned. Returns the new offset, advanced by the
46-
/// chunk size if a load happened.
47-
#[cfg(not(feature = "mem-unaligned"))]
4844
#[inline(always)]
49-
unsafe fn load_chunk_aligned<T: Copy>(
50-
src: *const usize,
51-
dst: *mut usize,
52-
load_sz: usize,
53-
offset: usize,
54-
) -> usize {
55-
let chunk_sz = core::mem::size_of::<T>();
56-
if (load_sz & chunk_sz) != 0 {
57-
*dst.wrapping_byte_add(offset).cast::<T>() = *src.wrapping_byte_add(offset).cast::<T>();
58-
offset | chunk_sz
59-
} else {
60-
offset
45+
unsafe fn copy_forward_bytes(mut dest: *mut u8, mut src: *const u8, n: usize) {
46+
let dest_end = dest.wrapping_add(n);
47+
while dest < dest_end {
48+
*dest = *src;
49+
dest = dest.wrapping_add(1);
50+
src = src.wrapping_add(1);
6151
}
6252
}
6353

@@ -72,13 +62,8 @@ unsafe fn load_aligned_partial(src: *const usize, load_sz: usize) -> usize {
7262
// (since `load_sz < WORD_SIZE`).
7363
const { assert!(WORD_SIZE <= 8) };
7464

75-
let mut i = 0;
7665
let mut out = 0usize;
77-
// We load in decreasing order, so the pointers remain sufficiently aligned for the next step.
78-
i = load_chunk_aligned::<u32>(src, &raw mut out, load_sz, i);
79-
i = load_chunk_aligned::<u16>(src, &raw mut out, load_sz, i);
80-
i = load_chunk_aligned::<u8>(src, &raw mut out, load_sz, i);
81-
debug_assert!(i == load_sz);
66+
copy_forward_bytes(&raw mut out as *mut u8, src as *mut u8, load_sz);
8267
out
8368
}
8469

@@ -94,31 +79,18 @@ unsafe fn load_aligned_end_partial(src: *const usize, load_sz: usize) -> usize {
9479
// (since `load_sz < WORD_SIZE`).
9580
const { assert!(WORD_SIZE <= 8) };
9681

97-
let mut i = 0;
9882
let mut out = 0usize;
83+
9984
// Obtain pointers pointing to the beginning of the range we want to load.
10085
let src_shifted = src.wrapping_byte_add(WORD_SIZE - load_sz);
10186
let out_shifted = (&raw mut out).wrapping_byte_add(WORD_SIZE - load_sz);
102-
// We load in increasing order, so by the time we reach `u16` things are 2-aligned etc.
103-
i = load_chunk_aligned::<u8>(src_shifted, out_shifted, load_sz, i);
104-
i = load_chunk_aligned::<u16>(src_shifted, out_shifted, load_sz, i);
105-
i = load_chunk_aligned::<u32>(src_shifted, out_shifted, load_sz, i);
106-
debug_assert!(i == load_sz);
87+
88+
copy_forward_bytes(out_shifted as *mut u8, src_shifted as *mut u8, load_sz);
10789
out
10890
}
10991

11092
#[inline(always)]
11193
pub unsafe fn copy_forward(mut dest: *mut u8, mut src: *const u8, mut n: usize) {
112-
#[inline(always)]
113-
unsafe fn copy_forward_bytes(mut dest: *mut u8, mut src: *const u8, n: usize) {
114-
let dest_end = dest.wrapping_add(n);
115-
while dest < dest_end {
116-
*dest = *src;
117-
dest = dest.wrapping_add(1);
118-
src = src.wrapping_add(1);
119-
}
120-
}
121-
12294
#[inline(always)]
12395
unsafe fn copy_forward_aligned_words(dest: *mut u8, src: *const u8, n: usize) {
12496
let mut dest_usize = dest as *mut usize;

0 commit comments

Comments
 (0)