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
// This file is part of faster, the SIMD library for humans.
// Copyright 2017 Adam Niederer <adam.niederer@gmail.com>

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy owf the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use crate::iters::{SIMDIter, SIMDIterator, SIMDObject};
#[allow(unused_imports)] // Remove for specialization
use crate::iters::SIMDAdapter;
use crate::arch::current::vecs::*;

/// A trait which transforms a contiguous collection into an owned stream of
/// vectors.
pub trait IntoSIMDIterator {
    type Iter : SIMDIterator;

    /// Return an iterator over this data which will automatically pack
    /// values into SIMD vectors. See `SIMDIterator::simd_map` and
    /// `SIMDIterator::simd_reduce` for more information.
    fn into_simd_iter(self, default: <Self::Iter as SIMDObject>::Vector) -> Self::Iter;
}

/// A trait which transforms a contiguous collection into a slice-backed stream
/// of vectors.
pub trait IntoSIMDRefIterator<'a> {
    type Iter : SIMDIterator;

    /// Return an iterator over this data which will automatically pack
    /// values into SIMD vectors. See `SIMDIterator::simd_map` and
    /// `SIMDIterator::simd_reduce` for more information.
    fn simd_iter(&'a self, default: <Self::Iter as SIMDObject>::Vector) -> Self::Iter;
}

/// A trait which transforms a contiguous collection into a mutable slice-backed
/// stream of vectors.
pub trait IntoSIMDRefMutIterator<'a> {
    type Iter : SIMDIterator;

    /// Return an iterator over this data which will automatically pack
    /// values into SIMD vectors. See `SIMDIterator::simd_map` and
    /// `SIMDIterator::simd_reduce` for more information.
    fn simd_iter_mut(&'a mut self, default: <Self::Iter as SIMDObject>::Vector) -> Self::Iter;
}

macro_rules! impl_array_intos {
    ($($el:ty, $vec:ty),*) => {
        $(
            #[cfg(feature = "std")]
            impl IntoSIMDIterator for Vec<$el> {
                type Iter = SIMDIter<Self>;

                #[inline(always)]
                fn into_simd_iter(self, default: $vec) -> Self::Iter {
                    SIMDIter {
                        data: self,
                        position: 0,
                        default: default,
                    }
                }
            }

            impl<'a> IntoSIMDRefIterator<'a> for &'a [$el] {
                type Iter = SIMDIter<Self>;

                #[inline(always)]
                fn simd_iter(&'a self, default: $vec) -> Self::Iter {
                    SIMDIter {
                        data: self,
                        position: 0,
                        default: default,
                    }
                }
            }

            impl<'a> IntoSIMDRefMutIterator<'a> for &'a mut [$el] {
                type Iter = SIMDIter<Self>;

                #[inline(always)]
                fn simd_iter_mut(&'a mut self, default: $vec) -> Self::Iter {
                    SIMDIter {
                        data: self,
                        position: 0,
                        default: default,
                    }
                }
            }

            impl<'a> IntoSIMDRefMutIterator<'a> for [$el] {
                type Iter = SIMDIter<&'a mut Self>;

                #[inline(always)]
                fn simd_iter_mut(&'a mut self, default: $vec) -> Self::Iter {
                    SIMDIter {
                        data: self,
                        position: 0,
                        default: default,
                    }
                }
            }

            impl<'a> IntoSIMDRefIterator<'a> for [$el] {
                type Iter = SIMDIter<&'a Self>;

                #[inline(always)]
                fn simd_iter(&'a self, default: $vec) -> Self::Iter {
                    SIMDIter {
                        data: self,
                        position: 0,
                        default: default,
                    }
                }
            }
        )*
    }
}

impl_array_intos!(u8, u8s,
                  i8, i8s,
                  u16, u16s,
                  i16, i16s,
                  u32, u32s,
                  i32, i32s,
                  f32, f32s,
                  u64, u64s,
                  i64, i64s,
                  f64, f64s);

// TODO: Specialization
// impl<I, S> IntoSIMDIterator for I where I : ExactSizeIterator + Iterator<Item = S>, S : Packable {
//     type Iter = SIMDAdapter<Self, S::Vector>;

//     #[inline(always)]
//     fn into_simd_iter(self, default: S::Vector) -> Self::Iter {
//         SIMDAdapter {
//             iter: self,
//             position: 0,
//             default: default,
//             scratch: default
//         }
//     }
// }