#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::{hash, ops::*};
union UnionCastFfi {
simd: crate::bool::simd_alias::BVec4A,
ffi: BVec4A,
}
impl From<crate::bool::simd_alias::BVec4A> for BVec4A {
#[inline]
fn from(simd: crate::bool::simd_alias::BVec4A) -> Self {
unsafe { UnionCastFfi { simd }.ffi }
}
}
impl From<BVec4A> for crate::bool::simd_alias::BVec4A {
#[inline]
fn from(ffi: BVec4A) -> Self {
unsafe { UnionCastFfi { ffi }.simd }
}
}
#[derive(Clone, Copy)]
#[repr(C, align(16))]
pub(crate) struct C128(u32, u32, u32, u32);
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct BVec4A(pub(crate) C128);
const MASK: [u32; 2] = [0, 0xff_ff_ff_ff];
const FALSE: BVec4A = BVec4A::new(false, false, false, false);
impl BVec4A {
#[inline]
pub const fn new(x: bool, y: bool, z: bool, w: bool) -> Self {
Self(C128(
MASK[x as usize],
MASK[y as usize],
MASK[z as usize],
MASK[w as usize],
))
}
#[inline]
pub fn bitmask(self) -> u32 {
crate::bool::simd_alias::BVec4A::bitmask(self.into())
}
#[inline]
pub fn from_bitmask(m: u32) -> Self {
glam_assert!(!((1u32 << 4) - 1) & m == 0x0);
let x = m & 0x1 == 0x1;
let y = (m >> 1) & 0x1 == 0x1;
let z = (m >> 2) & 0x1 == 0x1;
let w = (m >> 3) & 0x1 == 0x1;
Self::new(x, y, z, w)
}
#[inline]
pub fn any(self) -> bool {
crate::bool::simd_alias::BVec4A::any(self.into())
}
#[inline]
pub fn all(self) -> bool {
crate::bool::simd_alias::BVec4A::all(self.into())
}
#[inline]
fn into_bool_array(self) -> [bool; 4] {
let bitmask = self.bitmask();
[
(bitmask & 1) != 0,
(bitmask & 2) != 0,
(bitmask & 4) != 0,
(bitmask & 8) != 0,
]
}
#[inline]
fn into_u32_array(self) -> [u32; 4] {
let bitmask = self.bitmask();
[
MASK[(bitmask & 1) as usize],
MASK[((bitmask >> 1) & 1) as usize],
MASK[((bitmask >> 2) & 1) as usize],
MASK[((bitmask >> 3) & 1) as usize],
]
}
}
impl Default for BVec4A {
#[inline]
fn default() -> Self {
FALSE
}
}
impl PartialEq for BVec4A {
#[inline]
fn eq(&self, rhs: &Self) -> bool {
self.bitmask().eq(&rhs.bitmask())
}
}
impl Eq for BVec4A {}
impl hash::Hash for BVec4A {
#[inline]
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.bitmask().hash(state);
}
}
impl BitAnd for BVec4A {
type Output = Self;
#[inline]
fn bitand(self, rhs: Self) -> Self {
crate::bool::simd_alias::BVec4A::bitand(self.into(), rhs.into()).into()
}
}
impl BitAndAssign for BVec4A {
#[inline]
fn bitand_assign(&mut self, rhs: Self) {
*self = self.bitand(rhs);
}
}
impl BitOr for BVec4A {
type Output = Self;
#[inline]
fn bitor(self, rhs: Self) -> Self {
crate::bool::simd_alias::BVec4A::bitor(self.into(), rhs.into()).into()
}
}
impl BitOrAssign for BVec4A {
#[inline]
fn bitor_assign(&mut self, rhs: Self) {
*self = self.bitor(rhs);
}
}
impl BitXor for BVec4A {
type Output = Self;
#[inline]
fn bitxor(self, rhs: Self) -> Self {
crate::bool::simd_alias::BVec4A::bitxor(self.into(), rhs.into()).into()
}
}
impl BitXorAssign for BVec4A {
#[inline]
fn bitxor_assign(&mut self, rhs: Self) {
*self = self.bitxor(rhs);
}
}
impl Not for BVec4A {
type Output = Self;
#[inline]
fn not(self) -> Self {
crate::bool::simd_alias::BVec4A::not(self.into()).into()
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for BVec4A {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let arr = self.into_u32_array();
write!(
f,
"{}({:#x}, {:#x}, {:#x}, {:#x})",
stringify!(BVec4A),
arr[0],
arr[1],
arr[2],
arr[3]
)
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for BVec4A {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let arr = self.into_bool_array();
write!(f, "[{}, {}, {}, {}]", arr[0], arr[1], arr[2], arr[3])
}
}
impl From<BVec4A> for [bool; 4] {
#[inline]
fn from(mask: BVec4A) -> Self {
mask.into_bool_array()
}
}
impl From<BVec4A> for [u32; 4] {
#[inline]
fn from(mask: BVec4A) -> Self {
mask.into_u32_array()
}
}