Skip to content

Commit 0f0b8ba

Browse files
committed
simd::f32quat::conjugate
1 parent bc3202d commit 0f0b8ba

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

cidre/src/simd.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,12 @@ impl f32x4 {
223223
Self::load(&[r, g, b, a])
224224
}
225225

226+
#[inline]
226227
pub fn load(vals: &[f32; 4]) -> Self {
227228
Self(unsafe { std::arch::aarch64::vld1q_f32(vals.as_ptr()) })
228229
}
229230

231+
#[inline]
230232
pub fn splat(val: f32) -> Self {
231233
Self(unsafe { std::arch::aarch64::vdupq_n_f32(val) })
232234
}
@@ -892,6 +894,21 @@ impl f32quat {
892894
half_angle.cos(),
893895
]))
894896
}
897+
898+
#[inline]
899+
#[doc(alias = "simd_conjugate")]
900+
pub fn conjugate(self) -> Self {
901+
#[cfg(target_arch = "aarch64")]
902+
{
903+
let sign = f32x4::with_xyzw(-1.0, -1.0, -1.0, 1.0);
904+
Self(self.0 * sign)
905+
}
906+
907+
#[cfg(not(target_arch = "aarch64"))]
908+
{
909+
Self(f32x4::with_xyzw(-self.x(), -self.y(), -self.z(), self.w()))
910+
}
911+
}
895912
}
896913

897914
pub mod packed {
@@ -955,6 +972,7 @@ pub mod packed {
955972

956973
#[cfg(test)]
957974
mod tests {
975+
use super::conjugate;
958976
use super::f32quat;
959977
use super::f32x2;
960978
use super::f32x2x2;
@@ -1094,6 +1112,27 @@ mod tests {
10941112
assert_f32quat_close(lhs, rhs);
10951113
}
10961114

1115+
#[test]
1116+
fn f32quat_conjugate_matches_component_sign_flip() {
1117+
let q = f32quat(f32x4::with_xyzw(1.0, -2.0, 3.5, -4.0));
1118+
let c = q.conjugate();
1119+
let c_fn = conjugate(q);
1120+
1121+
let expected = f32quat(f32x4::with_xyzw(-1.0, 2.0, -3.5, -4.0));
1122+
assert_eq!(c, expected);
1123+
assert_eq!(c_fn, expected);
1124+
}
1125+
1126+
#[test]
1127+
fn f32quat_conjugate_is_inverse_for_unit_quat() {
1128+
let q = f32quat::with_angle(0.7, f32x3::with_xyz(0.0, 0.0, 1.0));
1129+
let qc = q.conjugate();
1130+
let identity = f32quat(f32x4::with_xyzw(0.0, 0.0, 0.0, 1.0));
1131+
1132+
assert_f32quat_close(q * qc, identity);
1133+
assert_f32quat_close(qc * q, identity);
1134+
}
1135+
10971136
#[test]
10981137
fn f32quat_from_matrix_identity() {
10991138
let q = f32quat::from_matrix(f32x4x4::identity());
@@ -1109,7 +1148,8 @@ mod tests {
11091148
f32x4::with_xyzw(0.0, 0.0, 1.0, 0.0),
11101149
);
11111150
let q = f32quat::from_matrix(m);
1112-
let expected = f32quat::with_angle(std::f32::consts::FRAC_PI_2, f32x3::with_xyz(0.0, 0.0, 1.0));
1151+
let expected =
1152+
f32quat::with_angle(std::f32::consts::FRAC_PI_2, f32x3::with_xyz(0.0, 0.0, 1.0));
11131153
assert_f32quat_equiv(q, expected);
11141154
}
11151155

0 commit comments

Comments
 (0)