glam_det 2.0.0

A simple and fast 3D math library for games and graphics.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// Copyright (C) 2020-2025 glam-det authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Generated from vec.rs.tera template. Edit the template, not the generated file.

use crate::bool::simd_alias::BVec4A;
use crate::f32::simd_alias::Vec4;

#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::ops::*;

#[cfg(target_arch = "x86")]
use core::arch::x86::*;
#[cfg(target_arch = "x86_64")]
use core::arch::x86_64::*;

use auto_ops_det::{impl_op_ex, impl_op_ex_commutative};
use core::ops;

union UnionCast {
    a: [f32; 4],
    v: UnitVec4,
}

union VecUnionCast {
    v: Vec4,
    uv: UnitVec4,
}

impl Vec4 {
    #[inline]
    pub fn as_unit_vec4_unchecked(self) -> UnitVec4 {
        unsafe { VecUnionCast { v: self }.uv }
    }
}

impl UnitVec4 {
    #[inline]
    pub fn as_vec4(self) -> Vec4 {
        unsafe { VecUnionCast { uv: self }.v }
    }
}

/// A 4-dimensionalunit vector with SIMD support.
///
/// This type uses 16 byte aligned SIMD vector type for storage.
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct UnitVec4(pub(crate) __m128);

impl UnitVec4 {
    /// A unit-length vector pointing along the positive X axis.
    pub const X: Self = Self::new_unchecked(1.0_f32, 0.0_f32, 0.0_f32, 0.0_f32);

    /// A unit-length vector pointing along the positive Y axis.
    pub const Y: Self = Self::new_unchecked(0.0_f32, 1.0_f32, 0.0_f32, 0.0_f32);

    /// A unit-length vector pointing along the positive Z axis.
    pub const Z: Self = Self::new_unchecked(0.0_f32, 0.0_f32, 1.0_f32, 0.0_f32);

    /// A unit-length vector pointing along the positive W axis.
    pub const W: Self = Self::new_unchecked(0.0_f32, 0.0_f32, 0.0_f32, 1.0_f32);

    /// A unit-length vector pointing along the negative X axis.
    pub const NEG_X: Self = Self::new_unchecked(-1.0_f32, 0.0_f32, 0.0_f32, 0.0_f32);

    /// A unit-length vector pointing along the negative Y axis.
    pub const NEG_Y: Self = Self::new_unchecked(0.0_f32, -1.0_f32, 0.0_f32, 0.0_f32);

    /// A unit-length vector pointing along the negative Z axis.
    pub const NEG_Z: Self = Self::new_unchecked(0.0_f32, 0.0_f32, -1.0_f32, 0.0_f32);

    /// A unit-length vector pointing along the negative W axis.
    pub const NEG_W: Self = Self::new_unchecked(0.0_f32, 0.0_f32, 0.0_f32, -1.0_f32);

    /// The unit axes.
    pub const AXES: [Self; 4] = [Self::X, Self::Y, Self::Z, Self::W];

    /// Creates a new unit vector without checking if it is normalized.
    #[inline]
    pub const fn new_unchecked(x: f32, y: f32, z: f32, w: f32) -> Self {
        unsafe { UnionCast { a: [x, y, z, w] }.v }
    }

    /// Creates a new unit vector after normalizing the parameters.
    ///
    /// For valid results, vector represented by the parameters must _not_ be of length zero, nor very close to zero.
    ///
    /// See also [`Vec4::normalize`].
    ///
    /// # Panics
    ///
    /// Will panic if vector represented by the parameters is zero length when `glam_assert` is enabled.
    #[inline]
    pub fn new_normalized(x: f32, y: f32, z: f32, w: f32) -> Self {
        Vec4::new(x, y, z, w).normalize().as_unit_vec4_unchecked()
    }

    /// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
    /// for each element of `self`.
    ///
    /// A true element in the mask uses the corresponding element from `if_true`, and false
    /// uses the element from `if_false`.
    #[inline]
    pub fn select(mask: BVec4A, if_true: Self, if_false: Self) -> Vec4 {
        Vec4::select(mask, if_true.as_vec4(), if_false.as_vec4())
    }

    /// Creates a new vector from an array.
    #[inline]
    pub const fn from_array_unchecked(a: [f32; 4]) -> Self {
        Self::new_unchecked(a[0], a[1], a[2], a[3])
    }

    /// `[x, y, z, w]`
    #[inline]
    pub const fn to_array(&self) -> [f32; 4] {
        unsafe { *(self as *const UnitVec4 as *const [f32; 4]) }
    }

    /// Creates a vector from the first 4 values in `slice`.
    ///
    /// # Panics
    ///
    /// Panics if `slice` is less than 4 elements long.
    #[inline]
    pub const fn from_slice_unchecked(slice: &[f32]) -> Self {
        Self::new_unchecked(slice[0], slice[1], slice[2], slice[3])
    }

    /// Writes the elements of `self` to the first 4 elements in `slice`.
    ///
    /// # Panics
    ///
    /// Panics if `slice` is less than 4 elements long.
    #[inline]
    pub fn write_to_slice(self, slice: &mut [f32]) {
        slice[0] = self.x;
        slice[1] = self.y;
        slice[2] = self.z;
        slice[3] = self.w;
    }

    /// Computes the dot product of `self` and `rhs`.
    #[inline]
    pub(crate) fn dot(self, rhs: Self) -> f32 {
        self.as_vec4().dot(rhs.as_vec4())
    }

    /// Returns the horizontal minimum of `self`.
    ///
    /// In other words this computes `min(x, y, ..)`.
    #[inline]
    pub fn min_element(self) -> f32 {
        self.as_vec4().min_element()
    }

    /// Returns the horizontal maximum of `self`.
    ///
    /// In other words this computes `max(x, y, ..)`.
    #[inline]
    pub fn max_element(self) -> f32 {
        self.as_vec4().max_element()
    }

    /// Returns a vector mask containing the result of a `==` comparison for each element of
    /// `self` and `rhs`.
    ///
    /// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all
    /// elements.
    #[inline]
    pub fn cmpeq(self, rhs: Self) -> BVec4A {
        self.as_vec4().cmpeq(rhs.as_vec4())
    }

    /// Returns a vector mask containing the result of a `!=` comparison for each element of
    /// `self` and `rhs`.
    ///
    /// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all
    /// elements.
    #[inline]
    pub fn cmpne(self, rhs: Self) -> BVec4A {
        self.as_vec4().cmpne(rhs.as_vec4())
    }

    /// Returns a vector mask containing the result of a `>=` comparison for each element of
    /// `self` and `rhs`.
    ///
    /// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all
    /// elements.
    #[inline]
    pub fn cmpge(self, rhs: Self) -> BVec4A {
        self.as_vec4().cmpge(rhs.as_vec4())
    }

    /// Returns a vector mask containing the result of a `>` comparison for each element of
    /// `self` and `rhs`.
    ///
    /// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all
    /// elements.
    #[inline]
    pub fn cmpgt(self, rhs: Self) -> BVec4A {
        self.as_vec4().cmpgt(rhs.as_vec4())
    }

    /// Returns a vector mask containing the result of a `<=` comparison for each element of
    /// `self` and `rhs`.
    ///
    /// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all
    /// elements.
    #[inline]
    pub fn cmple(self, rhs: Self) -> BVec4A {
        self.as_vec4().cmple(rhs.as_vec4())
    }

    /// Returns a vector mask containing the result of a `<` comparison for each element of
    /// `self` and `rhs`.
    ///
    /// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all
    /// elements.
    #[inline]
    pub fn cmplt(self, rhs: Self) -> BVec4A {
        self.as_vec4().cmple(rhs.as_vec4())
    }

    /// Returns a vector containing the absolute value of each element of `self`.
    #[inline]
    pub fn abs(self) -> Self {
        self.as_vec4().abs().as_unit_vec4_unchecked()
    }

    /// Returns a vector with elements representing the sign of `self`.
    ///
    /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
    /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
    /// - `NAN` if the number is `NAN`
    ///
    /// # Warning
    ///
    /// Because of the fact that `-UnitVec4::ZERO` output `UnitVec4::ZERO`,
    /// so the sign of `-UnitVec4::ZERO` is `1.0`, which is different from the behavior of `std::f32`.
    /// This phenomenon exists if some of vector elements is zero.
    #[inline]
    pub fn signum(self) -> Vec4 {
        self.as_vec4().signum()
    }

    /// Computes the Euclidean distance between two points in space.
    #[inline]
    pub fn distance(self, rhs: Self) -> f32 {
        self.as_vec4().distance(rhs.as_vec4())
    }

    /// Compute the squared euclidean distance between two points in space.
    #[inline]
    pub fn distance_squared(self, rhs: Self) -> f32 {
        self.as_vec4().distance_squared(rhs.as_vec4())
    }

    /// Returns the vector projection of `self` onto `rhs`.
    #[must_use]
    #[inline]
    pub fn project_onto(self, rhs: Self) -> Vec4 {
        rhs.as_vec4() * self.dot(rhs)
    }

    /// Returns the vector rejection of `self` from `rhs`.
    #[must_use]
    #[inline]
    pub fn reject_from(self, rhs: Self) -> Vec4 {
        self.as_vec4() - self.project_onto(rhs)
    }

    /// Performs a linear interpolation between `self` and `rhs` based on the value `s`.
    ///
    /// When `s` is `0.0`, the result will be equal to `self`.  When `s` is `1.0`, the result
    /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
    /// extrapolated.
    #[doc(alias = "mix")]
    #[inline]
    pub fn lerp(self, rhs: Self, s: f32) -> Vec4 {
        self.as_vec4().lerp(rhs.as_vec4(), s)
    }

    /// Returns true if the absolute difference of all elements between `self` and `rhs` is
    /// less than or equal to `max_abs_diff`.
    ///
    /// This can be used to compare if two vectors contain similar elements. It works best when
    /// comparing with a known value. The `max_abs_diff` that should be used depends on
    /// the values being compared against.
    ///
    /// For more see
    /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
    #[inline]
    pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
        Vec4::abs_diff_eq(self.as_vec4(), rhs.as_vec4(), max_abs_diff)
    }

    /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding
    /// error, yielding a more accurate result than an unfused multiply-add.
    ///
    /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
    /// architecture has a dedicated fma CPU instruction. However, this is not always true,
    /// and will be heavily dependant on designing algorithms with specific target hardware in
    /// mind.
    #[inline]
    pub fn mul_add(self, a: Self, b: Self) -> Vec4 {
        self.as_vec4().mul_add(a.as_vec4(), b.as_vec4())
    }
}

impl Default for UnitVec4 {
    #[inline]
    fn default() -> Self {
        Self::X
    }
}

impl PartialEq for UnitVec4 {
    #[inline]
    fn eq(&self, rhs: &Self) -> bool {
        self.cmpeq(*rhs).all()
    }
}

impl Neg for UnitVec4 {
    type Output = Self;
    #[inline]
    fn neg(self) -> Self {
        self.as_vec4().neg().as_unit_vec4_unchecked()
    }
}

impl_op_ex!(/ |a: &UnitVec4, b: &UnitVec4| -> Vec4 {
    a.as_vec4() / b.as_vec4()
});

impl_op_ex!(/ |a: &UnitVec4, b: &f32| -> Vec4 {
    a.as_vec4() / b
});

impl_op_ex!(/ |a: &f32, b: &UnitVec4| -> Vec4 {
    a / b.as_vec4()
});

impl_op_ex_commutative!(/ |a: &UnitVec4, b: &Vec4| -> Vec4 {
    a.as_vec4() / b
});

impl_op_ex!(*|a: &UnitVec4, b: &UnitVec4| -> Vec4 { a.as_vec4() * b.as_vec4() });

impl_op_ex!(*|a: &UnitVec4, b: &f32| -> Vec4 { a.as_vec4() * b });

impl_op_ex!(*|a: &f32, b: &UnitVec4| -> Vec4 { a * b.as_vec4() });

impl_op_ex_commutative!(*|a: &UnitVec4, b: &Vec4| -> Vec4 { a.as_vec4() * b });

impl_op_ex!(+ |a: &UnitVec4, b: &UnitVec4| -> Vec4 {
    a.as_vec4() + b.as_vec4()
});

impl_op_ex!(+ |a: &UnitVec4, b: &f32| -> Vec4 {
    a.as_vec4() + b
});

impl_op_ex!(+ |a: &f32, b: &UnitVec4| -> Vec4 {
    a + b.as_vec4()
});

impl_op_ex_commutative!(+ |a: &UnitVec4, b: &Vec4| -> Vec4 {
    a.as_vec4() + b
});

impl_op_ex!(-|a: &UnitVec4, b: &UnitVec4| -> Vec4 { a.as_vec4() - b.as_vec4() });

impl_op_ex!(-|a: &UnitVec4, b: &f32| -> Vec4 { a.as_vec4() - b });

impl_op_ex!(-|a: &f32, b: &UnitVec4| -> Vec4 { a - b.as_vec4() });

impl_op_ex_commutative!(-|a: &UnitVec4, b: &Vec4| -> Vec4 { a.as_vec4() - b });

impl_op_ex!(% |a: &UnitVec4, b: &UnitVec4| -> Vec4 {
    a.as_vec4() % b.as_vec4()
});

impl_op_ex!(% |a: &UnitVec4, b: &f32| -> Vec4 {
    a.as_vec4() % b
});

impl_op_ex!(% |a: &f32, b: &UnitVec4| -> Vec4 {
    a % b.as_vec4()
});

impl_op_ex_commutative!(% |a: &UnitVec4, b: &Vec4| -> Vec4 {
    a.as_vec4() % b
});

impl Index<usize> for UnitVec4 {
    type Output = f32;
    #[inline]
    fn index(&self, index: usize) -> &Self::Output {
        match index {
            0 => &self.x,
            1 => &self.y,
            2 => &self.z,
            3 => &self.w,
            _ => panic!("index out of bounds"),
        }
    }
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for UnitVec4 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
    }
}

#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for UnitVec4 {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_tuple(stringify!(UnitVec4))
            .field(&self.x)
            .field(&self.y)
            .field(&self.z)
            .field(&self.w)
            .finish()
    }
}

impl From<UnitVec4> for __m128 {
    #[inline]
    fn from(t: UnitVec4) -> Self {
        t.0
    }
}

impl From<UnitVec4> for [f32; 4] {
    #[inline]
    fn from(v: UnitVec4) -> Self {
        v.as_vec4().into()
    }
}

impl From<UnitVec4> for (f32, f32, f32, f32) {
    #[inline]
    fn from(v: UnitVec4) -> Self {
        v.as_vec4().into()
    }
}

impl Deref for UnitVec4 {
    type Target = crate::deref::Vec4<f32>;
    #[inline]
    fn deref(&self) -> &Self::Target {
        unsafe { &*(self as *const Self).cast() }
    }
}

#[cfg(not(target_arch = "spirv"))]
impl AsRef<[f32; 4]> for UnitVec4 {
    #[inline]
    fn as_ref(&self) -> &[f32; 4] {
        unsafe { &*(self as *const UnitVec4 as *const [f32; 4]) }
    }
}