glam_det 2.0.0

A simple and fast 3D math library for games and graphics.
Documentation
// Copyright (C) 2020-2025 glam-det authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Generated from vec_mask.rs.tera template. Edit the template, not the generated file.

#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::{hash, ops::*};

use crate::nums::num_traits::Bool;
use crate::nums::{bool32x4, u32x4};
use crate::BVec4;

/// A 4-dimensional 4 lanes SOA boolean vector.
/// This type is 16 byte aligned, same as bool32x4.
#[derive(Clone, Copy)]
#[repr(C)]
pub struct BVec4x4 {
    pub x: bool32x4,
    pub y: bool32x4,
    pub z: bool32x4,
    pub w: bool32x4,
}

const FALSE: BVec4x4 = BVec4x4::new(
    bool32x4::FALSE,
    bool32x4::FALSE,
    bool32x4::FALSE,
    bool32x4::FALSE,
);

impl BVec4x4 {
    /// Creates a new vector mask.
    #[inline]
    pub const fn new(x: bool32x4, y: bool32x4, z: bool32x4, w: bool32x4) -> Self {
        Self { x, y, z, w }
    }

    /// Returns a bitmask with the lowest 4 bits set from the elements of `self`.
    ///
    /// A true element results in a `1` bit and a false element in a `0` bit.  Element `x` goes
    /// into the first lowest bit, element `y` into the second, etc.
    #[inline]
    pub fn bitmask(self) -> u32x4 {
        let x = self.x.cast_u32x4() & u32x4::from([1_u32; 4]);
        let y = self.y.cast_u32x4() & u32x4::from([1_u32; 4]);
        let z = self.z.cast_u32x4() & u32x4::from([1_u32; 4]);
        let w = self.w.cast_u32x4() & u32x4::from([1_u32; 4]);
        x | (y << 1) | (z << 2) | (w << 3)
    }

    #[inline]
    pub fn from_bitmask(m: u32x4) -> Self {
        let mut ret = Self::default();
        let m_array: [u32; 4] = m.into();
        let lane1_array: [BVec4; 4] = m_array.map(BVec4::from_bitmask);
        ret.x = lane1_array
            .map(
                #[inline]
                |z| z.x,
            )
            .into();
        ret.y = lane1_array
            .map(
                #[inline]
                |z| z.y,
            )
            .into();
        ret.z = lane1_array
            .map(
                #[inline]
                |z| z.z,
            )
            .into();
        ret.w = lane1_array
            .map(
                #[inline]
                |z| z.w,
            )
            .into();
        ret
    }

    /// Returns true if any of the elements are true, false otherwise.
    #[inline]
    pub fn any(self) -> bool32x4 {
        self.x | self.y | self.z | self.w
    }

    /// Returns true if all the elements are true, false otherwise.
    #[inline]
    pub fn all(self) -> bool32x4 {
        self.x & self.y & self.z & self.w
    }

    #[inline]
    fn into_bool_array(self) -> [bool32x4; 4] {
        [self.x, self.y, self.z, self.w]
    }

    #[inline]
    fn into_u32_array(self) -> [u32x4; 4] {
        [
            self.x.cast_u32x4(),
            self.y.cast_u32x4(),
            self.z.cast_u32x4(),
            self.w.cast_u32x4(),
        ]
    }
}

impl Default for BVec4x4 {
    #[inline]
    fn default() -> Self {
        FALSE
    }
}

impl PartialEq for BVec4x4 {
    #[inline]
    fn eq(&self, rhs: &Self) -> bool {
        self.bitmask().eq(&rhs.bitmask())
    }
}

impl Eq for BVec4x4 {}

impl hash::Hash for BVec4x4 {
    #[inline]
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        <[u32; 4]>::from(self.bitmask()).hash(state);
    }
}

impl BitAnd for BVec4x4 {
    type Output = Self;
    #[inline]
    fn bitand(self, rhs: Self) -> Self {
        Self {
            x: self.x & rhs.x,
            y: self.y & rhs.y,
            z: self.z & rhs.z,
            w: self.w & rhs.w,
        }
    }
}

impl BitAndAssign for BVec4x4 {
    #[inline]
    fn bitand_assign(&mut self, rhs: Self) {
        *self = self.bitand(rhs);
    }
}

impl BitOr for BVec4x4 {
    type Output = Self;
    #[inline]
    fn bitor(self, rhs: Self) -> Self {
        Self {
            x: self.x | rhs.x,
            y: self.y | rhs.y,
            z: self.z | rhs.z,
            w: self.w | rhs.w,
        }
    }
}

impl BitOrAssign for BVec4x4 {
    #[inline]
    fn bitor_assign(&mut self, rhs: Self) {
        *self = self.bitor(rhs);
    }
}

impl BitXor for BVec4x4 {
    type Output = Self;
    #[inline]
    fn bitxor(self, rhs: Self) -> Self {
        Self {
            x: self.x ^ rhs.x,
            y: self.y ^ rhs.y,
            z: self.z ^ rhs.z,
            w: self.w ^ rhs.w,
        }
    }
}

impl BitXorAssign for BVec4x4 {
    #[inline]
    fn bitxor_assign(&mut self, rhs: Self) {
        *self = self.bitxor(rhs);
    }
}
impl Not for BVec4x4 {
    type Output = Self;
    #[inline]
    fn not(self) -> Self {
        Self {
            x: !self.x,
            y: !self.y,
            z: !self.z,
            w: !self.w,
        }
    }
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for BVec4x4 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let arr = self.into_u32_array();
        write!(
            f,
            "{}({:#x}, {:#x}, {:#x}, {:#x})",
            stringify!(BVec4x4),
            arr[0],
            arr[1],
            arr[2],
            arr[3]
        )
    }
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for BVec4x4 {
    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<BVec4x4> for [bool32x4; 4] {
    #[inline]
    fn from(mask: BVec4x4) -> Self {
        mask.into_bool_array()
    }
}

impl From<BVec4x4> for [u32x4; 4] {
    #[inline]
    fn from(mask: BVec4x4) -> Self {
        mask.into_u32_array()
    }
}