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
// 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::int_vector::IntVector;
use crate::sse_extensions::*;
use crate::structure::SIMDVector1;
use crate::structure::SIMDVector2;
use crate::structure::SIMDVector3;
use crate::structure::SIMDVector4;
use core::arch::x86_64::*;

#[derive(Copy, Clone, Debug)]
#[repr(C, align(16))]
pub struct FloatVector {
    pub data: __m128,
}

impl FloatVector {
    /// Returns a new FloatVector
    #[inline(always)]
    pub fn new(value: f32) -> FloatVector {
        unsafe {
            FloatVector {
                data: _mm_set1_ps(value),
            }
        }
    }
    #[inline(always)]
    pub fn zero() -> FloatVector {
        unsafe {
            FloatVector {
                data: _mm_setzero_ps(),
            }
        }
    }

    #[inline(always)]
    pub fn value(self) -> f32 {
        unsafe {
            return _mm_cvtss_f32(self.data);
        }
    }

    /// Compute the sum of two vectors and return it as a new vector.
    #[inline(always)]
    pub fn add(self, v2: FloatVector) -> FloatVector {
        unsafe {
            FloatVector {
                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: FloatVector) -> FloatVector {
        unsafe {
            FloatVector {
                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: FloatVector) -> FloatVector {
        unsafe {
            FloatVector {
                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: FloatVector) -> FloatVector {
        unsafe {
            FloatVector {
                data: _mm_div_ps(self.data, v2.data),
            }
        }
    }

    /// Fused Multiply Add.  Result = (a * b) + c.
    #[inline(always)]
    pub fn mul_add(self, v2: FloatVector, v3: FloatVector) -> FloatVector {
        unsafe {
            return FloatVector {
                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: FloatVector, v3: FloatVector) -> FloatVector {
        unsafe {
            return FloatVector {
                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: FloatVector, v3: FloatVector) -> FloatVector {
        unsafe {
            return FloatVector {
                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: FloatVector, v3: FloatVector) -> FloatVector {
        unsafe {
            return FloatVector {
                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: SIMDVector1>(self, v2: T) -> FloatVector {
        unsafe {
            FloatVector {
                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: SIMDVector1>(self, v2: T) -> FloatVector {
        unsafe {
            FloatVector {
                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: SIMDVector1>(self, v2: T) -> FloatVector {
        unsafe {
            FloatVector {
                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: SIMDVector1>(self, v2: T) -> FloatVector {
        unsafe {
            FloatVector {
                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: FloatVector) -> bool {
        unsafe {
            return _mm_movemask_epi8(_mm_castps_si128(_mm_cmpeq_ps(self.data, v2.data))) != 0;
        }
    }
    /// NotEquals, computed component-wise.  This compares bits, and is exact.
    #[inline(always)]
    pub fn not_equal(self, v2: FloatVector) -> bool {
        unsafe {
            return _mm_movemask_epi8(_mm_castps_si128(_mm_cmpneq_ps(self.data, v2.data))) != 0;
        }
    }
    /// Greater than or equal to, computed component-wise.  This compares bits, and is exact.
    #[inline(always)]
    pub fn greater_equal(self, v2: FloatVector) -> bool {
        unsafe {
            return _mm_movemask_epi8(_mm_castps_si128(_mm_cmpge_ps(self.data, v2.data))) != 0;
        }
    }
    /// Greater than, computed component-wise.  This compares bits, and is exact.
    #[inline(always)]
    pub fn greater(self, v2: FloatVector) -> bool {
        unsafe {
            return _mm_movemask_epi8(_mm_castps_si128(_mm_cmpgt_ps(self.data, v2.data))) != 0;
        }
    }
    /// Less than or equal to, computed component-wise.  This compares bits, and is exact.
    #[inline(always)]
    pub fn less_equal(self, v2: FloatVector) -> bool {
        unsafe {
            return _mm_movemask_epi8(_mm_castps_si128(_mm_cmple_ps(self.data, v2.data))) != 0;
        }
    }
    /// Less than, computed component-wise.  This compares bits, and is exact.
    #[inline(always)]
    pub fn less(self, v2: FloatVector) -> bool {
        unsafe {
            return _mm_movemask_epi8(_mm_castps_si128(_mm_cmplt_ps(self.data, v2.data))) != 0;
        }
    }
    /// 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: FloatVector) -> bool {
        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(FloatVector::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: FloatVector) -> bool {
        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(FloatVector::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: FloatVector) -> bool {
        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(FloatVector::from(ABSOLUTE_COMPARISON_EPSILON));
        return delta.greater(epsilon_bound);
    }
    /// The absolute value of each component of the vector.
    #[inline(always)]
    pub fn abs(self) -> FloatVector {
        unsafe {
            FloatVector {
                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: FloatVector) -> FloatVector {
        unsafe {
            FloatVector {
                data: _ico_copysign_ps(self.data, v2.data),
            }
        }
    }

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

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

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

    /// Truncate function.
    #[inline(always)]
    pub fn truncate(self) -> FloatVector {
        unsafe {
            FloatVector {
                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) -> IntVector {
        unsafe {
            IntVector {
                data: _mm_cvttps_epi32(self.floor().data),
            }
        }
    }

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

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

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

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

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

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

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

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

    /// Compute the component-wise min.
    #[inline(always)]
    pub fn min(self, v2: FloatVector) -> FloatVector {
        unsafe {
            FloatVector {
                data: _mm_min_ps(self.data, v2.data),
            }
        }
    }
    /// Choose component wise between A and B based on the mask.  False = A, True = B.
    #[inline(always)]
    pub fn select(self, v2: FloatVector, mask: bool) -> FloatVector {
        let mask_val: u32 = if mask { 0xFFFFFFFF } else { 0 };
        unsafe {
            FloatVector {
                data: _ico_select_ps(
                    self.data,
                    v2.data,
                    _mm_castsi128_ps(_mm_set1_epi32(core::mem::transmute::<u32, i32>(mask_val))),
                ),
            }
        }
    }
    /// Linear interpolate from a to b based on a float.
    #[inline(always)]
    pub fn lerp<T: Into<FloatVector>>(self, v2: FloatVector, t: T) -> FloatVector {
        unsafe {
            let t_val = t.into().data;
            let tmp = _mm_fnmadd_ps(self.data, t_val, self.data); //a - (a*t)
            FloatVector {
                data: _mm_fmadd_ps(v2.data, t_val, tmp),
            } //b * t + a
        }
    }
}

impl From<FloatVector> for f32 {
    #[inline(always)]
    fn from(v: FloatVector) -> f32 {
        return v.value();
    }
}
impl From<f32> for FloatVector {
    #[inline(always)]
    fn from(v: f32) -> FloatVector {
        return FloatVector::new(v);
    }
}
impl From<IntVector> for FloatVector {
    #[inline(always)]
    fn from(v: IntVector) -> FloatVector {
        unsafe {
            return FloatVector {
                data: _mm_cvtepi32_ps(v.data),
            };
        }
    }
}

impl<T: Into<FloatVector>> core::ops::Add<T> for FloatVector {
    type Output = FloatVector;
    #[inline(always)]
    fn add(self, _rhs: T) -> FloatVector {
        FloatVector::add(self, _rhs.into())
    }
}
impl<T: Into<FloatVector>> core::ops::AddAssign<T> for FloatVector {
    #[inline(always)]
    fn add_assign(&mut self, other: T) {
        *self = FloatVector::add(*self, other.into())
    }
}
impl<T: Into<FloatVector>> core::ops::Sub<T> for FloatVector {
    type Output = FloatVector;
    #[inline(always)]
    fn sub(self, _rhs: T) -> FloatVector {
        FloatVector::sub(self, _rhs.into())
    }
}

impl<T: Into<FloatVector>> core::ops::SubAssign<T> for FloatVector {
    #[inline(always)]
    fn sub_assign(&mut self, other: T) {
        *self = FloatVector::sub(*self, other.into())
    }
}
impl core::ops::Neg for FloatVector {
    type Output = FloatVector;
    #[inline(always)]
    fn neg(self) -> Self::Output {
        unsafe {
            return FloatVector {
                data: _mm_xor_ps(_ico_signbit_ps(), self.data),
            };
        }
    }
}

impl<T: Into<FloatVector>> core::ops::Mul<T> for FloatVector {
    type Output = FloatVector;
    #[inline(always)]
    fn mul(self, _rhs: T) -> FloatVector {
        FloatVector::mul(self, _rhs.into())
    }
}
impl<T: Into<FloatVector>> core::ops::MulAssign<T> for FloatVector {
    #[inline(always)]
    fn mul_assign(&mut self, _rhs: T) {
        *self = FloatVector::mul(*self, _rhs.into())
    }
}

impl<T: Into<FloatVector>> core::ops::Div<T> for FloatVector {
    type Output = FloatVector;
    #[inline(always)]
    fn div(self, _rhs: T) -> FloatVector {
        FloatVector::div(self, _rhs.into())
    }
}
impl<T: Into<FloatVector>> core::ops::DivAssign<T> for FloatVector {
    #[inline(always)]
    fn div_assign(&mut self, _rhs: T) {
        *self = FloatVector::div(*self, _rhs.into())
    }
}
impl PartialEq for FloatVector {
    #[inline(always)]
    fn eq(&self, other: &FloatVector) -> bool {
        return FloatVector::equal(*self, *other);
    }
}
impl PartialEq<f32> for FloatVector {
    #[inline(always)]
    fn eq(&self, other: &f32) -> bool {
        return self.value() == *other;
    }
}
impl PartialEq<FloatVector> for f32 {
    #[inline(always)]
    fn eq(&self, other: &FloatVector) -> bool {
        return *self == other.value();
    }
}
impl SIMDVector1 for FloatVector {
    fn data(self) -> __m128 {
        return self.data;
    }
    fn data_i(self) -> __m128i {
        return unsafe { _mm_castps_si128(self.data) };
    }
}
impl SIMDVector2 for FloatVector {
    fn data(self) -> __m128 {
        return self.data;
    }
    fn data_i(self) -> __m128i {
        return unsafe { _mm_castps_si128(self.data) };
    }
}
impl SIMDVector3 for FloatVector {
    fn data(self) -> __m128 {
        return self.data;
    }
    fn data_i(self) -> __m128i {
        return unsafe { _mm_castps_si128(self.data) };
    }
}
impl SIMDVector4 for FloatVector {
    fn data(self) -> __m128 {
        return self.data;
    }
    fn data_i(self) -> __m128i {
        return unsafe { _mm_castps_si128(self.data) };
    }
}
// #[cfg(test)]
// mod test;