use crate::bool::simd_alias::BVec4A;
use crate::f32::simd_alias::Vec4;
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::ops::*;
use core::arch::aarch64::*;
use auto_ops_det::{impl_op_ex, impl_op_ex_commutative};
use core::ops;
union UnionCast {
a: [f32; 4],
v: UnitVec4,
}
union VecUnionCast {
v: Vec4,
uv: UnitVec4,
}
impl Vec4 {
#[inline]
pub fn as_unit_vec4_unchecked(self) -> UnitVec4 {
unsafe { VecUnionCast { v: self }.uv }
}
}
impl UnitVec4 {
#[inline]
pub fn as_vec4(self) -> Vec4 {
unsafe { VecUnionCast { uv: self }.v }
}
}
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct UnitVec4(pub(crate) float32x4_t);
impl UnitVec4 {
pub const X: Self = Self::new_unchecked(1.0_f32, 0.0_f32, 0.0_f32, 0.0_f32);
pub const Y: Self = Self::new_unchecked(0.0_f32, 1.0_f32, 0.0_f32, 0.0_f32);
pub const Z: Self = Self::new_unchecked(0.0_f32, 0.0_f32, 1.0_f32, 0.0_f32);
pub const W: Self = Self::new_unchecked(0.0_f32, 0.0_f32, 0.0_f32, 1.0_f32);
pub const NEG_X: Self = Self::new_unchecked(-1.0_f32, 0.0_f32, 0.0_f32, 0.0_f32);
pub const NEG_Y: Self = Self::new_unchecked(0.0_f32, -1.0_f32, 0.0_f32, 0.0_f32);
pub const NEG_Z: Self = Self::new_unchecked(0.0_f32, 0.0_f32, -1.0_f32, 0.0_f32);
pub const NEG_W: Self = Self::new_unchecked(0.0_f32, 0.0_f32, 0.0_f32, -1.0_f32);
pub const AXES: [Self; 4] = [Self::X, Self::Y, Self::Z, Self::W];
#[inline]
pub const fn new_unchecked(x: f32, y: f32, z: f32, w: f32) -> Self {
unsafe { UnionCast { a: [x, y, z, w] }.v }
}
#[inline]
pub fn new_normalized(x: f32, y: f32, z: f32, w: f32) -> Self {
Vec4::new(x, y, z, w).normalize().as_unit_vec4_unchecked()
}
#[inline]
pub fn select(mask: BVec4A, if_true: Self, if_false: Self) -> Vec4 {
Vec4::select(mask, if_true.as_vec4(), if_false.as_vec4())
}
#[inline]
pub const fn from_array_unchecked(a: [f32; 4]) -> Self {
Self::new_unchecked(a[0], a[1], a[2], a[3])
}
#[inline]
pub const fn to_array(&self) -> [f32; 4] {
unsafe { *(self as *const UnitVec4 as *const [f32; 4]) }
}
#[inline]
pub const fn from_slice_unchecked(slice: &[f32]) -> Self {
Self::new_unchecked(slice[0], slice[1], slice[2], slice[3])
}
#[inline]
pub fn write_to_slice(self, slice: &mut [f32]) {
slice[0] = self.x;
slice[1] = self.y;
slice[2] = self.z;
slice[3] = self.w;
}
#[inline]
pub(crate) fn dot(self, rhs: Self) -> f32 {
self.as_vec4().dot(rhs.as_vec4())
}
#[inline]
pub fn min_element(self) -> f32 {
self.as_vec4().min_element()
}
#[inline]
pub fn max_element(self) -> f32 {
self.as_vec4().max_element()
}
#[inline]
pub fn cmpeq(self, rhs: Self) -> BVec4A {
self.as_vec4().cmpeq(rhs.as_vec4())
}
#[inline]
pub fn cmpne(self, rhs: Self) -> BVec4A {
self.as_vec4().cmpne(rhs.as_vec4())
}
#[inline]
pub fn cmpge(self, rhs: Self) -> BVec4A {
self.as_vec4().cmpge(rhs.as_vec4())
}
#[inline]
pub fn cmpgt(self, rhs: Self) -> BVec4A {
self.as_vec4().cmpgt(rhs.as_vec4())
}
#[inline]
pub fn cmple(self, rhs: Self) -> BVec4A {
self.as_vec4().cmple(rhs.as_vec4())
}
#[inline]
pub fn cmplt(self, rhs: Self) -> BVec4A {
self.as_vec4().cmple(rhs.as_vec4())
}
#[inline]
pub fn abs(self) -> Self {
self.as_vec4().abs().as_unit_vec4_unchecked()
}
#[inline]
pub fn signum(self) -> Vec4 {
self.as_vec4().signum()
}
#[inline]
pub fn distance(self, rhs: Self) -> f32 {
self.as_vec4().distance(rhs.as_vec4())
}
#[inline]
pub fn distance_squared(self, rhs: Self) -> f32 {
self.as_vec4().distance_squared(rhs.as_vec4())
}
#[must_use]
#[inline]
pub fn project_onto(self, rhs: Self) -> Vec4 {
rhs.as_vec4() * self.dot(rhs)
}
#[must_use]
#[inline]
pub fn reject_from(self, rhs: Self) -> Vec4 {
self.as_vec4() - self.project_onto(rhs)
}
#[doc(alias = "mix")]
#[inline]
pub fn lerp(self, rhs: Self, s: f32) -> Vec4 {
self.as_vec4().lerp(rhs.as_vec4(), s)
}
#[inline]
pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
Vec4::abs_diff_eq(self.as_vec4(), rhs.as_vec4(), max_abs_diff)
}
#[inline]
pub fn mul_add(self, a: Self, b: Self) -> Vec4 {
self.as_vec4().mul_add(a.as_vec4(), b.as_vec4())
}
}
impl Default for UnitVec4 {
#[inline]
fn default() -> Self {
Self::X
}
}
impl PartialEq for UnitVec4 {
#[inline]
fn eq(&self, rhs: &Self) -> bool {
self.cmpeq(*rhs).all()
}
}
impl Neg for UnitVec4 {
type Output = Self;
#[inline]
fn neg(self) -> Self {
self.as_vec4().neg().as_unit_vec4_unchecked()
}
}
impl_op_ex!(/ |a: &UnitVec4, b: &UnitVec4| -> Vec4 {
a.as_vec4() / b.as_vec4()
});
impl_op_ex!(/ |a: &UnitVec4, b: &f32| -> Vec4 {
a.as_vec4() / b
});
impl_op_ex!(/ |a: &f32, b: &UnitVec4| -> Vec4 {
a / b.as_vec4()
});
impl_op_ex_commutative!(/ |a: &UnitVec4, b: &Vec4| -> Vec4 {
a.as_vec4() / b
});
impl_op_ex!(*|a: &UnitVec4, b: &UnitVec4| -> Vec4 { a.as_vec4() * b.as_vec4() });
impl_op_ex!(*|a: &UnitVec4, b: &f32| -> Vec4 { a.as_vec4() * b });
impl_op_ex!(*|a: &f32, b: &UnitVec4| -> Vec4 { a * b.as_vec4() });
impl_op_ex_commutative!(*|a: &UnitVec4, b: &Vec4| -> Vec4 { a.as_vec4() * b });
impl_op_ex!(+ |a: &UnitVec4, b: &UnitVec4| -> Vec4 {
a.as_vec4() + b.as_vec4()
});
impl_op_ex!(+ |a: &UnitVec4, b: &f32| -> Vec4 {
a.as_vec4() + b
});
impl_op_ex!(+ |a: &f32, b: &UnitVec4| -> Vec4 {
a + b.as_vec4()
});
impl_op_ex_commutative!(+ |a: &UnitVec4, b: &Vec4| -> Vec4 {
a.as_vec4() + b
});
impl_op_ex!(-|a: &UnitVec4, b: &UnitVec4| -> Vec4 { a.as_vec4() - b.as_vec4() });
impl_op_ex!(-|a: &UnitVec4, b: &f32| -> Vec4 { a.as_vec4() - b });
impl_op_ex!(-|a: &f32, b: &UnitVec4| -> Vec4 { a - b.as_vec4() });
impl_op_ex_commutative!(-|a: &UnitVec4, b: &Vec4| -> Vec4 { a.as_vec4() - b });
impl_op_ex!(% |a: &UnitVec4, b: &UnitVec4| -> Vec4 {
a.as_vec4() % b.as_vec4()
});
impl_op_ex!(% |a: &UnitVec4, b: &f32| -> Vec4 {
a.as_vec4() % b
});
impl_op_ex!(% |a: &f32, b: &UnitVec4| -> Vec4 {
a % b.as_vec4()
});
impl_op_ex_commutative!(% |a: &UnitVec4, b: &Vec4| -> Vec4 {
a.as_vec4() % b
});
impl Index<usize> for UnitVec4 {
type Output = f32;
#[inline]
fn index(&self, index: usize) -> &Self::Output {
match index {
0 => &self.x,
1 => &self.y,
2 => &self.z,
3 => &self.w,
_ => panic!("index out of bounds"),
}
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for UnitVec4 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for UnitVec4 {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_tuple(stringify!(UnitVec4))
.field(&self.x)
.field(&self.y)
.field(&self.z)
.field(&self.w)
.finish()
}
}
impl From<UnitVec4> for float32x4_t {
#[inline]
fn from(t: UnitVec4) -> Self {
t.0
}
}
impl From<UnitVec4> for [f32; 4] {
#[inline]
fn from(v: UnitVec4) -> Self {
v.as_vec4().into()
}
}
impl From<UnitVec4> for (f32, f32, f32, f32) {
#[inline]
fn from(v: UnitVec4) -> Self {
v.as_vec4().into()
}
}
impl Deref for UnitVec4 {
type Target = crate::deref::Vec4<f32>;
#[inline]
fn deref(&self) -> &Self::Target {
unsafe { &*(self as *const Self).cast() }
}
}
#[cfg(not(target_arch = "spirv"))]
impl AsRef<[f32; 4]> for UnitVec4 {
#[inline]
fn as_ref(&self) -> &[f32; 4] {
unsafe { &*(self as *const UnitVec4 as *const [f32; 4]) }
}
}