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
// 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 of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use stdsimd::vendor::*;
use vecs::*;

pub trait PackedCmp {
    /// Return a vector where each element at an index i is the maximum of the
    /// elements at index i in `self` and `other`.
    ///
    /// ```
    /// extern crate faster;
    /// use faster::*;
    ///
    /// # fn main() {
    /// assert_eq!(i8s(0).max(i8s(2)), i8s(2));
    /// assert_eq!(i8s::halfs(1, 0).max(i8s::halfs(2, -1)), i8s::halfs(2, 0));
    /// # }
    /// ```
    fn max(&self, other: Self) -> Self;

    /// Return a vector where each element at an index i is the minimum of the
    /// elements at index i in `self` and `other`.
    ///
    /// ```
    /// extern crate faster;
    /// use faster::*;
    ///
    /// # fn main() {
    /// assert_eq!(i8s(0).min(i8s(2)), i8s(0));
    /// assert_eq!(i8s::halfs(1, 0).min(i8s::halfs(2, -1)), i8s::halfs(1, -1));
    /// # }
    /// ```
    fn min(&self, other: Self) -> Self;
}

rust_fallback_impl_binary! {
    impl PackedCmp for u8x16 where "sse2" {
        min => _mm_min_epu8(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
        max => _mm_max_epu8(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
    }
}

rust_fallback_impl_binary! {
    impl PackedCmp for i8x16 where "sse4.1" {
        min => _mm_min_epi8(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
        max => _mm_max_epi8(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
    }
}

rust_fallback_impl_binary! {
    impl PackedCmp for u16x8 where "sse4.1" {
        min => _mm_min_epu16(), [0, 1, 2, 3, 4, 5, 6, 7];
        max => _mm_max_epu16(), [0, 1, 2, 3, 4, 5, 6, 7];
    }
}

rust_fallback_impl_binary! {
    impl PackedCmp for i16x8 where "sse4.1" {
        min => _mm_min_epi16(), [0, 1, 2, 3, 4, 5, 6, 7];
        max => _mm_max_epi16(), [0, 1, 2, 3, 4, 5, 6, 7];
    }
}

rust_fallback_impl_binary! {
    impl PackedCmp for u32x4 where "sse4.1" {
        min => _mm_min_epu32(), [0, 1, 2, 3];
        max => _mm_max_epu32(), [0, 1, 2, 3];
    }
}

rust_fallback_impl_binary! {
    impl PackedCmp for i32x4 where "sse4.1" {
        min => _mm_min_epi32(), [0, 1, 2, 3];
        max => _mm_max_epi32(), [0, 1, 2, 3];
    }
}

rust_fallback_impl_binary! {
    impl PackedCmp for f32x4 where "sse" {
        min => _mm_min_ps(), [0, 1, 2, 3];
        max => _mm_max_ps(), [0, 1, 2, 3];
    }
}

rust_fallback_impl_binary! {
    impl PackedCmp for f64x2 where "sse2" {
        min => _mm_min_pd(), [0, 1];
        max => _mm_max_pd(), [0, 1];
    }
}

rust_fallback_impl_binary! {
    impl PackedCmp for u8x32 where "avx2" {
        min => _mm256_min_epu8(), [0, 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];
        max => _mm256_max_epu8(), [0, 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];
    }
}

rust_fallback_impl_binary! {
    impl PackedCmp for i8x32 where "avx2" {
        min => _mm256_min_epi8(), [0, 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];
        max => _mm256_max_epi8(), [0, 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];
    }
}

rust_fallback_impl_binary! {
    impl PackedCmp for u16x16 where "avx2" {
        min => _mm256_min_epu16(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
        max => _mm256_max_epu16(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
    }
}


rust_fallback_impl_binary! {
    impl PackedCmp for i16x16 where "avx2" {
        min => _mm256_min_epi16(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
        max => _mm256_max_epi16(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
    }
}

rust_fallback_impl_binary! {
    impl PackedCmp for u32x8 where "avx" {
        min => _mm256_min_epu32(), [0, 1, 2, 3, 4, 5, 6, 7];
        max => _mm256_max_epu32(), [0, 1, 2, 3, 4, 5, 6, 7];
    }
}

rust_fallback_impl_binary! {
    impl PackedCmp for i32x8 where "avx" {
        min => _mm256_min_epi32(), [0, 1, 2, 3, 4, 5, 6, 7];
        max => _mm256_max_epi32(), [0, 1, 2, 3, 4, 5, 6, 7];
    }
}

rust_fallback_impl_binary! {
    impl PackedCmp for f32x8 where "avx" {
        min => _mm256_min_ps(), [0, 1, 2, 3, 4, 5, 6, 7];
        max => _mm256_max_ps(), [0, 1, 2, 3, 4, 5, 6, 7];
    }
}

rust_fallback_impl_binary! {
    impl PackedCmp for f64x4 where "avx" {
        min => _mm256_min_pd(), [0, 1, 2, 3];
        max => _mm256_max_pd(), [0, 1, 2, 3];
    }
}