Skip to content

Commit

Permalink
Merge pull request #312 from Ralith/elementwise-unordered-atomics
Browse files Browse the repository at this point in the history
Tidy up unordered elementwise atomic memory intrinsics
  • Loading branch information
alexcrichton authored Aug 23, 2019
2 parents bc57f53 + 9775f08 commit 44dc22f
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type c_int = i16;
#[cfg(not(target_pointer_width = "16"))]
type c_int = i32;

use core::intrinsics::{atomic_load_unordered, atomic_store_unordered, unchecked_div};
use core::intrinsics::{atomic_load_unordered, atomic_store_unordered, exact_div};
use core::mem;
use core::ops::{BitOr, Shl};

Expand Down Expand Up @@ -63,9 +63,10 @@ pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
0
}

// `bytes` must be a multiple of `mem::size_of::<T>()`
fn memcpy_element_unordered_atomic<T: Copy>(dest: *mut T, src: *const T, bytes: usize) {
unsafe {
let n = unchecked_div(bytes, mem::size_of::<T>());
let n = exact_div(bytes, mem::size_of::<T>());
let mut i = 0;
while i < n {
atomic_store_unordered(dest.add(i), atomic_load_unordered(src.add(i)));
Expand All @@ -74,9 +75,10 @@ fn memcpy_element_unordered_atomic<T: Copy>(dest: *mut T, src: *const T, bytes:
}
}

// `bytes` must be a multiple of `mem::size_of::<T>()`
fn memmove_element_unordered_atomic<T: Copy>(dest: *mut T, src: *const T, bytes: usize) {
unsafe {
let n = unchecked_div(bytes, mem::size_of::<T>());
let n = exact_div(bytes, mem::size_of::<T>());
if src < dest as *const T {
// copy from end
let mut i = n;
Expand All @@ -95,18 +97,24 @@ fn memmove_element_unordered_atomic<T: Copy>(dest: *mut T, src: *const T, bytes:
}
}

// `T` must be a primitive integer type, and `bytes` must be a multiple of `mem::size_of::<T>()`
fn memset_element_unordered_atomic<T>(s: *mut T, c: u8, bytes: usize)
where
T: Copy + From<u8> + Shl<u32, Output = T> + BitOr<T, Output = T>,
{
unsafe {
let n = unchecked_div(bytes, mem::size_of::<T>());
let n = exact_div(bytes, mem::size_of::<T>());

// Construct a value of type `T` consisting of repeated `c`
// bytes, to let us ensure we write each `T` atomically.
let mut x = T::from(c);
let mut i = 1;
while i < mem::size_of::<T>() {
x = x << 8 | T::from(c);
i += 1;
}

// Write it to `s`
let mut i = 0;
while i < n {
atomic_store_unordered(s.add(i), x);
Expand Down

0 comments on commit 44dc22f

Please sign in to comment.