| 1 |
// { dg-options "-w" } |
| 2 |
#![feature(intrinsics)] |
| 3 |
|
| 4 |
pub use option::Option::{self, None, Some}; |
| 5 |
pub use result::Result::{self, Err, Ok}; |
| 6 |
|
| 7 |
mod option { |
| 8 |
pub enum Option<T> { |
| 9 |
None, |
| 10 |
Some(T), |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
mod result { |
| 15 |
pub enum Result<T, E> { |
| 16 |
Ok(T), |
| 17 |
Err(E), |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
#[lang = "sized"] |
| 22 |
pub trait Sized {} |
| 23 |
|
| 24 |
#[lang = "clone"] |
| 25 |
pub trait Clone: Sized { |
| 26 |
fn clone(&self) -> Self; |
| 27 |
|
| 28 |
fn clone_from(&mut self, source: &Self) { |
| 29 |
*self = source.clone() |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
mod impls { |
| 34 |
use super::Clone; |
| 35 |
|
| 36 |
macro_rules! impl_clone { |
| 37 |
($($t:ty)*) => { |
| 38 |
$( |
| 39 |
impl Clone for $t { |
| 40 |
fn clone(&self) -> Self { |
| 41 |
*self |
| 42 |
} |
| 43 |
} |
| 44 |
)* |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
impl_clone! { |
| 49 |
usize u8 u16 u32 u64 // u128 |
| 50 |
isize i8 i16 i32 i64 // i128 |
| 51 |
f32 f64 |
| 52 |
bool char |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
#[lang = "copy"] |
| 57 |
pub trait Copy: Clone { |
| 58 |
// Empty. |
| 59 |
} |
| 60 |
|
| 61 |
mod copy_impls { |
| 62 |
use super::Copy; |
| 63 |
|
| 64 |
macro_rules! impl_copy { |
| 65 |
($($t:ty)*) => { |
| 66 |
$( |
| 67 |
impl Copy for $t {} |
| 68 |
)* |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
impl_copy! { |
| 73 |
usize u8 u16 u32 u64 // u128 |
| 74 |
isize i8 i16 i32 i64 // i128 |
| 75 |
f32 f64 |
| 76 |
bool char |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
mod intrinsics { |
| 81 |
extern "rust-intrinsic" { |
| 82 |
pub fn add_with_overflow<T>(x: T, y: T) -> (T, bool); |
| 83 |
pub fn wrapping_add<T>(a: T, b: T) -> T; |
| 84 |
pub fn wrapping_sub<T>(a: T, b: T) -> T; |
| 85 |
pub fn rotate_left<T>(a: T, b: T) -> T; |
| 86 |
pub fn rotate_right<T>(a: T, b: T) -> T; |
| 87 |
pub fn offset<T>(ptr: *const T, count: isize) -> *const T; |
| 88 |
pub fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize); |
| 89 |
pub fn move_val_init<T>(dst: *mut T, src: T); |
| 90 |
pub fn uninit<T>() -> T; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
mod ptr { |
| 95 |
#[lang = "const_ptr"] |
| 96 |
impl<T> *const T { |
| 97 |
pub unsafe fn offset(self, count: isize) -> *const T { |
| 98 |
crate::intrinsics::offset(self, count) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
#[lang = "mut_ptr"] |
| 103 |
impl<T> *mut T { |
| 104 |
pub unsafe fn offset(self, count: isize) -> *mut T { |
| 105 |
crate::intrinsics::offset(self, count) as *mut T |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
pub unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) { |
| 110 |
let x = x as *mut u8; |
| 111 |
let y = y as *mut u8; |
| 112 |
let len = crate::mem::size_of::<T>() * count; |
| 113 |
swap_nonoverlapping_bytes(x, y, len) |
| 114 |
} |
| 115 |
|
| 116 |
pub unsafe fn swap_nonoverlapping_one<T>(x: *mut T, y: *mut T) { |
| 117 |
// For types smaller than the block optimization below, |
| 118 |
// just swap directly to avoid pessimizing codegen. |
| 119 |
if crate::mem::size_of::<T>() < 32 { |
| 120 |
let z = read(x); |
| 121 |
crate::intrinsics::copy_nonoverlapping(y, x, 1); |
| 122 |
write(y, z); |
| 123 |
} else { |
| 124 |
swap_nonoverlapping(x, y, 1); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
pub unsafe fn write<T>(dst: *mut T, src: T) { |
| 129 |
crate::intrinsics::move_val_init(&mut *dst, src) |
| 130 |
} |
| 131 |
|
| 132 |
pub unsafe fn read<T>(src: *const T) -> T { |
| 133 |
let mut tmp: T = crate::mem::uninitialized(); |
| 134 |
crate::intrinsics::copy_nonoverlapping(src, &mut tmp, 1); |
| 135 |
tmp |
| 136 |
} |
| 137 |
|
| 138 |
unsafe fn swap_nonoverlapping_bytes(x: *mut u8, y: *mut u8, len: usize) { |
| 139 |
struct Block(u64, u64, u64, u64); |
| 140 |
struct UnalignedBlock(u64, u64, u64, u64); |
| 141 |
|
| 142 |
let block_size = crate::mem::size_of::<Block>(); |
| 143 |
|
| 144 |
// Loop through x & y, copying them `Block` at a time |
| 145 |
// The optimizer should unroll the loop fully for most types |
| 146 |
// N.B. We can't use a for loop as the `range` impl calls `mem::swap` recursively |
| 147 |
let mut i: usize = 0; |
| 148 |
while i + block_size <= len { |
| 149 |
// Create some uninitialized memory as scratch space |
| 150 |
// Declaring `t` here avoids aligning the stack when this loop is unused |
| 151 |
let mut t: Block = crate::mem::uninitialized(); |
| 152 |
let t = &mut t as *mut _ as *mut u8; |
| 153 |
let x = x.offset(i as isize); |
| 154 |
let y = y.offset(i as isize); |
| 155 |
|
| 156 |
// Swap a block of bytes of x & y, using t as a temporary buffer |
| 157 |
// This should be optimized into efficient SIMD operations where available |
| 158 |
crate::intrinsics::copy_nonoverlapping(x, t, block_size); |
| 159 |
crate::intrinsics::copy_nonoverlapping(y, x, block_size); |
| 160 |
crate::intrinsics::copy_nonoverlapping(t, y, block_size); |
| 161 |
i += block_size; |
| 162 |
} |
| 163 |
|
| 164 |
if i < len { |
| 165 |
// Swap any remaining bytes |
| 166 |
let mut t: UnalignedBlock = crate::mem::uninitialized(); |
| 167 |
let rem = len - i; |
| 168 |
|
| 169 |
let t = &mut t as *mut _ as *mut u8; |
| 170 |
let x = x.offset(i as isize); |
| 171 |
let y = y.offset(i as isize); |
| 172 |
|
| 173 |
crate::intrinsics::copy_nonoverlapping(x, t, rem); |
| 174 |
crate::intrinsics::copy_nonoverlapping(y, x, rem); |
| 175 |
crate::intrinsics::copy_nonoverlapping(t, y, rem); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
mod mem { |
| 181 |
extern "rust-intrinsic" { |
| 182 |
pub fn transmute<T, U>(_: T) -> U; |
| 183 |
pub fn size_of<T>() -> usize; |
| 184 |
} |
| 185 |
|
| 186 |
pub fn swap<T>(x: &mut T, y: &mut T) { |
| 187 |
unsafe { |
| 188 |
crate::ptr::swap_nonoverlapping_one(x, y); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
pub fn test(dest: &mut i32, mut src: i32) -> i32 { |
| 193 |
swap(dest, &mut src); |
| 194 |
src |
| 195 |
} |
| 196 |
|
| 197 |
pub unsafe fn uninitialized<T>() -> T { |
| 198 |
crate::intrinsics::uninit() |
| 199 |
} |
| 200 |
} |