|
| 1 | +mod intrinsics { |
| 2 | + extern "rust-intrinsic" { |
| 3 | + pub fn offset<T>(ptr: *const T, count: isize) -> *const T; |
| 4 | + pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize); |
| 5 | + pub fn move_val_init<T>(dst: *mut T, src: T); |
| 6 | + pub fn uninit<T>() -> T; |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +mod ptr { |
| 11 | + #[lang = "const_ptr"] |
| 12 | + impl<T> *const T { |
| 13 | + pub unsafe fn offset(self, count: isize) -> *const T { |
| 14 | + intrinsics::offset(self, count) |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + #[lang = "mut_ptr"] |
| 19 | + impl<T> *mut T { |
| 20 | + pub unsafe fn offset(self, count: isize) -> *mut T { |
| 21 | + intrinsics::offset(self, count) as *mut T |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + pub unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) { |
| 26 | + let x = x as *mut T; |
| 27 | + let y = y as *mut T; |
| 28 | + let len = mem::size_of::<T>() * count; |
| 29 | + swap_nonoverlapping_bytes(x, y, len) |
| 30 | + } |
| 31 | + |
| 32 | + pub unsafe fn swap_nonoverlapping_one<T>(x: *mut T, y: *mut T) { |
| 33 | + // For types smaller than the block optimization below, |
| 34 | + // just swap directly to avoid pessimizing codegen. |
| 35 | + if mem::size_of::<T>() < 32 { |
| 36 | + let z = read(x); |
| 37 | + intrinsics::copy_nonoverlapping(y, x, 1); |
| 38 | + write(y, z); |
| 39 | + } else { |
| 40 | + swap_nonoverlapping(x, y, 1); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + pub unsafe fn write<T>(dst: *mut T, src: T) { |
| 45 | + intrinsics::move_val_init(&mut *dst, src) |
| 46 | + } |
| 47 | + |
| 48 | + pub unsafe fn read<T>(src: *const T) -> T { |
| 49 | + let mut tmp: T = mem::uninitialized(); |
| 50 | + intrinsics::copy_nonoverlapping(src, &mut tmp, 1); |
| 51 | + tmp |
| 52 | + } |
| 53 | + |
| 54 | + unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) { |
| 55 | + struct Block(u64, u64, u64, u64); |
| 56 | + struct UnalignedBlock(u64, u64, u64, u64); |
| 57 | + |
| 58 | + let block_size = mem::size_of::<Block>(); |
| 59 | + |
| 60 | + // Loop through x & y, copying them `Block` at a time |
| 61 | + // The optimizer should unroll the loop fully for most types |
| 62 | + // N.B. We can't use a for loop as the `range` impl calls `mem::swap` recursively |
| 63 | + let mut i = 0; |
| 64 | + while i + block_size <= len { |
| 65 | + // Create some uninitialized memory as scratch space |
| 66 | + // Declaring `t` here avoids aligning the stack when this loop is unused |
| 67 | + let mut t: Block = mem::uninitialized(); |
| 68 | + let t = &mut t as *mut _ as *mut u8; |
| 69 | + let x = x.offset(i as isize); |
| 70 | + let y = y.offset(i as isize); |
| 71 | + |
| 72 | + // Swap a block of bytes of x & y, using t as a temporary buffer |
| 73 | + // This should be optimized into efficient SIMD operations where available |
| 74 | + intrinsics::copy_nonoverlapping(x, t, block_size); |
| 75 | + intrinsics::copy_nonoverlapping(y, x, block_size); |
| 76 | + intrinsics::copy_nonoverlapping(t, y, block_size); |
| 77 | + i += block_size; |
| 78 | + } |
| 79 | + |
| 80 | + if i < len { |
| 81 | + // Swap any remaining bytes |
| 82 | + let mut t: UnalignedBlock = mem::uninitialized(); |
| 83 | + let rem = len - i; |
| 84 | + |
| 85 | + let t = &mut t as *mut _ as *mut u8; |
| 86 | + let x = x.offset(i as isize); |
| 87 | + let y = y.offset(i as isize); |
| 88 | + |
| 89 | + intrinsics::copy_nonoverlapping(x, t, rem); |
| 90 | + intrinsics::copy_nonoverlapping(y, x, rem); |
| 91 | + intrinsics::copy_nonoverlapping(t, y, rem); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +mod mem { |
| 97 | + extern "rust-intrinsic" { |
| 98 | + pub fn transmute<T, U>(_: T) -> U; |
| 99 | + pub fn size_of<T>() -> usize; |
| 100 | + } |
| 101 | + |
| 102 | + pub fn swap<T>(x: &mut T, y: &mut T) { |
| 103 | + unsafe { |
| 104 | + ptr::swap_nonoverlapping_one(x, y); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + pub fn replace<T>(dest: &mut T, mut src: T) -> T { |
| 109 | + swap(dest, &mut src); |
| 110 | + src |
| 111 | + } |
| 112 | + |
| 113 | + pub unsafe fn uninitialized<T>() -> T { |
| 114 | + intrinsics::uninit() |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +trait Step { |
| 119 | + fn replace_zero(&mut self) -> Self; |
| 120 | +} |
| 121 | + |
| 122 | +impl Step for i32 { |
| 123 | + fn replace_zero(&mut self) -> Self { |
| 124 | + mem::replace(self, 0) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +fn main() -> i32 { |
| 129 | + let a = 123; |
| 130 | + a.replace_zero(); |
| 131 | + a |
| 132 | +} |
0 commit comments