Skip to content

Commit 1b1596c

Browse files
authored
Rollup merge of #100823 - WaffleLapkin:less_offsets, r=scottmcm
Refactor some `std` code that works with pointer offstes This PR replaces `pointer::offset` in standard library with `pointer::add` and `pointer::sub`, [re]moving some casts and using `.addr()` while we are at it. This is a more complicated refactor than all other sibling PRs, so take a closer look when reviewing, please 😃 (though I've checked this multiple times and it looks fine). r? ````@scottmcm```` _split off from #100746, continuation of #100822_
2 parents bb5a016 + 98a3230 commit 1b1596c

File tree

3 files changed

+62
-62
lines changed

3 files changed

+62
-62
lines changed

library/core/src/fmt/num.rs

+46-46
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ macro_rules! impl_Display {
211211
fn $name(mut n: $u, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
212212
// 2^128 is about 3*10^38, so 39 gives an extra byte of space
213213
let mut buf = [MaybeUninit::<u8>::uninit(); 39];
214-
let mut curr = buf.len() as isize;
214+
let mut curr = buf.len();
215215
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);
216216
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
217217

@@ -228,7 +228,7 @@ macro_rules! impl_Display {
228228

229229
// eagerly decode 4 characters at a time
230230
while n >= 10000 {
231-
let rem = (n % 10000) as isize;
231+
let rem = (n % 10000) as usize;
232232
n /= 10000;
233233

234234
let d1 = (rem / 100) << 1;
@@ -238,37 +238,37 @@ macro_rules! impl_Display {
238238
// We are allowed to copy to `buf_ptr[curr..curr + 3]` here since
239239
// otherwise `curr < 0`. But then `n` was originally at least `10000^10`
240240
// which is `10^40 > 2^128 > n`.
241-
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
242-
ptr::copy_nonoverlapping(lut_ptr.offset(d2), buf_ptr.offset(curr + 2), 2);
241+
ptr::copy_nonoverlapping(lut_ptr.add(d1), buf_ptr.add(curr), 2);
242+
ptr::copy_nonoverlapping(lut_ptr.add(d2), buf_ptr.add(curr + 2), 2);
243243
}
244244

245245
// if we reach here numbers are <= 9999, so at most 4 chars long
246-
let mut n = n as isize; // possibly reduce 64bit math
246+
let mut n = n as usize; // possibly reduce 64bit math
247247

248248
// decode 2 more chars, if > 2 chars
249249
if n >= 100 {
250250
let d1 = (n % 100) << 1;
251251
n /= 100;
252252
curr -= 2;
253-
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
253+
ptr::copy_nonoverlapping(lut_ptr.add(d1), buf_ptr.add(curr), 2);
254254
}
255255

256256
// decode last 1 or 2 chars
257257
if n < 10 {
258258
curr -= 1;
259-
*buf_ptr.offset(curr) = (n as u8) + b'0';
259+
*buf_ptr.add(curr) = (n as u8) + b'0';
260260
} else {
261261
let d1 = n << 1;
262262
curr -= 2;
263-
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
263+
ptr::copy_nonoverlapping(lut_ptr.add(d1), buf_ptr.add(curr), 2);
264264
}
265265
}
266266

267267
// SAFETY: `curr` > 0 (since we made `buf` large enough), and all the chars are valid
268268
// UTF-8 since `DEC_DIGITS_LUT` is
269269
let buf_slice = unsafe {
270270
str::from_utf8_unchecked(
271-
slice::from_raw_parts(buf_ptr.offset(curr), buf.len() - curr as usize))
271+
slice::from_raw_parts(buf_ptr.add(curr), buf.len() - curr))
272272
};
273273
f.pad_integral(is_nonnegative, "", buf_slice)
274274
}
@@ -339,18 +339,18 @@ macro_rules! impl_Exp {
339339
// Since `curr` always decreases by the number of digits copied, this means
340340
// that `curr >= 0`.
341341
let mut buf = [MaybeUninit::<u8>::uninit(); 40];
342-
let mut curr = buf.len() as isize; //index for buf
342+
let mut curr = buf.len(); //index for buf
343343
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);
344344
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
345345

346346
// decode 2 chars at a time
347347
while n >= 100 {
348-
let d1 = ((n % 100) as isize) << 1;
348+
let d1 = ((n % 100) as usize) << 1;
349349
curr -= 2;
350350
// SAFETY: `d1 <= 198`, so we can copy from `lut_ptr[d1..d1 + 2]` since
351351
// `DEC_DIGITS_LUT` has a length of 200.
352352
unsafe {
353-
ptr::copy_nonoverlapping(lut_ptr.offset(d1), buf_ptr.offset(curr), 2);
353+
ptr::copy_nonoverlapping(lut_ptr.add(d1), buf_ptr.add(curr), 2);
354354
}
355355
n /= 100;
356356
exponent += 2;
@@ -362,7 +362,7 @@ macro_rules! impl_Exp {
362362
curr -= 1;
363363
// SAFETY: Safe since `40 > curr >= 0` (see comment)
364364
unsafe {
365-
*buf_ptr.offset(curr) = (n as u8 % 10_u8) + b'0';
365+
*buf_ptr.add(curr) = (n as u8 % 10_u8) + b'0';
366366
}
367367
n /= 10;
368368
exponent += 1;
@@ -372,18 +372,18 @@ macro_rules! impl_Exp {
372372
curr -= 1;
373373
// SAFETY: Safe since `40 > curr >= 0`
374374
unsafe {
375-
*buf_ptr.offset(curr) = b'.';
375+
*buf_ptr.add(curr) = b'.';
376376
}
377377
}
378378

379379
// SAFETY: Safe since `40 > curr >= 0`
380380
let buf_slice = unsafe {
381381
// decode last character
382382
curr -= 1;
383-
*buf_ptr.offset(curr) = (n as u8) + b'0';
383+
*buf_ptr.add(curr) = (n as u8) + b'0';
384384

385385
let len = buf.len() - curr as usize;
386-
slice::from_raw_parts(buf_ptr.offset(curr), len)
386+
slice::from_raw_parts(buf_ptr.add(curr), len)
387387
};
388388

389389
// stores 'e' (or 'E') and the up to 2-digit exponent
@@ -392,13 +392,13 @@ macro_rules! impl_Exp {
392392
// SAFETY: In either case, `exp_buf` is written within bounds and `exp_ptr[..len]`
393393
// is contained within `exp_buf` since `len <= 3`.
394394
let exp_slice = unsafe {
395-
*exp_ptr.offset(0) = if upper { b'E' } else { b'e' };
395+
*exp_ptr.add(0) = if upper { b'E' } else { b'e' };
396396
let len = if exponent < 10 {
397-
*exp_ptr.offset(1) = (exponent as u8) + b'0';
397+
*exp_ptr.add(1) = (exponent as u8) + b'0';
398398
2
399399
} else {
400400
let off = exponent << 1;
401-
ptr::copy_nonoverlapping(lut_ptr.offset(off), exp_ptr.offset(1), 2);
401+
ptr::copy_nonoverlapping(lut_ptr.add(off), exp_ptr.add(1), 2);
402402
3
403403
};
404404
slice::from_raw_parts(exp_ptr, len)
@@ -479,7 +479,7 @@ mod imp {
479479
impl_Exp!(i128, u128 as u128 via to_u128 named exp_u128);
480480

481481
/// Helper function for writing a u64 into `buf` going from last to first, with `curr`.
482-
fn parse_u64_into<const N: usize>(mut n: u64, buf: &mut [MaybeUninit<u8>; N], curr: &mut isize) {
482+
fn parse_u64_into<const N: usize>(mut n: u64, buf: &mut [MaybeUninit<u8>; N], curr: &mut usize) {
483483
let buf_ptr = MaybeUninit::slice_as_mut_ptr(buf);
484484
let lut_ptr = DEC_DIGITS_LUT.as_ptr();
485485
assert!(*curr > 19);
@@ -505,14 +505,14 @@ fn parse_u64_into<const N: usize>(mut n: u64, buf: &mut [MaybeUninit<u8>; N], cu
505505

506506
*curr -= 16;
507507

508-
ptr::copy_nonoverlapping(lut_ptr.offset(d1 as isize), buf_ptr.offset(*curr + 0), 2);
509-
ptr::copy_nonoverlapping(lut_ptr.offset(d2 as isize), buf_ptr.offset(*curr + 2), 2);
510-
ptr::copy_nonoverlapping(lut_ptr.offset(d3 as isize), buf_ptr.offset(*curr + 4), 2);
511-
ptr::copy_nonoverlapping(lut_ptr.offset(d4 as isize), buf_ptr.offset(*curr + 6), 2);
512-
ptr::copy_nonoverlapping(lut_ptr.offset(d5 as isize), buf_ptr.offset(*curr + 8), 2);
513-
ptr::copy_nonoverlapping(lut_ptr.offset(d6 as isize), buf_ptr.offset(*curr + 10), 2);
514-
ptr::copy_nonoverlapping(lut_ptr.offset(d7 as isize), buf_ptr.offset(*curr + 12), 2);
515-
ptr::copy_nonoverlapping(lut_ptr.offset(d8 as isize), buf_ptr.offset(*curr + 14), 2);
508+
ptr::copy_nonoverlapping(lut_ptr.add(d1 as usize), buf_ptr.add(*curr + 0), 2);
509+
ptr::copy_nonoverlapping(lut_ptr.add(d2 as usize), buf_ptr.add(*curr + 2), 2);
510+
ptr::copy_nonoverlapping(lut_ptr.add(d3 as usize), buf_ptr.add(*curr + 4), 2);
511+
ptr::copy_nonoverlapping(lut_ptr.add(d4 as usize), buf_ptr.add(*curr + 6), 2);
512+
ptr::copy_nonoverlapping(lut_ptr.add(d5 as usize), buf_ptr.add(*curr + 8), 2);
513+
ptr::copy_nonoverlapping(lut_ptr.add(d6 as usize), buf_ptr.add(*curr + 10), 2);
514+
ptr::copy_nonoverlapping(lut_ptr.add(d7 as usize), buf_ptr.add(*curr + 12), 2);
515+
ptr::copy_nonoverlapping(lut_ptr.add(d8 as usize), buf_ptr.add(*curr + 14), 2);
516516
}
517517
if n >= 1e8 as u64 {
518518
let to_parse = n % 1e8 as u64;
@@ -525,10 +525,10 @@ fn parse_u64_into<const N: usize>(mut n: u64, buf: &mut [MaybeUninit<u8>; N], cu
525525
let d4 = ((to_parse / 1e0 as u64) % 100) << 1;
526526
*curr -= 8;
527527

528-
ptr::copy_nonoverlapping(lut_ptr.offset(d1 as isize), buf_ptr.offset(*curr + 0), 2);
529-
ptr::copy_nonoverlapping(lut_ptr.offset(d2 as isize), buf_ptr.offset(*curr + 2), 2);
530-
ptr::copy_nonoverlapping(lut_ptr.offset(d3 as isize), buf_ptr.offset(*curr + 4), 2);
531-
ptr::copy_nonoverlapping(lut_ptr.offset(d4 as isize), buf_ptr.offset(*curr + 6), 2);
528+
ptr::copy_nonoverlapping(lut_ptr.add(d1 as usize), buf_ptr.add(*curr + 0), 2);
529+
ptr::copy_nonoverlapping(lut_ptr.add(d2 as usize), buf_ptr.add(*curr + 2), 2);
530+
ptr::copy_nonoverlapping(lut_ptr.add(d3 as usize), buf_ptr.add(*curr + 4), 2);
531+
ptr::copy_nonoverlapping(lut_ptr.add(d4 as usize), buf_ptr.add(*curr + 6), 2);
532532
}
533533
// `n` < 1e8 < (1 << 32)
534534
let mut n = n as u32;
@@ -540,8 +540,8 @@ fn parse_u64_into<const N: usize>(mut n: u64, buf: &mut [MaybeUninit<u8>; N], cu
540540
let d2 = (to_parse % 100) << 1;
541541
*curr -= 4;
542542

543-
ptr::copy_nonoverlapping(lut_ptr.offset(d1 as isize), buf_ptr.offset(*curr + 0), 2);
544-
ptr::copy_nonoverlapping(lut_ptr.offset(d2 as isize), buf_ptr.offset(*curr + 2), 2);
543+
ptr::copy_nonoverlapping(lut_ptr.add(d1 as usize), buf_ptr.add(*curr + 0), 2);
544+
ptr::copy_nonoverlapping(lut_ptr.add(d2 as usize), buf_ptr.add(*curr + 2), 2);
545545
}
546546

547547
// `n` < 1e4 < (1 << 16)
@@ -550,17 +550,17 @@ fn parse_u64_into<const N: usize>(mut n: u64, buf: &mut [MaybeUninit<u8>; N], cu
550550
let d1 = (n % 100) << 1;
551551
n /= 100;
552552
*curr -= 2;
553-
ptr::copy_nonoverlapping(lut_ptr.offset(d1 as isize), buf_ptr.offset(*curr), 2);
553+
ptr::copy_nonoverlapping(lut_ptr.add(d1 as usize), buf_ptr.add(*curr), 2);
554554
}
555555

556556
// decode last 1 or 2 chars
557557
if n < 10 {
558558
*curr -= 1;
559-
*buf_ptr.offset(*curr) = (n as u8) + b'0';
559+
*buf_ptr.add(*curr) = (n as u8) + b'0';
560560
} else {
561561
let d1 = n << 1;
562562
*curr -= 2;
563-
ptr::copy_nonoverlapping(lut_ptr.offset(d1 as isize), buf_ptr.offset(*curr), 2);
563+
ptr::copy_nonoverlapping(lut_ptr.add(d1 as usize), buf_ptr.add(*curr), 2);
564564
}
565565
}
566566
}
@@ -593,21 +593,21 @@ impl fmt::Display for i128 {
593593
fn fmt_u128(n: u128, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::Result {
594594
// 2^128 is about 3*10^38, so 39 gives an extra byte of space
595595
let mut buf = [MaybeUninit::<u8>::uninit(); 39];
596-
let mut curr = buf.len() as isize;
596+
let mut curr = buf.len();
597597

598598
let (n, rem) = udiv_1e19(n);
599599
parse_u64_into(rem, &mut buf, &mut curr);
600600

601601
if n != 0 {
602602
// 0 pad up to point
603-
let target = (buf.len() - 19) as isize;
603+
let target = buf.len() - 19;
604604
// SAFETY: Guaranteed that we wrote at most 19 bytes, and there must be space
605605
// remaining since it has length 39
606606
unsafe {
607607
ptr::write_bytes(
608-
MaybeUninit::slice_as_mut_ptr(&mut buf).offset(target),
608+
MaybeUninit::slice_as_mut_ptr(&mut buf).add(target),
609609
b'0',
610-
(curr - target) as usize,
610+
curr - target,
611611
);
612612
}
613613
curr = target;
@@ -616,16 +616,16 @@ fn fmt_u128(n: u128, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::R
616616
parse_u64_into(rem, &mut buf, &mut curr);
617617
// Should this following branch be annotated with unlikely?
618618
if n != 0 {
619-
let target = (buf.len() - 38) as isize;
619+
let target = buf.len() - 38;
620620
// The raw `buf_ptr` pointer is only valid until `buf` is used the next time,
621621
// buf `buf` is not used in this scope so we are good.
622622
let buf_ptr = MaybeUninit::slice_as_mut_ptr(&mut buf);
623623
// SAFETY: At this point we wrote at most 38 bytes, pad up to that point,
624624
// There can only be at most 1 digit remaining.
625625
unsafe {
626-
ptr::write_bytes(buf_ptr.offset(target), b'0', (curr - target) as usize);
626+
ptr::write_bytes(buf_ptr.add(target), b'0', curr - target);
627627
curr = target - 1;
628-
*buf_ptr.offset(curr) = (n as u8) + b'0';
628+
*buf_ptr.add(curr) = (n as u8) + b'0';
629629
}
630630
}
631631
}
@@ -634,8 +634,8 @@ fn fmt_u128(n: u128, is_nonnegative: bool, f: &mut fmt::Formatter<'_>) -> fmt::R
634634
// UTF-8 since `DEC_DIGITS_LUT` is
635635
let buf_slice = unsafe {
636636
str::from_utf8_unchecked(slice::from_raw_parts(
637-
MaybeUninit::slice_as_mut_ptr(&mut buf).offset(curr),
638-
buf.len() - curr as usize,
637+
MaybeUninit::slice_as_mut_ptr(&mut buf).add(curr),
638+
buf.len() - curr,
639639
))
640640
};
641641
f.pad_integral(is_nonnegative, "", buf_slice)

library/core/src/slice/memchr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ pub fn memrchr(x: u8, text: &[u8]) -> Option<usize> {
141141
// SAFETY: offset starts at len - suffix.len(), as long as it is greater than
142142
// min_aligned_offset (prefix.len()) the remaining distance is at least 2 * chunk_bytes.
143143
unsafe {
144-
let u = *(ptr.offset(offset as isize - 2 * chunk_bytes as isize) as *const Chunk);
145-
let v = *(ptr.offset(offset as isize - chunk_bytes as isize) as *const Chunk);
144+
let u = *(ptr.add(offset - 2 * chunk_bytes) as *const Chunk);
145+
let v = *(ptr.add(offset - chunk_bytes) as *const Chunk);
146146

147147
// Break if there is a matching byte.
148148
let zu = contains_zero_byte(u ^ repeated_x);

library/std/src/sys/sgx/abi/usercalls/alloc.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,9 @@ where
316316
// | small1 | Chunk smaller than 8 bytes
317317
// +--------+
318318
fn region_as_aligned_chunks(ptr: *const u8, len: usize) -> (usize, usize, usize) {
319-
let small0_size = if ptr as usize % 8 == 0 { 0 } else { 8 - ptr as usize % 8 };
320-
let small1_size = (len - small0_size as usize) % 8;
321-
let big_size = len - small0_size as usize - small1_size as usize;
319+
let small0_size = if ptr.is_aligned_to(8) { 0 } else { 8 - ptr.addr() % 8 };
320+
let small1_size = (len - small0_size) % 8;
321+
let big_size = len - small0_size - small1_size;
322322

323323
(small0_size, big_size, small1_size)
324324
}
@@ -364,8 +364,8 @@ pub(crate) unsafe fn copy_to_userspace(src: *const u8, dst: *mut u8, len: usize)
364364
mfence
365365
lfence
366366
",
367-
val = in(reg_byte) *src.offset(off as isize),
368-
dst = in(reg) dst.offset(off as isize),
367+
val = in(reg_byte) *src.add(off),
368+
dst = in(reg) dst.add(off),
369369
seg_sel = in(reg) &mut seg_sel,
370370
options(nostack, att_syntax)
371371
);
@@ -378,8 +378,8 @@ pub(crate) unsafe fn copy_to_userspace(src: *const u8, dst: *mut u8, len: usize)
378378
assert!(is_enclave_range(src, len));
379379
assert!(is_user_range(dst, len));
380380
assert!(len < isize::MAX as usize);
381-
assert!(!(src as usize).overflowing_add(len).1);
382-
assert!(!(dst as usize).overflowing_add(len).1);
381+
assert!(!src.addr().overflowing_add(len).1);
382+
assert!(!dst.addr().overflowing_add(len).1);
383383

384384
if len < 8 {
385385
// Can't align on 8 byte boundary: copy safely byte per byte
@@ -404,17 +404,17 @@ pub(crate) unsafe fn copy_to_userspace(src: *const u8, dst: *mut u8, len: usize)
404404

405405
unsafe {
406406
// Copy small0
407-
copy_bytewise_to_userspace(src, dst, small0_size as _);
407+
copy_bytewise_to_userspace(src, dst, small0_size);
408408

409409
// Copy big
410-
let big_src = src.offset(small0_size as _);
411-
let big_dst = dst.offset(small0_size as _);
412-
copy_quadwords(big_src as _, big_dst, big_size);
410+
let big_src = src.add(small0_size);
411+
let big_dst = dst.add(small0_size);
412+
copy_quadwords(big_src, big_dst, big_size);
413413

414414
// Copy small1
415-
let small1_src = src.offset(big_size as isize + small0_size as isize);
416-
let small1_dst = dst.offset(big_size as isize + small0_size as isize);
417-
copy_bytewise_to_userspace(small1_src, small1_dst, small1_size as _);
415+
let small1_src = src.add(big_size + small0_size);
416+
let small1_dst = dst.add(big_size + small0_size);
417+
copy_bytewise_to_userspace(small1_src, small1_dst, small1_size);
418418
}
419419
}
420420
}

0 commit comments

Comments
 (0)