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
//! Implements portable vector shuffles with immediate indices.

// FIXME: comprehensive tests
// https://github.com/rust-lang-nursery/packed_simd/issues/20

/// Shuffles vector elements.
///
/// This macro returns a new vector that contains a shuffle of the elements in
/// one (`shuffle!(vec, [indices...])`) or two (`shuffle!(vec0, vec1,
/// [indices...])`) input vectors.
///
/// The type of `vec0` and `vec1` must be equal, and the element type of the
/// resulting vector is the element type of the input vector.
///
/// The number of `indices` must be a power-of-two in range `[0, 64)`, since
/// currently, the largest vector supported by the library has 64 lanes. The
/// length of the resulting vector equals the number of indices provided.
///
/// The indices must be in range `[0, M * N)` where `M` is the number of input
/// vectors (`1` or `2`) and `N` is the number of lanes of the input vectors.
/// The indices `i` in range `[0, N)` refer to the `i`-th element of `vec0`,
/// while the indices in range `[N, 2*N)` refer to the `i - N`-th element of
/// `vec1`.
///
/// # Examples
///
/// Shuffling elements of two vectors:
///
/// ```
/// # use packed_simd_2::*;
/// # fn main() {
/// // Shuffle allows reordering the elements:
/// let x = i32x4::new(1, 2, 3, 4);
/// let y = i32x4::new(5, 6, 7, 8);
/// let r = shuffle!(x, y, [4, 0, 5, 1]);
/// assert_eq!(r, i32x4::new(5, 1, 6, 2));
///
/// // The resulting vector can als be smaller than the input:
/// let r = shuffle!(x, y, [1, 6]);
/// assert_eq!(r, i32x2::new(2, 7));
///
/// // Or larger:
/// let r = shuffle!(x, y, [1, 3, 4, 2, 1, 7, 2, 2]);
/// assert_eq!(r, i32x8::new(2, 4, 5, 3, 2, 8, 3, 3));
/// // At most 2 * the number of lanes in the input vector.
/// # }
/// ```
///
/// Shuffling elements of one vector:
///
/// ```
/// # use packed_simd_2::*;
/// # fn main() {
/// // Shuffle allows reordering the elements of a vector:
/// let x = i32x4::new(1, 2, 3, 4);
/// let r = shuffle!(x, [2, 1, 3, 0]);
/// assert_eq!(r, i32x4::new(3, 2, 4, 1));
///
/// // The resulting vector can be smaller than the input:
/// let r = shuffle!(x, [1, 3]);
/// assert_eq!(r, i32x2::new(2, 4));
///
/// // Equal:
/// let r = shuffle!(x, [1, 3, 2, 0]);
/// assert_eq!(r, i32x4::new(2, 4, 3, 1));
///
/// // Or larger:
/// let r = shuffle!(x, [1, 3, 2, 2, 1, 3, 2, 2]);
/// assert_eq!(r, i32x8::new(2, 4, 3, 3, 2, 4, 3, 3));
/// // At most 2 * the number of lanes in the input vector.
/// # }
/// ```
#[macro_export]
macro_rules! shuffle {
    ($vec0:expr, $vec1:expr, [$l0:expr, $l1:expr]) => {{
        #[allow(unused_unsafe)]
        unsafe {
            $crate::Simd($crate::__shuffle_vector2::<{[$l0, $l1]}, _, _>(
                $vec0.0,
                $vec1.0,
            ))
        }
    }};
    ($vec0:expr, $vec1:expr, [$l0:expr, $l1:expr, $l2:expr, $l3:expr]) => {{
        #[allow(unused_unsafe)]
        unsafe {
            $crate::Simd($crate::__shuffle_vector4::<{[$l0, $l1, $l2, $l3]}, _, _>(
                $vec0.0,
                $vec1.0,
            ))
        }
    }};
    ($vec0:expr, $vec1:expr,
     [$l0:expr, $l1:expr, $l2:expr, $l3:expr,
      $l4:expr, $l5:expr, $l6:expr, $l7:expr]) => {{
        #[allow(unused_unsafe)]
        unsafe {
            $crate::Simd($crate::__shuffle_vector8::<{[$l0, $l1, $l2, $l3, $l4, $l5, $l6, $l7]}, _, _>(
                $vec0.0,
                $vec1.0,
            ))
        }
    }};
    ($vec0:expr, $vec1:expr,
     [$l0:expr, $l1:expr, $l2:expr, $l3:expr,
      $l4:expr, $l5:expr, $l6:expr, $l7:expr,
      $l8:expr, $l9:expr, $l10:expr, $l11:expr,
      $l12:expr, $l13:expr, $l14:expr, $l15:expr]) => {{
        #[allow(unused_unsafe)]
        unsafe {
            $crate::Simd($crate::__shuffle_vector16::<{
                [
                    $l0, $l1, $l2, $l3, $l4, $l5, $l6, $l7, $l8, $l9, $l10,
                    $l11, $l12, $l13, $l14, $l15,
                ]
            }, _, _>(
                $vec0.0,
                $vec1.0,
            ))
        }
    }};
    ($vec0:expr, $vec1:expr,
     [$l0:expr, $l1:expr, $l2:expr, $l3:expr,
      $l4:expr, $l5:expr, $l6:expr, $l7:expr,
      $l8:expr, $l9:expr, $l10:expr, $l11:expr,
      $l12:expr, $l13:expr, $l14:expr, $l15:expr,
      $l16:expr, $l17:expr, $l18:expr, $l19:expr,
      $l20:expr, $l21:expr, $l22:expr, $l23:expr,
      $l24:expr, $l25:expr, $l26:expr, $l27:expr,
      $l28:expr, $l29:expr, $l30:expr, $l31:expr]) => {{
        #[allow(unused_unsafe)]
        unsafe {
            $crate::Simd($crate::__shuffle_vector32::<{
                [
                    $l0, $l1, $l2, $l3, $l4, $l5, $l6, $l7, $l8, $l9, $l10,
                    $l11, $l12, $l13, $l14, $l15, $l16, $l17, $l18, $l19,
                    $l20, $l21, $l22, $l23, $l24, $l25, $l26, $l27, $l28,
                    $l29, $l30, $l31,
                ]
            }, _, _>(
                $vec0.0,
                $vec1.0,
            ))
        }
    }};
    ($vec0:expr, $vec1:expr,
     [$l0:expr, $l1:expr, $l2:expr, $l3:expr,
      $l4:expr, $l5:expr, $l6:expr, $l7:expr,
      $l8:expr, $l9:expr, $l10:expr, $l11:expr,
      $l12:expr, $l13:expr, $l14:expr, $l15:expr,
      $l16:expr, $l17:expr, $l18:expr, $l19:expr,
      $l20:expr, $l21:expr, $l22:expr, $l23:expr,
      $l24:expr, $l25:expr, $l26:expr, $l27:expr,
      $l28:expr, $l29:expr, $l30:expr, $l31:expr,
      $l32:expr, $l33:expr, $l34:expr, $l35:expr,
      $l36:expr, $l37:expr, $l38:expr, $l39:expr,
      $l40:expr, $l41:expr, $l42:expr, $l43:expr,
      $l44:expr, $l45:expr, $l46:expr, $l47:expr,
      $l48:expr, $l49:expr, $l50:expr, $l51:expr,
      $l52:expr, $l53:expr, $l54:expr, $l55:expr,
      $l56:expr, $l57:expr, $l58:expr, $l59:expr,
      $l60:expr, $l61:expr, $l62:expr, $l63:expr]) => {{
        #[allow(unused_unsafe)]
        unsafe {
            $crate::Simd($crate::__shuffle_vector64::<{[
                $l0, $l1, $l2, $l3, $l4, $l5, $l6, $l7, $l8, $l9, $l10,
                $l11, $l12, $l13, $l14, $l15, $l16, $l17, $l18, $l19,
                $l20, $l21, $l22, $l23, $l24, $l25, $l26, $l27, $l28,
                $l29, $l30, $l31, $l32, $l33, $l34, $l35, $l36, $l37,
                $l38, $l39, $l40, $l41, $l42, $l43, $l44, $l45, $l46,
                $l47, $l48, $l49, $l50, $l51, $l52, $l53, $l54, $l55,
                $l56, $l57, $l58, $l59, $l60, $l61, $l62, $l63,
            ]}, _, _>(
                $vec0.0,
                $vec1.0,
            ))
        }
     }};
    ($vec:expr, [$($l:expr),*]) => {
        match $vec {
            v => shuffle!(v, v, [$($l),*])
        }
    };
}