Debian Codegraph @

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

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