Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
use align_to
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 26, 2021
1 parent 1a385ff commit 4bf5c3e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 30 deletions.
24 changes: 11 additions & 13 deletions src/compute/aggregate/sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
array::{Array, PrimitiveArray},
bitmap::Bitmap,
};
use num_traits::Zero;

/// Object that can reduce itself to a number. This is used in the context of SIMD to reduce
/// a MD (e.g. `[f32; 16]`) into a single number (`f32`).
Expand Down Expand Up @@ -40,22 +41,19 @@ fn split_by_alignment<U, T>(values: &[T]) -> (&[T], &[T]) {
fn nonnull_sum<T>(values: &[T]) -> T
where
T: NativeType + Simd + Add<Output = T> + std::iter::Sum<T>,
T::Simd: Add<Output = T::Simd> + Sum<T>,
T::Simd: Add<Output = T::Simd> + Sum<T> + Copy,
{
let (head, aligned_values) = split_by_alignment::<T::Simd, _>(values);

let mut chunks = aligned_values.chunks_exact(T::Simd::LANES);

// Safety:
// we just made sure that we work on a slice af data aligned to T::Simd
let sum = chunks.by_ref().fold(T::Simd::default(), |acc, chunk| {
acc + unsafe { T::Simd::from_chunk_aligned_unchecked(chunk) }
});
// T::Simd is the vector type T and the alignment is similar to aligning to [T; alignment]
// the alignment of T::Simd ensures that it fits T.
let (head, simd_vals, tail) = unsafe { values.align_to::<T::Simd>() };

let remainder = T::Simd::from_incomplete_chunk(chunks.remainder(), T::default());
let reduced = sum + remainder;
let mut reduced = T::Simd::from_incomplete_chunk(&[], T::default());
for chunk in simd_vals {
reduced = reduced + *chunk
}

reduced.simd_sum() + head.iter().copied().sum()
reduced.simd_sum() + head.iter().copied().sum() + tail.iter().copied().sum()
}

/// # Panics
Expand Down Expand Up @@ -111,7 +109,7 @@ where
pub fn sum_primitive<T>(array: &PrimitiveArray<T>) -> Option<T>
where
T: NativeType + Simd + Add<Output = T> + std::iter::Sum<T>,
T::Simd: Add<Output = T::Simd> + Sum<T>,
T::Simd: Add<Output = T::Simd> + Sum<T> + Copy,
{
let null_count = array.null_count();

Expand Down
7 changes: 0 additions & 7 deletions src/types/simd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ pub trait NativeSimd: Default {
/// * iff `v.len()` != `T::LANES`
fn from_chunk(v: &[Self::Native]) -> Self;

/// Convert itself from a slice.
/// # Safety:
/// Caller must ensure:
/// * `v.len() == T::LANES`
/// * slice is aligned to `Self`
unsafe fn from_chunk_aligned_unchecked(v: &[Self::Native]) -> Self;

/// creates a new Self from `v` by populating items from `v` up to its length.
/// Items from `v` at positions larger than the number of lanes are ignored;
/// remaining items are populated with `remaining`.
Expand Down
5 changes: 0 additions & 5 deletions src/types/simd/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ macro_rules! simd {
($name)(v.try_into().unwrap())
}

#[inline]
unsafe fn from_chunk_aligned_unchecked(v: &[$type]) -> Self {
($name)(v.try_into().unwrap())
}

#[inline]
fn from_incomplete_chunk(v: &[$type], remaining: $type) -> Self {
let mut a = [remaining; $lanes];
Expand Down
5 changes: 0 additions & 5 deletions src/types/simd/packed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ macro_rules! simd {
<$name>::from_slice_unaligned(v)
}

#[inline]
unsafe fn from_chunk_aligned_unchecked(v: &[$type]) -> Self {
<$name>::from_slice_aligned_unchecked(v)
}

#[inline]
fn from_incomplete_chunk(v: &[$type], remaining: $type) -> Self {
let mut a = [remaining; $lanes];
Expand Down

0 comments on commit 4bf5c3e

Please sign in to comment.