rill_core/math/vector/traits.rs
1use core::fmt;
2use core::ops::{Add, Div, Mul, Neg, Rem, Sub};
3
4use crate::Scalar;
5use crate::Transcendental;
6
7/// Core trait for vector types (basic operations).
8///
9/// Parameterised by element type `T: Scalar` and lane width `N`.
10pub trait Vector<T: Scalar, const N: usize>:
11 Copy
12 + Clone
13 + Send
14 + Sync
15 + 'static
16 + Default
17 + PartialEq
18 + fmt::Debug
19 + Add<Output = Self>
20 + Sub<Output = Self>
21 + Mul<Output = Self>
22 + Div<Output = Self>
23 + Rem<Output = Self>
24 + Neg<Output = Self>
25{
26 /// Construct a vector with all lanes set to the same value.
27 fn splat(value: T) -> Self;
28 /// Load a vector from a slice (panics if slice is too short).
29 fn load(slice: &[T]) -> Self;
30 /// Store the vector lanes into a slice.
31 fn store(&self, slice: &mut [T]);
32 /// Extract the value at the given lane index.
33 fn extract(&self, index: usize) -> T;
34 /// Return a new vector with the value at the given lane replaced.
35 fn insert(&self, index: usize, value: T) -> Self;
36
37 /// Lane-wise addition.
38 fn add(&self, other: &Self) -> Self;
39 /// Lane-wise subtraction.
40 fn sub(&self, other: &Self) -> Self;
41 /// Lane-wise multiplication.
42 fn mul(&self, other: &Self) -> Self;
43 /// Lane-wise division.
44 fn div(&self, other: &Self) -> Self;
45 /// Lane-wise remainder.
46 fn rem(&self, other: &Self) -> Self;
47 /// Lane-wise negation.
48 fn neg(&self) -> Self;
49
50 /// Lane-wise absolute value.
51 fn abs(&self) -> Self;
52 /// Lane-wise minimum.
53 fn min(&self, other: &Self) -> Self;
54 /// Lane-wise maximum.
55 fn max(&self, other: &Self) -> Self;
56 /// Lane-wise clamp to the inclusive range `[min, max]`.
57 fn clamp(&self, min: &Self, max: &Self) -> Self;
58}
59
60/// Trait for vector types with transcendental operations.
61///
62/// Only available for `T: Transcendental` (f32, f64).
63pub trait VectorTranscendental<T: Transcendental, const N: usize>: Vector<T, N> {
64 /// Lane-wise square root.
65 fn sqrt(&self) -> Self;
66 /// Lane-wise exponential (e^x).
67 fn exp(&self) -> Self;
68 /// Lane-wise natural logarithm.
69 fn ln(&self) -> Self;
70 /// Lane-wise sine (input in radians).
71 fn sin(&self) -> Self;
72 /// Lane-wise cosine (input in radians).
73 fn cos(&self) -> Self;
74 /// Lane-wise tangent (input in radians).
75 fn tan(&self) -> Self;
76}
77
78/// Scalar-vector arithmetic operations.
79///
80/// Each method broadcasts the scalar across all lanes.
81pub trait VectorScalarOps<T: Scalar, const N: usize> {
82 /// Add a scalar to every lane.
83 fn add_scalar(&self, scalar: T) -> Self;
84 /// Subtract a scalar from every lane.
85 fn sub_scalar(&self, scalar: T) -> Self;
86 /// Multiply every lane by a scalar.
87 fn mul_scalar(&self, scalar: T) -> Self;
88 /// Divide every lane by a scalar.
89 fn div_scalar(&self, scalar: T) -> Self;
90 /// Compute the remainder of every lane divided by a scalar.
91 fn rem_scalar(&self, scalar: T) -> Self;
92}
93
94/// Horizontal reduction operations (vector → scalar).
95pub trait VectorReduce<T: Scalar, const N: usize> {
96 /// Sum of all lanes.
97 fn horizontal_sum(&self) -> T;
98 /// Product of all lanes.
99 fn horizontal_product(&self) -> T;
100 /// Minimum value across all lanes.
101 fn horizontal_min(&self) -> T;
102 /// Maximum value across all lanes.
103 fn horizontal_max(&self) -> T;
104 /// Arithmetic mean of all lanes.
105 fn horizontal_mean(&self) -> T;
106}
107
108/// Vector comparison and masking operations.
109///
110/// Produces a bitmask (or SIMD mask) from lane-wise comparisons,
111/// and allows selecting between two vectors based on a mask.
112pub trait VectorMask<T: Scalar, const N: usize> {
113 /// The mask type (e.g. `i32` bitmask or SIMD mask register).
114 type Mask;
115
116 /// Lane-wise equality comparison.
117 fn eq(&self, other: &Self) -> Self::Mask;
118 /// Lane-wise inequality comparison.
119 fn ne(&self, other: &Self) -> Self::Mask;
120 /// Lane-wise greater-than comparison.
121 fn gt(&self, other: &Self) -> Self::Mask;
122 /// Lane-wise greater-or-equal comparison.
123 fn ge(&self, other: &Self) -> Self::Mask;
124 /// Lane-wise less-than comparison.
125 fn lt(&self, other: &Self) -> Self::Mask;
126 /// Lane-wise less-or-equal comparison.
127 fn le(&self, other: &Self) -> Self::Mask;
128 /// Select lanes from `self` (where mask is truthy) or `other`.
129 fn select(&self, other: &Self, mask: Self::Mask) -> Self;
130 /// Returns true if all mask lanes are set.
131 fn all(mask: &Self::Mask) -> bool;
132}