ico_math/
vector3.rs

1// Copyright 2019 Icosahedra, LLC
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6//
7
8use crate::float_vector::FloatVector;
9use crate::raw::RawVector_f32;
10use crate::sse_extensions::*;
11use crate::structure::SIMDVector3;
12use crate::vector2::Vector2;
13use crate::vector3_bool::Vector3Bool;
14use crate::vector3_int::Vector3Int;
15use crate::vector4::Vector4;
16use core::arch::x86_64::*;
17
18/// A vector of 3 floats (x,y,z).
19#[derive(Copy, Clone, Debug)]
20#[repr(C, align(16))]
21pub struct Vector3 {
22    pub data: __m128,
23}
24
25impl Vector3 {
26    /// Construct a new vector from f32 components.
27    ///
28    /// # Examples
29    ///
30    /// ```
31    /// use ico_math::Vector3;
32    /// let one_two_three = Vector3::new(1.0, 2.0, 3.0);
33    /// ```
34    #[inline(always)]
35    pub fn new(x: f32, y: f32, z: f32) -> Vector3 {
36        unsafe {
37            Vector3 {
38                data: _mm_set_ps(0.0f32, z, y, x),
39            }
40        }
41    }
42
43    /// Set all values of the vector to the same f32 value.
44    ///
45    /// # Examples
46    ///
47    /// ```
48    /// use ico_math::Vector3;
49    /// let ones = Vector3::set(1.0);
50    /// ```
51    #[inline(always)]
52    pub fn set<T: Into<FloatVector>>(value: T) -> Vector3 {
53        return Vector3 {
54            data: value.into().data,
55        };
56    }
57
58    /// Construct a new vector of zeros.
59    ///
60    /// # Examples
61    ///
62    /// ```
63    /// use ico_math::Vector3;
64    /// let zeros = Vector3::zero();
65    /// ```
66    #[inline(always)]
67    pub fn zero() -> Vector3 {
68        unsafe {
69            Vector3 {
70                data: _mm_setzero_ps(),
71            }
72        }
73    }
74
75    /// Get the x value of the vector, broadcast to all components as a FloatVector (xxxx).
76    /// # Examples
77    ///
78    /// ```
79    /// use ico_math::Vector3;
80    /// let one_two_three = Vector3::new(1.0, 2.0, 3.0);
81    /// let one = one_two_three.x();
82    /// ```
83    #[inline(always)]
84    pub fn x(self) -> FloatVector {
85        return FloatVector {
86            data: self.xxxx().data,
87        };
88    }
89
90    /// Get the y value of the vector, broadcast to all components as a FloatVector (yyyy).
91    /// # Examples
92    ///
93    /// ```
94    /// use ico_math::Vector3;
95    /// let one_two_three = Vector3::new(1.0, 2.0, 3.0);
96    /// let two = one_two_three.y();
97    /// ```
98    #[inline(always)]
99    pub fn y(self) -> FloatVector {
100        return FloatVector {
101            data: self.yyyy().data,
102        };
103    }
104
105    /// Get the z value of the vector, broadcast to all components as a FloatVector (zzzz).
106    /// # Examples
107    ///
108    /// ```
109    /// use ico_math::Vector3;
110    /// let one_two_three = Vector3::new(1.0, 2.0, 3.0);
111    /// let three = one_two_three.z();
112    /// ```
113    #[inline(always)]
114    pub fn z(self) -> FloatVector {
115        return FloatVector {
116            data: self.zzzz().data,
117        };
118    }
119
120    /// Load a value from aligned memory.
121    /// # Examples
122    ///
123    /// ```
124    /// use ico_math::Vector3;
125    /// use ico_math::raw::RawVector_f32;
126    /// let raw = RawVector_f32{data:[0.0,1.0,2.0,0.0]};
127    /// let one_two_three = Vector3::load(&raw);
128    /// ```
129    #[inline(always)]
130    pub fn load(raw: &RawVector_f32) -> Vector3 {
131        unsafe {
132            // Use the sledgehammer cast here.  It's fine because RawVector is aligned and c-like.
133            return Vector3 {
134                data: _mm_load_ps(core::mem::transmute(raw)),
135            };
136        }
137    }
138
139    /// Store a value to aligned memory.
140    /// # Examples
141    ///
142    /// ```
143    /// use ico_math::Vector3;
144    /// use ico_math::raw::RawVector_f32;
145    /// let mut raw = RawVector_f32{data:[0.0; 4]};
146    /// let one_two_three = Vector3::new(1.0, 2.0, 3.0);
147    /// one_two_three.store(&mut raw);
148    /// ```
149    #[inline(always)]
150    pub fn store(self, dst: &mut RawVector_f32) {
151        unsafe {
152            // Use the sledgehammer cast here.  It's fine because RawVector is aligned and c-like.
153            _mm_store_ps(core::mem::transmute(dst), self.data);
154        }
155    }
156
157    /// Set the x value of this vector, leaving the other components unchanged.
158    /// # Examples
159    ///
160    /// ```
161    /// use ico_math::Vector3;
162    /// let mut one_two_three = Vector3::new(1.0, 2.0, 3.0);
163    /// one_two_three.set_x(0.5);
164    /// ```
165    #[inline(always)]
166    pub fn set_x<T: Into<FloatVector>>(&mut self, value: T) {
167        unsafe {
168            self.data = _mm_move_ss(self.data, value.into().data);
169        }
170    }
171
172    /// Set the y value of this vector, leaving the other components unchanged.
173    /// # Examples
174    ///
175    /// ```
176    /// use ico_math::Vector3;
177    /// let mut one_two_three = Vector3::new(1.0, 2.0, 3.0);
178    /// one_two_three.set_y(0.5);
179    /// ```
180    #[inline(always)]
181    pub fn set_y<T: Into<FloatVector>>(&mut self, value: T) {
182        unsafe {
183            let v1 = _mm_move_ss(value.into().data, self.data);
184            self.data = _mm_shuffle_ps(v1, self.data, _ico_shuffle(3, 2, 1, 0));
185        }
186    }
187
188    /// Set the z value of this vector, leaving the other components unchanged.
189    /// This doesn't modify W.
190    /// # Examples
191    ///
192    /// ```
193    /// use ico_math::Vector3;
194    /// let mut one_two_three = Vector3::new(1.0, 2.0, 3.0);
195    /// one_two_three.set_z(0.5);
196    /// ```
197    #[inline(always)]
198    pub fn set_z<T: Into<FloatVector>>(&mut self, value: T) {
199        unsafe {
200            let v1 = _mm_move_ss(self.data, value.into().data);
201            self.data = _mm_shuffle_ps(self.data, v1, _ico_shuffle(3, 0, 1, 0));
202        }
203    }
204
205    /// Compute the 3 element dot-product, and return it as a broadcast FloatVector.
206    #[inline(always)]
207    pub fn dot(self, v2: Vector3) -> FloatVector {
208        unsafe {
209            let tmp0 = _mm_mul_ps(self.data, v2.data); //xyzw
210            let tmp1 = _mm_castsi128_ps(_mm_slli_si128(_mm_castps_si128(tmp0), 4)); //0xyz
211            let tmp2 = _mm_add_ps(tmp0, tmp1); //x xy, yz, wz
212            let tmp3 = _mm_moveldup_ps(tmp2); // x x yz yz
213            FloatVector {
214                data: _mm_add_ps(tmp3, _wzyx(tmp3)),
215            }
216        }
217    }
218
219    /// Right handed cross product.
220    #[inline(always)]
221    pub fn cross(self, rhs: Vector3) -> Vector3 {
222        unsafe {
223            return Vector3 {
224                data: _ico_cross_ps(self.data, rhs.data),
225            };
226        }
227    }
228
229    /// Compute the sum of two vectors and return it as a new vector.
230    #[inline(always)]
231    pub fn add(self, v2: Vector3) -> Vector3 {
232        unsafe {
233            Vector3 {
234                data: _mm_add_ps(self.data, v2.data),
235            }
236        }
237    }
238
239    /// Subtract a vector and return it as a new vector.
240    #[inline(always)]
241    pub fn sub(self, v2: Vector3) -> Vector3 {
242        unsafe {
243            Vector3 {
244                data: _mm_sub_ps(self.data, v2.data),
245            }
246        }
247    }
248
249    /// Multiply two vectors component-wise, and return it as a new vector.  
250    /// (x1 * x2, y1 * y2, z1 * z2, w1 * w2)
251    #[inline(always)]
252    pub fn mul(self, v2: Vector3) -> Vector3 {
253        unsafe {
254            Vector3 {
255                data: _mm_mul_ps(self.data, v2.data),
256            }
257        }
258    }
259
260    /// Divide two vectors component-wise, and return it as a new vector.  
261    /// (x1 / x2, y1 / y2, z1 / z2, w1 / w2)
262    #[inline(always)]
263    pub fn div(self, v2: Vector3) -> Vector3 {
264        unsafe {
265            Vector3 {
266                data: _mm_div_ps(self.data, v2.data),
267            }
268        }
269    }
270
271    /// Fused Multiply Add.  Result = (a * b) + c.
272    #[inline(always)]
273    pub fn mul_add(self, v2: Vector3, v3: Vector3) -> Vector3 {
274        unsafe {
275            return Vector3 {
276                data: _mm_fmadd_ps(self.data, v2.data, v3.data),
277            };
278        }
279    }
280
281    /// Fused Multiply Sub.  Result = (a * b) - c.
282    #[inline(always)]
283    pub fn mul_sub(self, v2: Vector3, v3: Vector3) -> Vector3 {
284        unsafe {
285            return Vector3 {
286                data: _mm_fmsub_ps(self.data, v2.data, v3.data),
287            };
288        }
289    }
290
291    /// Negated Fused Multiply Add.  Result = -(a * b) + c.
292    #[inline(always)]
293    pub fn neg_mul_add(self, v2: Vector3, v3: Vector3) -> Vector3 {
294        unsafe {
295            return Vector3 {
296                data: _mm_fnmadd_ps(self.data, v2.data, v3.data),
297            };
298        }
299    }
300
301    /// Negated Fused Multiply Sub.  Result = -(a * b) - c.
302    #[inline(always)]
303    pub fn neg_mul_sub(self, v2: Vector3, v3: Vector3) -> Vector3 {
304        unsafe {
305            return Vector3 {
306                data: _mm_fnmsub_ps(self.data, v2.data, v3.data),
307            };
308        }
309    }
310
311    /// Compute the bitwise AND of two vectors.
312    /// This function treats inputs as binary data, and doesn't perform any conversions.
313    #[inline(always)]
314    pub fn and<T: SIMDVector3>(self, v2: T) -> Vector3 {
315        unsafe {
316            Vector3 {
317                data: _mm_and_ps(self.data, v2.data()),
318            }
319        }
320    }
321
322    /// Compute the bitwise OR of two vectors.
323    /// This function treats inputs as binary data, and doesn't perform any conversions.
324    #[inline(always)]
325    pub fn or<T: SIMDVector3>(self, v2: T) -> Vector3 {
326        unsafe {
327            Vector3 {
328                data: _mm_or_ps(self.data, v2.data()),
329            }
330        }
331    }
332
333    /// Compute the bitwise ANDNOT of two vectors.
334    /// This function treats inputs as binary data, and doesn't perform any conversions.
335    #[inline(always)]
336    pub fn andnot<T: SIMDVector3>(self, v2: T) -> Vector3 {
337        unsafe {
338            Vector3 {
339                data: _mm_andnot_ps(self.data, v2.data()),
340            }
341        }
342    }
343
344    /// Compute the bitwise XOR of two vectors.
345    /// This function treats inputs as binary data, and doesn't perform any conversions.
346    #[inline(always)]
347    pub fn xor<T: SIMDVector3>(self, v2: T) -> Vector3 {
348        unsafe {
349            Vector3 {
350                data: _mm_xor_ps(self.data, v2.data()),
351            }
352        }
353    }
354
355    /// Equals, computed component-wise.  This compares bits, and is exact.
356    #[inline(always)]
357    pub fn equal(self, v2: Vector3) -> Vector3Bool {
358        unsafe {
359            Vector3Bool {
360                data: _mm_castps_si128(_mm_cmpeq_ps(self.data, v2.data)),
361            }
362        }
363    }
364    /// NotEquals, computed component-wise.  This compares bits, and is exact.
365    #[inline(always)]
366    pub fn not_equal(self, v2: Vector3) -> Vector3Bool {
367        unsafe {
368            Vector3Bool {
369                data: _mm_castps_si128(_mm_cmpneq_ps(self.data, v2.data)),
370            }
371        }
372    }
373    /// Greater than or equal to, computed component-wise.  This compares bits, and is exact.
374    #[inline(always)]
375    pub fn greater_equal(self, v2: Vector3) -> Vector3Bool {
376        unsafe {
377            Vector3Bool {
378                data: _mm_castps_si128(_mm_cmpge_ps(self.data, v2.data)),
379            }
380        }
381    }
382    /// Greater than, computed component-wise.  This compares bits, and is exact.
383    #[inline(always)]
384    pub fn greater(self, v2: Vector3) -> Vector3Bool {
385        unsafe {
386            Vector3Bool {
387                data: _mm_castps_si128(_mm_cmpgt_ps(self.data, v2.data)),
388            }
389        }
390    }
391    /// Less than or equal to, computed component-wise.  This compares bits, and is exact.
392    #[inline(always)]
393    pub fn less_equal(self, v2: Vector3) -> Vector3Bool {
394        unsafe {
395            Vector3Bool {
396                data: _mm_castps_si128(_mm_cmple_ps(self.data, v2.data)),
397            }
398        }
399    }
400    /// Less than, computed component-wise.  This compares bits, and is exact.
401    #[inline(always)]
402    pub fn less(self, v2: Vector3) -> Vector3Bool {
403        unsafe {
404            Vector3Bool {
405                data: _mm_castps_si128(_mm_cmplt_ps(self.data, v2.data)),
406            }
407        }
408    }
409
410    /// Relative and absolute epsilon comparison.  
411    /// Uses machine epsilon as absolute, and 4*machine epsilon for relative.
412    /// return abs(a - b) <= max(machine_epsilon, (max( abs(a), abs(b) ) * relative_epsilon);
413    /// Adapted from Knuth.  
414    /// https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
415    #[inline(always)]
416    pub fn approx_equal(self, v2: Vector3) -> Vector3Bool {
417        let delta = (self - v2).abs();
418        let abs_a = self.abs();
419        let abs_b = v2.abs();
420        let epsilon_bound = (abs_a.max(abs_b) * RELATIVE_COMPARISON_EPSILON)
421            .max(Vector3::from(ABSOLUTE_COMPARISON_EPSILON));
422        return delta.less_equal(epsilon_bound);
423    }
424
425    /// Adapted from Knuth with an added absolute epsilon
426    /// return (a - b) > max(machine_epsilon, (max( abs(a), abs(b) ) * relative_epsilon);
427    #[inline(always)]
428    pub fn definitely_greater(self, v2: Vector3) -> Vector3Bool {
429        let delta = self.sub(v2);
430        let abs_a = self.abs();
431        let abs_b = v2.abs();
432        let epsilon_bound = (abs_a.max(abs_b) * RELATIVE_COMPARISON_EPSILON)
433            .max(Vector3::from(ABSOLUTE_COMPARISON_EPSILON));
434        return delta.greater(epsilon_bound);
435    }
436    /// Adapted from Knuth with an added absolute epsilon
437    /// return (a - b) > max(machine_epsilon, (max( abs(a), abs(b) ) * relative_epsilon);
438    #[inline(always)]
439    pub fn definitely_less(self, v2: Vector3) -> Vector3Bool {
440        let delta = v2.sub(self);
441        let abs_a = self.abs();
442        let abs_b = v2.abs();
443        let epsilon_bound = (abs_a.max(abs_b) * RELATIVE_COMPARISON_EPSILON)
444            .max(Vector3::from(ABSOLUTE_COMPARISON_EPSILON));
445        return delta.greater(epsilon_bound);
446    }
447
448    /// The absolute value of each component of the vector.
449    #[inline(always)]
450    pub fn abs(self) -> Vector3 {
451        unsafe {
452            Vector3 {
453                data: _ico_abs_ps(self.data),
454            }
455        }
456    }
457
458    /// Take the magnitude of the first argument (self), and use the sign of the second argument to produce a new vector
459    #[inline(always)]
460    pub fn copysign(self, v2: Vector3) -> Vector3 {
461        unsafe {
462            Vector3 {
463                data: _ico_copysign_ps(self.data, v2.data),
464            }
465        }
466    }
467
468    /// Floor function.  Returns signed 0 when applicable.
469    #[inline(always)]
470    pub fn floor(self) -> Vector3 {
471        unsafe {
472            Vector3 {
473                data: _mm_floor_ps(self.data),
474            }
475        }
476    }
477
478    /// Ceil function.  Returns signed 0 when applicable.
479    #[inline(always)]
480    pub fn ceil(self) -> Vector3 {
481        unsafe {
482            Vector3 {
483                data: _mm_ceil_ps(self.data),
484            }
485        }
486    }
487
488    /// Round to nearest even function. Returns signed 0 when applicable.
489    #[inline(always)]
490    pub fn round(self) -> Vector3 {
491        unsafe {
492            Vector3 {
493                data: _mm_round_ps(self.data, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC),
494            }
495        }
496    }
497
498    /// Truncate function.
499    #[inline(always)]
500    pub fn truncate(self) -> Vector3 {
501        unsafe {
502            Vector3 {
503                data: _mm_round_ps(self.data, _MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC),
504            }
505        }
506    }
507
508    /// Convert to an integer vector using the floor function.
509    #[inline(always)]
510    pub fn floor_to_int(self) -> Vector3Int {
511        unsafe {
512            Vector3Int {
513                data: _mm_cvttps_epi32(self.floor().data),
514            }
515        }
516    }
517
518    /// Convert to an integer vector using the ceil function.
519    #[inline(always)]
520    pub fn ceil_to_int(self) -> Vector3Int {
521        unsafe {
522            Vector3Int {
523                data: _mm_cvttps_epi32(self.ceil().data),
524            }
525        }
526    }
527    /// Convert to an integer vector using the round function.
528    #[inline(always)]
529    pub fn round_to_int(self) -> Vector3Int {
530        unsafe {
531            Vector3Int {
532                data: _mm_cvttps_epi32(self.round().data),
533            }
534        }
535    }
536
537    /// Convert to an integer vector using the truncate function.
538    #[inline(always)]
539    pub fn truncate_to_int(self) -> Vector3Int {
540        unsafe {
541            Vector3Int {
542                data: _mm_cvttps_epi32(self.truncate().data),
543            }
544        }
545    }
546
547    /// Compute the fractional component of each component
548    /// Result = X - Floor(x)
549    #[inline(always)]
550    pub fn frac(self) -> Vector3 {
551        return Vector3::sub(self, Vector3::floor(self));
552    }
553
554    /// Compute the square root of each component
555    #[inline(always)]
556    pub fn sqrt(self) -> Vector3 {
557        unsafe {
558            Vector3 {
559                data: _mm_sqrt_ps(self.data),
560            }
561        }
562    }
563
564    /// Compute the approximate sin of each component
565    #[inline(always)]
566    pub fn sin(self) -> Vector3 {
567        unsafe {
568            Vector3 {
569                data: _ico_sin_ps(self.data),
570            }
571        }
572    }
573
574    /// Compute the approximate cos of each component
575    #[inline(always)]
576    pub fn cos(self) -> Vector3 {
577        unsafe {
578            Vector3 {
579                data: _ico_cos_ps(self.data),
580            }
581        }
582    }
583
584    /// Compute the approximate tan of each component
585    #[inline(always)]
586    pub fn tan(self) -> Vector3 {
587        unsafe {
588            Vector3 {
589                data: _ico_tan_ps(self.data),
590            }
591        }
592    }
593
594    /// Compute the approximate acos of each component
595    #[inline(always)]
596    pub fn acos(self) -> Vector3 {
597        unsafe {
598            Vector3 {
599                data: _ico_acos_ps(self.data),
600            }
601        }
602    }
603
604    /// Compute the approximate asin of each component
605    #[inline(always)]
606    pub fn asin(self) -> Vector3 {
607        unsafe {
608            Vector3 {
609                data: _ico_asin_ps(self.data),
610            }
611        }
612    }
613    #[inline(always)]
614    pub fn atan2(self, x: Vector3) -> Vector3 {
615        unsafe {
616            Vector3 {
617                data: _ico_atan2_ps(self.data, x.data),
618            }
619        }
620    }
621    /// Compute the component-wise max.
622    #[inline(always)]
623    pub fn max(self, v2: Vector3) -> Vector3 {
624        unsafe {
625            Vector3 {
626                data: _mm_max_ps(self.data, v2.data),
627            }
628        }
629    }
630
631    /// Compute the component-wise min.
632    #[inline(always)]
633    pub fn min(self, v2: Vector3) -> Vector3 {
634        unsafe {
635            Vector3 {
636                data: _mm_min_ps(self.data, v2.data),
637            }
638        }
639    }
640
641    #[inline(always)]
642    pub fn horizontal_min(self) -> FloatVector {
643        let x = self.x();
644        let xy = x.min(self.y());
645        return xy.min(self.z());
646    }
647
648    #[inline(always)]
649    pub fn horizontal_max(self) -> FloatVector {
650        let x = self.x();
651        let xy = x.max(self.y());
652        return xy.max(self.z());
653    }
654
655    /// Choose component wise between A and B based on the mask.  False = A, True = B.
656    #[inline(always)]
657    pub fn select(self, v2: Vector3, mask: Vector3Bool) -> Vector3 {
658        unsafe {
659            Vector3 {
660                data: _ico_select_ps(self.data, v2.data, _mm_castsi128_ps(mask.data)),
661            }
662        }
663    }
664    /// Linear interpolate from a to b based on a float.
665    #[inline(always)]
666    pub fn lerp<T: Into<FloatVector>>(self, v2: Vector3, t: T) -> Vector3 {
667        unsafe {
668            let t_val = t.into().data;
669            let tmp = _mm_fnmadd_ps(self.data, t_val, self.data); //a - (a*t)
670            Vector3 {
671                data: _mm_fmadd_ps(v2.data, t_val, tmp),
672            } //b * t + a
673        }
674    }
675
676    /// Normalize the vector.  Returns a zero vector if the result would be infinity or NAN.
677    #[inline(always)]
678    pub fn normalize(self) -> Vector3 {
679        let length = FloatVector::sqrt(Vector3::dot(self, self));
680        let norm = Vector3::div(self, Vector3::from(length));
681
682        unsafe {
683            // This catches infinity, NAN.  Zero vectors are possible - but that is fine - we failed
684            let result_length_sqr = Vector3::from(Vector3::dot(norm, norm));
685            let mask_less = Vector3::less(
686                result_length_sqr,
687                Vector3 {
688                    data: _ico_two_ps(),
689                },
690            );
691            return Vector3::and(norm, mask_less);
692        }
693    }
694
695    /// Normalize the vector.  Returns a zero vector if the result would be infinity or NAN.
696    /// Also returns the length of the vector prior to normalization.
697    #[inline(always)]
698    pub fn normalize_length(self) -> (Vector3, FloatVector) {
699        let length = FloatVector::sqrt(Vector3::dot(self, self));
700        let norm = Vector3::div(self, Vector3::from(length));
701
702        unsafe {
703            // This catches infinity, NAN.  Zero vectors are possible - but that is fine - we failed
704            let result_length_sqr = Vector3::from(Vector3::dot(norm, norm));
705            let mask_less = Vector3::less(
706                result_length_sqr,
707                Vector3 {
708                    data: _ico_two_ps(),
709                },
710            );
711            return (Vector3::and(norm, mask_less), length);
712        }
713    }
714
715    /// The square magnitude of the vector.  Equal to Dot(self, self).
716    #[inline(always)]
717    pub fn sqr_magnitude(self) -> FloatVector {
718        return Vector3::dot(self, self);
719    }
720
721    /// The magnitude of the vector.  Equal to Sqrt(Dot(self, self)).
722    #[inline(always)]
723    pub fn magnitude(self) -> FloatVector {
724        return FloatVector::sqrt(Vector3::dot(self, self));
725    }
726
727    #[inline(always)]
728    pub fn xxxx(self) -> Vector4 {
729        unsafe {
730            return Vector4 {
731                data: _xxxx(self.data),
732            };
733        }
734    }
735    #[inline(always)]
736    pub fn yyyy(self) -> Vector4 {
737        unsafe {
738            return Vector4 {
739                data: _yyyy(self.data),
740            };
741        }
742    }
743    #[inline(always)]
744    pub fn zzzz(self) -> Vector4 {
745        unsafe {
746            return Vector4 {
747                data: _zzzz(self.data),
748            };
749        }
750    }
751
752    #[inline(always)]
753    pub fn xxx(self) -> Vector3 {
754        unsafe {
755            return Vector3 {
756                data: _xxxw(self.data),
757            };
758        }
759    }
760    #[inline(always)]
761    pub fn xxy(self) -> Vector3 {
762        unsafe {
763            return Vector3 {
764                data: _xxyw(self.data),
765            };
766        }
767    }
768    #[inline(always)]
769    pub fn xxz(self) -> Vector3 {
770        unsafe {
771            return Vector3 {
772                data: _xxzw(self.data),
773            };
774        }
775    }
776    #[inline(always)]
777    pub fn xyx(self) -> Vector3 {
778        unsafe {
779            return Vector3 {
780                data: _xyxw(self.data),
781            };
782        }
783    }
784    #[inline(always)]
785    pub fn xyy(self) -> Vector3 {
786        unsafe {
787            return Vector3 {
788                data: _xyyw(self.data),
789            };
790        }
791    }
792    #[inline(always)]
793    pub fn xyz(self) -> Vector3 {
794        unsafe {
795            return Vector3 {
796                data: _xyzw(self.data),
797            };
798        }
799    }
800    #[inline(always)]
801    pub fn xzx(self) -> Vector3 {
802        unsafe {
803            return Vector3 {
804                data: _xzxw(self.data),
805            };
806        }
807    }
808    #[inline(always)]
809    pub fn xzy(self) -> Vector3 {
810        unsafe {
811            return Vector3 {
812                data: _xzyw(self.data),
813            };
814        }
815    }
816    #[inline(always)]
817    pub fn xzz(self) -> Vector3 {
818        unsafe {
819            return Vector3 {
820                data: _xzzw(self.data),
821            };
822        }
823    }
824
825    #[inline(always)]
826    pub fn yxx(self) -> Vector3 {
827        unsafe {
828            return Vector3 {
829                data: _yxxw(self.data),
830            };
831        }
832    }
833    #[inline(always)]
834    pub fn yxy(self) -> Vector3 {
835        unsafe {
836            return Vector3 {
837                data: _yxyw(self.data),
838            };
839        }
840    }
841    #[inline(always)]
842    pub fn yxz(self) -> Vector3 {
843        unsafe {
844            return Vector3 {
845                data: _yxzw(self.data),
846            };
847        }
848    }
849    #[inline(always)]
850    pub fn yyx(self) -> Vector3 {
851        unsafe {
852            return Vector3 {
853                data: _yyxw(self.data),
854            };
855        }
856    }
857    #[inline(always)]
858    pub fn yyy(self) -> Vector3 {
859        unsafe {
860            return Vector3 {
861                data: _yyyw(self.data),
862            };
863        }
864    }
865    #[inline(always)]
866    pub fn yyz(self) -> Vector3 {
867        unsafe {
868            return Vector3 {
869                data: _yyzw(self.data),
870            };
871        }
872    }
873    #[inline(always)]
874    pub fn yzx(self) -> Vector3 {
875        unsafe {
876            return Vector3 {
877                data: _yzxw(self.data),
878            };
879        }
880    }
881    #[inline(always)]
882    pub fn yzy(self) -> Vector3 {
883        unsafe {
884            return Vector3 {
885                data: _yzyw(self.data),
886            };
887        }
888    }
889    #[inline(always)]
890    pub fn yzz(self) -> Vector3 {
891        unsafe {
892            return Vector3 {
893                data: _yzzw(self.data),
894            };
895        }
896    }
897
898    #[inline(always)]
899    pub fn zxx(self) -> Vector3 {
900        unsafe {
901            return Vector3 {
902                data: _zxxw(self.data),
903            };
904        }
905    }
906    #[inline(always)]
907    pub fn zxy(self) -> Vector3 {
908        unsafe {
909            return Vector3 {
910                data: _zxyw(self.data),
911            };
912        }
913    }
914    #[inline(always)]
915    pub fn zxz(self) -> Vector3 {
916        unsafe {
917            return Vector3 {
918                data: _zxzw(self.data),
919            };
920        }
921    }
922    #[inline(always)]
923    pub fn zyx(self) -> Vector3 {
924        unsafe {
925            return Vector3 {
926                data: _zyxw(self.data),
927            };
928        }
929    }
930    #[inline(always)]
931    pub fn zyy(self) -> Vector3 {
932        unsafe {
933            return Vector3 {
934                data: _zyyw(self.data),
935            };
936        }
937    }
938    #[inline(always)]
939    pub fn zyz(self) -> Vector3 {
940        unsafe {
941            return Vector3 {
942                data: _zyzw(self.data),
943            };
944        }
945    }
946    #[inline(always)]
947    pub fn zzx(self) -> Vector3 {
948        unsafe {
949            return Vector3 {
950                data: _zzxw(self.data),
951            };
952        }
953    }
954    #[inline(always)]
955    pub fn zzy(self) -> Vector3 {
956        unsafe {
957            return Vector3 {
958                data: _zzyw(self.data),
959            };
960        }
961    }
962    #[inline(always)]
963    pub fn zzz(self) -> Vector3 {
964        unsafe {
965            return Vector3 {
966                data: _zzzw(self.data),
967            };
968        }
969    }
970}
971
972impl From<f32> for Vector3 {
973    #[inline(always)]
974    fn from(v: f32) -> Vector3 {
975        return Vector3::set(v);
976    }
977}
978
979// impl From<Vector3> for [f32;3] {
980//     #[inline(always)]
981//     fn from(v : Vector3) -> [f32;3] {
982//         unsafe{
983//         let mut raw= core::mem::MaybeUninit::<RawVector_f32>::uninit().assume_init();//RawVector_f32{data:core::mem::MaybeUninit::<[f32;4]>::uninit().assume_init()};// = RawVector_f32{data:[0.0;4]};
984//         v.store(& mut raw);
985//         return raw.data_3;
986//         }
987//     }
988// }
989
990impl From<FloatVector> for Vector3 {
991    #[inline(always)]
992    fn from(v: FloatVector) -> Vector3 {
993        return Vector3 { data: v.data };
994    }
995}
996
997impl From<Vector2> for Vector3 {
998    #[inline(always)]
999    fn from(v: Vector2) -> Vector3 {
1000        unsafe {
1001            return Vector3 {
1002                data: _mm_movelh_ps(v.data, _mm_setzero_ps()),
1003            };
1004        }
1005    }
1006}
1007
1008impl From<Vector4> for Vector3 {
1009    #[inline(always)]
1010    fn from(v: Vector4) -> Vector3 {
1011        Vector3 { data: v.data }
1012    }
1013}
1014
1015impl From<Vector3Int> for Vector3 {
1016    #[inline(always)]
1017    fn from(v: Vector3Int) -> Vector3 {
1018        unsafe {
1019            return Vector3 {
1020                data: _mm_cvtepi32_ps(v.data),
1021            };
1022        }
1023    }
1024}
1025
1026impl core::ops::Add for Vector3 {
1027    type Output = Vector3;
1028    #[inline(always)]
1029    fn add(self, _rhs: Vector3) -> Vector3 {
1030        Vector3::add(self, _rhs)
1031    }
1032}
1033
1034impl core::ops::AddAssign for Vector3 {
1035    #[inline(always)]
1036    fn add_assign(&mut self, other: Vector3) {
1037        *self = Vector3::add(*self, other)
1038    }
1039}
1040
1041impl core::ops::Sub for Vector3 {
1042    type Output = Vector3;
1043    #[inline(always)]
1044    fn sub(self, _rhs: Vector3) -> Vector3 {
1045        Vector3::sub(self, _rhs)
1046    }
1047}
1048
1049impl core::ops::SubAssign for Vector3 {
1050    #[inline(always)]
1051    fn sub_assign(&mut self, other: Vector3) {
1052        *self = Vector3::sub(*self, other)
1053    }
1054}
1055
1056impl core::ops::Neg for Vector3 {
1057    type Output = Vector3;
1058    #[inline(always)]
1059    fn neg(self) -> Self::Output {
1060        unsafe {
1061            return Vector3 {
1062                data: _mm_xor_ps(_ico_signbit_ps(), self.data),
1063            };
1064        }
1065    }
1066}
1067
1068impl<T: Into<FloatVector>> core::ops::Mul<T> for Vector3 {
1069    type Output = Vector3;
1070    #[inline(always)]
1071    fn mul(self, _rhs: T) -> Vector3 {
1072        return Vector3::mul(self, Vector3::from(_rhs.into()));
1073    }
1074}
1075
1076impl<T: Into<FloatVector>> core::ops::MulAssign<T> for Vector3 {
1077    #[inline(always)]
1078    fn mul_assign(&mut self, _rhs: T) {
1079        *self = Vector3::mul(*self, Vector3::from(_rhs.into()));
1080    }
1081}
1082
1083impl core::ops::Mul<Vector3> for FloatVector {
1084    type Output = Vector3;
1085    #[inline(always)]
1086    fn mul(self: FloatVector, _rhs: Vector3) -> Vector3 {
1087        return Vector3::mul(_rhs, Vector3::from(self));
1088    }
1089}
1090
1091impl<T: Into<FloatVector>> core::ops::Div<T> for Vector3 {
1092    type Output = Vector3;
1093    #[inline(always)]
1094    fn div(self, _rhs: T) -> Vector3 {
1095        return Vector3::div(self, Vector3::from(_rhs.into()));
1096    }
1097}
1098
1099impl core::ops::Div<Vector3> for FloatVector {
1100    type Output = Vector3;
1101    #[inline(always)]
1102    fn div(self: FloatVector, _rhs: Vector3) -> Vector3 {
1103        return Vector3::div(Vector3::from(self), _rhs);
1104    }
1105}
1106
1107impl<T: Into<FloatVector>> core::ops::DivAssign<T> for Vector3 {
1108    #[inline(always)]
1109    fn div_assign(&mut self, _rhs: T) {
1110        *self = Vector3::div(*self, Vector3::from(_rhs.into()));
1111    }
1112}
1113
1114impl PartialEq for Vector3 {
1115    #[inline(always)]
1116    fn eq(&self, other: &Vector3) -> bool {
1117        return Vector3::equal(*self, *other).all();
1118    }
1119}
1120
1121impl SIMDVector3 for Vector3 {
1122    #[inline(always)]
1123    fn data(self) -> __m128 {
1124        return self.data;
1125    }
1126    #[inline(always)]
1127    fn data_i(self) -> __m128i {
1128        return unsafe { _mm_castps_si128(self.data) };
1129    }
1130}
1131
1132#[cfg(test)]
1133mod test;