RMath: change fallback Float4 to be a tuple-struct.

This commit is contained in:
Nathan Vegdahl 2022-07-15 00:49:37 -07:00
parent a93a3f09da
commit fa7be4e58c

View File

@ -12,21 +12,19 @@ mod fallback {
#[derive(Debug, Copy, Clone)]
#[repr(C, align(16))]
pub struct Float4 {
n: [f32; 4],
}
pub struct Float4([f32; 4]);
impl Float4 {
/// Create a new `Float4` with the given components.
#[inline(always)]
pub fn new(a: f32, b: f32, c: f32, d: f32) -> Self {
Self { n: [a, b, c, d] }
Self([a, b, c, d])
}
/// Create a new `Float4` with all elements set to `n`.
#[inline(always)]
pub fn splat(n: f32) -> Self {
Self { n: [n, n, n, n] }
Self([n, n, n, n])
}
/// Component-wise fused multiply-add.
@ -34,55 +32,49 @@ mod fallback {
/// `(self * a) + b` with only one rounding error.
#[inline(always)]
pub fn mul_add(self, a: Self, b: Self) -> Self {
Self {
n: [
self.n[0].mul_add(a.n[0], b.n[0]),
self.n[1].mul_add(a.n[1], b.n[1]),
self.n[2].mul_add(a.n[2], b.n[2]),
self.n[3].mul_add(a.n[3], b.n[3]),
],
}
Self([
self.0[0].mul_add(a.0[0], b.0[0]),
self.0[1].mul_add(a.0[1], b.0[1]),
self.0[2].mul_add(a.0[2], b.0[2]),
self.0[3].mul_add(a.0[3], b.0[3]),
])
}
/// Vertical minimum.
#[inline(always)]
pub fn min(self, a: Self) -> Self {
Self {
n: [
self.n[0].min(a.n[0]),
self.n[1].min(a.n[1]),
self.n[2].min(a.n[2]),
self.n[3].min(a.n[3]),
],
}
Self([
self.0[0].min(a.0[0]),
self.0[1].min(a.0[1]),
self.0[2].min(a.0[2]),
self.0[3].min(a.0[3]),
])
}
/// Vertical maximum.
#[inline(always)]
pub fn max(self, a: Self) -> Self {
Self {
n: [
self.n[0].max(a.n[0]),
self.n[1].max(a.n[1]),
self.n[2].max(a.n[2]),
self.n[3].max(a.n[3]),
],
}
Self([
self.0[0].max(a.0[0]),
self.0[1].max(a.0[1]),
self.0[2].max(a.0[2]),
self.0[3].max(a.0[3]),
])
}
// /// Horizontal minimum.
// #[inline(always)]
// pub fn hmin(self) -> f32 {
// let a = self.n[0].min(self.n[1]);
// let b = self.n[2].min(self.n[3]);
// let a = self.0[0].min(self.0[1]);
// let b = self.0[2].min(self.0[3]);
// a.min(b)
// }
// /// Horizontal maximum.
// #[inline(always)]
// pub fn hmax(self) -> f32 {
// let a = self.n[0].max(self.n[1]);
// let b = self.n[2].max(self.n[3]);
// let a = self.0[0].max(self.0[1]);
// let b = self.0[2].max(self.0[3]);
// a.max(b)
// }
@ -91,54 +83,46 @@ mod fallback {
#[inline(always)]
pub fn a(self) -> f32 {
self.n[0]
self.0[0]
}
#[inline(always)]
pub fn b(self) -> f32 {
self.n[1]
self.0[1]
}
#[inline(always)]
pub fn c(self) -> f32 {
self.n[2]
self.0[2]
}
#[inline(always)]
pub fn d(self) -> f32 {
self.n[3]
self.0[3]
}
#[inline(always)]
#[must_use]
pub fn set_a(self, n: f32) -> Self {
Self {
n: [n, self.n[1], self.n[2], self.n[3]],
}
Self([n, self.0[1], self.0[2], self.0[3]])
}
#[inline(always)]
#[must_use]
pub fn set_b(self, n: f32) -> Self {
Self {
n: [self.n[0], n, self.n[2], self.n[3]],
}
Self([self.0[0], n, self.0[2], self.0[3]])
}
#[inline(always)]
#[must_use]
pub fn set_c(self, n: f32) -> Self {
Self {
n: [self.n[0], self.n[1], n, self.n[3]],
}
Self([self.0[0], self.0[1], n, self.0[3]])
}
#[inline(always)]
#[must_use]
pub fn set_d(self, n: f32) -> Self {
Self {
n: [self.n[0], self.n[1], self.n[2], n],
}
Self([self.0[0], self.0[1], self.0[2], n])
}
//-----------------------------------------------------
@ -146,44 +130,44 @@ mod fallback {
#[inline(always)]
pub fn aaaa(self) -> Self {
let a = self.n[0];
Self { n: [a, a, a, a] }
let a = self.0[0];
Self([a, a, a, a])
}
#[inline(always)]
pub fn bbbb(self) -> Self {
let b = self.n[1];
Self { n: [b, b, b, b] }
let b = self.0[1];
Self([b, b, b, b])
}
#[inline(always)]
pub fn cccc(self) -> Self {
let c = self.n[2];
Self { n: [c, c, c, c] }
let c = self.0[2];
Self([c, c, c, c])
}
#[inline(always)]
pub fn dddd(self) -> Self {
let d = self.n[3];
Self { n: [d, d, d, d] }
let d = self.0[3];
Self([d, d, d, d])
}
#[inline(always)]
pub fn bcad(self) -> Self {
let a = self.n[0];
let b = self.n[1];
let c = self.n[2];
let d = self.n[3];
Self { n: [b, c, a, d] }
let a = self.0[0];
let b = self.0[1];
let c = self.0[2];
let d = self.0[3];
Self([b, c, a, d])
}
#[inline(always)]
pub fn cabd(self) -> Self {
let a = self.n[0];
let b = self.n[1];
let c = self.n[2];
let d = self.n[3];
Self { n: [c, a, b, d] }
let a = self.0[0];
let b = self.0[1];
let c = self.0[2];
let d = self.0[3];
Self([c, a, b, d])
}
}
@ -192,14 +176,12 @@ mod fallback {
#[inline(always)]
fn add(self, rhs: Self) -> Self {
Self {
n: [
self.n[0] + rhs.n[0],
self.n[1] + rhs.n[1],
self.n[2] + rhs.n[2],
self.n[3] + rhs.n[3],
],
}
Self([
self.0[0] + rhs.0[0],
self.0[1] + rhs.0[1],
self.0[2] + rhs.0[2],
self.0[3] + rhs.0[3],
])
}
}
@ -208,14 +190,12 @@ mod fallback {
#[inline(always)]
fn sub(self, rhs: Self) -> Self {
Self {
n: [
self.n[0] - rhs.n[0],
self.n[1] - rhs.n[1],
self.n[2] - rhs.n[2],
self.n[3] - rhs.n[3],
],
}
Self([
self.0[0] - rhs.0[0],
self.0[1] - rhs.0[1],
self.0[2] - rhs.0[2],
self.0[3] - rhs.0[3],
])
}
}
@ -224,14 +204,12 @@ mod fallback {
#[inline(always)]
fn mul(self, rhs: Self) -> Self {
Self {
n: [
self.n[0] * rhs.n[0],
self.n[1] * rhs.n[1],
self.n[2] * rhs.n[2],
self.n[3] * rhs.n[3],
],
}
Self([
self.0[0] * rhs.0[0],
self.0[1] * rhs.0[1],
self.0[2] * rhs.0[2],
self.0[3] * rhs.0[3],
])
}
}
@ -240,14 +218,12 @@ mod fallback {
#[inline(always)]
fn mul(self, rhs: f32) -> Self {
Self {
n: [
self.n[0] * rhs,
self.n[1] * rhs,
self.n[2] * rhs,
self.n[3] * rhs,
],
}
Self([
self.0[0] * rhs,
self.0[1] * rhs,
self.0[2] * rhs,
self.0[3] * rhs,
])
}
}
@ -256,14 +232,12 @@ mod fallback {
#[inline(always)]
fn div(self, rhs: Self) -> Self {
Self {
n: [
self.n[0] / rhs.n[0],
self.n[1] / rhs.n[1],
self.n[2] / rhs.n[2],
self.n[3] / rhs.n[3],
],
}
Self([
self.0[0] / rhs.0[0],
self.0[1] / rhs.0[1],
self.0[2] / rhs.0[2],
self.0[3] / rhs.0[3],
])
}
}
@ -272,14 +246,12 @@ mod fallback {
#[inline(always)]
fn div(self, rhs: f32) -> Self {
Self {
n: [
self.n[0] / rhs,
self.n[1] / rhs,
self.n[2] / rhs,
self.n[3] / rhs,
],
}
Self([
self.0[0] / rhs,
self.0[1] / rhs,
self.0[2] / rhs,
self.0[3] / rhs,
])
}
}
@ -288,9 +260,7 @@ mod fallback {
#[inline(always)]
fn neg(self) -> Self {
Self {
n: [-self.n[0], -self.n[1], -self.n[2], -self.n[3]],
}
Self([-self.0[0], -self.0[1], -self.0[2], -self.0[3]])
}
}