comparison utils/src/math.rs @ 791:a29122662cde

utils: Simplify translate_2d and align Mat4 to 16 bytes This lowers the amount of instructions from 61 to 32 on PowerPC with AltiVec, and from 25 to 14 on amd64 with AVX2.
author Link Mauve <linkmauve@linkmauve.fr>
date Sat, 17 Jan 2026 14:19:58 +0100
parents d005f5927447
children
comparison
equal deleted inserted replaced
790:d005f5927447 791:a29122662cde
1 //! Various helpers to deal with vectors and matrices. 1 //! Various helpers to deal with vectors and matrices.
2 2
3 use const_for::const_for; 3 use const_for::const_for;
4 4
5 /// A 4×4 f32 matrix type. 5 /// A 4×4 f32 matrix type.
6 #[repr(align(16))]
6 pub struct Mat4 { 7 pub struct Mat4 {
7 inner: [[f32; 4]; 4] 8 inner: [[f32; 4]; 4]
8 } 9 }
9 10
10 impl Mat4 { 11 impl Mat4 {
107 }); 108 });
108 }); 109 });
109 } 110 }
110 111
111 /// Translate the matrix by a 2D offset. 112 /// Translate the matrix by a 2D offset.
112 pub const fn translate_2d(&mut self, x: f32, y: f32) { 113 pub const fn translate_2d(&mut self, mut x: f32, mut y: f32) {
113 let offset = [x, y, 0.]; 114 x *= self.inner[3][0];
114 self.translate(offset); 115 y *= self.inner[3][1];
116 const_for!(i in 0..4 => {
117 self.inner[0][i] += x;
118 self.inner[1][i] += y;
119 });
115 } 120 }
116 } 121 }
117 122
118 impl std::ops::Mul<Mat4> for Mat4 { 123 impl std::ops::Mul<Mat4> for Mat4 {
119 type Output = Mat4; 124 type Output = Mat4;