Skip to main content

rill_core/math/vector/
scalar.rs

1//! # Scalar implementations of vector operations
2//!
3//! Fallback implementations for platforms without SIMD support or for debugging.
4
5use super::traits::*;
6use crate::{Scalar, Transcendental};
7
8// -----------------------------------------------------------------------------
9// Scalar vector types
10
11/// Scalar vector of 1 element
12#[derive(Copy, Clone, Debug, PartialEq)]
13pub struct ScalarVector1<T: Scalar>([T; 1]);
14
15/// Scalar vector of 2 elements
16#[derive(Copy, Clone, Debug, PartialEq)]
17pub struct ScalarVector2<T: Scalar>([T; 2]);
18
19/// Scalar vector of 4 elements
20#[derive(Copy, Clone, Debug, PartialEq)]
21pub struct ScalarVector4<T: Scalar>([T; 4]);
22
23impl<T: Scalar> ScalarVector4<T> {
24    /// Construct a vector by applying a function to each lane index.
25    pub fn from_fn<F: FnMut(usize) -> T>(f: F) -> Self {
26        Self(core::array::from_fn(f))
27    }
28
29    /// Apply a function to each lane without changing the element type.
30    pub fn map<F: FnMut(T) -> T>(&self, mut f: F) -> Self {
31        Self::from_fn(|i| f(self.0[i]))
32    }
33}
34
35/// Scalar vector of 8 elements
36#[derive(Copy, Clone, Debug, PartialEq)]
37pub struct ScalarVector8<T: Scalar>([T; 8]);
38
39// -----------------------------------------------------------------------------
40// Vector implementation for ScalarVector4
41// -----------------------------------------------------------------------------
42
43impl<T: Scalar> Vector<T, 4> for ScalarVector4<T> {
44    fn splat(value: T) -> Self {
45        ScalarVector4([value; 4])
46    }
47
48    fn load(slice: &[T]) -> Self {
49        let mut arr = [T::ZERO; 4];
50        arr.copy_from_slice(&slice[0..4]);
51        ScalarVector4(arr)
52    }
53
54    fn store(&self, slice: &mut [T]) {
55        slice[0..4].copy_from_slice(&self.0);
56    }
57
58    fn extract(&self, index: usize) -> T {
59        self.0[index]
60    }
61
62    fn insert(&self, index: usize, value: T) -> Self {
63        let mut arr = self.0;
64        arr[index] = value;
65        ScalarVector4(arr)
66    }
67
68    fn add(&self, other: &Self) -> Self {
69        ScalarVector4(core::array::from_fn(|i| self.0[i] + other.0[i]))
70    }
71
72    fn sub(&self, other: &Self) -> Self {
73        ScalarVector4(core::array::from_fn(|i| self.0[i] - other.0[i]))
74    }
75
76    fn mul(&self, other: &Self) -> Self {
77        ScalarVector4(core::array::from_fn(|i| self.0[i] * other.0[i]))
78    }
79
80    fn div(&self, other: &Self) -> Self {
81        ScalarVector4(core::array::from_fn(|i| self.0[i] / other.0[i]))
82    }
83
84    fn rem(&self, other: &Self) -> Self {
85        ScalarVector4(core::array::from_fn(|i| self.0[i] % other.0[i]))
86    }
87
88    fn neg(&self) -> Self {
89        ScalarVector4(core::array::from_fn(|i| -self.0[i]))
90    }
91
92    fn abs(&self) -> Self {
93        ScalarVector4(core::array::from_fn(|i| self.0[i].abs()))
94    }
95
96    fn min(&self, other: &Self) -> Self {
97        ScalarVector4(core::array::from_fn(|i| self.0[i].min(other.0[i])))
98    }
99
100    fn max(&self, other: &Self) -> Self {
101        ScalarVector4(core::array::from_fn(|i| self.0[i].max(other.0[i])))
102    }
103
104    fn clamp(&self, min: &Self, max: &Self) -> Self {
105        ScalarVector4(core::array::from_fn(|i| {
106            self.0[i].clamp(min.0[i], max.0[i])
107        }))
108    }
109}
110
111impl<T: Transcendental> VectorTranscendental<T, 4> for ScalarVector4<T> {
112    fn sqrt(&self) -> Self {
113        ScalarVector4(core::array::from_fn(|i| self.0[i].sqrt()))
114    }
115    fn exp(&self) -> Self {
116        ScalarVector4(core::array::from_fn(|i| self.0[i].exp()))
117    }
118    fn ln(&self) -> Self {
119        ScalarVector4(core::array::from_fn(|i| self.0[i].ln()))
120    }
121    fn sin(&self) -> Self {
122        ScalarVector4(core::array::from_fn(|i| self.0[i].sin()))
123    }
124    fn cos(&self) -> Self {
125        ScalarVector4(core::array::from_fn(|i| self.0[i].cos()))
126    }
127    fn tan(&self) -> Self {
128        ScalarVector4(core::array::from_fn(|i| self.0[i].tan()))
129    }
130}
131
132impl<T: Scalar + PartialEq> VectorMask<T, 4> for ScalarVector4<T> {
133    type Mask = ScalarVector4<T>;
134
135    fn eq(&self, other: &Self) -> Self::Mask {
136        ScalarVector4(core::array::from_fn(|i| {
137            if self.0[i] == other.0[i] {
138                T::ONE
139            } else {
140                T::ZERO
141            }
142        }))
143    }
144    fn ne(&self, other: &Self) -> Self::Mask {
145        ScalarVector4(core::array::from_fn(|i| {
146            if self.0[i] != other.0[i] {
147                T::ONE
148            } else {
149                T::ZERO
150            }
151        }))
152    }
153    fn gt(&self, other: &Self) -> Self::Mask {
154        ScalarVector4(core::array::from_fn(|i| {
155            if self.0[i] > other.0[i] {
156                T::ONE
157            } else {
158                T::ZERO
159            }
160        }))
161    }
162    fn ge(&self, other: &Self) -> Self::Mask {
163        ScalarVector4(core::array::from_fn(|i| {
164            if self.0[i] >= other.0[i] {
165                T::ONE
166            } else {
167                T::ZERO
168            }
169        }))
170    }
171    fn lt(&self, other: &Self) -> Self::Mask {
172        ScalarVector4(core::array::from_fn(|i| {
173            if self.0[i] < other.0[i] {
174                T::ONE
175            } else {
176                T::ZERO
177            }
178        }))
179    }
180    fn le(&self, other: &Self) -> Self::Mask {
181        ScalarVector4(core::array::from_fn(|i| {
182            if self.0[i] <= other.0[i] {
183                T::ONE
184            } else {
185                T::ZERO
186            }
187        }))
188    }
189    fn select(&self, other: &Self, mask: Self::Mask) -> Self {
190        ScalarVector4(core::array::from_fn(|i| {
191            if mask.0[i] != T::ZERO {
192                self.0[i]
193            } else {
194                other.0[i]
195            }
196        }))
197    }
198    fn all(mask: &Self::Mask) -> bool {
199        mask.0.iter().all(|&v| v != T::ZERO)
200    }
201}
202
203impl<T: Scalar> Default for ScalarVector4<T> {
204    fn default() -> Self {
205        ScalarVector4([T::ZERO; 4])
206    }
207}
208
209impl<T: Scalar> Vector<T, 1> for ScalarVector1<T> {
210    fn splat(value: T) -> Self {
211        ScalarVector1([value; 1])
212    }
213
214    fn load(slice: &[T]) -> Self {
215        let mut arr = [T::ZERO; 1];
216        arr.copy_from_slice(&slice[0..1]);
217        ScalarVector1(arr)
218    }
219
220    fn store(&self, slice: &mut [T]) {
221        slice[0..1].copy_from_slice(&self.0);
222    }
223
224    fn extract(&self, index: usize) -> T {
225        self.0[index]
226    }
227
228    fn insert(&self, index: usize, value: T) -> Self {
229        let mut arr = self.0;
230        arr[index] = value;
231        ScalarVector1(arr)
232    }
233
234    fn add(&self, other: &Self) -> Self {
235        let mut arr = [T::ZERO; 1];
236        arr[0] = self.0[0] + other.0[0];
237        ScalarVector1(arr)
238    }
239
240    fn sub(&self, other: &Self) -> Self {
241        let mut arr = [T::ZERO; 1];
242        arr[0] = self.0[0] - other.0[0];
243        ScalarVector1(arr)
244    }
245
246    fn mul(&self, other: &Self) -> Self {
247        let mut arr = [T::ZERO; 1];
248        arr[0] = self.0[0] * other.0[0];
249        ScalarVector1(arr)
250    }
251
252    fn div(&self, other: &Self) -> Self {
253        let mut arr = [T::ZERO; 1];
254        arr[0] = self.0[0] / other.0[0];
255        ScalarVector1(arr)
256    }
257
258    fn rem(&self, other: &Self) -> Self {
259        let mut arr = [T::ZERO; 1];
260        arr[0] = self.0[0] % other.0[0];
261        ScalarVector1(arr)
262    }
263
264    fn neg(&self) -> Self {
265        let mut arr = [T::ZERO; 1];
266        arr[0] = -self.0[0];
267        ScalarVector1(arr)
268    }
269
270    fn abs(&self) -> Self {
271        let mut arr = [T::ZERO; 1];
272        arr[0] = self.0[0].abs();
273        ScalarVector1(arr)
274    }
275
276    fn min(&self, other: &Self) -> Self {
277        let mut arr = [T::ZERO; 1];
278        arr[0] = self.0[0].min(other.0[0]);
279        ScalarVector1(arr)
280    }
281
282    fn max(&self, other: &Self) -> Self {
283        let mut arr = [T::ZERO; 1];
284        arr[0] = self.0[0].max(other.0[0]);
285        ScalarVector1(arr)
286    }
287
288    fn clamp(&self, min: &Self, max: &Self) -> Self {
289        let mut arr = [T::ZERO; 1];
290        arr[0] = self.0[0].clamp(min.0[0], max.0[0]);
291        ScalarVector1(arr)
292    }
293}
294
295impl<T: Transcendental> VectorTranscendental<T, 1> for ScalarVector1<T> {
296    fn sqrt(&self) -> Self {
297        let mut a = [T::ZERO; 1];
298        a[0] = self.0[0].sqrt();
299        ScalarVector1(a)
300    }
301    fn exp(&self) -> Self {
302        let mut a = [T::ZERO; 1];
303        a[0] = self.0[0].exp();
304        ScalarVector1(a)
305    }
306    fn ln(&self) -> Self {
307        let mut a = [T::ZERO; 1];
308        a[0] = self.0[0].ln();
309        ScalarVector1(a)
310    }
311    fn sin(&self) -> Self {
312        let mut a = [T::ZERO; 1];
313        a[0] = self.0[0].sin();
314        ScalarVector1(a)
315    }
316    fn cos(&self) -> Self {
317        let mut a = [T::ZERO; 1];
318        a[0] = self.0[0].cos();
319        ScalarVector1(a)
320    }
321    fn tan(&self) -> Self {
322        let mut a = [T::ZERO; 1];
323        a[0] = self.0[0].tan();
324        ScalarVector1(a)
325    }
326}
327
328impl<T: Scalar> Default for ScalarVector1<T> {
329    fn default() -> Self {
330        ScalarVector1([T::ZERO; 1])
331    }
332}
333
334// Implementations for ScalarVector2 and ScalarVector8 are similar (omitted for brevity)
335
336impl<T: Scalar> Vector<T, 2> for ScalarVector2<T> {
337    fn splat(value: T) -> Self {
338        ScalarVector2([value; 2])
339    }
340
341    fn load(slice: &[T]) -> Self {
342        let mut arr = [T::ZERO; 2];
343        arr.copy_from_slice(&slice[0..2]);
344        ScalarVector2(arr)
345    }
346
347    fn store(&self, slice: &mut [T]) {
348        slice[0..2].copy_from_slice(&self.0);
349    }
350
351    fn extract(&self, index: usize) -> T {
352        self.0[index]
353    }
354
355    fn insert(&self, index: usize, value: T) -> Self {
356        let mut arr = self.0;
357        arr[index] = value;
358        ScalarVector2(arr)
359    }
360
361    fn add(&self, other: &Self) -> Self {
362        ScalarVector2(core::array::from_fn(|i| self.0[i] + other.0[i]))
363    }
364
365    fn sub(&self, other: &Self) -> Self {
366        ScalarVector2(core::array::from_fn(|i| self.0[i] - other.0[i]))
367    }
368
369    fn mul(&self, other: &Self) -> Self {
370        ScalarVector2(core::array::from_fn(|i| self.0[i] * other.0[i]))
371    }
372
373    fn div(&self, other: &Self) -> Self {
374        ScalarVector2(core::array::from_fn(|i| self.0[i] / other.0[i]))
375    }
376
377    fn rem(&self, other: &Self) -> Self {
378        ScalarVector2(core::array::from_fn(|i| self.0[i] % other.0[i]))
379    }
380
381    fn neg(&self) -> Self {
382        ScalarVector2(core::array::from_fn(|i| -self.0[i]))
383    }
384
385    fn abs(&self) -> Self {
386        ScalarVector2(core::array::from_fn(|i| self.0[i].abs()))
387    }
388
389    fn min(&self, other: &Self) -> Self {
390        ScalarVector2(core::array::from_fn(|i| self.0[i].min(other.0[i])))
391    }
392
393    fn max(&self, other: &Self) -> Self {
394        ScalarVector2(core::array::from_fn(|i| self.0[i].max(other.0[i])))
395    }
396
397    fn clamp(&self, min: &Self, max: &Self) -> Self {
398        ScalarVector2(core::array::from_fn(|i| {
399            self.0[i].clamp(min.0[i], max.0[i])
400        }))
401    }
402}
403
404impl<T: Transcendental> VectorTranscendental<T, 2> for ScalarVector2<T> {
405    fn sqrt(&self) -> Self {
406        ScalarVector2(core::array::from_fn(|i| self.0[i].sqrt()))
407    }
408    fn exp(&self) -> Self {
409        ScalarVector2(core::array::from_fn(|i| self.0[i].exp()))
410    }
411    fn ln(&self) -> Self {
412        ScalarVector2(core::array::from_fn(|i| self.0[i].ln()))
413    }
414    fn sin(&self) -> Self {
415        ScalarVector2(core::array::from_fn(|i| self.0[i].sin()))
416    }
417    fn cos(&self) -> Self {
418        ScalarVector2(core::array::from_fn(|i| self.0[i].cos()))
419    }
420    fn tan(&self) -> Self {
421        ScalarVector2(core::array::from_fn(|i| self.0[i].tan()))
422    }
423}
424
425impl<T: Scalar> Default for ScalarVector2<T> {
426    fn default() -> Self {
427        ScalarVector2([T::ZERO; 2])
428    }
429}
430
431// -----------------------------------------------------------------------------
432// Operator implementations
433// -----------------------------------------------------------------------------
434
435use std::ops::{
436    Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign,
437};
438
439impl<T: Scalar> Add for ScalarVector4<T> {
440    type Output = Self;
441
442    fn add(self, rhs: Self) -> Self {
443        ScalarVector4(core::array::from_fn(|i| self.0[i] + rhs.0[i]))
444    }
445}
446
447impl<T: Scalar> Sub for ScalarVector4<T> {
448    type Output = Self;
449
450    fn sub(self, rhs: Self) -> Self {
451        ScalarVector4(core::array::from_fn(|i| self.0[i] - rhs.0[i]))
452    }
453}
454
455impl<T: Scalar> Mul for ScalarVector4<T> {
456    type Output = Self;
457
458    fn mul(self, rhs: Self) -> Self {
459        ScalarVector4(core::array::from_fn(|i| self.0[i] * rhs.0[i]))
460    }
461}
462
463impl<T: Scalar> Div for ScalarVector4<T> {
464    type Output = Self;
465
466    fn div(self, rhs: Self) -> Self {
467        ScalarVector4(core::array::from_fn(|i| self.0[i] / rhs.0[i]))
468    }
469}
470
471impl<T: Scalar> Rem for ScalarVector4<T> {
472    type Output = Self;
473
474    fn rem(self, rhs: Self) -> Self {
475        ScalarVector4(core::array::from_fn(|i| self.0[i] % rhs.0[i]))
476    }
477}
478
479impl<T: Scalar> Neg for ScalarVector4<T> {
480    type Output = Self;
481
482    fn neg(self) -> Self {
483        ScalarVector4(core::array::from_fn(|i| -self.0[i]))
484    }
485}
486
487impl<T: Scalar> Add for ScalarVector2<T> {
488    type Output = Self;
489
490    fn add(self, rhs: Self) -> Self {
491        ScalarVector2(core::array::from_fn(|i| self.0[i] + rhs.0[i]))
492    }
493}
494
495impl<T: Scalar> Sub for ScalarVector2<T> {
496    type Output = Self;
497
498    fn sub(self, rhs: Self) -> Self {
499        ScalarVector2(core::array::from_fn(|i| self.0[i] - rhs.0[i]))
500    }
501}
502
503impl<T: Scalar> Mul for ScalarVector2<T> {
504    type Output = Self;
505
506    fn mul(self, rhs: Self) -> Self {
507        ScalarVector2(core::array::from_fn(|i| self.0[i] * rhs.0[i]))
508    }
509}
510
511impl<T: Scalar> Div for ScalarVector2<T> {
512    type Output = Self;
513
514    fn div(self, rhs: Self) -> Self {
515        ScalarVector2(core::array::from_fn(|i| self.0[i] / rhs.0[i]))
516    }
517}
518
519impl<T: Scalar> Rem for ScalarVector2<T> {
520    type Output = Self;
521
522    fn rem(self, rhs: Self) -> Self {
523        ScalarVector2(core::array::from_fn(|i| self.0[i] % rhs.0[i]))
524    }
525}
526
527impl<T: Scalar> Neg for ScalarVector2<T> {
528    type Output = Self;
529
530    fn neg(self) -> Self {
531        ScalarVector2(core::array::from_fn(|i| -self.0[i]))
532    }
533}
534
535impl<T: Scalar> Add for ScalarVector1<T> {
536    type Output = Self;
537
538    fn add(self, rhs: Self) -> Self {
539        let mut arr = [T::ZERO; 1];
540        arr[0] = self.0[0] + rhs.0[0];
541        ScalarVector1(arr)
542    }
543}
544
545impl<T: Scalar> Sub for ScalarVector1<T> {
546    type Output = Self;
547
548    fn sub(self, rhs: Self) -> Self {
549        let mut arr = [T::ZERO; 1];
550        arr[0] = self.0[0] - rhs.0[0];
551        ScalarVector1(arr)
552    }
553}
554
555impl<T: Scalar> Mul for ScalarVector1<T> {
556    type Output = Self;
557
558    fn mul(self, rhs: Self) -> Self {
559        let mut arr = [T::ZERO; 1];
560        arr[0] = self.0[0] * rhs.0[0];
561        ScalarVector1(arr)
562    }
563}
564
565impl<T: Scalar> Div for ScalarVector1<T> {
566    type Output = Self;
567
568    fn div(self, rhs: Self) -> Self {
569        let mut arr = [T::ZERO; 1];
570        arr[0] = self.0[0] / rhs.0[0];
571        ScalarVector1(arr)
572    }
573}
574
575impl<T: Scalar> Rem for ScalarVector1<T> {
576    type Output = Self;
577
578    fn rem(self, rhs: Self) -> Self {
579        let mut arr = [T::ZERO; 1];
580        arr[0] = self.0[0] % rhs.0[0];
581        ScalarVector1(arr)
582    }
583}
584
585impl<T: Scalar> Neg for ScalarVector1<T> {
586    type Output = Self;
587
588    fn neg(self) -> Self {
589        let mut arr = [T::ZERO; 1];
590        arr[0] = -self.0[0];
591        ScalarVector1(arr)
592    }
593}
594
595// -----------------------------------------------------------------------------
596// Scalar operations
597// -----------------------------------------------------------------------------
598
599impl<T: Scalar> Mul<T> for ScalarVector4<T> {
600    type Output = Self;
601
602    fn mul(self, rhs: T) -> Self {
603        self * Self::splat(rhs)
604    }
605}
606
607impl<T: Scalar> Div<T> for ScalarVector4<T> {
608    type Output = Self;
609
610    fn div(self, rhs: T) -> Self {
611        self / Self::splat(rhs)
612    }
613}
614
615impl<T: Scalar> Add<T> for ScalarVector4<T> {
616    type Output = Self;
617
618    fn add(self, rhs: T) -> Self {
619        self + Self::splat(rhs)
620    }
621}
622
623impl<T: Scalar> Sub<T> for ScalarVector4<T> {
624    type Output = Self;
625
626    fn sub(self, rhs: T) -> Self {
627        self - Self::splat(rhs)
628    }
629}
630
631impl<T: Scalar> Rem<T> for ScalarVector4<T> {
632    type Output = Self;
633
634    fn rem(self, rhs: T) -> Self {
635        self % Self::splat(rhs)
636    }
637}
638
639// Compound assignment operators for ScalarVector4
640
641impl<T: Scalar> AddAssign for ScalarVector4<T> {
642    fn add_assign(&mut self, rhs: Self) {
643        *self = *self + rhs;
644    }
645}
646
647impl<T: Scalar> SubAssign for ScalarVector4<T> {
648    fn sub_assign(&mut self, rhs: Self) {
649        *self = *self - rhs;
650    }
651}
652
653impl<T: Scalar> MulAssign for ScalarVector4<T> {
654    fn mul_assign(&mut self, rhs: Self) {
655        *self = *self * rhs;
656    }
657}
658
659impl<T: Scalar> DivAssign for ScalarVector4<T> {
660    fn div_assign(&mut self, rhs: Self) {
661        *self = *self / rhs;
662    }
663}
664
665impl<T: Scalar> RemAssign for ScalarVector4<T> {
666    fn rem_assign(&mut self, rhs: Self) {
667        *self = *self % rhs;
668    }
669}
670
671impl<T: Scalar> Mul<T> for ScalarVector2<T> {
672    type Output = Self;
673
674    fn mul(self, rhs: T) -> Self {
675        self * Self::splat(rhs)
676    }
677}
678
679impl<T: Scalar> Div<T> for ScalarVector2<T> {
680    type Output = Self;
681
682    fn div(self, rhs: T) -> Self {
683        self / Self::splat(rhs)
684    }
685}
686
687impl<T: Scalar> Add<T> for ScalarVector2<T> {
688    type Output = Self;
689
690    fn add(self, rhs: T) -> Self {
691        self + Self::splat(rhs)
692    }
693}
694
695impl<T: Scalar> Sub<T> for ScalarVector2<T> {
696    type Output = Self;
697
698    fn sub(self, rhs: T) -> Self {
699        self - Self::splat(rhs)
700    }
701}
702
703impl<T: Scalar> Rem<T> for ScalarVector2<T> {
704    type Output = Self;
705
706    fn rem(self, rhs: T) -> Self {
707        self % Self::splat(rhs)
708    }
709}
710
711impl<T: Scalar> AddAssign for ScalarVector2<T> {
712    fn add_assign(&mut self, rhs: Self) {
713        *self = *self + rhs;
714    }
715}
716
717impl<T: Scalar> SubAssign for ScalarVector2<T> {
718    fn sub_assign(&mut self, rhs: Self) {
719        *self = *self - rhs;
720    }
721}
722
723impl<T: Scalar> MulAssign for ScalarVector2<T> {
724    fn mul_assign(&mut self, rhs: Self) {
725        *self = *self * rhs;
726    }
727}
728
729impl<T: Scalar> DivAssign for ScalarVector2<T> {
730    fn div_assign(&mut self, rhs: Self) {
731        *self = *self / rhs;
732    }
733}
734
735impl<T: Scalar> RemAssign for ScalarVector2<T> {
736    fn rem_assign(&mut self, rhs: Self) {
737        *self = *self % rhs;
738    }
739}
740
741impl<T: Scalar> Mul<T> for ScalarVector1<T> {
742    type Output = Self;
743
744    fn mul(self, rhs: T) -> Self {
745        self * Self::splat(rhs)
746    }
747}
748
749impl<T: Scalar> Div<T> for ScalarVector1<T> {
750    type Output = Self;
751
752    fn div(self, rhs: T) -> Self {
753        self / Self::splat(rhs)
754    }
755}
756
757impl<T: Scalar> Add<T> for ScalarVector1<T> {
758    type Output = Self;
759
760    fn add(self, rhs: T) -> Self {
761        self + Self::splat(rhs)
762    }
763}
764
765impl<T: Scalar> Sub<T> for ScalarVector1<T> {
766    type Output = Self;
767
768    fn sub(self, rhs: T) -> Self {
769        self - Self::splat(rhs)
770    }
771}
772
773impl<T: Scalar> Rem<T> for ScalarVector1<T> {
774    type Output = Self;
775
776    fn rem(self, rhs: T) -> Self {
777        self % Self::splat(rhs)
778    }
779}
780
781impl<T: Scalar> AddAssign for ScalarVector1<T> {
782    fn add_assign(&mut self, rhs: Self) {
783        *self = *self + rhs;
784    }
785}
786
787impl<T: Scalar> SubAssign for ScalarVector1<T> {
788    fn sub_assign(&mut self, rhs: Self) {
789        *self = *self - rhs;
790    }
791}
792
793impl<T: Scalar> MulAssign for ScalarVector1<T> {
794    fn mul_assign(&mut self, rhs: Self) {
795        *self = *self * rhs;
796    }
797}
798
799impl<T: Scalar> DivAssign for ScalarVector1<T> {
800    fn div_assign(&mut self, rhs: Self) {
801        *self = *self / rhs;
802    }
803}
804
805impl<T: Scalar> RemAssign for ScalarVector1<T> {
806    fn rem_assign(&mut self, rhs: Self) {
807        *self = *self % rhs;
808    }
809}
810
811// -----------------------------------------------------------------------------
812// Tests
813// -----------------------------------------------------------------------------
814
815#[cfg(test)]
816mod tests {
817    use super::*;
818
819    #[test]
820    fn test_scalar_vector4_basic() {
821        let a = ScalarVector4::<f32>::splat(2.0);
822        let b = ScalarVector4::<f32>::splat(3.0);
823        let c = a + b;
824        assert_eq!(c.extract(0), 5.0);
825        assert_eq!(c.extract(3), 5.0);
826    }
827
828    #[test]
829    fn test_scalar_vector4_math() {
830        let a = ScalarVector4::<f32>::splat(0.0);
831        let b = a.sin();
832        assert_eq!(b.extract(0), 0.0);
833
834        let c = ScalarVector4::<f32>::splat(1.0);
835        let d = c.sqrt();
836        assert_eq!(d.extract(0), 1.0);
837    }
838
839    #[test]
840    fn test_scalar_vector2() {
841        let a = ScalarVector2::<f64>::splat(5.0);
842        let b = ScalarVector2::<f64>::splat(2.0);
843        let c = a * b;
844        assert_eq!(c.extract(0), 10.0);
845        assert_eq!(c.extract(1), 10.0);
846    }
847}