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