Debian Codegraph @

debian/missing-sources/glam-0.17.3/src/vec4.rs

raw symbols SCIP
1 use crate::core::traits::vector::*;
2
3 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
4 use crate::BVec4A;
5 use crate::{BVec4, DVec2, DVec3, IVec2, IVec3, UVec2, UVec3, Vec2, Vec3, Vec3A, XYZW};
6 #[cfg(not(target_arch = "spirv"))]
7 use core::fmt;
8 use core::ops::*;
9
10 #[cfg(not(feature = "std"))]
11 use num_traits::Float;
12
13 #[cfg(all(
14 target_arch = "x86",
15 target_feature = "sse2",
16 not(feature = "scalar-math")
17 ))]
18 use core::arch::x86::*;
19 #[cfg(all(
20 target_arch = "x86_64",
21 target_feature = "sse2",
22 not(feature = "scalar-math")
23 ))]
24 use core::arch::x86_64::*;
25
26 #[cfg(feature = "std")]
27 use std::iter::{Product, Sum};
28
29 use core::f32;
30
31 macro_rules! impl_vec4_common_methods {
32 ($t:ty, $vec2:ident, $vec3:ident, $vec4:ident, $mask:ident, $inner:ident) => {
33 /// All zeroes.
34 pub const ZERO: Self = Self(VectorConst::ZERO);
35
36 /// All ones.
37 pub const ONE: Self = Self(VectorConst::ONE);
38
39 /// `[1, 0, 0, 0]`: a unit-length vector pointing along the positive X axis.
40 pub const X: Self = Self(Vector4Const::X);
41
42 /// `[0, 1, 0, 0]`: a unit-length vector pointing along the positive Y axis.
43 pub const Y: Self = Self(Vector4Const::Y);
44
45 /// `[0, 0, 1, 0]`: a unit-length vector pointing along the positive Z axis.
46 pub const Z: Self = Self(Vector4Const::Z);
47
48 /// `[0, 0, 0, 1]`: a unit-length vector pointing along the positive W axis.
49 pub const W: Self = Self(Vector4Const::W);
50
51 /// The unit axes.
52 pub const AXES: [Self; 4] = [Self::X, Self::Y, Self::Z, Self::W];
53
54 /// Creates a new 4D vector.
55 #[inline(always)]
56 pub fn new(x: $t, y: $t, z: $t, w: $t) -> Self {
57 Self(Vector4::new(x, y, z, w))
58 }
59
60 /// Creates a `Vec3` from the `x`, `y` and `z` elements of `self`, discarding `w`.
61 ///
62 /// Truncation to `Vec3` may also be performed by using `self.xyz()` or `Vec3::from()`.
63 ///
64 /// To truncate to `Vec3A` use `Vec3A::from()`.
65 #[inline(always)]
66 pub fn truncate(self) -> $vec3 {
67 $vec3::new(self.x, self.y, self.z)
68 }
69
70 /// `[x, y, z, w]`
71 #[inline(always)]
72 pub fn to_array(&self) -> [$t; 4] {
73 [self.x, self.y, self.z, self.w]
74 }
75
76 impl_vecn_common_methods!($t, $vec4, $mask, $inner, Vector4);
77 };
78 }
79
80 macro_rules! impl_vec4_common_traits {
81 ($t:ty, $new:ident, $vec2:ident, $vec3:ident, $vec4:ident, $mask:ident, $inner:ident) => {
82 /// Creates a 4-dimensional vector.
83 #[inline(always)]
84 pub fn $new(x: $t, y: $t, z: $t, w: $t) -> $vec4 {
85 $vec4::new(x, y, z, w)
86 }
87
88 impl Index<usize> for $vec4 {
89 type Output = $t;
90 #[inline(always)]
91 fn index(&self, index: usize) -> &Self::Output {
92 match index {
93 0 => &self.x,
94 1 => &self.y,
95 2 => &self.z,
96 3 => &self.w,
97 _ => panic!("index out of bounds"),
98 }
99 }
100 }
101
102 impl IndexMut<usize> for $vec4 {
103 #[inline(always)]
104 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
105 match index {
106 0 => &mut self.x,
107 1 => &mut self.y,
108 2 => &mut self.z,
109 3 => &mut self.w,
110 _ => panic!("index out of bounds"),
111 }
112 }
113 }
114
115 #[cfg(not(target_arch = "spirv"))]
116 impl fmt::Debug for $vec4 {
117 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
118 fmt.debug_tuple(stringify!($vec4))
119 .field(&self.x)
120 .field(&self.y)
121 .field(&self.z)
122 .field(&self.w)
123 .finish()
124 }
125 }
126
127 #[cfg(not(target_arch = "spirv"))]
128 impl fmt::Display for $vec4 {
129 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
130 write!(fmt, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
131 }
132 }
133
134 impl From<($t, $t, $t, $t)> for $vec4 {
135 #[inline(always)]
136 fn from(t: ($t, $t, $t, $t)) -> Self {
137 Self(Vector4::from_tuple(t))
138 }
139 }
140
141 impl From<$vec4> for ($t, $t, $t, $t) {
142 #[inline(always)]
143 fn from(v: $vec4) -> Self {
144 Vector4::into_tuple(v.0)
145 }
146 }
147
148 impl From<($vec3, $t)> for $vec4 {
149 #[inline(always)]
150 fn from((v, w): ($vec3, $t)) -> Self {
151 Self::new(v.x, v.y, v.z, w)
152 }
153 }
154
155 impl From<($vec2, $t, $t)> for $vec4 {
156 #[inline(always)]
157 fn from((v, z, w): ($vec2, $t, $t)) -> Self {
158 Self::new(v.x, v.y, z, w)
159 }
160 }
161
162 impl From<($vec2, $vec2)> for $vec4 {
163 #[inline(always)]
164 fn from((v, u): ($vec2, $vec2)) -> Self {
165 Self::new(v.x, v.y, u.x, u.y)
166 }
167 }
168
169 impl From<$vec4> for $vec3 {
170 /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`.
171 #[inline(always)]
172 fn from(v: $vec4) -> Self {
173 Self(v.into_xyz())
174 }
175 }
176
177 impl From<$vec4> for $vec2 {
178 /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z` and
179 /// `w`.
180 #[inline(always)]
181 fn from(v: $vec4) -> Self {
182 Self(v.into_xy())
183 }
184 }
185
186 impl Deref for $vec4 {
187 type Target = XYZW<$t>;
188 #[inline(always)]
189 fn deref(&self) -> &Self::Target {
190 self.0.as_ref_xyzw()
191 }
192 }
193
194 impl DerefMut for $vec4 {
195 #[inline(always)]
196 fn deref_mut(&mut self) -> &mut Self::Target {
197 self.0.as_mut_xyzw()
198 }
199 }
200
201 impl_vecn_common_traits!($t, 4, $vec4, $inner, Vector4);
202 };
203 }
204
205 macro_rules! impl_vec4_signed_methods {
206 ($t:ty, $vec2:ident, $vec3:ident, $vec4:ident, $mask:ident, $inner:ident) => {
207 impl_vec4_common_methods!($t, $vec2, $vec3, $vec4, $mask, $inner);
208 impl_vecn_signed_methods!($t, $vec4, $mask, $inner, SignedVector4);
209 };
210 }
211
212 macro_rules! impl_vec4_signed_traits {
213 ($t:ty, $new:ident, $vec2:ident, $vec3:ident, $vec4:ident, $mask:ident, $inner:ident) => {
214 impl_vec4_common_traits!($t, $new, $vec2, $vec3, $vec4, $mask, $inner);
215 impl_vecn_signed_traits!($t, 4, $vec4, $inner, SignedVector4);
216 };
217 }
218
219 macro_rules! impl_vec4_float_methods {
220 ($t:ty, $vec2:ident, $vec3:ident, $vec4:ident, $mask:ident, $inner:ident) => {
221 impl_vec4_signed_methods!($t, $vec2, $vec3, $vec4, $mask, $inner);
222 impl_vecn_float_methods!($t, $vec4, $mask, $inner, FloatVector4);
223 };
224 }
225
226 // implement `Vec4` functionality
227 macro_rules! impl_f32_vec4 {
228 ($new:ident, $vec2:ident, $vec3:ident, $vec4:ident, $mask:ident, $inner:ident) => {
229 impl $vec4 {
230 impl_vec4_float_methods!(f32, $vec2, $vec3, $vec4, $mask, $inner);
231 impl_vecn_as_f64!(DVec4, x, y, z, w);
232 impl_vecn_as_i32!(IVec4, x, y, z, w);
233 impl_vecn_as_u32!(UVec4, x, y, z, w);
234 }
235 impl_vec4_signed_traits!(f32, $new, $vec2, $vec3, $vec4, $mask, $inner);
236 };
237 }
238
239 #[cfg(any(not(target_feature = "sse2"), feature = "scalar-math"))]
240 type XYZWF32 = XYZW<f32>;
241
242 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
243 type XYZWF32 = __m128;
244
245 /// A 4-dimensional vector.
246 ///
247 /// This type uses 16 byte aligned SIMD vector type for storage on supported platforms.
248 #[derive(Clone, Copy)]
249 #[cfg_attr(
250 not(any(feature = "scalar-math", target_arch = "spriv")),
251 repr(align(16))
252 )]
253 #[cfg_attr(any(feature = "scalar-math", target_arch = "spriv"), repr(transparent))]
254 pub struct Vec4(pub(crate) XYZWF32);
255
256 #[cfg(any(not(target_feature = "sse2"), feature = "scalar-math"))]
257 impl_f32_vec4!(vec4, Vec2, Vec3, Vec4, BVec4, XYZWF32);
258
259 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
260 impl_f32_vec4!(vec4, Vec2, Vec3, Vec4, BVec4A, __m128);
261
262 impl From<Vec4> for Vec3A {
263 /// Creates a `Vec3A` from the `x`, `y` and `z` elements of `self` discarding `w`.
264 ///
265 /// On architectures where SIMD is supported such as SSE2 on x86_64 this conversion is a noop.
266 #[inline(always)]
267 fn from(v: Vec4) -> Self {
268 #[allow(clippy::useless_conversion)]
269 Self(v.0.into())
270 }
271 }
272
273 type XYZWF64 = XYZW<f64>;
274
275 /// A 4-dimensional vector.
276 #[derive(Clone, Copy)]
277 #[repr(transparent)]
278 pub struct DVec4(pub(crate) XYZWF64);
279
280 impl DVec4 {
281 impl_vec4_float_methods!(f64, DVec2, DVec3, DVec4, BVec4, XYZWF64);
282 impl_vecn_as_f32!(Vec4, x, y, z, w);
283 impl_vecn_as_i32!(IVec4, x, y, z, w);
284 impl_vecn_as_u32!(UVec4, x, y, z, w);
285 }
286 impl_vec4_signed_traits!(f64, dvec4, DVec2, DVec3, DVec4, BVec4, XYZWF64);
287
288 type XYZWI32 = XYZW<i32>;
289
290 /// A 4-dimensional vector.
291 #[derive(Clone, Copy)]
292 #[repr(transparent)]
293 pub struct IVec4(pub(crate) XYZWI32);
294
295 impl IVec4 {
296 impl_vec4_signed_methods!(i32, IVec2, IVec3, IVec4, BVec4, XYZWI32);
297 impl_vecn_as_f32!(Vec4, x, y, z, w);
298 impl_vecn_as_f64!(DVec4, x, y, z, w);
299 impl_vecn_as_u32!(UVec4, x, y, z, w);
300 }
301 impl_vec4_signed_traits!(i32, ivec4, IVec2, IVec3, IVec4, BVec4, XYZWI32);
302 impl_vecn_eq_hash_traits!(i32, 4, IVec4);
303
304 type XYZWU32 = XYZW<u32>;
305
306 /// A 4-dimensional vector.
307 #[derive(Clone, Copy)]
308 #[repr(transparent)]
309 pub struct UVec4(pub(crate) XYZWU32);
310
311 impl UVec4 {
312 impl_vec4_common_methods!(u32, UVec2, UVec3, UVec4, BVec4, XYZWU32);
313 impl_vecn_as_f32!(Vec4, x, y, z, w);
314 impl_vecn_as_f64!(DVec4, x, y, z, w);
315 impl_vecn_as_i32!(IVec4, x, y, z, w);
316 }
317 impl_vec4_common_traits!(u32, uvec4, UVec2, UVec3, UVec4, BVec4, XYZWU32);
318 impl_vecn_eq_hash_traits!(u32, 4, UVec4);
319
320 #[test]
321 fn test_vec4_private() {
322 assert_eq!(
323 vec4(1.0, 1.0, 1.0, 1.0).mul_add(vec4(0.5, 2.0, -4.0, 0.0), vec4(-1.0, -1.0, -1.0, -1.0)),
324 vec4(-0.5, 1.0, -5.0, -1.0)
325 );
326 }
327
328 #[cfg(test)]
329 mod tests {
330 use super::{vec4, Vec3};
331
332 #[test]
333 fn from_vec3() {
334 assert_eq!(
335 vec4(1.0, 2.0, 3.0, 4.0),
336 (Vec3::new(1.0, 2.0, 3.0), 4.0).into()
337 );
338 }
339 }
340
341 #[cfg(any(feature = "scalar-math", target_arch = "spriv"))]
342 mod const_test_vec4 {
343 const_assert_eq!(
344 core::mem::align_of::<f32>(),
345 core::mem::align_of::<super::Vec4>()
346 );
347 const_assert_eq!(16, core::mem::size_of::<super::Vec4>());
348 }
349
350 #[cfg(not(any(feature = "scalar-math", target_arch = "spriv")))]
351 mod const_test_vec4 {
352 const_assert_eq!(16, core::mem::align_of::<super::Vec4>());
353 const_assert_eq!(16, core::mem::size_of::<super::Vec4>());
354 }
355
356 mod const_test_dvec4 {
357 const_assert_eq!(
358 core::mem::align_of::<f64>(),
359 core::mem::align_of::<super::DVec4>()
360 );
361 const_assert_eq!(32, core::mem::size_of::<super::DVec4>());
362 }
363
364 mod const_test_ivec4 {
365 const_assert_eq!(
366 core::mem::align_of::<i32>(),
367 core::mem::align_of::<super::IVec4>()
368 );
369 const_assert_eq!(16, core::mem::size_of::<super::IVec4>());
370 }
371
372 mod const_test_uvec4 {
373 const_assert_eq!(
374 core::mem::align_of::<u32>(),
375 core::mem::align_of::<super::UVec4>()
376 );
377 const_assert_eq!(16, core::mem::size_of::<super::UVec4>());
378 }