Skip to content

Commit

Permalink
fill_via_chunks: better value names
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Sep 14, 2021
1 parent 9972046 commit 93ade1a
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions rand_core/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,22 @@ impl ToLe for u64 {

fn fill_via_chunks<T: ToLe>(src: &[T], dest: &mut [u8]) -> (usize, usize) {
let size = core::mem::size_of::<T>();
let chunk_size_u8 = min(src.len() * size, dest.len());
let chunk_size = (chunk_size_u8 + size - 1) / size;
let byte_len = min(src.len() * size, dest.len());
let num_chunks = (byte_len + size - 1) / size;

if cfg!(target_endian = "little") {
// On LE we can do a simple copy, which is 25-50% faster:
unsafe {
core::ptr::copy_nonoverlapping(
src.as_ptr() as *const u8,
dest.as_mut_ptr(),
chunk_size_u8,
byte_len,
);
}
} else {
// This code is valid on all arches, but slower than the above:
let mut i = 0;
let mut iter = dest[..chunk_size_u8].chunks_exact_mut(size);
let mut iter = dest[..byte_len].chunks_exact_mut(size);
while let Some(chunk) = iter.next() {
chunk.copy_from_slice(src[i].to_le_bytes().as_ref());
i += 1;
Expand All @@ -97,7 +97,7 @@ fn fill_via_chunks<T: ToLe>(src: &[T], dest: &mut [u8]) -> (usize, usize) {
}
}

(chunk_size, chunk_size_u8)
(num_chunks, byte_len)
}

/// Implement `fill_bytes` by reading chunks from the output buffer of a block
Expand Down

0 comments on commit 93ade1a

Please sign in to comment.