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
use core::ops::{Add, Div, Mul, Neg, Rem, Sub};
use std::fmt::Debug;

pub trait Scalar:
    'static
    + Sized
    + Copy
    + Debug
    + Zero
    + One
    + Add<Output = Self>
    + Sub<Output = Self>
    + Mul<Output = Self>
    + Div<Output = Self>
    + PartialOrd
{
}

/// A vector over the scalar field `Self::Scalar`.
pub trait Vector:
    'static
    + Sized
    + Copy
    + Splat<Self::Scalar>
    + Map<Self::Scalar>
    + ZipMap<Self::Scalar>
    + Fold<Self::Scalar>
    + Ones
    + Zero
    + VectorArithmetic<Self::Scalar>
    + LatticeOrder
    + Min<Self::Scalar>
    + Max<Self::Scalar>
    + PartialEq
{
    type Scalar: Scalar;

    #[inline]
    fn is_positive(self) -> bool {
        self.with_lattice_ord() > Self::ZERO.with_lattice_ord()
    }
}

pub trait Vector2: Vector + From<[Self::Scalar; 2]> {
    fn x(self) -> Self::Scalar;
    fn y(self) -> Self::Scalar;

    #[inline]
    fn as_array<T>(self) -> [T; 2]
    where
        Self::Scalar: PrimitiveCast<T>,
    {
        [self.x().cast(), self.y().cast()]
    }
}

pub trait Vector3: Vector + From<[Self::Scalar; 3]> {
    fn x(self) -> Self::Scalar;
    fn y(self) -> Self::Scalar;
    fn z(self) -> Self::Scalar;

    #[inline]
    fn as_array<T>(self) -> [T; 3]
    where
        Self::Scalar: PrimitiveCast<T>,
    {
        [self.x().cast(), self.y().cast(), self.z().cast()]
    }
}

pub trait VectorArithmetic<T>:
    Sized
    + Add<T, Output = Self>
    + Add<Output = Self>
    + Div<T, Output = Self>
    + Div<Self, Output = Self>
    + Mul<T, Output = Self>
    + Mul<Self, Output = Self>
    + Rem<T, Output = Self>
    + Rem<Self, Output = Self>
    + Sub<T, Output = Self>
    + Sub<Output = Self>
{
}

pub trait Splat<T> {
    /// Creates a vector with all components equal to `value`.
    fn splat(value: T) -> Self;
}

pub trait Map<T> {
    /// Applies `f` to all components, returning the results as `Self`.
    fn map(self, f: impl Fn(T) -> T) -> Self;
}

pub trait ZipMap<T> {
    /// Zips the components of `self` and `other`, applying `f`, and returning the results as `Self`.
    fn zip_map(self, other: Self, f: impl Fn(T, T) -> T) -> Self;
}

pub trait Fold<T> {
    fn fold<Out>(self, init: Out, f: impl Fn(T, Out) -> Out) -> Out;
}

pub trait Min<T> {
    /// Returns the least component.
    fn min_element(self) -> T;
}

pub trait Max<T> {
    /// Returns the greatest component.
    fn max_element(self) -> T;
}

pub trait Zero {
    const ZERO: Self;
}

pub trait One {
    const ONE: Self;
}

pub trait Ones {
    /// A vector of all ones.
    const ONES: Self;
}

/// A trait denoting that the `PartialOrd` for `Self::LatticeVector` is consistent with the
/// [lattice](https://en.wikipedia.org/wiki/Lattice_(order)) structure.
pub trait LatticeOrder {
    type LatticeVector: PartialOrd;

    fn with_lattice_ord(self) -> Self::LatticeVector;

    fn least_upper_bound(self, other: Self) -> Self;
    fn greatest_lower_bound(self, other: Self) -> Self;
}

/// A newtype that can be used to override the `PartialOrd` implementation of `T` so that it is consistent with `LatticeOrder`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct WithLatticeOrd<T>(pub T);

pub trait Bounded {
    const MIN: Self;
    const MAX: Self;
}

mod signed_vector {
    use super::*;

    pub trait SignedVector: Vector + Abs + Neg {}

    pub trait Abs {
        fn abs(self) -> Self;
    }
}
pub use signed_vector::*;

mod integer_vector {
    use super::*;

    use core::ops::{BitAnd, BitOr, BitXor, Not, Shl, Shr};
    use std::convert::TryInto;

    pub trait IntegerScalar:
        Scalar
        + TryInto<u64>
        + PrimitiveCast<u8>
        + PrimitiveCast<u16>
        + PrimitiveCast<u32>
        + PrimitiveCast<u64>
    {
        fn is_power_of_two(self) -> bool;
        fn trailing_zeros(self) -> Self;
    }

    /// A `Vector<T>` where `T` is some integer.
    ///
    /// This enables `Eq`, bitwise logical operations, and bit shifting.
    pub trait IntegerVector:
        Vector<Scalar = Self::IntScalar>
        + Eq
        + ScalarBitwiseLogic<Self::Scalar>
        + VectorBitwiseLogic
        + AllShiftOps<Self::IntScalar>
    {
        type IntScalar: IntegerScalar;

        #[inline]
        fn all_dimensions_are_powers_of_two(self) -> bool {
            self.fold(true, |c, out| out && c.is_power_of_two())
        }
    }

    pub trait ScalarBitwiseLogic<Rhs>:
        Sized + BitAnd<Rhs, Output = Self> + BitOr<Rhs, Output = Self> + BitXor<Rhs, Output = Self>
    {
    }

    pub trait VectorBitwiseLogic:
        Sized
        + BitAnd<Self, Output = Self>
        + BitOr<Self, Output = Self>
        + BitXor<Self, Output = Self>
        + Not<Output = Self>
    {
    }

    pub trait ShiftOps<Rhs>: Sized + Shl<Rhs, Output = Self> + Shr<Rhs, Output = Self> {}

    // Only support shifting by unsigned primitives; signed shifting has surprising behavior.
    // HOWEVER we do support shifting by `Self` as it makes generic code much simpler.
    pub trait AllShiftOps<T>:
        ShiftOps<Self>
        + ShiftOps<T>
        + ShiftOps<Self::UintVec>
        + ShiftOps<u8>
        + ShiftOps<u16>
        + ShiftOps<u32>
    {
        type UintVec;
    }

    /// Primitive integer casts.
    pub trait PrimitiveCast<T> {
        fn cast(self) -> T;
    }
}
pub use integer_vector::*;

mod float_vector {
    use super::*;

    pub trait FloatVector: Vector + RoundingOps + CastInteger {}

    impl<T> FloatVector for T where T: Vector + RoundingOps + CastInteger {}

    pub trait RoundingOps {
        fn floor(self) -> Self;
        fn ceil(self) -> Self;
    }

    pub trait CastInteger {
        type Int;
        fn cast_int(self) -> Self::Int;
    }
}
pub use float_vector::*;

mod scalar_impl {
    use super::*;

    macro_rules! impl_integer_scalar {
        ($t:ident) => {
            impl Scalar for $t {}

            impl IntegerScalar for $t {
                #[inline]
                fn is_power_of_two(self) -> bool {
                    PrimitiveCast::<u32>::cast(self).is_power_of_two()
                }
                #[inline]
                fn trailing_zeros(self) -> Self {
                    self.trailing_zeros() as $t
                }
            }

            impl PrimitiveCast<u8> for $t {
                #[inline]
                fn cast(self) -> u8 {
                    self as u8
                }
            }
            impl PrimitiveCast<u16> for $t {
                #[inline]
                fn cast(self) -> u16 {
                    self as u16
                }
            }
            impl PrimitiveCast<u32> for $t {
                #[inline]
                fn cast(self) -> u32 {
                    self as u32
                }
            }
            impl PrimitiveCast<u64> for $t {
                #[inline]
                fn cast(self) -> u64 {
                    self as u64
                }
            }

            impl Zero for $t {
                const ZERO: $t = 0;
            }
            impl One for $t {
                const ONE: $t = 1;
            }
        };
    }

    macro_rules! impl_float_scalar {
        ($t:ty) => {
            impl Scalar for $t {}

            impl Zero for $t {
                const ZERO: $t = 0.0;
            }
            impl One for $t {
                const ONE: $t = 1.0;
            }
        };
    }

    impl_integer_scalar!(u8);
    impl_integer_scalar!(u16);
    impl_integer_scalar!(u32);
    impl_integer_scalar!(i8);
    impl_integer_scalar!(i16);
    impl_integer_scalar!(i32);

    impl_float_scalar!(f32);
    impl_float_scalar!(f64);

    macro_rules! impl_bounded {
        ($t:ident) => {
            impl Bounded for $t {
                const MIN: Self = $t::MIN;
                const MAX: Self = $t::MAX;
            }
        };
    }

    impl_bounded!(u8);
    impl_bounded!(u16);
    impl_bounded!(u32);
    impl_bounded!(i8);
    impl_bounded!(i16);
    impl_bounded!(i32);
}