From cf8e9f9aec25b6441d3198d7ddb4bd6a6b9e1475 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Fri, 24 May 2013 13:58:55 -0400 Subject: make the memcpy/memmove intrinsics higher-level This allows them to make use of the type's alignment, instead of being pessimistic and assuming it is only 1. --- src/libstd/ptr.rs | 60 ++++++++++++++++++++++++++------------- src/libstd/unstable/intrinsics.rs | 21 +++++++++++--- 2 files changed, 58 insertions(+), 23 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/ptr.rs b/src/libstd/ptr.rs index d1c0ffe7953..97a7d0e11a6 100644 --- a/src/libstd/ptr.rs +++ b/src/libstd/ptr.rs @@ -26,12 +26,6 @@ pub mod libc_ { #[nolink] #[abi = "cdecl"] pub extern { - #[rust_stack] - unsafe fn memmove(dest: *mut c_void, - src: *const c_void, - n: libc::size_t) - -> *c_void; - #[rust_stack] unsafe fn memset(dest: *mut c_void, c: libc::c_int, @@ -97,15 +91,28 @@ pub fn is_not_null(ptr: *const T) -> bool { !is_null(ptr) } * and destination may overlap. */ #[inline(always)] -#[cfg(target_word_size = "32")] +#[cfg(target_word_size = "32", stage0)] pub unsafe fn copy_memory(dst: *mut T, src: *const T, count: uint) { use unstable::intrinsics::memmove32; let n = count * sys::size_of::(); memmove32(dst as *mut u8, src as *u8, n as u32); } +/** + * Copies data from one location to another + * + * Copies `count` elements (not bytes) from `src` to `dst`. The source + * and destination may overlap. + */ +#[inline(always)] +#[cfg(target_word_size = "32", not(stage0))] +pub unsafe fn copy_memory(dst: *mut T, src: *const T, count: uint) { + use unstable::intrinsics::memmove32; + memmove32(dst, src, count as u32); +} + #[inline(always)] -#[cfg(target_word_size = "64")] +#[cfg(target_word_size = "64", stage0)] pub unsafe fn copy_memory(dst: *mut T, src: *const T, count: uint) { use unstable::intrinsics::memmove64; let n = count * sys::size_of::(); @@ -113,25 +120,40 @@ pub unsafe fn copy_memory(dst: *mut T, src: *const T, count: uint) { } #[inline(always)] -#[cfg(target_word_size = "32")] +#[cfg(target_word_size = "64", not(stage0))] +pub unsafe fn copy_memory(dst: *mut T, src: *const T, count: uint) { + use unstable::intrinsics::memmove64; + memmove64(dst, src as *T, count as u64); +} + +#[inline(always)] +#[cfg(target_word_size = "32", stage0)] +pub unsafe fn copy_nonoverlapping_memory(dst: *mut T, src: *const T, count: uint) { + use unstable::intrinsics::memmove32; + let n = count * sys::size_of::(); + memmove32(dst as *mut u8, src as *u8, n as u32); +} + +#[inline(always)] +#[cfg(target_word_size = "32", not(stage0))] pub unsafe fn copy_nonoverlapping_memory(dst: *mut T, src: *const T, count: uint) { - #[cfg(stage0)] - use memcpy32 = unstable::intrinsics::memmove32; - #[cfg(not(stage0))] use unstable::intrinsics::memcpy32; + memcpy32(dst, src as *T, count as u32); +} + +#[inline(always)] +#[cfg(target_word_size = "64", stage0)] +pub unsafe fn copy_nonoverlapping_memory(dst: *mut T, src: *const T, count: uint) { + use unstable::intrinsics::memmove64; let n = count * sys::size_of::(); - memcpy32(dst as *mut u8, src as *u8, n as u32); + memmove64(dst as *mut u8, src as *u8, n as u64); } #[inline(always)] -#[cfg(target_word_size = "64")] +#[cfg(target_word_size = "64", not(stage0))] pub unsafe fn copy_nonoverlapping_memory(dst: *mut T, src: *const T, count: uint) { - #[cfg(stage0)] - use memcpy64 = unstable::intrinsics::memmove64; - #[cfg(not(stage0))] use unstable::intrinsics::memcpy64; - let n = count * sys::size_of::(); - memcpy64(dst as *mut u8, src as *u8, n as u64); + memcpy64(dst, src as *T, count as u64); } #[inline(always)] diff --git a/src/libstd/unstable/intrinsics.rs b/src/libstd/unstable/intrinsics.rs index 521708621fc..c0975d6a526 100644 --- a/src/libstd/unstable/intrinsics.rs +++ b/src/libstd/unstable/intrinsics.rs @@ -127,18 +127,31 @@ pub extern "rust-intrinsic" { /// Get the address of the `__morestack` stack growth function. pub fn morestack_addr() -> *(); - /// Equivalent to the `llvm.memcpy.p0i8.0i8.i32` intrinsic. + /// Equivalent to the `llvm.memcpy.p0i8.0i8.i32` intrinsic, with a size of + /// `count` * `size_of::()` and an alignment of `min_align_of::()` #[cfg(not(stage0))] - pub fn memcpy32(dst: *mut u8, src: *u8, size: u32); - /// Equivalent to the `llvm.memcpy.p0i8.0i8.i64` intrinsic. + pub fn memcpy32(dst: *mut T, src: *T, count: u32); + /// Equivalent to the `llvm.memcpy.p0i8.0i8.i64` intrinsic, with a size of + /// `count` * `size_of::()` and an alignment of `min_align_of::()` #[cfg(not(stage0))] - pub fn memcpy64(dst: *mut u8, src: *u8, size: u64); + pub fn memcpy64(dst: *mut T, src: *T, count: u64); /// Equivalent to the `llvm.memmove.p0i8.0i8.i32` intrinsic. + #[cfg(stage0)] pub fn memmove32(dst: *mut u8, src: *u8, size: u32); /// Equivalent to the `llvm.memmove.p0i8.0i8.i64` intrinsic. + #[cfg(stage0)] pub fn memmove64(dst: *mut u8, src: *u8, size: u64); + /// Equivalent to the `llvm.memmove.p0i8.0i8.i32` intrinsic, with a size of + /// `count` * `size_of::()` and an alignment of `min_align_of::()` + #[cfg(not(stage0))] + pub fn memmove32(dst: *mut T, src: *T, count: u32); + /// Equivalent to the `llvm.memmove.p0i8.0i8.i64` intrinsic, with a size of + /// `count` * `size_of::()` and an alignment of `min_align_of::()` + #[cfg(not(stage0))] + pub fn memmove64(dst: *mut T, src: *T, count: u64); + pub fn sqrtf32(x: f32) -> f32; pub fn sqrtf64(x: f64) -> f64; -- cgit 1.4.1-3-g733a5 From 2eaa5dbb6033a19ab03d40d01e90511b28015d0f Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Fri, 24 May 2013 17:55:49 -0400 Subject: C++0x -> C++11 --- src/libstd/unstable/intrinsics.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/unstable/intrinsics.rs b/src/libstd/unstable/intrinsics.rs index c0975d6a526..1aeec0e0b1c 100644 --- a/src/libstd/unstable/intrinsics.rs +++ b/src/libstd/unstable/intrinsics.rs @@ -16,7 +16,7 @@ The corresponding definitions are in librustc/middle/trans/foreign.rs. The atomic intrinsics provide common atomic operations on machine words, with multiple possible memory orderings. They obey the same -semantics as C++0x. See the LLVM documentation on [[atomics]]. +semantics as C++11. See the LLVM documentation on [[atomics]]. [atomics]: http://llvm.org/docs/Atomics.html -- cgit 1.4.1-3-g733a5 From d9c0f0f188eb93fab1077d02de200bc4b961690f Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Fri, 24 May 2013 18:05:27 -0400 Subject: add memset32/memset64 --- src/librustc/middle/trans/foreign.rs | 26 ++++++++++++++++++++++++++ src/librustc/middle/trans/type_use.rs | 3 ++- src/librustc/middle/typeck/check/mod.rs | 24 ++++++++++++++++++++++++ src/libstd/ptr.rs | 24 ++++++++++++++++++++++++ src/libstd/unstable/intrinsics.rs | 10 ++++++++++ 5 files changed, 86 insertions(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/librustc/middle/trans/foreign.rs b/src/librustc/middle/trans/foreign.rs index 6f420fc44ca..7bfec9c89cb 100644 --- a/src/librustc/middle/trans/foreign.rs +++ b/src/librustc/middle/trans/foreign.rs @@ -897,6 +897,32 @@ pub fn trans_intrinsic(ccx: @CrateContext, let llfn = *bcx.ccx().intrinsics.get(&~"llvm.memmove.p0i8.p0i8.i64"); Call(bcx, llfn, [dst_ptr, src_ptr, Mul(bcx, size, count), align, volatile]); } + ~"memset32" => { + let tp_ty = substs.tys[0]; + let lltp_ty = type_of::type_of(ccx, tp_ty); + let align = C_i32(machine::llalign_of_min(ccx, lltp_ty) as i32); + let size = C_i32(machine::llsize_of_real(ccx, lltp_ty) as i32); + + let dst_ptr = PointerCast(bcx, get_param(decl, first_real_arg), T_ptr(T_i8())); + let val = get_param(decl, first_real_arg + 1); + let count = get_param(decl, first_real_arg + 2); + let volatile = C_i1(false); + let llfn = *bcx.ccx().intrinsics.get(&~"llvm.memset.p0i8.i32"); + Call(bcx, llfn, [dst_ptr, val, Mul(bcx, size, count), align, volatile]); + } + ~"memset64" => { + let tp_ty = substs.tys[0]; + let lltp_ty = type_of::type_of(ccx, tp_ty); + let align = C_i32(machine::llalign_of_min(ccx, lltp_ty) as i32); + let size = C_i64(machine::llsize_of_real(ccx, lltp_ty) as i64); + + let dst_ptr = PointerCast(bcx, get_param(decl, first_real_arg), T_ptr(T_i8())); + let val = get_param(decl, first_real_arg + 1); + let count = get_param(decl, first_real_arg + 2); + let volatile = C_i1(false); + let llfn = *bcx.ccx().intrinsics.get(&~"llvm.memset.p0i8.i64"); + Call(bcx, llfn, [dst_ptr, val, Mul(bcx, size, count), align, volatile]); + } ~"sqrtf32" => { let x = get_param(decl, first_real_arg); let sqrtf = *ccx.intrinsics.get(&~"llvm.sqrt.f32"); diff --git a/src/librustc/middle/trans/type_use.rs b/src/librustc/middle/trans/type_use.rs index a9416a46ddf..ceb229c79bd 100644 --- a/src/librustc/middle/trans/type_use.rs +++ b/src/librustc/middle/trans/type_use.rs @@ -135,7 +135,8 @@ pub fn type_uses_for(ccx: @CrateContext, fn_id: def_id, n_tps: uint) ~"visit_tydesc" | ~"forget" | ~"frame_address" | ~"morestack_addr" => 0, - ~"memcpy32" | ~"memcpy64" | ~"memmove32" | ~"memmove64" => use_repr, + ~"memcpy32" | ~"memcpy64" | ~"memmove32" | ~"memmove64" | + ~"memset32" | ~"memset64" => use_repr, ~"sqrtf32" | ~"sqrtf64" | ~"powif32" | ~"powif64" | ~"sinf32" | ~"sinf64" | ~"cosf32" | ~"cosf64" | diff --git a/src/librustc/middle/typeck/check/mod.rs b/src/librustc/middle/typeck/check/mod.rs index 1d959a54c0e..d1714555f20 100644 --- a/src/librustc/middle/typeck/check/mod.rs +++ b/src/librustc/middle/typeck/check/mod.rs @@ -3597,6 +3597,30 @@ pub fn check_intrinsic_type(ccx: @mut CrateCtxt, it: @ast::foreign_item) { ], ty::mk_nil()) } + ~"memset32" => { + (1, + ~[ + ty::mk_ptr(tcx, ty::mt { + ty: param(ccx, 0), + mutbl: ast::m_mutbl + }), + ty::mk_u8(), + ty::mk_u32() + ], + ty::mk_nil()) + } + ~"memset64" => { + (1, + ~[ + ty::mk_ptr(tcx, ty::mt { + ty: param(ccx, 0), + mutbl: ast::m_mutbl + }), + ty::mk_u8(), + ty::mk_u64() + ], + ty::mk_nil()) + } ~"sqrtf32" => (0, ~[ ty::mk_f32() ], ty::mk_f32()), ~"sqrtf64" => (0, ~[ ty::mk_f64() ], ty::mk_f64()), ~"powif32" => { diff --git a/src/libstd/ptr.rs b/src/libstd/ptr.rs index 97a7d0e11a6..e787558c6e4 100644 --- a/src/libstd/ptr.rs +++ b/src/libstd/ptr.rs @@ -19,6 +19,7 @@ use sys; #[cfg(not(test))] use cmp::{Eq, Ord}; use uint; +#[cfg(stage0)] pub mod libc_ { use libc::c_void; use libc; @@ -157,11 +158,26 @@ pub unsafe fn copy_nonoverlapping_memory(dst: *mut T, src: *const T, count: u } #[inline(always)] +#[cfg(stage0)] pub unsafe fn set_memory(dst: *mut T, c: int, count: uint) { let n = count * sys::size_of::(); libc_::memset(dst as *mut c_void, c as libc::c_int, n as size_t); } +#[inline(always)] +#[cfg(target_word_size = "32", not(stage0))] +pub unsafe fn set_memory(dst: *mut T, c: u8, count: uint) { + use unstable::intrinsics::memset32; + memset32(dst, c, count as u32); +} + +#[inline(always)] +#[cfg(target_word_size = "64", not(stage0))] +pub unsafe fn set_memory(dst: *mut T, c: u8, count: uint) { + use unstable::intrinsics::memset64; + memset64(dst, c, count as u64); +} + /** Transform a region pointer - &T - to an unsafe pointer - *T. This is safe, but is implemented with an unsafe block due to @@ -603,4 +619,12 @@ pub mod ptr_tests { }); } } + + #[test] + fn test_set_memory() { + let mut xs = [0u8, ..20]; + let ptr = vec::raw::to_mut_ptr(xs); + unsafe { set_memory(ptr, 5u8, xs.len()); } + assert_eq!(xs, [5u8, ..20]); + } } diff --git a/src/libstd/unstable/intrinsics.rs b/src/libstd/unstable/intrinsics.rs index 1aeec0e0b1c..908c5e23ab0 100644 --- a/src/libstd/unstable/intrinsics.rs +++ b/src/libstd/unstable/intrinsics.rs @@ -31,6 +31,7 @@ A quick refresher on memory ordering: with atomic types and is equivalent to Java's `volatile`. */ + #[abi = "rust-intrinsic"] pub extern "rust-intrinsic" { @@ -152,6 +153,15 @@ pub extern "rust-intrinsic" { #[cfg(not(stage0))] pub fn memmove64(dst: *mut T, src: *T, count: u64); + /// Equivalent to the `llvm.memset.p0i8.i32` intrinsic, with a size of + /// `count` * `size_of::()` and an alignment of `min_align_of::()` + #[cfg(not(stage0))] + pub fn memset32(dst: *mut T, val: u8, count: u32); + /// Equivalent to the `llvm.memset.p0i8.i64` intrinsic, with a size of + /// `count` * `size_of::()` and an alignment of `min_align_of::()` + #[cfg(not(stage0))] + pub fn memset64(dst: *mut T, val: u8, count: u64); + pub fn sqrtf32(x: f32) -> f32; pub fn sqrtf64(x: f64) -> f64; -- cgit 1.4.1-3-g733a5 From f6023a01d43557b45dda550ef50d689877b87226 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sat, 25 May 2013 08:40:02 -0400 Subject: use uninit for cast::transmute_copy --- src/libstd/cast.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libstd') diff --git a/src/libstd/cast.rs b/src/libstd/cast.rs index cde22afd34a..d4a87f562e1 100644 --- a/src/libstd/cast.rs +++ b/src/libstd/cast.rs @@ -15,7 +15,7 @@ use unstable::intrinsics; /// Casts the value at `src` to U. The two types must have the same length. pub unsafe fn transmute_copy(src: &T) -> U { - let mut dest: U = intrinsics::init(); + let mut dest: U = intrinsics::uninit(); { let dest_ptr: *mut u8 = transmute(&mut dest); let src_ptr: *u8 = transmute(src); -- cgit 1.4.1-3-g733a5 From b25c5201028d7e08ef6c40385fd848157ea72567 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sat, 25 May 2013 11:43:11 -0400 Subject: make transmute_copy use memcpy, and inline it --- src/libstd/cast.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/libstd') diff --git a/src/libstd/cast.rs b/src/libstd/cast.rs index d4a87f562e1..40a62c12bb3 100644 --- a/src/libstd/cast.rs +++ b/src/libstd/cast.rs @@ -14,6 +14,7 @@ use sys; use unstable::intrinsics; /// Casts the value at `src` to U. The two types must have the same length. +#[cfg(stage0)] pub unsafe fn transmute_copy(src: &T) -> U { let mut dest: U = intrinsics::uninit(); { @@ -26,6 +27,26 @@ pub unsafe fn transmute_copy(src: &T) -> U { dest } +#[cfg(target_word_size = "32", not(stage0))] +#[inline(always)] +pub unsafe fn transmute_copy(src: &T) -> U { + let mut dest: U = intrinsics::uninit(); + let dest_ptr: *mut u8 = transmute(&mut dest); + let src_ptr: *u8 = transmute(src); + intrinsics::memcpy32(dest_ptr, src_ptr, sys::size_of::() as u64); + dest +} + +#[cfg(target_word_size = "64", not(stage0))] +#[inline(always)] +pub unsafe fn transmute_copy(src: &T) -> U { + let mut dest: U = intrinsics::uninit(); + let dest_ptr: *mut u8 = transmute(&mut dest); + let src_ptr: *u8 = transmute(src); + intrinsics::memcpy64(dest_ptr, src_ptr, sys::size_of::() as u64); + dest +} + /** * Move a thing into the void * -- cgit 1.4.1-3-g733a5 From 14846613bbd72003dd8f244b64ad679b586b6dd9 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sat, 25 May 2013 11:47:11 -0400 Subject: inline bump_box_refcount --- src/libstd/cast.rs | 1 + 1 file changed, 1 insertion(+) (limited to 'src/libstd') diff --git a/src/libstd/cast.rs b/src/libstd/cast.rs index 40a62c12bb3..9ef5d8ad6f7 100644 --- a/src/libstd/cast.rs +++ b/src/libstd/cast.rs @@ -64,6 +64,7 @@ pub unsafe fn forget(thing: T) { intrinsics::forget(thing); } * and/or reinterpret_cast when such calls would otherwise scramble a box's * reference count */ +#[inline(always)] pub unsafe fn bump_box_refcount(t: @T) { forget(t); } /** -- cgit 1.4.1-3-g733a5 From e6c04dea0325af808198306e283c17f90d31fc26 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Mon, 27 May 2013 18:14:00 -0400 Subject: fix casts on 32-bit --- src/libstd/cast.rs | 2 +- src/libstd/ptr.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/cast.rs b/src/libstd/cast.rs index 9ef5d8ad6f7..30ad41f0ca2 100644 --- a/src/libstd/cast.rs +++ b/src/libstd/cast.rs @@ -33,7 +33,7 @@ pub unsafe fn transmute_copy(src: &T) -> U { let mut dest: U = intrinsics::uninit(); let dest_ptr: *mut u8 = transmute(&mut dest); let src_ptr: *u8 = transmute(src); - intrinsics::memcpy32(dest_ptr, src_ptr, sys::size_of::() as u64); + intrinsics::memcpy32(dest_ptr, src_ptr, sys::size_of::() as u32); dest } diff --git a/src/libstd/ptr.rs b/src/libstd/ptr.rs index e787558c6e4..309129b7f13 100644 --- a/src/libstd/ptr.rs +++ b/src/libstd/ptr.rs @@ -109,7 +109,7 @@ pub unsafe fn copy_memory(dst: *mut T, src: *const T, count: uint) { #[cfg(target_word_size = "32", not(stage0))] pub unsafe fn copy_memory(dst: *mut T, src: *const T, count: uint) { use unstable::intrinsics::memmove32; - memmove32(dst, src, count as u32); + memmove32(dst, src as *T, count as u32); } #[inline(always)] -- cgit 1.4.1-3-g733a5