Debian Codegraph @

debian/missing-sources/glam-0.20.5/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 any(
250 not(any(
251 feature = "scalar-math",
252 target_arch = "spirv",
253 target_feature = "sse2",
254 target_feature = "simd128"
255 )),
256 feature = "cuda"
257 ),
258 repr(C, align(16))
259 )]
260 #[cfg_attr(
261 all(
262 any(
263 feature = "scalar-math",
264 target_arch = "spirv",
265 target_feature = "sse2",
266 target_feature = "simd128"
267 ),
268 not(feature = "cuda")
269 ),
270 repr(transparent)
271 )]
272 pub struct Vec4(pub(crate) XYZWF32);
273
274 #[cfg(any(
275 not(any(target_feature = "sse2", target_feature = "simd128")),
276 feature = "scalar-math"
277 ))]
278 impl_f32_vec4!(vec4, Vec2, Vec3, Vec4, BVec4, XYZWF32);
279
280 #[cfg(all(
281 any(target_feature = "sse2", target_feature = "simd128"),
282 not(feature = "scalar-math")
283 ))]
284 impl_f32_vec4!(vec4, Vec2, Vec3, Vec4, BVec4A, XYZWF32);
285
286 impl From<Vec4> for Vec3A {
287 /// Creates a `Vec3A` from the `x`, `y` and `z` elements of `self` discarding `w`.
288 ///
289 /// On architectures where SIMD is supported such as SSE2 on `x86_64` this conversion is a noop.
290 #[inline(always)]
291 fn from(v: Vec4) -> Self {
292 #[allow(clippy::useless_conversion)]
293 Self(v.0.into())
294 }
295 }
296
297 impl From<(Vec3A, f32)> for Vec4 {
298 #[inline(always)]
299 fn from((v, w): (Vec3A, f32)) -> Self {
300 v.extend(w)
301 }
302 }
303
304 impl From<(f32, Vec3A)> for Vec4 {
305 #[inline(always)]
306 fn from((x, v): (f32, Vec3A)) -> Self {
307 Self::new(x, v.x, v.y, v.z)
308 }
309 }
310
311 type XYZWF64 = XYZW<f64>;
312
313 /// A 4-dimensional vector.
314 #[derive(Clone, Copy)]
315 #[cfg_attr(not(feature = "cuda"), repr(transparent))]
316 #[cfg_attr(feature = "cuda", repr(C, align(16)))]
317 pub struct DVec4(pub(crate) XYZWF64);
318
319 impl DVec4 {
320 impl_vec4_float_methods!(f64, DVec2, DVec3, DVec4, BVec4, XYZWF64);
321 impl_as_vec4!();
322 impl_as_ivec4!();
323 impl_as_uvec4!();
324 }
325 impl_vec4_signed_traits!(f64, dvec4, DVec2, DVec3, DVec4, BVec4, XYZWF64);
326
327 type XYZWI32 = XYZW<i32>;
328
329 /// A 4-dimensional vector.
330 #[derive(Clone, Copy)]
331 #[cfg_attr(not(feature = "cuda"), repr(transparent))]
332 #[cfg_attr(feature = "cuda", repr(C, align(16)))]
333 pub struct IVec4(pub(crate) XYZWI32);
334
335 impl IVec4 {
336 impl_vec4_signed_methods!(i32, IVec2, IVec3, IVec4, BVec4, XYZWI32);
337 impl_as_vec4!();
338 impl_as_dvec4!();
339 impl_as_uvec4!();
340 }
341 impl_vec4_signed_traits!(i32, ivec4, IVec2, IVec3, IVec4, BVec4, XYZWI32);
342 impl_vecn_eq_hash_traits!(i32, 4, IVec4);
343
344 impl_vecn_scalar_shift_op_traits!(IVec4, i8, XYZWI32);
345 impl_vecn_scalar_shift_op_traits!(IVec4, i16, XYZWI32);
346 impl_vecn_scalar_shift_op_traits!(IVec4, i32, XYZWI32);
347 impl_vecn_scalar_shift_op_traits!(IVec4, u8, XYZWI32);
348 impl_vecn_scalar_shift_op_traits!(IVec4, u16, XYZWI32);
349 impl_vecn_scalar_shift_op_traits!(IVec4, u32, XYZWI32);
350
351 impl_vecn_shift_op_traits!(IVec4, IVec4, XYZWI32);
352 impl_vecn_shift_op_traits!(IVec4, UVec4, XYZWI32);
353
354 impl_vecn_scalar_bit_op_traits!(IVec4, i32, XYZWI32);
355
356 impl_vecn_bit_op_traits!(IVec4, XYZWI32);
357
358 type XYZWU32 = XYZW<u32>;
359
360 /// A 4-dimensional vector.
361 #[derive(Clone, Copy)]
362 #[cfg_attr(not(feature = "cuda"), repr(transparent))]
363 #[cfg_attr(feature = "cuda", repr(C, align(16)))]
364 pub struct UVec4(pub(crate) XYZWU32);
365
366 impl UVec4 {
367 impl_vec4_common_methods!(u32, UVec2, UVec3, UVec4, BVec4, XYZWU32);
368 impl_as_vec4!();
369 impl_as_dvec4!();
370 impl_as_ivec4!();
371 }
372 impl_vec4_common_traits!(u32, uvec4, UVec2, UVec3, UVec4, BVec4, XYZWU32);
373 impl_vecn_eq_hash_traits!(u32, 4, UVec4);
374
375 impl_vecn_scalar_shift_op_traits!(UVec4, i8, XYZWU32);
376 impl_vecn_scalar_shift_op_traits!(UVec4, i16, XYZWU32);
377 impl_vecn_scalar_shift_op_traits!(UVec4, i32, XYZWU32);
378 impl_vecn_scalar_shift_op_traits!(UVec4, u8, XYZWU32);
379 impl_vecn_scalar_shift_op_traits!(UVec4, u16, XYZWU32);
380 impl_vecn_scalar_shift_op_traits!(UVec4, u32, XYZWU32);
381
382 impl_vecn_shift_op_traits!(UVec4, IVec4, XYZWU32);
383 impl_vecn_shift_op_traits!(UVec4, UVec4, XYZWU32);
384
385 impl_vecn_scalar_bit_op_traits!(UVec4, u32, XYZWU32);
386
387 impl_vecn_bit_op_traits!(UVec4, XYZWU32);
388
389 mod const_test_vec4 {
390 #[cfg(all(
391 any(feature = "scalar-math", target_arch = "spirv"),
392 not(feature = "cuda")
393 ))]
394 const_assert_eq!(
395 core::mem::align_of::<f32>(),
396 core::mem::align_of::<super::Vec4>()
397 );
398 #[cfg(not(any(feature = "scalar-math", target_arch = "spirv")))]
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 #[cfg(not(feature = "cuda"))]
405 const_assert_eq!(
406 core::mem::align_of::<f64>(),
407 core::mem::align_of::<super::DVec4>()
408 );
409 #[cfg(feature = "cuda")]
410 const_assert_eq!(16, core::mem::align_of::<super::DVec4>());
411 const_assert_eq!(32, core::mem::size_of::<super::DVec4>());
412 }
413
414 mod const_test_ivec4 {
415 #[cfg(not(feature = "cuda"))]
416 const_assert_eq!(
417 core::mem::align_of::<i32>(),
418 core::mem::align_of::<super::IVec4>()
419 );
420 #[cfg(feature = "cuda")]
421 const_assert_eq!(16, core::mem::align_of::<super::IVec4>());
422 const_assert_eq!(16, core::mem::size_of::<super::IVec4>());
423 }
424
425 mod const_test_uvec4 {
426 #[cfg(not(feature = "cuda"))]
427 const_assert_eq!(
428 core::mem::align_of::<u32>(),
429 core::mem::align_of::<super::UVec4>()
430 );
431 #[cfg(feature = "cuda")]
432 const_assert_eq!(16, core::mem::align_of::<super::UVec4>());
433 const_assert_eq!(16, core::mem::size_of::<super::UVec4>());
434 }