ico_math 0.1.6

An opinionated SIMD Math Library for games and graphics in Rust.
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
// Copyright 2019 Icosahedra, LLC
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//

use crate::float_vector::FloatVector;
use crate::raw::RawVector_f32;
use crate::sse_extensions::*;
use crate::structure::SIMDVector2;
use crate::vector2_bool::Vector2Bool;
use crate::vector2_int::Vector2Int;
use crate::vector3::Vector3;
use crate::vector4::Vector4;
use core::arch::x86_64::*;

/// A vector of 2 floats (x,y).
#[derive(Copy, Clone, Debug)]
#[repr(C, align(16))]
pub struct Vector2 {
    pub data: __m128,
}

impl Vector2 {
    /// Construct a new vector from f32 components.
    #[inline(always)]
    pub fn new(x: f32, y: f32) -> Vector2 {
        unsafe {
            Vector2 {
                data: _mm_set_ps(0.0f32, 0.0f32, y, x),
            }
        }
    }

    /// Set all values of the vector to the same f32 value.
    #[inline(always)]
    pub fn set<T: Into<FloatVector>>(value: T) -> Vector2 {
        return Vector2 {
            data: value.into().data,
        };
    }

    /// Construct a new vector of zeros.
    #[inline(always)]
    pub fn zero() -> Vector2 {
        unsafe {
            Vector2 {
                data: _mm_setzero_ps(),
            }
        }
    }

    /// Get the x value of the vector, broadcast to all components as a FloatVector (xxxx).
    #[inline(always)]
    pub fn x(self) -> FloatVector {
        return FloatVector {
            data: self.xxxx().data,
        };
    }

    /// Get the y value of the vector, broadcast to all components as a FloatVector (yyyy).
    #[inline(always)]
    pub fn y(self) -> FloatVector {
        return FloatVector {
            data: self.yyyy().data,
        };
    }
    /// Load a value from aligned memory.
    #[inline(always)]
    pub fn load(raw: &RawVector_f32) -> Vector2 {
        unsafe {
            // Use the sledgehammer cast here.  It's fine because RawVector is aligned and c-like.
            return Vector2 {
                data: _mm_load_ps(core::mem::transmute(raw)),
            };
        }
    }

    /// Store a value to aligned memory.
    #[inline(always)]
    pub fn store(self, dst: &mut RawVector_f32) {
        unsafe {
            // Use the sledgehammer cast here.  It's fine because RawVector is aligned and c-like.
            _mm_store_ps(core::mem::transmute(dst), self.data);
        }
    }

    /// Set the x value of this vector, leaving the other components unchanged.
    #[inline(always)]
    pub fn set_x<T: Into<FloatVector>>(&mut self, value: T) {
        unsafe {
            self.data = _mm_move_ss(self.data, value.into().data);
        }
    }

    /// Set the y value of this vector, leaving the other components unchanged.
    #[inline(always)]
    pub fn set_y<T: Into<FloatVector>>(&mut self, value: T) {
        unsafe {
            let v1 = _mm_move_ss(value.into().data, self.data);
            self.data = _mm_shuffle_ps(v1, self.data, _ico_shuffle(3, 2, 1, 0));
        }
    }

    /// Compute the 2 element dot-product, and return it as a broadcast FloatVector.
    #[inline(always)]
    pub fn dot(self, v1: Vector2) -> FloatVector {
        unsafe {
            let tmp0 = _mm_mul_ps(self.data, v1.data);
            let mut tmp1 = _yxzw(tmp0); // _mm_shuffle_ps(tmp0,tmp0, _ico_shuffle(3, 2, 0, 1)); //yxzw

            tmp1 = _mm_add_ps(tmp0, tmp1); //xy,xy,qq,qq
            return FloatVector {
                data: _mm_unpacklo_ps(tmp1, tmp1),
            }; //xy,xy.xy,xy
        }
    }

    /// Rotate the vector by radians
    /// Right handed system, positive rotation is counterclockwise about the axis of rotation.
    #[inline(always)]
    pub fn rotate<T: Into<FloatVector>>(self, radians: T) -> Vector2 {
        let tmp = radians.into();
        //let f = radians.sin_cos();

        //let sn: f32 = f.0 as f32;
        //let cs: f32 = f.1 as f32;
        let sn = tmp.sin().value();
        let mut sncs = Vector4::from(tmp.cos());
        sncs.set_z(sn);
        sncs.set_w(-sn);
        unsafe {
            // Any values below the epsilon get clamped to zero.  This fixes precision issues around zero.
            let epsilon = _mm_set1_ps(ABSOLUTE_COMPARISON_EPSILON);
            //let sncs = _mm_set_ps(-sn, sn, cs, cs);
            let mask = _mm_cmpgt_ps(_ico_abs_ps(sncs.data), epsilon);

            let masked_sncs = _mm_and_ps(sncs.data, mask);

            //x1y1x2y2
            let xyxy = _mm_movelh_ps(self.data, self.data);
            //x * cs, y * cs, x*sn, y*-sn
            let v2 = _mm_mul_ps(xyxy, masked_sncs);

            //x1 + y2, y1 + x2,
            //x = x * cs - y * sn;
            //y = x * sn + y * cs;
            return Vector2 {
                data: _mm_add_ps(v2, _wzyx(v2)),
            }; //wzyx
        }
    }

    /// Compute the sum of two vectors and return it as a new vector.
    #[inline(always)]
    pub fn add(self, v2: Vector2) -> Vector2 {
        unsafe {
            Vector2 {
                data: _mm_add_ps(self.data, v2.data),
            }
        }
    }

    /// Subtract a vector and return it as a new vector.
    #[inline(always)]
    pub fn sub(self, v2: Vector2) -> Vector2 {
        unsafe {
            Vector2 {
                data: _mm_sub_ps(self.data, v2.data),
            }
        }
    }

    /// Multiply two vectors component-wise, and return it as a new vector.  
    /// (x1 * x2, y1 * y2, z1 * z2, w1 * w2)
    #[inline(always)]
    pub fn mul(self, v2: Vector2) -> Vector2 {
        unsafe {
            Vector2 {
                data: _mm_mul_ps(self.data, v2.data),
            }
        }
    }

    /// Divide two vectors component-wise, and return it as a new vector.  
    /// (x1 / x2, y1 / y2, z1 / z2, w1 / w2)
    #[inline(always)]
    pub fn div(self, v2: Vector2) -> Vector2 {
        unsafe {
            Vector2 {
                data: _mm_div_ps(self.data, v2.data),
            }
        }
    }

    /// Fused Multiply Add.  Result = (a * b) + c.
    #[inline(always)]
    pub fn mul_add(self, v2: Vector2, v3: Vector2) -> Vector2 {
        unsafe {
            return Vector2 {
                data: _mm_fmadd_ps(self.data, v2.data, v3.data),
            };
        }
    }

    /// Fused Multiply Sub.  Result = (a * b) - c.
    #[inline(always)]
    pub fn mul_sub(self, v2: Vector2, v3: Vector2) -> Vector2 {
        unsafe {
            return Vector2 {
                data: _mm_fmsub_ps(self.data, v2.data, v3.data),
            };
        }
    }

    /// Negated Fused Multiply Add.  Result = -(a * b) + c.
    #[inline(always)]
    pub fn neg_mul_add(self, v2: Vector2, v3: Vector2) -> Vector2 {
        unsafe {
            return Vector2 {
                data: _mm_fnmadd_ps(self.data, v2.data, v3.data),
            };
        }
    }

    /// Negated Fused Multiply Sub.  Result = -(a * b) - c.
    #[inline(always)]
    pub fn neg_mul_sub(self, v2: Vector2, v3: Vector2) -> Vector2 {
        unsafe {
            return Vector2 {
                data: _mm_fnmsub_ps(self.data, v2.data, v3.data),
            };
        }
    }

    /// Compute the bitwise AND of two vectors.
    /// This function treats inputs as binary data, and doesn't perform any conversions.
    #[inline(always)]
    pub fn and<T: SIMDVector2>(self, v2: T) -> Vector2 {
        unsafe {
            Vector2 {
                data: _mm_and_ps(self.data, v2.data()),
            }
        }
    }

    /// Compute the bitwise OR of two vectors.
    /// This function treats inputs as binary data, and doesn't perform any conversions.
    #[inline(always)]
    pub fn or<T: SIMDVector2>(self, v2: T) -> Vector2 {
        unsafe {
            Vector2 {
                data: _mm_or_ps(self.data, v2.data()),
            }
        }
    }

    /// Compute the bitwise ANDNOT of two vectors.
    /// This function treats inputs as binary data, and doesn't perform any conversions.
    #[inline(always)]
    pub fn andnot<T: SIMDVector2>(self, v2: T) -> Vector2 {
        unsafe {
            Vector2 {
                data: _mm_andnot_ps(self.data, v2.data()),
            }
        }
    }

    /// Compute the bitwise XOR of two vectors.
    /// This function treats inputs as binary data, and doesn't perform any conversions.
    #[inline(always)]
    pub fn xor<T: SIMDVector2>(self, v2: T) -> Vector2 {
        unsafe {
            Vector2 {
                data: _mm_xor_ps(self.data, v2.data()),
            }
        }
    }

    /// Equals, computed component-wise.  This compares bits, and is exact.
    #[inline(always)]
    pub fn equal(self, v2: Vector2) -> Vector2Bool {
        unsafe {
            Vector2Bool {
                data: _mm_castps_si128(_mm_cmpeq_ps(self.data, v2.data)),
            }
        }
    }
    /// NotEquals, computed component-wise.  This compares bits, and is exact.
    #[inline(always)]
    pub fn not_equal(self, v2: Vector2) -> Vector2Bool {
        unsafe {
            Vector2Bool {
                data: _mm_castps_si128(_mm_cmpneq_ps(self.data, v2.data)),
            }
        }
    }
    /// Greater than or equal to, computed component-wise.  This compares bits, and is exact.
    #[inline(always)]
    pub fn greater_equal(self, v2: Vector2) -> Vector2Bool {
        unsafe {
            Vector2Bool {
                data: _mm_castps_si128(_mm_cmpge_ps(self.data, v2.data)),
            }
        }
    }
    /// Greater than, computed component-wise.  This compares bits, and is exact.
    #[inline(always)]
    pub fn greater(self, v2: Vector2) -> Vector2Bool {
        unsafe {
            Vector2Bool {
                data: _mm_castps_si128(_mm_cmpgt_ps(self.data, v2.data)),
            }
        }
    }
    /// Less than or equal to, computed component-wise.  This compares bits, and is exact.
    #[inline(always)]
    pub fn less_equal(self, v2: Vector2) -> Vector2Bool {
        unsafe {
            Vector2Bool {
                data: _mm_castps_si128(_mm_cmple_ps(self.data, v2.data)),
            }
        }
    }
    /// Less than, computed component-wise.  This compares bits, and is exact.
    #[inline(always)]
    pub fn less(self, v2: Vector2) -> Vector2Bool {
        unsafe {
            Vector2Bool {
                data: _mm_castps_si128(_mm_cmplt_ps(self.data, v2.data)),
            }
        }
    }
    /// Relative and absolute epsilon comparison.  
    /// Uses machine epsilon as absolute, and 4*machine epsilon for relative.
    /// return abs(a - b) <= max(machine_epsilon, (max( abs(a), abs(b) ) * relative_epsilon);
    /// Adapted from Knuth.  
    /// https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
    #[inline(always)]
    pub fn approx_equal(self, v2: Vector2) -> Vector2Bool {
        let delta = (self - v2).abs();
        let abs_a = self.abs();
        let abs_b = v2.abs();
        let epsilon_bound = (abs_a.max(abs_b) * RELATIVE_COMPARISON_EPSILON)
            .max(Vector2::from(ABSOLUTE_COMPARISON_EPSILON));
        return delta.less_equal(epsilon_bound);
    }

    /// Adapted from Knuth with an added absolute epsilon
    /// return (a - b) > max(machine_epsilon, (max( abs(a), abs(b) ) * relative_epsilon);
    #[inline(always)]
    pub fn definitely_greater(self, v2: Vector2) -> Vector2Bool {
        let delta = self.sub(v2);
        let abs_a = self.abs();
        let abs_b = v2.abs();
        let epsilon_bound = (abs_a.max(abs_b) * RELATIVE_COMPARISON_EPSILON)
            .max(Vector2::from(ABSOLUTE_COMPARISON_EPSILON));
        return delta.greater(epsilon_bound);
    }
    /// Adapted from Knuth with an added absolute epsilon
    /// return (a - b) > max(machine_epsilon, (max( abs(a), abs(b) ) * relative_epsilon);
    #[inline(always)]
    pub fn definitely_less(self, v2: Vector2) -> Vector2Bool {
        let delta = v2.sub(self);
        let abs_a = self.abs();
        let abs_b = v2.abs();
        let epsilon_bound = (abs_a.max(abs_b) * RELATIVE_COMPARISON_EPSILON)
            .max(Vector2::from(ABSOLUTE_COMPARISON_EPSILON));
        return delta.greater(epsilon_bound);
    }
    /// The absolute value of each component of the vector.
    #[inline(always)]
    pub fn abs(self) -> Vector2 {
        unsafe {
            Vector2 {
                data: _ico_abs_ps(self.data),
            }
        }
    }

    /// Take the magnitude of the first argument (self), and use the sign of the second argument to produce a new vector
    #[inline(always)]
    pub fn copysign(self, v2: Vector2) -> Vector2 {
        unsafe {
            Vector2 {
                data: _ico_copysign_ps(self.data, v2.data),
            }
        }
    }

    /// Floor function.  Returns signed 0 when applicable.
    #[inline(always)]
    pub fn floor(self) -> Vector2 {
        unsafe {
            Vector2 {
                data: _mm_floor_ps(self.data),
            }
        }
    }

    /// Ceil function.  Returns signed 0 when applicable.
    #[inline(always)]
    pub fn ceil(self) -> Vector2 {
        unsafe {
            Vector2 {
                data: _mm_ceil_ps(self.data),
            }
        }
    }

    /// Round to nearest even function. Returns signed 0 when applicable.
    #[inline(always)]
    pub fn round(self) -> Vector2 {
        unsafe {
            Vector2 {
                data: _mm_round_ps(self.data, _MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC),
            }
        }
    }

    /// Truncate function.
    #[inline(always)]
    pub fn truncate(self) -> Vector2 {
        unsafe {
            Vector2 {
                data: _mm_round_ps(self.data, _MM_FROUND_TO_ZERO | _MM_FROUND_NO_EXC),
            }
        }
    }

    /// Convert to an integer vector using the floor function.
    #[inline(always)]
    pub fn floor_to_int(self) -> Vector2Int {
        unsafe {
            Vector2Int {
                data: _mm_cvttps_epi32(self.floor().data),
            }
        }
    }

    /// Convert to an integer vector using the ceil function.
    #[inline(always)]
    pub fn ceil_to_int(self) -> Vector2Int {
        unsafe {
            Vector2Int {
                data: _mm_cvttps_epi32(self.ceil().data),
            }
        }
    }
    /// Convert to an integer vector using the round function.
    #[inline(always)]
    pub fn round_to_int(self) -> Vector2Int {
        unsafe {
            Vector2Int {
                data: _mm_cvttps_epi32(self.round().data),
            }
        }
    }

    /// Convert to an integer vector using the truncate function.
    #[inline(always)]
    pub fn truncate_to_int(self) -> Vector2Int {
        unsafe {
            Vector2Int {
                data: _mm_cvttps_epi32(self.truncate().data),
            }
        }
    }

    /// Compute the fractional component of each component
    /// Result = X - Floor(x)
    #[inline(always)]
    pub fn frac(self) -> Vector2 {
        return Vector2::sub(self, Vector2::floor(self));
    }

    /// Compute the square root of each component
    #[inline(always)]
    pub fn sqrt(self) -> Vector2 {
        unsafe {
            Vector2 {
                data: _mm_sqrt_ps(self.data),
            }
        }
    }

    /// Compute the approximate sin of each component
    #[inline(always)]
    pub fn sin(self) -> Vector2 {
        unsafe {
            Vector2 {
                data: _ico_sin_ps(self.data),
            }
        }
    }

    /// Compute the approximate cos of each component
    #[inline(always)]
    pub fn cos(self) -> Vector2 {
        unsafe {
            Vector2 {
                data: _ico_cos_ps(self.data),
            }
        }
    }
    /// Compute the approximate tan of each component
    #[inline(always)]
    pub fn tan(self) -> Vector2 {
        unsafe {
            Vector2 {
                data: _ico_tan_ps(self.data),
            }
        }
    }
    /// Compute the approximate acos of each component
    #[inline(always)]
    pub fn acos(self) -> Vector2 {
        unsafe {
            Vector2 {
                data: _ico_acos_ps(self.data),
            }
        }
    }

    /// Compute the approximate asin of each component
    #[inline(always)]
    pub fn asin(self) -> Vector2 {
        unsafe {
            Vector2 {
                data: _ico_asin_ps(self.data),
            }
        }
    }
    #[inline(always)]
    pub fn atan2(self, x: Vector2) -> Vector2 {
        unsafe {
            Vector2 {
                data: _ico_atan2_ps(self.data, x.data),
            }
        }
    }
    /// Compute the component-wise max.
    #[inline(always)]
    pub fn max(self, v2: Vector2) -> Vector2 {
        unsafe {
            Vector2 {
                data: _mm_max_ps(self.data, v2.data),
            }
        }
    }

    /// Compute the component-wise min.
    #[inline(always)]
    pub fn min(self, v2: Vector2) -> Vector2 {
        unsafe {
            Vector2 {
                data: _mm_min_ps(self.data, v2.data),
            }
        }
    }

    #[inline(always)]
    pub fn horizontal_min(self) -> FloatVector {
        let x = self.x();
        return FloatVector {
            data: x.min(self.y()).data,
        };
    }

    #[inline(always)]
    pub fn horizontal_max(self) -> FloatVector {
        let x = self.x();
        return FloatVector {
            data: x.max(self.y()).data,
        };
    }

    /// Choose component wise between A and B based on the mask.  False = A, True = B.
    #[inline(always)]
    pub fn select(self, v2: Vector2, mask: Vector2Bool) -> Vector2 {
        unsafe {
            Vector2 {
                data: _ico_select_ps(self.data, v2.data, _mm_castsi128_ps(mask.data)),
            }
        }
    }
    /// Linear interpolate from a to b based on a float.
    #[inline(always)]
    pub fn lerp<T: Into<FloatVector>>(self, v2: Vector2, t: T) -> Vector2 {
        unsafe {
            let t_val = t.into().data;
            let tmp = _mm_fnmadd_ps(self.data, t_val, self.data); //a - (a*t)
            Vector2 {
                data: _mm_fmadd_ps(v2.data, t_val, tmp),
            } //b * t + a
        }
    }

    /// Normalize the vector.  Returns a zero vector if the result would be infinity or NAN.
    #[inline(always)]
    pub fn normalize(self) -> Vector2 {
        let length = FloatVector::sqrt(Vector2::dot(self, self));
        let norm = Vector2::div(self, Vector2::from(length));

        unsafe {
            // This catches infinity, NAN.  Zero vectors are possible - but that is fine - we failed
            let result_length_sqr = Vector2::from(Vector2::dot(norm, norm));
            let mask_less = Vector2::less(
                result_length_sqr,
                Vector2 {
                    data: _ico_two_ps(),
                },
            );
            return Vector2::and(norm, mask_less);
        }
    }

    /// Normalize the vector.  Returns a zero vector if the result would be infinity or NAN.
    /// Also returns the length of the vector prior to normalization.
    #[inline(always)]
    pub fn normalize_length(self) -> (Vector2, FloatVector) {
        let length = FloatVector::sqrt(Vector2::dot(self, self));
        let norm = Vector2::div(self, Vector2::from(length));

        unsafe {
            // This catches infinity, NAN.  Zero vectors are possible - but that is fine - we failed
            let result_length_sqr = Vector2::from(Vector2::dot(norm, norm));
            let mask_less = Vector2::less(
                result_length_sqr,
                Vector2 {
                    data: _ico_two_ps(),
                },
            );
            return (Vector2::and(norm, mask_less), length);
        }
    }

    /// The square magnitude of the vector.  Equal to Dot(self, self).
    #[inline(always)]
    pub fn sqr_magnitude(self) -> FloatVector {
        return Vector2::dot(self, self);
    }

    /// The magnitude of the vector.  Equal to Sqrt(Dot(self, self)).
    #[inline(always)]
    pub fn magnitude(self) -> FloatVector {
        return FloatVector::sqrt(Vector2::dot(self, self));
    }

    #[inline(always)]
    pub fn xxxx(self) -> Vector4 {
        unsafe {
            return Vector4 {
                data: _xxxx(self.data),
            };
        }
    }
    #[inline(always)]
    pub fn yyyy(self) -> Vector4 {
        unsafe {
            return Vector4 {
                data: _yyyy(self.data),
            };
        }
    }

    #[inline(always)]
    pub fn xx(self) -> Vector2 {
        unsafe {
            return Vector2 {
                data: _xxzw(self.data),
            };
        }
    }
    #[inline(always)]
    pub fn xy(self) -> Vector2 {
        unsafe {
            return Vector2 {
                data: _xyzw(self.data),
            };
        }
    }
    #[inline(always)]
    pub fn yx(self) -> Vector2 {
        unsafe {
            return Vector2 {
                data: _yxzw(self.data),
            };
        }
    }
    #[inline(always)]
    pub fn yy(self) -> Vector2 {
        unsafe {
            return Vector2 {
                data: _yyzw(self.data),
            };
        }
    }
}

impl From<f32> for Vector2 {
    #[inline(always)]
    fn from(v: f32) -> Vector2 {
        return Vector2::set(v);
    }
}
impl From<FloatVector> for Vector2 {
    #[inline(always)]
    fn from(v: FloatVector) -> Vector2 {
        return Vector2 { data: v.data };
    }
}
impl From<Vector3> for Vector2 {
    #[inline(always)]
    fn from(v: Vector3) -> Vector2 {
        Vector2 { data: v.data }
    }
}
impl From<Vector4> for Vector2 {
    #[inline(always)]
    fn from(v: Vector4) -> Vector2 {
        Vector2 { data: v.data }
    }
}
impl From<Vector2Int> for Vector2 {
    #[inline(always)]
    fn from(v: Vector2Int) -> Vector2 {
        unsafe {
            return Vector2 {
                data: _mm_cvtepi32_ps(v.data),
            };
        }
    }
}

impl core::ops::Add for Vector2 {
    type Output = Vector2;
    #[inline(always)]
    fn add(self, _rhs: Vector2) -> Vector2 {
        Vector2::add(self, _rhs)
    }
}
impl core::ops::AddAssign for Vector2 {
    #[inline(always)]
    fn add_assign(&mut self, other: Vector2) {
        *self = Vector2::add(*self, other)
    }
}
impl core::ops::Sub for Vector2 {
    type Output = Vector2;
    #[inline(always)]
    fn sub(self, _rhs: Vector2) -> Vector2 {
        Vector2::sub(self, _rhs)
    }
}
impl core::ops::SubAssign for Vector2 {
    #[inline(always)]
    fn sub_assign(&mut self, other: Vector2) {
        *self = Vector2::sub(*self, other)
    }
}
impl core::ops::Neg for Vector2 {
    type Output = Vector2;
    #[inline(always)]
    fn neg(self) -> Self::Output {
        unsafe {
            return Vector2 {
                data: _mm_xor_ps(_ico_signbit_ps(), self.data),
            };
        }
    }
}
impl<T: Into<FloatVector>> core::ops::Mul<T> for Vector2 {
    type Output = Vector2;
    #[inline(always)]
    fn mul(self, _rhs: T) -> Vector2 {
        return Vector2::mul(self, Vector2::from(_rhs.into()));
    }
}
impl<T: Into<FloatVector>> core::ops::MulAssign<T> for Vector2 {
    #[inline(always)]
    fn mul_assign(&mut self, _rhs: T) {
        *self = Vector2::mul(*self, Vector2::from(_rhs.into()));
    }
}
impl core::ops::Mul<Vector2> for FloatVector {
    type Output = Vector2;
    #[inline(always)]
    fn mul(self: FloatVector, _rhs: Vector2) -> Vector2 {
        return Vector2::mul(_rhs, Vector2::from(self));
    }
}

impl<T: Into<FloatVector>> core::ops::Div<T> for Vector2 {
    type Output = Vector2;
    #[inline(always)]
    fn div(self, _rhs: T) -> Vector2 {
        return Vector2::div(self, Vector2::from(_rhs.into()));
    }
}
impl core::ops::Div<Vector2> for FloatVector {
    type Output = Vector2;
    #[inline(always)]
    fn div(self: FloatVector, _rhs: Vector2) -> Vector2 {
        return Vector2::div(Vector2::from(self), _rhs);
    }
}
impl<T: Into<FloatVector>> core::ops::DivAssign<T> for Vector2 {
    #[inline(always)]
    fn div_assign(&mut self, _rhs: T) {
        *self = Vector2::div(*self, Vector2::from(_rhs.into()));
    }
}

impl PartialEq for Vector2 {
    #[inline(always)]
    fn eq(&self, other: &Vector2) -> bool {
        return Vector2::equal(*self, *other).all();
    }
}

impl SIMDVector2 for Vector2 {
    #[inline(always)]
    fn data(self) -> __m128 {
        return self.data;
    }
    #[inline(always)]
    fn data_i(self) -> __m128i {
        return unsafe { _mm_castps_si128(self.data) };
    }
}
#[cfg(test)]
mod test;