Skip to content

Use intrinsics for Mask::{to,from}_array #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions crates/core_simd/src/masks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
)]
mod mask_impl;

use crate::simd::intrinsics;
use crate::simd::{LaneCount, Simd, SimdElement, SupportedLaneCount};
use core::cmp::Ordering;
use core::fmt;
use core::{fmt, mem};

mod sealed {
use super::*;
Expand Down Expand Up @@ -105,22 +106,39 @@ where
Self(mask_impl::Mask::splat(value))
}

/// Converts an array to a SIMD vector.
/// Converts an array of bools to a SIMD mask.
pub fn from_array(array: [bool; LANES]) -> Self {
let mut vector = Self::splat(false);
for (i, v) in array.iter().enumerate() {
vector.set(i, *v);
// SAFETY: Rust's bool has a layout of 1 byte (u8) with a value of
// true: 0b_0000_0001
// false: 0b_0000_0000
// Thus, an array of bools is also a valid array of bytes: [u8; N]
// This would be hypothetically valid as an "in-place" transmute,
// but these are "dependently-sized" types, so copy elision it is!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

transmute_copy doesn't check if the input and output types have the same size.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, but we know they're the same size since the input type is [bool; LANES] and the output type is [u8; LANES] and Rust guarantees that bool is a single byte.

unsafe {
let bytes: [u8; LANES] = mem::transmute_copy(&array);
let bools: Simd<i8, LANES> =
intrinsics::simd_ne(Simd::from_array(bytes), Simd::splat(0u8));
Comment on lines +119 to +120
Copy link
Member

@bjorn3 bjorn3 Nov 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary? Can't this be just Simd::from_array(bytes)?

Edit: is it because this needs -1 for true instead of 1?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. It could also be simd_neg, come to think of it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simd_ne is best because llvm knows the result is supposed to be a mask, with neg llvm doesn't know that, so will likely optimize more poorly

Mask::from_int_unchecked(intrinsics::simd_cast(bools))
}
vector
}

/// Converts a SIMD vector to an array.
/// Converts a SIMD mask to an array of bools.
pub fn to_array(self) -> [bool; LANES] {
let mut array = [false; LANES];
for (i, v) in array.iter_mut().enumerate() {
*v = self.test(i);
// This follows mostly the same logic as from_array.
// SAFETY: Rust's bool has a layout of 1 byte (u8) with a value of
// true: 0b_0000_0001
// false: 0b_0000_0000
// Thus, an array of bools is also a valid array of bytes: [u8; N]
// Since our masks are equal to integers where all bits are set,
// we can simply convert them to i8s, and then bitand them by the
// bitpattern for Rust's "true" bool.
// This would be hypothetically valid as an "in-place" transmute,
// but these are "dependently-sized" types, so copy elision it is!
unsafe {
let mut bytes: Simd<i8, LANES> = intrinsics::simd_cast(self.to_int());
bytes &= Simd::splat(1i8);
mem::transmute_copy(&bytes)
}
array
}

/// Converts a vector of integers to a mask, where 0 represents `false` and -1
Expand Down