Skip to content

Commit b91ec3a

Browse files
committed
play around with the loop conditions a bit
1 parent 1fd36a5 commit b91ec3a

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

compiler-builtins/src/mem/impls.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ pub unsafe fn copy_forward(mut dest: *mut u8, mut src: *const u8, mut n: usize)
142142

143143
let mut dest_usize = dest as *mut usize;
144144
let dest_end = dest.wrapping_add(n) as *mut usize;
145+
let dest_end = dest_end.wrapping_sub(1); // the last loop iteration is handled separately
145146

146147
// Calculate the misalignment offset and shift needed to reassemble value.
147148
// Since `src` is definitely not aligned, `offset` is in the range 1..WORD_SIZE.
@@ -152,7 +153,7 @@ pub unsafe fn copy_forward(mut dest: *mut u8, mut src: *const u8, mut n: usize)
152153
let mut src_aligned = src.wrapping_byte_sub(offset) as *mut usize;
153154
let mut prev_word = load_aligned_end_partial(src_aligned, WORD_SIZE - offset);
154155

155-
while dest_usize.wrapping_add(1) < dest_end {
156+
while dest_usize < dest_end {
156157
src_aligned = src_aligned.wrapping_add(1);
157158
let cur_word = *src_aligned;
158159
let reassembled = if cfg!(target_endian = "little") {
@@ -257,6 +258,7 @@ pub unsafe fn copy_backward(dest: *mut u8, src: *const u8, mut n: usize) {
257258

258259
let mut dest_usize = dest as *mut usize;
259260
let dest_start = dest.wrapping_sub(n) as *mut usize; // we're moving towards the start
261+
let dest_start = dest_start.wrapping_add(1); // the last loop iteration is handled separately
260262

261263
// Calculate the misalignment offset and shift needed to reassemble value.
262264
// Since `src` is definitely not aligned, `offset` is in the range 1..WORD_SIZE.
@@ -267,7 +269,7 @@ pub unsafe fn copy_backward(dest: *mut u8, src: *const u8, mut n: usize) {
267269
let mut src_aligned = src.wrapping_byte_sub(offset) as *mut usize;
268270
let mut prev_word = load_aligned_partial(src_aligned, offset);
269271

270-
while dest_start.wrapping_add(1) < dest_usize {
272+
while dest_start < dest_usize {
271273
src_aligned = src_aligned.wrapping_sub(1);
272274
let cur_word = *src_aligned;
273275
let reassembled = if cfg!(target_endian = "little") {

0 commit comments

Comments
 (0)