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
use std::{
    marker::PhantomData,
    ops::{Index, IndexMut},
};

use crate::traits::Simd;

use super::{
    conversion::{simd_container_flat_slice, simd_container_flat_slice_mut},
    packed::PackedMxN,
};

#[doc(hidden)]
pub trait AccessStrategy {
    fn flat_to_packed(x: usize, y: usize) -> (usize, usize);
}

#[derive(Clone, Debug)]
#[doc(hidden)]
pub struct Rows;

#[derive(Clone, Debug)]
#[doc(hidden)]
pub struct Columns;

impl AccessStrategy for Rows {
    #[inline]
    fn flat_to_packed(x: usize, y: usize) -> (usize, usize) { (x, y) }
}

impl AccessStrategy for Columns {
    #[inline]
    fn flat_to_packed(x: usize, y: usize) -> (usize, usize) { (y, x) }
}

/// A dynamic (heap allocated) matrix with one axis aligned for fast and safe SIMD access that
/// also provides a flat view on its data.
///
/// You can use [`MatrixD`] when you need to deal with multiple SIMD vectors, but
/// want them arranged in a compact cache-friendly manner. Internally this struct is backed by a
/// continuous vector of aligned vectors, and dynamically sliced according to row / column access.
///
/// # Example
///
/// ```rust
/// use packed_simd::*;
/// use simd_aligned::*;
///
/// // Create a matrix of height 10x`f32` and width 5x`f32`, optimized for row access.
/// let mut m = MatrixD::<f32s, Rows>::with_dimension(10, 5);
///
/// // A `RowOptimized` matrix provides `row` access. In this example, you could query
/// // rows `0` to `9` and receive vectors that can hold a total of at least `5` elements.
/// let _ = m.row(4);
///
/// // But accessing columns doesn't work, as there is no continuous view in memory.
/// // m.column(3); --> panic!
///
/// // However, you can always get a flat view of the matrix, for "scalar-speed"
/// // query and update all elements:
/// let mut m_flat = m.flat_mut();
///
/// m_flat[(2, 4)] = 42_f32;
/// ```
#[derive(Clone, Debug)]
pub struct MatrixD<T, A>
where
    T: Simd + Default + Clone,
    A: AccessStrategy,
{
    pub(crate) simd_rows: PackedMxN<T>,
    phantom: PhantomData<A>,
}

impl<T, O> MatrixD<T, O>
where
    T: Simd + Default + Clone,
    O: AccessStrategy,
{
    /// Creates a new [`MatrixD`] with the given dimension.
    #[inline]
    pub fn with_dimension(width: usize, height: usize) -> Self {
        let (x, y) = O::flat_to_packed(width, height);

        Self {
            simd_rows: PackedMxN::with(T::default(), x, y),
            phantom: PhantomData,
        }
    }

    /// Returns the size as (`rows`, `columns`).
    pub fn dimension(&self) -> (usize, usize) { O::flat_to_packed(self.simd_rows.rows, self.simd_rows.row_length) }

    /// Provides a flat, immutable view of the contained data.
    #[inline]
    pub fn flat(&self) -> MatrixFlat<'_, T, O> {
        MatrixFlat {
            matrix: self,
            phantom: PhantomData,
        }
    }

    /// Provides a flat mutable view of the contained data.
    #[inline]
    pub fn flat_mut(&mut self) -> MatrixFlatMut<'_, T, O> {
        MatrixFlatMut {
            matrix: self,
            phantom: PhantomData,
        }
    }
}

impl<T> MatrixD<T, Rows>
where
    T: Simd + Default + Clone,
{
    #[inline]
    pub fn row(&self, i: usize) -> &[T] {
        let range = self.simd_rows.range_for_row(i);
        &self.simd_rows.data[range]
    }

    #[inline]
    pub fn row_iter(&self) -> Matrix2DIter<'_, T, Rows> { Matrix2DIter { matrix: self, index: 0 } }

    #[inline]
    pub fn row_mut(&mut self, i: usize) -> &mut [T] {
        let range = self.simd_rows.range_for_row(i);
        &mut self.simd_rows.data[range]
    }

    #[inline]
    pub fn row_as_flat(&self, i: usize) -> &[T::Element] {
        let row = self.row(i);
        simd_container_flat_slice(row, self.simd_rows.row_length)
    }

    #[inline]
    pub fn row_as_flat_mut(&mut self, i: usize) -> &mut [T::Element] {
        let length = self.simd_rows.row_length;
        let row = self.row_mut(i);
        simd_container_flat_slice_mut(row, length)
    }
}

impl<T> MatrixD<T, Columns>
where
    T: Simd + Default + Clone,
{
    #[inline]
    pub fn column(&self, i: usize) -> &[T] {
        let range = self.simd_rows.range_for_row(i);
        &self.simd_rows.data[range]
    }

    #[inline]
    pub fn column_iter(&self) -> Matrix2DIter<'_, T, Columns> { Matrix2DIter { matrix: self, index: 0 } }

    #[inline]
    pub fn column_mut(&mut self, i: usize) -> &mut [T] {
        let range = self.simd_rows.range_for_row(i);
        &mut self.simd_rows.data[range]
    }

    #[inline]
    pub fn column_as_flat(&self, i: usize) -> &[T::Element] {
        let column = self.column(i);
        simd_container_flat_slice(column, self.simd_rows.row_length)
    }

    #[inline]
    pub fn column_as_flat_mut(&mut self, i: usize) -> &mut [T::Element] {
        let length = self.simd_rows.row_length;
        let column = self.column_mut(i);
        simd_container_flat_slice_mut(column, length)
    }
}

/// Produced by [`MatrixD::flat`], this allow for flat matrix access.
pub struct MatrixFlat<'a, T: 'a, A: 'a>
where
    T: Simd + Default + Clone,
    A: AccessStrategy,
{
    matrix: &'a MatrixD<T, A>,
    phantom: PhantomData<A>, // Do we actually need this / is there a better way?
}

/// Provided by [`MatrixD::flat_mut`], this allow for flat, mutable matrix access.
pub struct MatrixFlatMut<'a, T: 'a, A: 'a>
where
    T: Simd + Default + Clone,
    A: AccessStrategy,
{
    matrix: &'a mut MatrixD<T, A>,
    phantom: PhantomData<A>, // Do we actually need this / is there a better way?
}

impl<'a, T, A> Index<(usize, usize)> for MatrixFlat<'a, T, A>
where
    T: Simd + Default + Clone,
    A: AccessStrategy,
{
    type Output = T::Element;

    #[inline]
    fn index(&self, index: (usize, usize)) -> &Self::Output {
        let (row, x) = A::flat_to_packed(index.0, index.1);
        let row_slice = self.matrix.simd_rows.row_as_flat(row);

        &row_slice[x]
    }
}

impl<'a, T, A> Index<(usize, usize)> for MatrixFlatMut<'a, T, A>
where
    T: Simd + Default + Clone,
    A: AccessStrategy,
{
    type Output = T::Element;

    #[inline]
    fn index(&self, index: (usize, usize)) -> &Self::Output {
        let (row, x) = A::flat_to_packed(index.0, index.1);
        let row_slice = self.matrix.simd_rows.row_as_flat(row);

        &row_slice[x]
    }
}

impl<'a, T, A> IndexMut<(usize, usize)> for MatrixFlatMut<'a, T, A>
where
    T: Simd + Default + Clone,
    A: AccessStrategy,
{
    #[inline]
    fn index_mut(&mut self, index: (usize, usize)) -> &mut Self::Output {
        let (row, x) = A::flat_to_packed(index.0, index.1);
        let row_slice = self.matrix.simd_rows.row_as_flat_mut(row);

        &mut row_slice[x]
    }
}

/// Basic iterator struct to go over matrix
#[derive(Clone, Debug)]
pub struct Matrix2DIter<'a, T: 'a, O: 'a>
where
    T: Simd + Default + Clone,
    O: AccessStrategy,
{
    /// Reference to the matrix we iterate over.
    pub(crate) matrix: &'a MatrixD<T, O>,

    /// Current index of vector iteration.
    pub(crate) index: usize,
}

impl<'a, T, O> Iterator for Matrix2DIter<'a, T, O>
where
    T: Simd + Default + Clone,
    O: AccessStrategy,
{
    type Item = &'a [T];

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.index >= self.matrix.simd_rows.rows {
            None
        } else {
            let range = self.matrix.simd_rows.range_for_row(self.index);
            self.index += 1;
            Some(&self.matrix.simd_rows.data[range])
        }
    }
}

#[cfg(test)]
mod test {
    use super::{Columns, MatrixD, Rows};
    use crate::*;

    #[test]
    fn allocation_size() {
        let m_1_1_r = MatrixD::<f32x4, Rows>::with_dimension(1, 1);
        let m_1_1_c = MatrixD::<f32x4, Columns>::with_dimension(1, 1);

        let m_5_5_r = MatrixD::<f32x4, Rows>::with_dimension(5, 5);
        let m_5_5_c = MatrixD::<f32x4, Columns>::with_dimension(5, 5);

        let m_1_4_r = MatrixD::<f32x4, Rows>::with_dimension(1, 4);
        let m_4_1_c = MatrixD::<f32x4, Columns>::with_dimension(4, 1);

        let m_4_1_r = MatrixD::<f32x4, Rows>::with_dimension(4, 1);
        let m_1_4_c = MatrixD::<f32x4, Columns>::with_dimension(1, 4);

        assert_eq!(m_1_1_r.simd_rows.data.len(), 1);
        assert_eq!(m_1_1_c.simd_rows.data.len(), 1);

        assert_eq!(m_5_5_r.simd_rows.data.len(), 10);
        assert_eq!(m_5_5_c.simd_rows.data.len(), 10);

        assert_eq!(m_1_4_r.simd_rows.data.len(), 1);
        assert_eq!(m_4_1_c.simd_rows.data.len(), 1);

        assert_eq!(m_4_1_r.simd_rows.data.len(), 4);
        assert_eq!(m_1_4_c.simd_rows.data.len(), 4);
    }

    #[test]
    fn access() {
        let mut m_5_5_r = MatrixD::<f32x4, Rows>::with_dimension(5, 5);
        let mut m_5_5_c = MatrixD::<f32x4, Columns>::with_dimension(5, 5);

        assert_eq!(m_5_5_c.column(0).len(), 2);
        assert_eq!(m_5_5_c.column_mut(0).len(), 2);
        assert_eq!(m_5_5_c.column_as_flat(0).len(), 5);
        assert_eq!(m_5_5_c.column_as_flat_mut(0).len(), 5);

        assert_eq!(m_5_5_r.row(0).len(), 2);
        assert_eq!(m_5_5_r.row_mut(0).len(), 2);
        assert_eq!(m_5_5_r.row_as_flat(0).len(), 5);
        assert_eq!(m_5_5_r.row_as_flat_mut(0).len(), 5);

        let mut count = 0;

        for _ in m_5_5_c.column_iter() {
            count += 1
        }

        for _ in m_5_5_r.row_iter() {
            count += 1
        }

        let r1 = m_5_5_r.row(3);
        let r2 = m_5_5_r.row(4);

        let mut sum = f32x4::splat(0_f32);

        for (x, y) in r1.iter().zip(r2) {
            sum += *x + *y;
        }

        assert_eq!(count, 10);
    }

    #[test]
    fn flattened() {
        let mut m_1_5_r = MatrixD::<f32x4, Rows>::with_dimension(1, 5);
        let mut m_1_5_c = MatrixD::<f32x4, Columns>::with_dimension(1, 5);
        let mut m_5_1_c = MatrixD::<f32x4, Columns>::with_dimension(5, 1);
        let mut m_5_1_r = MatrixD::<f32x4, Rows>::with_dimension(5, 1);

        let mut m_1_5_r_flat = m_1_5_r.flat_mut();
        let mut m_1_5_c_flat = m_1_5_c.flat_mut();
        let mut m_5_1_c_flat = m_5_1_c.flat_mut();
        let mut m_5_1_r_flat = m_5_1_r.flat_mut();

        m_1_5_r_flat[(0, 4)] = 1.0;
        m_1_5_c_flat[(0, 4)] = 1.0;
        m_5_1_r_flat[(4, 0)] = 1.0;
        m_5_1_c_flat[(4, 0)] = 1.0;
    }
}