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
use crate::bt_scalar::{*};
use crate::bt_casti_to128f;
use crate::bt_castf_to128d;
use crate::bt_castd_to128f;
use std::arch::x86_64::{*};
use std::ops::{AddAssign, SubAssign, MulAssign, DivAssign, Add, Mul, Sub, Neg, Div};

#[macro_export]
macro_rules! bt_shuffle {
    ($x:expr,$y:expr, $z:expr, $w:expr)=>{
        ($w << 6 | $z << 4 | $y << 2 | $x) & 0xff
    }
}

#[macro_export]
macro_rules! bt_pshufd_ps {
    ($_a:expr,$_mask:expr)=>{
        _mm_shuffle_ps($_a, $_a, $_mask)
    }
}

#[macro_export]
macro_rules! bt_splat3_ps {
    ($_a:expr,$_i:expr)=>{
        bt_pshufd_ps!($_a, bt_shuffle!($_i, $_i, $_i, 3))
    }
}

#[macro_export]
macro_rules! bt_splat_ps {
    ($_a:expr,$_i:expr)=>{
        bt_pshufd_ps!($_a, bt_shuffle!($_i, $_i, $_i, $_i))
    }
}

#[macro_export]
macro_rules! btv_3absi_mask {
    ()=>{
        _mm_set_epi32(0x00000000, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF)
    }
}

#[macro_export]
macro_rules! btv_abs_mask {
    ()=>{
        _mm_set_epi32(0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF)
    }
}

#[macro_export]
macro_rules! btv_fff0_mask {
    ()=>{
        _mm_set_epi32(0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF)
    }
}

#[macro_export]
macro_rules! btv3_absf_mask {
    () => {
        bt_casti_to128f!(btv_3absi_mask!())
    };
}

#[macro_export]
macro_rules! btv_fff0f_mask {
    () => {
        bt_casti_to128f!(btv_fff0_mask!())
    };
}

#[macro_export]
macro_rules! btv_xyz_maskf {
    () => {
        btv_fff0f_mask!()
    };
}

#[macro_export]
macro_rules! btv_absf_mask {
    () => {
        bt_casti_to128f!(btv_abs_mask!())
    };
}


#[macro_export]
macro_rules! btv_mzero_mask {
    () => {
        _mm_set_ps(-0.0, -0.0, -0.0, -0.0)
    };
}

#[macro_export]
macro_rules! v_1110 {
    () => {
        _mm_set_ps(0.0, 1.0, 1.0, 1.0)
    };
}

#[macro_export]
macro_rules! v_half {
    () => {
        _mm_set_ps(0.5, 0.5, 0.5, 0.5)
    };
}

#[macro_export]
macro_rules! v_1_5 {
    () => {
        _mm_set_ps(1.5, 1.5, 1.5, 1.5)
    };
}

//--------------------------------------------------------------------------------------------------
union SimdToArray {
    array: [f32; 4],
    simd: BtSimdFloat4,
}

pub struct BtVector3 {
    m_vec128: SimdToArray,
}

impl BtVector3 {
    #[inline(always)]
    pub fn get128(&self) -> BtSimdFloat4 { unsafe { return self.m_vec128.simd; } }

    #[inline(always)]
    pub fn set128(&mut self, v128: BtSimdFloat4) { self.m_vec128.simd = v128; }

    pub fn new_default() -> BtVector3 {
        return BtVector3 {
            m_vec128: SimdToArray { array: [0.0, 0.0, 0.0, 0.0] }
        };
    }

    /// Constructor from scalars
    pub fn new(_x: BtScalar, _y: BtScalar, _z: BtScalar) -> BtVector3 {
        return BtVector3 {
            m_vec128: SimdToArray { array: [_x, _y, _z, 0.0] }
        };
    }

    /// Set Vector
    pub fn new_simd(v: BtSimdFloat4) -> BtVector3 {
        return BtVector3 {
            m_vec128: SimdToArray { simd: v }
        };
    }
}

impl AddAssign for BtVector3 {
    #[inline(always)]
    fn add_assign(&mut self, rhs: Self) {
        self.m_vec128.simd = unsafe { _mm_sub_ps(self.m_vec128.simd, rhs.m_vec128.simd) }
    }
}

impl SubAssign for BtVector3 {
    #[inline(always)]
    fn sub_assign(&mut self, rhs: Self) {
        self.m_vec128.simd = unsafe { _mm_sub_ps(self.m_vec128.simd, rhs.m_vec128.simd) }
    }
}

impl MulAssign<BtScalar> for BtVector3 {
    #[inline(always)]
    fn mul_assign(&mut self, rhs: BtScalar) {
        unsafe {
            let mut vs = _mm_load_ss(&rhs); //    (S 0 0 0)
            vs = bt_pshufd_ps!(vs, 0x80); //    (S S S 0.0)
            self.m_vec128.simd = _mm_mul_ps(self.m_vec128.simd, vs)
        }
    }
}

impl MulAssign<&BtScalar> for BtVector3 {
    #[inline(always)]
    fn mul_assign(&mut self, rhs: &BtScalar) {
        unsafe {
            let mut vs = _mm_load_ss(&*rhs); //    (S 0 0 0)
            vs = bt_pshufd_ps!(vs, 0x80); //    (S S S 0.0)
            self.m_vec128.simd = _mm_mul_ps(self.m_vec128.simd, vs)
        }
    }
}

impl MulAssign for BtVector3 {
    #[inline(always)]
    fn mul_assign(&mut self, rhs: BtVector3) {
        unsafe {
            self.m_vec128.simd = _mm_mul_ps(self.m_vec128.simd, rhs.m_vec128.simd);
        }
    }
}

impl DivAssign<BtScalar> for BtVector3 {
    #[inline(always)]
    fn div_assign(&mut self, rhs: BtScalar) {
        self.mul_assign(1.0 / rhs)
    }
}

macro_rules! impl_bin_add_vector {
    ($lhs:ty, $rhs:ty) => {
        /// Return the sum of two vectors (Point symantics)
        impl Add<$rhs> for $lhs {
            type Output = BtVector3;

            fn add(self, rhs: $rhs) -> Self::Output {
                unsafe {
                    return BtVector3::new_simd(_mm_add_ps(self.m_vec128.simd, rhs.m_vec128.simd));
                }
            }
        }
    }
}

impl_bin_add_vector!(BtVector3, BtVector3);
impl_bin_add_vector!(&BtVector3, BtVector3);
impl_bin_add_vector!(BtVector3, &BtVector3);
impl_bin_add_vector!(&BtVector3, &BtVector3);

macro_rules! impl_bin_mul_vector {
    ($lhs:ty, $rhs:ty) => {
        /// Return the sum of two vectors (Point symantics)
        impl Mul<$rhs> for $lhs {
            type Output = BtVector3;

            fn mul(self, rhs: $rhs) -> Self::Output {
                unsafe {
                    return BtVector3::new_simd(_mm_mul_ps(self.m_vec128.simd, rhs.m_vec128.simd));
                }
            }
        }
    }
}

impl_bin_mul_vector!(BtVector3, BtVector3);
impl_bin_mul_vector!(&BtVector3, BtVector3);
impl_bin_mul_vector!(BtVector3, &BtVector3);
impl_bin_mul_vector!(&BtVector3, &BtVector3);

macro_rules! impl_bin_sub_vector {
    ($lhs:ty, $rhs:ty) => {
        // Return the difference between two vectors
        impl Sub<$rhs> for $lhs {
            type Output = BtVector3;

            #[allow(overflowing_literals)]
            fn sub(self, rhs: $rhs) -> Self::Output {
                unsafe {
                    let r = _mm_sub_ps(self.m_vec128.simd, rhs.m_vec128.simd);
                    let b = btv_fff0f_mask!();
                    return BtVector3::new_simd(_mm_and_ps(r, b));
                }
            }
        }
    }
}

impl_bin_sub_vector!(BtVector3, BtVector3);
impl_bin_sub_vector!(&BtVector3, BtVector3);
impl_bin_sub_vector!(BtVector3, &BtVector3);
impl_bin_sub_vector!(&BtVector3, &BtVector3);

/// Return the negative of the vector
impl Neg for BtVector3 {
    type Output = BtVector3;

    #[allow(overflowing_literals)]
    fn neg(self) -> Self::Output {
        unsafe {
            let r = _mm_xor_ps(self.m_vec128.simd, btv_mzero_mask!());
            return BtVector3::new_simd(_mm_and_ps(r, btv_fff0f_mask!()));
        }
    }
}

impl Mul<BtScalar> for BtVector3 {
    type Output = BtVector3;

    fn mul(self, rhs: f32) -> Self::Output {
        unsafe {
            let mut vs = _mm_load_ss(&rhs); //    (S 0 0 0)
            vs = bt_pshufd_ps!(vs, 0x80); //    (S S S 0.0)
            return BtVector3::new_simd(_mm_mul_ps(self.m_vec128.simd, vs));
        }
    }
}

impl Div<BtScalar> for BtVector3 {
    type Output = BtVector3;

    fn div(self, rhs: f32) -> Self::Output {
        return self * 1.0 / rhs;
    }
}

impl Div for BtVector3 {
    type Output = BtVector3;

    #[allow(overflowing_literals)]
    fn div(self, rhs: Self) -> Self::Output {
        unsafe {
            let mut vec = _mm_div_ps(self.m_vec128.simd, rhs.m_vec128.simd);
            vec = _mm_and_ps(vec, btv_fff0f_mask!());
            return BtVector3::new_simd(vec);
        }
    }
}

impl PartialEq for BtVector3 {
    fn eq(&self, other: &Self) -> bool {
        unsafe {
            return 0xf == _mm_movemask_ps(_mm_cmpeq_ps(self.m_vec128.simd, other.m_vec128.simd));
        }
    }
}

impl Eq for BtVector3 {}

//--------------------------------------------------------------------------------------------------
impl BtVector3 {
    #[inline(always)]
    pub fn dot(&self, v: &BtVector3) -> BtScalar {
        unsafe {
            let mut vd = _mm_mul_ps(self.m_vec128.simd, v.m_vec128.simd);
            let z = _mm_movehl_ps(vd, vd);
            let y = _mm_shuffle_ps(vd, vd, 0x55);
            vd = _mm_add_ss(vd, y);
            vd = _mm_add_ss(vd, z);
            return _mm_cvtss_f32(vd);
        }
    }

    /// Return the length of the vector squared
    #[inline(always)]
    pub fn length2(&self) -> BtScalar { return self.dot(self); }

    /// Return the length of the vector
    #[inline(always)]
    pub fn length(&self) -> BtScalar {
        return self.length2().sqrt();
    }

    /// Return the norm (length) of the vector
    #[inline(always)]
    pub fn norm(&self) -> BtScalar { return self.length(); }

    /// Return the norm (length) of the vector
    #[inline(always)]
    pub fn safe_norm(&self) -> BtScalar {
        let d = self.length2();
        // workaround for some clang/gcc issue of sqrtf(tiny number) = -INF
        if d > SIMD_EPSILON {
            return d.sqrt();
        }
        return 0.0;
    }

    /// Return the distance squared between the ends of this and another vector
    /// This is symantically treating the vector like a point
    #[inline(always)]
    pub fn distance2(&self, v: &BtVector3) -> BtScalar {
        return (v - self).length2();
    }

    /// Return the distance between the ends of this and another vector
    /// This is symantically treating the vector like a point
    #[inline(always)]
    pub fn distance(&self, v: &BtVector3) -> BtScalar {
        return (v - self).length();
    }

    #[inline(always)]
    pub fn safe_normalize(&mut self) {
        let l2 = self.length2();

        if l2 >= SIMD_EPSILON * SIMD_EPSILON {
            self.div_assign(l2.sqrt());
        } else {
            self.set_value(1.0, 0.0, 0.0);
        }
    }

    /// Normalize this vector
    /// * x^2 + y^2 + z^2 = 1
    #[inline(always)]
    pub fn normalize(&mut self) -> &mut BtVector3 {
        unsafe {
            // dot product first
            let mut vd = _mm_mul_ps(self.m_vec128.simd, self.m_vec128.simd);
            let mut z = _mm_movehl_ps(vd, vd);
            let mut y = _mm_shuffle_ps(vd, vd, 0x55);
            vd = _mm_add_ss(vd, y);
            vd = _mm_add_ss(vd, z);

            // NR step 1/sqrt(x) - vd is x, y is output
            y = _mm_rsqrt_ss(vd); // estimate

            //  one step NR
            z = v_1_5!();
            vd = _mm_mul_ss(vd, v_half!()); // vd * 0.5
            // x2 = vd;
            vd = _mm_mul_ss(vd, y); // vd * 0.5 * y0
            vd = _mm_mul_ss(vd, y); // vd * 0.5 * y0 * y0
            z = _mm_sub_ss(z, vd);  // 1.5 - vd * 0.5 * y0 * y0

            y = _mm_mul_ss(y, z); // y0 * (1.5 - vd * 0.5 * y0 * y0)

            y = bt_splat_ps!(y, 0x80);
            self.m_vec128.simd = _mm_mul_ps(self.m_vec128.simd, y);

            return self;
        }
    }

    // Return a normalized version of this vector
    #[inline(always)]
    pub fn normalized(&self) -> &BtVector3 {
        let nrm = self.clone();

        return nrm.normalized();
    }

    /// Return a rotated version of this vector
    /// - Parameter: w_axis The axis to rotate about
    /// - Parameter: angle The angle to rotate by
    #[inline(always)]
    #[allow(overflowing_literals)]
    pub fn rotate(&self, w_axis: BtVector3, _angle: BtScalar) -> BtVector3 {
        unsafe {
            let mut o = _mm_mul_ps(w_axis.m_vec128.simd, self.m_vec128.simd);
            let ssin = _angle.sin();
            let c = w_axis.cross(self).m_vec128.simd;
            o = _mm_and_ps(o, btv_fff0f_mask!());
            let scos = _angle.cos();

            let mut vsin = _mm_load_ss(&ssin); //    (S 0 0 0)
            let mut vcos = _mm_load_ss(&scos); //    (S 0 0 0)

            let y = bt_pshufd_ps!(o, 0xC9); //    (y z x 0)
            let z = bt_pshufd_ps!(o, 0xD2); //    (z x y 0)
            o = _mm_add_ps(o, y);
            vsin = bt_pshufd_ps!(vsin, 0x80); //    (S S S 0)
            o = _mm_add_ps(o, z);
            vcos = bt_pshufd_ps!(vcos, 0x80); //    (S S S 0)

            vsin = _mm_mul_ps(vsin, c);
            o = _mm_mul_ps(o, w_axis.m_vec128.simd);
            let x = _mm_sub_ps(self.m_vec128.simd, o);

            o = _mm_add_ps(o, vsin);
            vcos = _mm_mul_ps(vcos, x);
            o = _mm_add_ps(o, vcos);

            return BtVector3::new_simd(o);
        }
    }

    /// Return the angle between this and another vector
    /// - Parameter: v The other vector
    #[inline(always)]
    pub fn angle(&self, v: &BtVector3) -> BtScalar {
        let s = (self.length2() * v.length2()).sqrt();
        return (self.dot(v) / s).cos();
    }

    /// Return a vector with the absolute values of each element
    #[inline(always)]
    pub fn absolute(&self) -> BtVector3 {
        unsafe {
            return BtVector3::new_simd(_mm_and_ps(self.m_vec128.simd, btv3_absf_mask!()));
        }
    }

    /// Return the cross product between this and another vector
    /// - Parameter: v The other vector */
    #[inline(always)]
    pub fn cross(&self, v: &BtVector3) -> BtVector3 {
        unsafe {
            let mut t = bt_pshufd_ps!(self.m_vec128.simd, bt_shuffle!(1, 2, 0, 3));   //    (Y Z X 0)
            let mut vv = bt_pshufd_ps!(v.m_vec128.simd, bt_shuffle!(1, 2, 0, 3)); //    (Y Z X 0)

            vv = _mm_mul_ps(vv, self.m_vec128.simd);
            t = _mm_mul_ps(t, v.m_vec128.simd);
            vv = _mm_sub_ps(vv, t);

            vv = bt_pshufd_ps!(vv,  bt_shuffle!(1, 2, 0, 3));
            return BtVector3::new_simd(vv);
        }
    }

    #[inline(always)]
    pub fn triple(&self, v1: &BtVector3, v2: &BtVector3) -> BtScalar {
        unsafe {
            // cross:
            let mut t = _mm_shuffle_ps(v1.m_vec128.simd, v1.m_vec128.simd, bt_shuffle!(1, 2, 0, 3)); //    (Y Z X 0)
            let mut v = _mm_shuffle_ps(v2.m_vec128.simd, v2.m_vec128.simd, bt_shuffle!(1, 2, 0, 3)); //    (Y Z X 0)

            v = _mm_mul_ps(v, v1.m_vec128.simd);
            t = _mm_mul_ps(t, v2.m_vec128.simd);
            v = _mm_sub_ps(v, t);

            v = _mm_shuffle_ps(v, v, bt_shuffle!(1, 2, 0, 3));

            // dot:
            v = _mm_mul_ps(v, self.m_vec128.simd);
            let z = _mm_movehl_ps(v, v);
            let y = _mm_shuffle_ps(v, v, 0x55);
            v = _mm_add_ss(v, y);
            v = _mm_add_ss(v, z);
            return _mm_cvtss_f32(v);
        }
    }

    /// Return the axis with the smallest value
    /// Note return values are 0,1,2 for x, y, or z
    #[inline(always)]
    pub fn min_axis(&self) -> i32 {
        unsafe {
            return match self.m_vec128.array[0] < self.m_vec128.array[1] {
                true => {
                    match self.m_vec128.array[0] < self.m_vec128.array[2] {
                        true => { 0 }
                        false => { 2 }
                    }
                }
                false => {
                    match self.m_vec128.array[1] < self.m_vec128.array[2] {
                        true => { 1 }
                        false => { 2 }
                    }
                }
            };
        }
    }

    /// Return the axis with the largest value
    /// Note return values are 0,1,2 for x, y, or z
    #[inline(always)]
    pub fn max_axis(&self) -> i32 {
        unsafe {
            return match self.m_vec128.array[0] < self.m_vec128.array[1] {
                true => {
                    match self.m_vec128.array[1] < self.m_vec128.array[2] {
                        true => { 2 }
                        false => { 1 }
                    }
                }
                false => {
                    match self.m_vec128.array[0] < self.m_vec128.array[2] {
                        true => { 2 }
                        false => { 0 }
                    }
                }
            };
        }
    }

    #[inline(always)]
    pub fn furthest_axis(&self) -> i32 { return self.absolute().min_axis(); }

    #[inline(always)]
    pub fn closest_axis(&self) -> i32 { return self.absolute().max_axis(); }

    #[inline(always)]
    pub fn set_interpolate3(&mut self, v0: BtVector3, v1: BtVector3, rt: BtScalar) {
        unsafe {
            let mut vrt = _mm_load_ss(&rt); //    (rt 0 0 0)
            let s = 1.0 - rt;
            let mut vs = _mm_load_ss(&s); //    (S 0 0 0)
            vs = bt_pshufd_ps!(vs, 0x80); //    (S S S 0.0)
            let r0 = _mm_mul_ps(v0.m_vec128.simd, vs);
            vrt = bt_pshufd_ps!(vrt, 0x80); //    (rt rt rt 0.0)
            let r1 = _mm_mul_ps(v1.m_vec128.simd, vrt);
            let tmp3 = _mm_add_ps(r0, r1);
            self.m_vec128.simd = tmp3;
        }
    }

    /// Return the linear interpolation between this and another vector
    /// - Parameter:  v The other vector
    /// - Parameter:  t The ration of this to v (t = 0 => return this, t=1 => return other)
    #[inline(always)]
    pub fn lerp(&self, v: &BtVector3, t: &BtScalar) -> BtVector3 {
        unsafe {
            let mut vt = _mm_load_ss(t); //    (t 0 0 0)
            vt = bt_pshufd_ps!(vt, 0x80); //    (rt rt rt 0.0)
            let mut vl = _mm_sub_ps(v.m_vec128.simd, self.m_vec128.simd);
            vl = _mm_mul_ps(vl, vt);
            vl = _mm_add_ps(vl, self.m_vec128.simd);

            return BtVector3::new_simd(vl);
        }
    }

    /// Return the x value */
    #[inline(always)]
    pub fn get_x(&self) -> BtScalar { unsafe { return self.m_vec128.array[0]; } }

    /// Return the y value */
    #[inline(always)]
    pub fn get_y(&self) -> BtScalar { unsafe { return self.m_vec128.array[1]; } }

    /// Return the z value */
    #[inline(always)]
    pub fn get_z(&self) -> BtScalar { unsafe { return self.m_vec128.array[2]; } }

    /// Set the x value */
    #[inline(always)]
    pub fn set_x(&mut self, _x: BtScalar) { unsafe { self.m_vec128.array[0] = _x; } }

    /// Set the y value */
    #[inline(always)]
    pub fn set_y(&mut self, _y: BtScalar) { unsafe { self.m_vec128.array[1] = _y; } }

    /// Set the z value */
    #[inline(always)]
    pub fn set_z(&mut self, _z: BtScalar) { unsafe { self.m_vec128.array[2] = _z; } }

    /// Set the w value */
    #[inline(always)]
    pub fn set_w(&mut self, _w: BtScalar) { unsafe { self.m_vec128.array[3] = _w; } }

    /// Return the x value */
    #[inline(always)]
    pub fn x(&self) -> BtScalar { unsafe { return self.m_vec128.array[0]; } }

    /// Return the y value */
    #[inline(always)]
    pub fn y(&self) -> BtScalar { unsafe { return self.m_vec128.array[1]; } }

    /// Return the z value */
    #[inline(always)]
    pub fn z(&self) -> BtScalar { unsafe { return self.m_vec128.array[2]; } }

    /// Return the w value */
    #[inline(always)]
    pub fn w(&self) -> BtScalar { unsafe { return self.m_vec128.array[3]; } }

    /// Set each element to the max of the current values and the values of another btVector3
    /// - Parameter:  other The other btVector3 to compare with
    #[inline(always)]
    pub fn set_max(&mut self, other: BtVector3) {
        unsafe {
            self.m_vec128.simd = _mm_max_ps(self.m_vec128.simd, other.m_vec128.simd);
        }
    }

    /// Set each element to the min of the current values and the values of another btVector3
    /// - Parameter:  other The other btVector3 to compare with
    #[inline(always)]
    pub fn set_min(&mut self, other: BtVector3) {
        unsafe {
            self.m_vec128.simd = _mm_min_ps(self.m_vec128.simd, other.m_vec128.simd);
        }
    }

    #[inline(always)]
    pub fn set_value(&mut self, _x: BtScalar, _y: BtScalar, _z: BtScalar) {
        self.m_vec128.array = [_x, _y, _z, 0.0]
    }

    #[allow(overflowing_literals)]
    pub fn get_skew_symmetric_matrix(&self, v0: &mut BtVector3, v1: &mut BtVector3, v2: &mut BtVector3) {
        unsafe {
            let v = _mm_and_ps(self.m_vec128.simd, btv_fff0f_mask!());
            let mut vv0 = _mm_xor_ps(btv_mzero_mask!(), v);
            let mut vv2 = _mm_movelh_ps(vv0, v);

            let vv1 = _mm_shuffle_ps(v, vv0, 0xCE);

            vv0 = _mm_shuffle_ps(vv0, v, 0xDB);
            vv2 = _mm_shuffle_ps(vv2, v, 0xF9);

            v0.m_vec128.simd = vv0;
            v1.m_vec128.simd = vv1;
            v2.m_vec128.simd = vv2;
        }
    }

    pub fn set_zero(&mut self) {
        unsafe {
            self.m_vec128.simd = _mm_xor_ps(self.m_vec128.simd, self.m_vec128.simd);
        }
    }

    #[inline(always)]
    pub fn is_zero(&self) -> bool {
        unsafe {
            return self.m_vec128.array[0] == 0.0 && self.m_vec128.array[1] == 0.0 && self.m_vec128.array[2] == 0.0;
        }
    }

    #[inline(always)]
    pub fn fuzzy_zero(&self) -> bool { return self.length2() < SIMD_EPSILON * SIMD_EPSILON; }

    /// returns index of maximum dot product between this and vectors in array[]
    /// - Parameter:  array The other vectors
    /// - Parameter:  array_count The number of other vectors
    /// - Parameter:  dotOut The maximum dot product */
    #[inline(always)]
    pub fn max_dot(&self, array: BtVector3, array_count: i64, dot_out: &mut BtScalar) -> i64 {
        return 0;
    }

    /// returns index of minimum dot product between this and vectors in array[]
    /// - Parameter:  array The other vectors
    /// - Parameter:  array_count The number of other vectors
    /// - Parameter:  dotOut The minimum dot product */
    #[inline(always)]
    pub fn min_dot(&self, array: BtVector3, array_count: i64, dot_out: &mut BtScalar) -> i64 {
        return 0;
    }

    //// create a vector as  btVector3( this->dot( btVector3 v0 ), this->dot( btVector3 v1), this->dot( btVector3 v2 ))
    #[inline(always)]
    #[allow(overflowing_literals)]
    pub fn dot3(&self, v0: BtVector3, v1: BtVector3, v2: BtVector3) -> BtVector3 {
        unsafe {
            let a0 = _mm_mul_ps(v0.m_vec128.simd, self.m_vec128.simd);
            let a1 = _mm_mul_ps(v1.m_vec128.simd, self.m_vec128.simd);
            let mut a2 = _mm_mul_ps(v2.m_vec128.simd, self.m_vec128.simd);
            let b0 = _mm_unpacklo_ps(a0, a1);
            let b1 = _mm_unpackhi_ps(a0, a1);
            let b2 = _mm_unpacklo_ps(a2, _mm_setzero_ps());
            let mut r = _mm_movelh_ps(b0, b2);
            r = _mm_add_ps(r, _mm_movehl_ps(b2, b0));
            a2 = _mm_and_ps(a2, btv_xyz_maskf!());
            r = _mm_add_ps(r, bt_castd_to128f!(_mm_move_sd( bt_castf_to128d!(a2), bt_castf_to128d!(b1))));
            return BtVector3::new_simd(r);
        }
    }
}

/// Return the dot product between two vectors
#[inline(always)]
pub fn bt_dot(v1: &BtVector3, v2: &BtVector3) -> BtScalar { return v1.dot(v2); }

/**@brief Return the distance squared between two vectors */
#[inline(always)]
pub fn bt_distance2(v1: &BtVector3, v2: &BtVector3) -> BtScalar { return v1.distance2(v2); }

/**@brief Return the distance between two vectors */
#[inline(always)]
pub fn bt_distance(v1: &BtVector3, v2: &BtVector3) -> BtScalar { return v1.distance(v2); }

/**@brief Return the angle between two vectors */
#[inline(always)]
pub fn bt_angle(v1: &BtVector3, v2: &BtVector3) -> BtScalar { return v1.angle(v2); }

/**@brief Return the cross product of two vectors */
#[inline(always)]
pub fn bt_cross(v1: &BtVector3, v2: &BtVector3) -> BtVector3 { return v1.cross(v2); }

#[inline(always)]
pub fn bt_triple(v1: &BtVector3, v2: &BtVector3, v3: &BtVector3) -> BtScalar {
    return v1.triple(v2, v3);
}

/// Return the linear interpolation between two vectors
/// - Parameter:  v1 One vector
/// - Parameter:  v2 The other vector
/// - Parameter:  t The ration of this to v (t = 0 => return v1, t=1 => return v2)
#[inline(always)]
pub fn lerp(v1: &BtVector3, v2: &BtVector3, t: &BtScalar) -> BtVector3 { return v1.lerp(v2, t); }

//--------------------------------------------------------------------------------------------------
#[test]
pub fn test() {
    let mut a = BtVector3::new_default();
    let b: BtScalar = 10.0;
    a /= b;

    a.set_value(10.0, 20.0, 4.0);
    unsafe {
        assert_eq!(a.m_vec128.array[0], 10.0);
    }
}