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
use super::*;

magic! {
  if #[cfg(target_feature="sse2")] {
    /// Four `i32` values packed together.
    #[repr(C, align(16))]
    pub struct i32x4 {
      pub(crate) sse: m128i
    }
  } else {
    /// Four `i32` values packed together.
    #[repr(C, align(16))]
    pub struct i32x4 {
      pub(crate) arr: [i32; 4]
    }
  }
}
impl Clone for i32x4 {
  #[inline(always)]
  fn clone(&self) -> Self {
    *self
  }
}
impl Copy for i32x4 {}
impl Default for i32x4 {
  #[inline(always)]
  fn default() -> Self {
    Self::zeroed()
  }
}
unsafe impl Zeroable for i32x4 {}
unsafe impl Pod for i32x4 {}

/// Allows us to declare `i32x4` values in a `const` context. Uninteresting otherwise.
/// 
/// See the [`const_i32_as_i32x4`] macro for usage.
#[allow(non_camel_case_types)]
#[repr(C, align(16))]
#[allow(missing_docs)]
pub union ConstUnionHack_i32x4 {
  pub narrow_arr: [i32; 4],
  pub wide_thing: i32x4,
}

/// Declares an `i32x4` const identifier.
///
/// ## Broadcast A Single Value
///
/// * **Usage:** `const_i32_as_i32x4!(#[meta]* vis ident, val);`
///
/// The value should be a single `i32` expression, which is then duplicated into
/// all lanes of the constant declaration.
///
/// ```rust
/// use wide::*;
/// const_i32_as_i32x4!(
///   /// The maximum `i32` value
///   pub MAX, core::i32::MAX
/// );
/// ```
///
/// ## Select Each Lane
///
/// * **Usage:** `const_i32_as_i32x4!(#[meta]* vis ident, a, b, c, d);`
///
/// Each of `a`, `b`, `c`, and `d` are an `i32` expression when are then placed
/// into the constant declaration (low lane to high lane).
///
/// ```rust
/// use wide::*;
/// const_i32_as_i32x4!(
///   /// 1, 2, 3, 4
///   pub ONE_TWO_THREE_FOUR, 1, 2, 3, 4
/// );
/// ```
#[macro_export]
macro_rules! const_i32_as_i32x4 {
  // broadcast a single value
  ($(#[$attrs:meta])* $v:vis $i:ident, $val:expr) => {
    $(#[$attrs])*
    $v const $i: i32x4 = {
      let cuh = ConstUnionHack_i32x4 {
        narrow_arr: [$val, $val, $val, $val],
      };
      unsafe { cuh.wide_thing }
    };
  };
  // select each lane's value
  ($(#[$attrs:meta])* $v:vis $i:ident, $a:expr, $b:expr, $c:expr, $d:expr) => {
    $(#[$attrs])*
    $v const $i: i32x4 = {
      let cuh = ConstUnionHack_i32x4 {
        narrow_arr: [$a, $b, $c, $d],
      };
      unsafe { cuh.wide_thing }
    };
  };
}

impl i32x4 {
  //
  // core::i32
  //

  const_i32_as_i32x4!(
    /// Maximum `i32` value.
    pub MAX, core::i32::MAX
  );

  const_i32_as_i32x4!(
    /// Minimum `i32` value.
    pub MIN, core::i32::MIN
  );

  //
  // others
  //

  const_i32_as_i32x4!(
    /// 0
    pub ZERO, 0_i32
  );

  const_i32_as_i32x4!(
    /// 1
    pub ONE, 1_i32
  );
}

impl BitAnd for i32x4 {
  type Output = Self;
  #[inline]
  fn bitand(self, rhs: Self) -> Self {
    magic! { if #[cfg(target_feature="sse2")] {
      Self { sse: self.sse.bitand(rhs.sse) }
    } else {
      Self { arr: [
        self.arr[0].bitand(rhs.arr[0]),
        self.arr[1].bitand(rhs.arr[1]),
        self.arr[2].bitand(rhs.arr[2]),
        self.arr[3].bitand(rhs.arr[3]),
      ] }
    }}
  }
}

impl BitAndAssign for i32x4 {
  #[inline]
  fn bitand_assign(&mut self, rhs: Self) {
    magic! { if #[cfg(target_feature="sse2")] {
      self.sse.bitand_assign(rhs.sse)
    } else {
      self.arr[0].bitand_assign(rhs.arr[0]);
      self.arr[1].bitand_assign(rhs.arr[1]);
      self.arr[2].bitand_assign(rhs.arr[2]);
      self.arr[3].bitand_assign(rhs.arr[3]);
    }}
  }
}

impl i32x4 {
  /// Lanewise `==`
  #[inline]
  pub fn cmp_eq(self, rhs: Self) -> Self {
    magic! { if #[cfg(target_feature="sse2")] {
      Self { sse: self.sse.cmp_eq_i32(rhs.sse) }
    } else {
      let op = |a:i32, b:i32|{
        if a == b {
          -1
        } else {
          0
        }
      };
      Self { arr: [
        op(self.arr[0], rhs.arr[0]),
        op(self.arr[1], rhs.arr[1]),
        op(self.arr[2], rhs.arr[2]),
        op(self.arr[3], rhs.arr[3]),
      ] }
    }}
  }

  /// Lanewise `>`
  #[inline]
  pub fn cmp_gt(self, rhs: Self) -> Self {
    magic! { if #[cfg(target_feature="sse2")] {
      Self { sse: self.sse.cmp_gt_i32(rhs.sse) }
    } else {
      let op = |a:i32, b:i32|{
        if a > b {
          -1
        } else {
          0
        }
      };
      Self { arr: [
        op(self.arr[0], rhs.arr[0]),
        op(self.arr[1], rhs.arr[1]),
        op(self.arr[2], rhs.arr[2]),
        op(self.arr[3], rhs.arr[3]),
      ] }
    }}
  }
}

impl Not for i32x4 {
  type Output = Self;

  #[inline]
  fn not(self) -> Self {
    magic! { if #[cfg(target_feature="sse2")] {
      Self { sse: !self.sse }
    } else {
      Self { arr: [
        !self.arr[0],
        !self.arr[1],
        !self.arr[2],
        !self.arr[3],
      ] }
    }}
  }
}

impl i32x4 {
  /// Make a new `i32x4`
  #[inline(always)]
  pub fn new(a: i32, b: i32, c: i32, d: i32) -> Self {
    magic! {if #[cfg(target_feature="sse2")] {
      Self { sse: m128i::set_reverse_i32(a,b,c,d) }
    } else {
      Self { arr: [a,b,c,d] }
    }}
  }
}

impl From<i32> for i32x4 {
  #[inline]
  fn from(i: i32) -> Self {
    Self::new(i, i, i, i)
  }
}

impl Shl<i32> for i32x4 {
  type Output = Self;
  #[inline]
  fn shl(self, rhs: i32) -> Self {
    magic! {if #[cfg(target_feature="sse2")] {
      Self { sse: self.sse.shift_left_i32(i32x4::from(rhs).sse) }
    } else {
      Self { arr: [
        self.arr[0] << rhs,
        self.arr[1] << rhs,
        self.arr[2] << rhs,
        self.arr[3] << rhs,
      ] }
    }}
  }
}

impl BitXor for i32x4 {
  type Output = Self;
  #[inline]
  fn bitxor(self, rhs: Self) -> Self {
    magic! {if #[cfg(target_feature="sse2")] {
      Self { sse: self.sse.bitxor(rhs.sse) }
    } else {
      Self { arr: [
        self.arr[0].bitxor(rhs.arr[0]),
        self.arr[1].bitxor(rhs.arr[1]),
        self.arr[2].bitxor(rhs.arr[2]),
        self.arr[3].bitxor(rhs.arr[3]),
      ] }
    }}
  }
}

impl Add for i32x4 {
  type Output = Self;
  #[inline]
  fn add(self, rhs: Self) -> Self {
    magic! {if #[cfg(target_feature="sse2")] {
      Self { sse: self.sse.add_i32(rhs.sse) }
    } else {
      Self { arr: [
        self.arr[0].add(rhs.arr[0]),
        self.arr[1].add(rhs.arr[1]),
        self.arr[2].add(rhs.arr[2]),
        self.arr[3].add(rhs.arr[3]),
      ] }
    }}
  }
}

impl BitOr for i32x4 {
  type Output = Self;
  #[inline]
  fn bitor(self, rhs: Self) -> Self {
    magic! { if #[cfg(target_feature="sse2")] {
      Self { sse: self.sse.bitor(rhs.sse) }
    } else {
      Self { arr: [
        self.arr[0].bitor(rhs.arr[0]),
        self.arr[1].bitor(rhs.arr[1]),
        self.arr[2].bitor(rhs.arr[2]),
        self.arr[3].bitor(rhs.arr[3]),
      ] }
    }}
  }
}

impl BitOrAssign for i32x4 {
  #[inline]
  fn bitor_assign(&mut self, rhs: Self) {
    magic! { if #[cfg(target_feature="sse2")] {
      self.sse.bitor_assign(rhs.sse)
    } else {
      self.arr[0].bitor_assign(rhs.arr[0]);
      self.arr[1].bitor_assign(rhs.arr[1]);
      self.arr[2].bitor_assign(rhs.arr[2]);
      self.arr[3].bitor_assign(rhs.arr[3]);
    }}
  }
}

impl i32x4 {
  /// Cast directly to `f32x4` (no bit change).
  #[inline(always)]
  pub fn cast_f32x4(self) -> f32x4 {
    magic! {if #[cfg(target_feature="sse2")] {
      f32x4 { sse: self.sse.cast_m128() }
    } else {
      cast(self)
    }}
  }
}