#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::ops::*;
#[cfg(target_arch = "x86")]
use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;
#[repr(C)]
union UnionCast {
a: [u32; 4],
v: BVec3A,
}
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct BVec3A(pub(crate) __m128);
const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
impl BVec3A {
pub const FALSE: Self = Self::splat(false);
pub const TRUE: Self = Self::splat(true);
#[inline(always)]
#[must_use]
pub const fn new(x: bool, y: bool, z: bool) -> Self {
unsafe {
UnionCast {
a: [MASK[x as usize], MASK[y as usize], MASK[z as usize], 0],
}
.v
}
}
#[inline]
#[must_use]
pub const fn splat(v: bool) -> Self {
Self::new(v, v, v)
}
#[inline]
#[must_use]
pub fn bitmask(self) -> u32 {
unsafe { (_mm_movemask_ps(self.0) as u32) & 0x7 }
}
#[inline]
#[must_use]
pub fn any(self) -> bool {
self.bitmask() != 0
}
#[inline]
#[must_use]
pub fn all(self) -> bool {
self.bitmask() == 0x7
}
#[inline]
#[must_use]
pub fn test(&self, index: usize) -> bool {
match index {
0 => (self.bitmask() & (1 << 0)) != 0,
1 => (self.bitmask() & (1 << 1)) != 0,
2 => (self.bitmask() & (1 << 2)) != 0,
_ => panic!("index out of bounds"),
}
}
#[inline]
pub fn set(&mut self, index: usize, value: bool) {
use crate::Vec3A;
let mut v = Vec3A(self.0);
v[index] = f32::from_bits(MASK[value as usize]);
*self = Self(v.0);
}
#[inline]
#[must_use]
fn into_bool_array(self) -> [bool; 3] {
let bitmask = self.bitmask();
[(bitmask & 1) != 0, (bitmask & 2) != 0, (bitmask & 4) != 0]
}
#[inline]
#[must_use]
fn into_u32_array(self) -> [u32; 3] {
let bitmask = self.bitmask();
[
MASK[(bitmask & 1) as usize],
MASK[((bitmask >> 1) & 1) as usize],
MASK[((bitmask >> 2) & 1) as usize],
]
}
}
impl Default for BVec3A {
#[inline]
fn default() -> Self {
Self::FALSE
}
}
impl PartialEq for BVec3A {
#[inline]
fn eq(&self, rhs: &Self) -> bool {
self.bitmask().eq(&rhs.bitmask())
}
}
impl Eq for BVec3A {}
impl core::hash::Hash for BVec3A {
#[inline]
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.bitmask().hash(state);
}
}
impl BitAnd for BVec3A {
type Output = Self;
#[inline]
fn bitand(self, rhs: Self) -> Self {
Self(unsafe { _mm_and_ps(self.0, rhs.0) })
}
}
impl BitAndAssign for BVec3A {
#[inline]
fn bitand_assign(&mut self, rhs: Self) {
*self = self.bitand(rhs);
}
}
impl BitOr for BVec3A {
type Output = Self;
#[inline]
fn bitor(self, rhs: Self) -> Self {
Self(unsafe { _mm_or_ps(self.0, rhs.0) })
}
}
impl BitOrAssign for BVec3A {
#[inline]
fn bitor_assign(&mut self, rhs: Self) {
*self = self.bitor(rhs);
}
}
impl BitXor for BVec3A {
type Output = Self;
#[inline]
fn bitxor(self, rhs: Self) -> Self {
Self(unsafe { _mm_xor_ps(self.0, rhs.0) })
}
}
impl BitXorAssign for BVec3A {
#[inline]
fn bitxor_assign(&mut self, rhs: Self) {
*self = self.bitxor(rhs);
}
}
impl Not for BVec3A {
type Output = Self;
#[inline]
fn not(self) -> Self {
Self(unsafe { _mm_andnot_ps(self.0, _mm_set_ps1(f32::from_bits(0xff_ff_ff_ff))) })
}
}
impl From<BVec3A> for __m128 {
#[inline]
fn from(t: BVec3A) -> Self {
t.0
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for BVec3A {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let arr = self.into_u32_array();
write!(
f,
"{}({:#x}, {:#x}, {:#x})",
stringify!(BVec3A),
arr[0],
arr[1],
arr[2]
)
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for BVec3A {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let arr = self.into_bool_array();
write!(f, "[{}, {}, {}]", arr[0], arr[1], arr[2])
}
}
impl From<BVec3A> for [bool; 3] {
#[inline]
fn from(mask: BVec3A) -> Self {
mask.into_bool_array()
}
}
impl From<BVec3A> for [u32; 3] {
#[inline]
fn from(mask: BVec3A) -> Self {
mask.into_u32_array()
}
}