Debian Codegraph @

debian/missing-sources/safe_arch/src/x86_x64/sse4_1.rs

raw
1 #![cfg(target_feature = "sse4.1")]
2
3 use super::*;
4
5 /// Blends the `i16` lanes according to the immediate mask.
6 ///
7 /// Each bit 0 though 7 controls lane 0 through 7. Use 0 for the `a` value and
8 /// 1 for the `b` value.
9 ///
10 /// * **Intrinsic:** [`_mm_blend_epi16`]
11 /// * **Assembly:** `pblendw xmm, xmm, imm8`
12 #[must_use]
13 #[inline(always)]
14 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
15 pub fn blend_imm_i16_m128i<const IMM: i32>(a: m128i, b: m128i) -> m128i {
16 m128i(unsafe { _mm_blend_epi16(a.0, b.0, IMM) })
17 }
18
19 /// Blends the `i16` lanes according to the immediate mask.
20 ///
21 /// Bits 0 and 1 control where output lane 0 and 1 come from. Use 0 for the `a`
22 /// value and 1 for the `b` value.
23 ///
24 /// * **Intrinsic:** [`_mm_blend_pd`]
25 /// * **Assembly:** `blendpd xmm, xmm, imm8`
26 #[must_use]
27 #[inline(always)]
28 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
29 pub fn blend_imm_m128d<const IMM: i32>(a: m128d, b: m128d) -> m128d {
30 m128d(unsafe { _mm_blend_pd(a.0, b.0, IMM) })
31 }
32
33 /// Blends the lanes according to the immediate mask.
34 ///
35 /// Bits 0 to 3 control where output lane 0 to 3 come from. Use 0 for the `a`
36 /// value and 1 for the `b` value.
37 ///
38 /// * **Intrinsic:** [`_mm_blend_ps`]
39 /// * **Assembly:** `blendps xmm, xmm, imm8`
40 #[must_use]
41 #[inline(always)]
42 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
43 pub fn blend_imm_m128<const IMM: i32>(a: m128, b: m128) -> m128 {
44 m128(unsafe { _mm_blend_ps(a.0, b.0, IMM) })
45 }
46
47 /// Blend the `i8` lanes according to a runtime varying mask.
48 ///
49 /// The sign bit of each `i8` lane in the `mask` value determines if the output
50 /// lane uses `a` (mask non-negative) or `b` (mask negative).
51 ///
52 /// * **Intrinsic:** [`_mm_blendv_epi8`]
53 /// * **Assembly:** `pblendvb xmm, xmm`
54 #[must_use]
55 #[inline(always)]
56 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
57 pub fn blend_varying_i8_m128i(a: m128i, b: m128i, mask: m128i) -> m128i {
58 m128i(unsafe { _mm_blendv_epi8(a.0, b.0, mask.0) })
59 }
60
61 /// Blend the lanes according to a runtime varying mask.
62 ///
63 /// The sign bit of each lane in the `mask` value determines if the output
64 /// lane uses `a` (mask non-negative) or `b` (mask negative).
65 ///
66 /// * **Intrinsic:** [`_mm_blendv_pd`]
67 /// * **Assembly:** `blendvpd xmm, xmm`
68 #[must_use]
69 #[inline(always)]
70 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
71 pub fn blend_varying_m128d(a: m128d, b: m128d, mask: m128d) -> m128d {
72 m128d(unsafe { _mm_blendv_pd(a.0, b.0, mask.0) })
73 }
74
75 /// Blend the lanes according to a runtime varying mask.
76 ///
77 /// The sign bit of each lane in the `mask` value determines if the output
78 /// lane uses `a` (mask non-negative) or `b` (mask negative).
79 ///
80 /// * **Intrinsic:** [`_mm_blendv_ps`]
81 /// * **Assembly:** `blendvps xmm, xmm`
82 #[must_use]
83 #[inline(always)]
84 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
85 pub fn blend_varying_m128(a: m128, b: m128, mask: m128) -> m128 {
86 m128(unsafe { _mm_blendv_ps(a.0, b.0, mask.0) })
87 }
88
89 /// Round each lane to a whole number, towards positive infinity.
90 ///
91 /// * **Intrinsic:** [`_mm_ceil_pd`]
92 /// * **Assembly:** `roundpd xmm, xmm, imm8`
93 #[must_use]
94 #[inline(always)]
95 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
96 pub fn ceil_m128d(a: m128d) -> m128d {
97 m128d(unsafe { _mm_ceil_pd(a.0) })
98 }
99
100 /// Round each lane to a whole number, towards positive infinity.
101 ///
102 /// * **Intrinsic:** [`_mm_ceil_ps`]
103 /// * **Assembly:** `roundps xmm, xmm, imm8`
104 #[must_use]
105 #[inline(always)]
106 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
107 pub fn ceil_m128(a: m128) -> m128 {
108 m128(unsafe { _mm_ceil_ps(a.0) })
109 }
110
111 /// Round the low lane of `b` toward positive infinity, high lane is `a`.
112 ///
113 /// * **Intrinsic:** [`_mm_ceil_sd`]
114 /// * **Assembly:** `roundsd xmm, xmm, imm8`
115 #[must_use]
116 #[inline(always)]
117 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
118 pub fn ceil_m128d_s(a: m128d, b: m128d) -> m128d {
119 m128d(unsafe { _mm_ceil_sd(a.0, b.0) })
120 }
121
122 /// Round the low lane of `b` toward positive infinity, other lanes `a`.
123 ///
124 /// * **Intrinsic:** [`_mm_ceil_ss`]
125 /// * **Assembly:** `roundss xmm, xmm, imm8`
126 #[must_use]
127 #[inline(always)]
128 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
129 pub fn ceil_m128_s(a: m128, b: m128) -> m128 {
130 m128(unsafe { _mm_ceil_ss(a.0, b.0) })
131 }
132
133 /// Lanewise `a == b` with lanes as `i64`.
134 ///
135 /// All bits 1 for true (`-1`), all bit 0 for false (`0`).
136 ///
137 /// * **Intrinsic:** [`_mm_cmpeq_epi64`]
138 /// * **Assembly:** `pcmpeqq xmm, xmm`
139 #[must_use]
140 #[inline(always)]
141 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
142 pub fn cmp_eq_mask_i64_m128i(a: m128i, b: m128i) -> m128i {
143 m128i(unsafe { _mm_cmpeq_epi64(a.0, b.0) })
144 }
145
146 /// Convert the lower four `i16` lanes to four `i32` lanes.
147 ///
148 /// * **Intrinsic:** [`_mm_cvtepi16_epi32`]
149 /// * **Assembly:** `pmovsxwd xmm, xmm`
150 #[must_use]
151 #[inline(always)]
152 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
153 pub fn convert_to_i32_m128i_from_lower4_i16_m128i(a: m128i) -> m128i {
154 m128i(unsafe { _mm_cvtepi16_epi32(a.0) })
155 }
156
157 /// Convert the lower two `i64` lanes to two `i32` lanes.
158 ///
159 /// * **Intrinsic:** [`_mm_cvtepi16_epi64`]
160 /// * **Assembly:** `pmovsxwq xmm, xmm`
161 #[must_use]
162 #[inline(always)]
163 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
164 pub fn convert_to_i16_m128i_from_lower2_i16_m128i(a: m128i) -> m128i {
165 m128i(unsafe { _mm_cvtepi16_epi64(a.0) })
166 }
167
168 /// Convert the lower two `i32` lanes to two `i64` lanes.
169 ///
170 /// * **Intrinsic:** [`_mm_cvtepi32_epi64`]
171 /// * **Assembly:** `_mm_cvtepi32_epi64`
172 #[must_use]
173 #[inline(always)]
174 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
175 pub fn convert_to_i64_m128i_from_lower2_i32_m128i(a: m128i) -> m128i {
176 m128i(unsafe { _mm_cvtepi32_epi64(a.0) })
177 }
178
179 /// Convert the lower eight `i8` lanes to eight `i16` lanes.
180 ///
181 /// * **Intrinsic:** [`_mm_cvtepi8_epi16`]
182 /// * **Assembly:** `pmovsxbw xmm, xmm`
183 #[must_use]
184 #[inline(always)]
185 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
186 pub fn convert_to_i16_m128i_from_lower8_i8_m128i(a: m128i) -> m128i {
187 m128i(unsafe { _mm_cvtepi8_epi16(a.0) })
188 }
189
190 /// Convert the lower four `i8` lanes to four `i32` lanes.
191 ///
192 /// * **Intrinsic:** [`_mm_cvtepi8_epi32`]
193 /// * **Assembly:** `pmovsxbd xmm, xmm`
194 #[must_use]
195 #[inline(always)]
196 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
197 pub fn convert_to_i32_m128i_from_lower4_i8_m128i(a: m128i) -> m128i {
198 m128i(unsafe { _mm_cvtepi8_epi32(a.0) })
199 }
200
201 /// Convert the lower two `i8` lanes to two `i64` lanes.
202 ///
203 /// * **Intrinsic:** [`_mm_cvtepi8_epi64`]
204 /// * **Assembly:** `pmovsxbq xmm, xmm`
205 #[must_use]
206 #[inline(always)]
207 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
208 pub fn convert_to_i64_m128i_from_lower2_i8_m128i(a: m128i) -> m128i {
209 m128i(unsafe { _mm_cvtepi8_epi64(a.0) })
210 }
211
212 /// Convert the lower four `u16` lanes to four `u32` lanes.
213 ///
214 /// * **Intrinsic:** [`_mm_cvtepu16_epi32`]
215 /// * **Assembly:** `pmovzxwd xmm, xmm`
216 #[must_use]
217 #[inline(always)]
218 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
219 pub fn convert_to_u32_m128i_from_lower4_u16_m128i(a: m128i) -> m128i {
220 m128i(unsafe { _mm_cvtepu16_epi32(a.0) })
221 }
222
223 /// Convert the lower two `u16` lanes to two `u64` lanes.
224 ///
225 /// * **Intrinsic:** [`_mm_cvtepu16_epi64`]
226 /// * **Assembly:** `pmovzxwq xmm, xmm`
227 #[must_use]
228 #[inline(always)]
229 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
230 pub fn convert_to_u64_m128i_from_lower2_u16_m128i(a: m128i) -> m128i {
231 m128i(unsafe { _mm_cvtepu16_epi64(a.0) })
232 }
233
234 /// Convert the lower two `u32` lanes to two `u64` lanes.
235 ///
236 /// * **Intrinsic:** [`_mm_cvtepu32_epi64`]
237 /// * **Assembly:** `pmovzxdq xmm, xmm`
238 #[must_use]
239 #[inline(always)]
240 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
241 pub fn convert_to_u64_m128i_from_lower2_u32_m128i(a: m128i) -> m128i {
242 m128i(unsafe { _mm_cvtepu32_epi64(a.0) })
243 }
244
245 /// Convert the lower eight `u8` lanes to eight `u16` lanes.
246 ///
247 /// * **Intrinsic:** [`_mm_cvtepu8_epi16`]
248 /// * **Assembly:** `pmovzxbw xmm, xmm`
249 #[must_use]
250 #[inline(always)]
251 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
252 pub fn convert_to_u16_m128i_from_lower8_u8_m128i(a: m128i) -> m128i {
253 m128i(unsafe { _mm_cvtepu8_epi16(a.0) })
254 }
255
256 /// Convert the lower four `u8` lanes to four `u32` lanes.
257 ///
258 /// * **Intrinsic:** [`_mm_cvtepu8_epi32`]
259 /// * **Assembly:** `pmovzxbd xmm, xmm`
260 #[must_use]
261 #[inline(always)]
262 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
263 pub fn convert_to_u32_m128i_from_lower4_u8_m128i(a: m128i) -> m128i {
264 m128i(unsafe { _mm_cvtepu8_epi32(a.0) })
265 }
266
267 /// Convert the lower two `u8` lanes to two `u64` lanes.
268 ///
269 /// * **Intrinsic:** [`_mm_cvtepu8_epi64`]
270 /// * **Assembly:** `pmovzxbq xmm, xmm`
271 #[must_use]
272 #[inline(always)]
273 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
274 pub fn convert_to_u64_m128i_from_lower2_u8_m128i(a: m128i) -> m128i {
275 m128i(unsafe { _mm_cvtepu8_epi64(a.0) })
276 }
277
278 /// Performs a dot product of two `m128d` registers.
279 ///
280 /// The output details are determined by the constant:
281 /// * For each lane, you can multiply that lane from `a` and `b` or you can take
282 /// a default of 0.0
283 /// * Bits 4 and 5 determines if we mul lanes 0 in `a` and `b`, and lanes 1 in
284 /// `a` and `b`.
285 /// * This forms two temporary `f64` values which are summed to a single `f64`.
286 /// * For each output lane, you can have the sum in that lane or 0.0.
287 /// * Bits 0 and 1 determines if an output lane is our sum or 0.0.
288 ///
289 /// * **Intrinsic:** [`_mm_dp_pd`]
290 /// * **Assembly:** `dppd xmm, xmm, imm8`
291 #[must_use]
292 #[inline(always)]
293 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
294 pub fn dot_product_m128d<const IMM: i32>(a: m128d, b: m128d) -> m128d {
295 m128d(unsafe { _mm_dp_pd(a.0, b.0, IMM) })
296 }
297
298 /// Performs a dot product of two `m128` registers.
299 ///
300 /// The output details are determined by a control mask:
301 /// * For each lane, you can multiply that lane from `a` and `b` or you can take
302 /// a default of 0.0
303 /// * Bits 4 through 7 determine if we should mul lanes 0 through 3.
304 /// * This forms four temporary `f32` values which are summed to a single `f32`.
305 /// * For each output lane, you can have the sum in that lane or 0.0.
306 /// * Bits 0 through 3 determines if the `sum` is in lanes 0 through 3.
307 ///
308 /// * **Intrinsic:** [`_mm_dp_ps`]
309 /// * **Assembly:** `dpps xmm, xmm, imm8`
310 #[must_use]
311 #[inline(always)]
312 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
313 pub fn dot_product_m128<const IMM: i32>(a: m128, b: m128) -> m128 {
314 m128(unsafe { _mm_dp_ps(a.0, b.0, IMM) })
315 }
316
317 /// Gets the `i32` lane requested. Only the lowest 2 bits are considered.
318 ///
319 /// * **Intrinsic:** [`_mm_extract_epi32`]
320 /// * **Assembly:** `pextrd r32, xmm, imm8`
321 #[must_use]
322 #[inline(always)]
323 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
324 pub fn extract_i32_imm_m128i<const IMM: i32>(a: m128i) -> i32 {
325 unsafe { _mm_extract_epi32(a.0, IMM) }
326 }
327
328 /// Gets the `i64` lane requested. Only the lowest bit is considered.
329 ///
330 /// ```
331 /// # use safe_arch::*;
332 /// let a = m128i::from([5_i64, 6]);
333 /// assert_eq!(extract_i64_imm_m128i::<1>(a), 6_i64);
334 /// ```
335 #[must_use]
336 #[inline(always)]
337 #[cfg(target_arch = "x86_64")]
338 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
339 pub fn extract_i64_imm_m128i<const IMM: i32>(a: m128i) -> i64 {
340 unsafe { _mm_extract_epi64(a.0, IMM) }
341 }
342
343 /// Gets the `i8` lane requested. Only the lowest 4 bits are considered.
344 ///
345 /// ```
346 /// # use safe_arch::*;
347 /// let a = m128i::from([0_i8, 1, 2, 3, 4, 5, 6, 101, 8, 9, 10, 11, 12, 13, 14, 15]);
348 /// assert_eq!(extract_i8_as_i32_imm_m128i::<7>(a), 101_i32);
349 /// ```
350 #[must_use]
351 #[inline(always)]
352 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
353 pub fn extract_i8_as_i32_imm_m128i<const IMM: i32>(a: m128i) -> i32 {
354 unsafe { _mm_extract_epi8(a.0, IMM) }
355 }
356
357 /// Gets the `f32` lane requested. Returns as an `i32` bit pattern.
358 ///
359 /// ```
360 /// # use safe_arch::*;
361 /// let a = m128::from_array([5.0, 6.0, 7.0, 8.0]);
362 /// assert_eq!(extract_f32_as_i32_bits_imm_m128::<3>(a), 8_f32.to_bits() as i32);
363 /// ```
364 #[must_use]
365 #[inline(always)]
366 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
367 pub fn extract_f32_as_i32_bits_imm_m128<const IMM: i32>(a: m128) -> i32 {
368 unsafe { _mm_extract_ps(a.0, IMM) }
369 }
370
371 /// Round each lane to a whole number, towards negative infinity
372 ///
373 /// ```
374 /// # use safe_arch::*;
375 /// let a = m128d::from_array([-0.1, 1.8]);
376 /// assert_eq!(floor_m128d(a).to_array(), [-1.0, 1.0]);
377 /// ```
378 #[must_use]
379 #[inline(always)]
380 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
381 pub fn floor_m128d(a: m128d) -> m128d {
382 m128d(unsafe { _mm_floor_pd(a.0) })
383 }
384
385 /// Round each lane to a whole number, towards negative infinity
386 ///
387 /// ```
388 /// # use safe_arch::*;
389 /// let a = m128::from_array([-0.1, 1.8, 2.5, 3.0]);
390 /// assert_eq!(floor_m128(a).to_array(), [-1.0, 1.0, 2.0, 3.0]);
391 /// ```
392 #[must_use]
393 #[inline(always)]
394 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
395 pub fn floor_m128(a: m128) -> m128 {
396 m128(unsafe { _mm_floor_ps(a.0) })
397 }
398
399 /// Round the low lane of `b` toward negative infinity, high lane is `a`.
400 ///
401 /// ```
402 /// # use safe_arch::*;
403 /// let a = m128d::from_array([-0.1, 1.8]);
404 /// let b = m128d::from_array([2.5, 3.0]);
405 /// assert_eq!(floor_m128d_s(a, b).to_array(), [2.0, 1.8]);
406 /// ```
407 #[must_use]
408 #[inline(always)]
409 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
410 pub fn floor_m128d_s(a: m128d, b: m128d) -> m128d {
411 m128d(unsafe { _mm_floor_sd(a.0, b.0) })
412 }
413
414 /// Round the low lane of `b` toward negative infinity, other lanes `a`.
415 ///
416 /// ```
417 /// # use safe_arch::*;
418 /// let a = m128::from_array([-0.1, 1.8, 5.0, 6.0]);
419 /// let b = m128::from_array([2.5, 3.0, 10.0, 20.0]);
420 /// assert_eq!(floor_m128_s(a, b).to_array(), [2.0, 1.8, 5.0, 6.0]);
421 /// ```
422 #[must_use]
423 #[inline(always)]
424 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
425 pub fn floor_m128_s(a: m128, b: m128) -> m128 {
426 m128(unsafe { _mm_floor_ss(a.0, b.0) })
427 }
428
429 /// Inserts a new value for the `i32` lane specified.
430 ///
431 /// ```
432 /// # use safe_arch::*;
433 /// let a = m128i::from([5, 6, 7, 8]);
434 /// let b: [i32; 4] = insert_i32_imm_m128i::<1>(a, 23).into();
435 /// assert_eq!(b, [5, 23, 7, 8]);
436 /// ```
437 #[must_use]
438 #[inline(always)]
439 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
440 pub fn insert_i32_imm_m128i<const IMM: i32>(a: m128i, new: i32) -> m128i {
441 m128i(unsafe { _mm_insert_epi32(a.0, new, IMM) })
442 }
443
444 /// Inserts a new value for the `i64` lane specified.
445 ///
446 /// ```
447 /// # use safe_arch::*;
448 /// let a = m128i::from([5_i64, 6]);
449 /// let b: [i64; 2] = insert_i64_imm_m128i::<1>(a, 23).into();
450 /// assert_eq!(b, [5_i64, 23]);
451 /// ```
452 #[must_use]
453 #[inline(always)]
454 #[cfg(target_arch = "x86_64")]
455 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
456 pub fn insert_i64_imm_m128i<const IMM: i32>(a: m128i, new: i64) -> m128i {
457 m128i(unsafe { _mm_insert_epi64(a.0, new, IMM) })
458 }
459
460 /// Inserts a new value for the `i64` lane specified.
461 ///
462 /// ```
463 /// # use safe_arch::*;
464 /// let a = m128i::from([0_i8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
465 /// let b: [i8; 16] = insert_i8_imm_m128i::<1>(a, 23).into();
466 /// assert_eq!(b, [0_i8, 23, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
467 /// ```
468 #[must_use]
469 #[inline(always)]
470 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
471 pub fn insert_i8_imm_m128i<const IMM: i32>(a: m128i, new: i32) -> m128i {
472 m128i(unsafe { _mm_insert_epi8(a.0, new, IMM) })
473 }
474
475 /// Inserts a lane from `$b` into `$a`, optionally at a new position.
476 ///
477 /// Also, you can zero out any lanes you like for free as part of the same
478 /// operation. If you don't specify the mask argument then no lanes are zeroed.
479 ///
480 /// ```
481 /// # use safe_arch::*;
482 /// let a = m128::from_array([1.0, 2.0, 3.0, 4.0]);
483 /// let b = m128::from_array([5.0, 6.0, 7.0, 8.0]);
484 /// //
485 /// let c = insert_f32_imm_m128::<0b00_11_0000>(a, b).to_array();
486 /// assert_eq!(c, [1.0, 2.0, 3.0, 5.0]);
487 /// //
488 /// let c = insert_f32_imm_m128::<0b00_11_0110>(a, b).to_array();
489 /// assert_eq!(c, [1.0, 0.0, 0.0, 5.0]);
490 /// ```
491 #[must_use]
492 #[inline(always)]
493 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
494 pub fn insert_f32_imm_m128<const IMM: i32>(a: m128, b: m128) -> m128 {
495 m128(unsafe { _mm_insert_ps(a.0, b.0, IMM) })
496 }
497
498 /// Lanewise `max(a, b)` with lanes as `i32`.
499 /// ```
500 /// # use safe_arch::*;
501 /// let a = m128i::from([1, 2, 3, 4]);
502 /// let b = m128i::from([5, 6, -7, 8]);
503 /// let c: [i32; 4] = max_i32_m128i(a, b).into();
504 /// assert_eq!(c, [5, 6, 3, 8]);
505 /// ```
506 #[must_use]
507 #[inline(always)]
508 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
509 pub fn max_i32_m128i(a: m128i, b: m128i) -> m128i {
510 m128i(unsafe { _mm_max_epi32(a.0, b.0) })
511 }
512
513 /// Lanewise `max(a, b)` with lanes as `i8`.
514 /// ```
515 /// # use safe_arch::*;
516 /// let a = m128i::from([0_i8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 127]);
517 /// let b = m128i::from([0_i8, 11, 2, -13, 4, 15, 6, -17, -8, 19, -20, 21, 22, -23, 24, 127]);
518 /// let c: [i8; 16] = max_i8_m128i(a, b).into();
519 /// assert_eq!(c, [0, 11, 2, 3, 4, 15, 6, 7, 8, 19, 10, 21, 22, 13, 24, 127]);
520 /// ```
521 #[must_use]
522 #[inline(always)]
523 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
524 pub fn max_i8_m128i(a: m128i, b: m128i) -> m128i {
525 m128i(unsafe { _mm_max_epi8(a.0, b.0) })
526 }
527
528 /// Lanewise `max(a, b)` with lanes as `u16`.
529 /// ```
530 /// # use safe_arch::*;
531 /// let a = m128i::from([1_u16, 2, 300, 400, 1, 2, 3, 4]);
532 /// let b = m128i::from([5_u16, 6, 7, 8, 15, 26, 37, 48]);
533 /// let c: [u16; 8] = max_u16_m128i(a, b).into();
534 /// assert_eq!(c, [5_u16, 6, 300, 400, 15, 26, 37, 48]);
535 /// ```
536 #[must_use]
537 #[inline(always)]
538 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
539 pub fn max_u16_m128i(a: m128i, b: m128i) -> m128i {
540 m128i(unsafe { _mm_max_epu16(a.0, b.0) })
541 }
542
543 /// Lanewise `max(a, b)` with lanes as `u32`.
544 /// ```
545 /// # use safe_arch::*;
546 /// let a = m128i::from([1, 200, 3, 4]);
547 /// let b = m128i::from([5, 6, 7, 8]);
548 /// let c: [u32; 4] = max_u32_m128i(a, b).into();
549 /// assert_eq!(c, [5, 200, 7, 8]);
550 /// ```
551 #[must_use]
552 #[inline(always)]
553 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
554 pub fn max_u32_m128i(a: m128i, b: m128i) -> m128i {
555 m128i(unsafe { _mm_max_epu32(a.0, b.0) })
556 }
557
558 /// Lanewise `min(a, b)` with lanes as `i32`.
559 /// ```
560 /// # use safe_arch::*;
561 /// let a = m128i::from([1, 2, 3, 4]);
562 /// let b = m128i::from([5, 6, -7, 8]);
563 /// let c: [i32; 4] = min_i32_m128i(a, b).into();
564 /// assert_eq!(c, [1, 2, -7, 4]);
565 /// ```
566 #[must_use]
567 #[inline(always)]
568 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
569 pub fn min_i32_m128i(a: m128i, b: m128i) -> m128i {
570 m128i(unsafe { _mm_min_epi32(a.0, b.0) })
571 }
572
573 /// Lanewise `min(a, b)` with lanes as `i8`.
574 /// ```
575 /// # use safe_arch::*;
576 /// let a = m128i::from([0_i8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 127]);
577 /// let b = m128i::from([0_i8, 11, 2, -13, 4, 15, 6, -17, -8, 19, -20, 21, 22, -23, 24, 127]);
578 /// let c: [i8; 16] = min_i8_m128i(a, b).into();
579 /// assert_eq!(c, [0_i8, 1, 2, -13, 4, 5, 6, -17, -8, 9, -20, 11, 12, -23, 14, 127]);
580 /// ```
581 #[must_use]
582 #[inline(always)]
583 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
584 pub fn min_i8_m128i(a: m128i, b: m128i) -> m128i {
585 m128i(unsafe { _mm_min_epi8(a.0, b.0) })
586 }
587
588 /// Lanewise `min(a, b)` with lanes as `u16`.
589 /// ```
590 /// # use safe_arch::*;
591 /// let a = m128i::from([1_u16, 2, 300, 400, 1, 2, 3, 4]);
592 /// let b = m128i::from([5_u16, 6, 7, 8, 15, 26, 37, 48]);
593 /// let c: [u16; 8] = min_u16_m128i(a, b).into();
594 /// assert_eq!(c, [1_u16, 2, 7, 8, 1, 2, 3, 4]);
595 /// ```
596 #[must_use]
597 #[inline(always)]
598 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
599 pub fn min_u16_m128i(a: m128i, b: m128i) -> m128i {
600 m128i(unsafe { _mm_min_epu16(a.0, b.0) })
601 }
602
603 /// Lanewise `min(a, b)` with lanes as `u32`.
604 /// ```
605 /// # use safe_arch::*;
606 /// let a = m128i::from([1, 200, 3, 4]);
607 /// let b = m128i::from([5, 6, 7, 8]);
608 /// let c: [u32; 4] = min_u32_m128i(a, b).into();
609 /// assert_eq!(c, [1, 6, 3, 4]);
610 /// ```
611 #[must_use]
612 #[inline(always)]
613 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
614 pub fn min_u32_m128i(a: m128i, b: m128i) -> m128i {
615 m128i(unsafe { _mm_min_epu32(a.0, b.0) })
616 }
617
618 /// Min `u16` value, position, and other lanes zeroed.
619 ///
620 /// ```
621 /// # use safe_arch::*;
622 /// let a = m128i::from([120_u16, 24, 300, 400, 90, 129, 31, 114]);
623 /// let c: [u16; 8] = min_position_u16_m128i(a).into();
624 /// assert_eq!(c, [24_u16, 1, 0, 0, 0, 0, 0, 0]);
625 ///
626 /// // the position favors the leftmost minimum
627 /// let a = m128i::from([120_u16, 24, 24, 400, 90, 129, 31, 114]);
628 /// let c: [u16; 8] = min_position_u16_m128i(a).into();
629 /// assert_eq!(c, [24_u16, 1, 0, 0, 0, 0, 0, 0]);
630 /// ```
631 #[must_use]
632 #[inline(always)]
633 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
634 pub fn min_position_u16_m128i(a: m128i) -> m128i {
635 m128i(unsafe { _mm_minpos_epu16(a.0) })
636 }
637
638 /// Computes eight `u16` "sum of absolute difference" values according to the
639 /// bytes selected.
640 ///
641 /// * `a` can be 0 or 1, and specifies to skip the first fur `$a` values or not.
642 /// * `b` can be 0, 1, 2, or 3 and specifies to skip the first four times that
643 /// many values in `$b`.
644 ///
645 /// This is used for some HD codec thing, and I don't really get what the point
646 /// is, but I'm sure someone uses it. If you can write better docs about what
647 /// this does please file a PR.
648 ///
649 /// ```
650 /// # use safe_arch::*;
651 /// let a = m128i::from([0_u8, 1, 56, 3, 255, 5, 127, 7, 128, 9, 100, 101, 123, 13, 154, 125]);
652 /// let b = m128i::from([12_u8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
653 /// //
654 /// let c: [u16; 8] = multi_packed_sum_abs_diff_u8_m128i::<0b00_00>(a, b).into();
655 /// assert_eq!(c, [66, 319, 301, 390, 376, 263, 253, 236]);
656 /// //
657 /// let c: [u16; 8] = multi_packed_sum_abs_diff_u8_m128i::<0b00_01>(a, b).into();
658 /// assert_eq!(c, [62, 305, 305, 372, 372, 245, 249, 222]);
659 /// //
660 /// let c: [u16; 8] = multi_packed_sum_abs_diff_u8_m128i::<0b00_10>(a, b).into();
661 /// assert_eq!(c, [70, 305, 305, 372, 372, 241, 241, 210]);
662 /// //
663 /// let c: [u16; 8] = multi_packed_sum_abs_diff_u8_m128i::<0b00_11>(a, b).into();
664 /// assert_eq!(c, [78, 305, 305, 372, 372, 241, 241, 210]);
665 /// //
666 /// let c: [u16; 8] = multi_packed_sum_abs_diff_u8_m128i::<0b01_00>(a, b).into();
667 /// assert_eq!(c, [376, 263, 253, 236, 320, 321, 319, 373]);
668 /// //
669 /// let c: [u16; 8] = multi_packed_sum_abs_diff_u8_m128i::<0b01_01>(a, b).into();
670 /// assert_eq!(c, [372, 245, 249, 222, 316, 311, 315, 369]);
671 /// //
672 /// let c: [u16; 8] = multi_packed_sum_abs_diff_u8_m128i::<0b01_10>(a, b).into();
673 /// assert_eq!(c, [372, 241, 241, 210, 300, 295, 299, 353]);
674 /// //
675 /// let c: [u16; 8] = multi_packed_sum_abs_diff_u8_m128i::<0b01_11>(a, b).into();
676 /// assert_eq!(c, [372, 241, 241, 210, 292, 285, 287, 339]);
677 /// ```
678 #[must_use]
679 #[inline(always)]
680 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
681 pub fn multi_packed_sum_abs_diff_u8_m128i<const IMM: i32>(a: m128i, b: m128i) -> m128i {
682 m128i(unsafe { _mm_mpsadbw_epu8(a.0, b.0, IMM) })
683 }
684
685 /// Multiplies the odd `i32` lanes and gives the widened (`i64`) results.
686 ///
687 /// ```
688 /// # use safe_arch::*;
689 /// let a = m128i::from([1, 7, i32::MAX, 7]);
690 /// let b = m128i::from([-5, 7, i32::MAX, 7]);
691 /// let c: [i64; 2] = mul_widen_i32_odd_m128i(a, b).into();
692 /// assert_eq!(c, [(-1 * 5), (i32::MAX as i64 * i32::MAX as i64)]);
693 /// ```
694 #[must_use]
695 #[inline(always)]
696 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
697 pub fn mul_widen_i32_odd_m128i(a: m128i, b: m128i) -> m128i {
698 m128i(unsafe { _mm_mul_epi32(a.0, b.0) })
699 }
700
701 /// Lanewise `a * b` with 32-bit lanes.
702 ///
703 /// This keeps the low 32-bits from each 64-bit output,
704 /// so it actually works for both `i32` and `u32`.
705 /// ```
706 /// # use safe_arch::*;
707 /// let ai = m128i::from([1, 2000000, -300, 45689]);
708 /// let bi = m128i::from([5, 6000000, 700, -89109]);
709 /// let ci: [i32; 4] = mul_32_m128i(ai, bi).into();
710 /// assert_eq!(ci, [5, -138625024, -210000, 223666195]);
711 ///
712 /// let au = m128i::from([u32::MAX, 26, 5678, 1234567890]);
713 /// let bu = m128i::from([u32::MAX, 74, 9101112, 765]);
714 /// let cu: [u32; 4] = mul_32_m128i(au, bu).into();
715 /// assert_eq!(cu, [1, 1924, 136506384, 3846598026]);
716 /// ```
717 #[must_use]
718 #[inline(always)]
719 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
720 pub fn mul_32_m128i(a: m128i, b: m128i) -> m128i {
721 m128i(unsafe { _mm_mullo_epi32(a.0, b.0) })
722 }
723
724 /// Saturating convert `i32` to `u16`, and pack the values.
725 /// ```
726 /// # use safe_arch::*;
727 /// let a = m128i::from([1, 2, 3, 4]);
728 /// let b = m128i::from([9, -10, -11, i32::MAX]);
729 /// let c: [u16; 8] = pack_i32_to_u16_m128i(a, b).into();
730 /// assert_eq!(c, [1, 2, 3, 4, 9, 0, 0, u16::MAX]);
731 /// ```
732 #[must_use]
733 #[inline(always)]
734 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
735 pub fn pack_i32_to_u16_m128i(a: m128i, b: m128i) -> m128i {
736 m128i(unsafe { _mm_packus_epi32(a.0, b.0) })
737 }
738
739 /// Rounds each lane in the style specified.
740 ///
741 /// ```
742 /// # use safe_arch::*;
743 /// let a = m128d::from_array([-0.1, 1.6]);
744 /// //
745 /// assert_eq!(round_m128d::<{ round_op!(Nearest) }>(a).to_array(), [0.0, 2.0]);
746 /// //
747 /// assert_eq!(round_m128d::<{ round_op!(NegInf) }>(a).to_array(), [-1.0, 1.0]);
748 /// //
749 /// assert_eq!(round_m128d::<{ round_op!(PosInf) }>(a).to_array(), [0.0, 2.0]);
750 /// //
751 /// assert_eq!(round_m128d::<{ round_op!(Zero) }>(a).to_array(), [0.0, 1.0]);
752 /// ```
753 #[must_use]
754 #[inline(always)]
755 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
756 pub fn round_m128d<const MODE: i32>(a: m128d) -> m128d {
757 m128d(unsafe { _mm_round_pd(a.0, MODE) })
758 }
759
760 /// Rounds `$b` low as specified, keeps `$a` high.
761 ///
762 /// ```
763 /// # use safe_arch::*;
764 /// let a = m128d::from_array([f64::NAN, 900.0]);
765 /// //
766 /// let b = m128d::from_array([-0.1, f64::NAN]);
767 /// //
768 /// assert_eq!(round_m128d_s::<{ round_op!(Nearest) }>(a, b).to_array(), [0.0, 900.0]);
769 /// assert_eq!(round_m128d_s::<{ round_op!(NegInf) }>(a, b).to_array(), [-1.0, 900.0]);
770 /// //
771 /// let b = m128d::from_array([2.4, f64::NAN]);
772 /// //
773 /// assert_eq!(round_m128d_s::<{ round_op!(PosInf) }>(a, b).to_array(), [3.0, 900.0]);
774 /// assert_eq!(round_m128d_s::<{ round_op!(Zero) }>(a, b).to_array(), [2.0, 900.0]);
775 /// ```
776 #[must_use]
777 #[inline(always)]
778 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
779 pub fn round_m128d_s<const MODE: i32>(a: m128d, b: m128d) -> m128d {
780 m128d(unsafe { _mm_round_sd(a.0, b.0, MODE) })
781 }
782
783 /// Rounds each lane in the style specified.
784 ///
785 /// ```
786 /// # use safe_arch::*;
787 /// let a = m128::from_array([-0.1, 1.6, 3.3, 4.5]);
788 /// //
789 /// assert_eq!(round_m128::<{ round_op!(Nearest) }>(a).to_array(), [0.0, 2.0, 3.0, 4.0]);
790 /// //
791 /// assert_eq!(round_m128::<{ round_op!(NegInf) }>(a).to_array(), [-1.0, 1.0, 3.0, 4.0]);
792 /// //
793 /// assert_eq!(round_m128::<{ round_op!(PosInf) }>(a).to_array(), [0.0, 2.0, 4.0, 5.0]);
794 /// //
795 /// assert_eq!(round_m128::<{ round_op!(Zero) }>(a).to_array(), [0.0, 1.0, 3.0, 4.0]);
796 /// ```
797 #[must_use]
798 #[inline(always)]
799 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
800 pub fn round_m128<const MODE: i32>(a: m128) -> m128 {
801 m128(unsafe { _mm_round_ps(a.0, MODE) })
802 }
803
804 /// Rounds `$b` low as specified, other lanes use `$a`.
805 ///
806 /// ```
807 /// # use safe_arch::*;
808 /// let a = m128::from_array([f32::NAN, 6.0, 7.0, 8.0]);
809 /// //
810 /// let b = m128::from_array([-0.1, f32::NAN, f32::NAN, f32::NAN]);
811 /// //
812 /// assert_eq!(round_m128_s::<{ round_op!(Nearest) }>(a, b).to_array(), [0.0, 6.0, 7.0, 8.0]);
813 /// assert_eq!(round_m128_s::<{ round_op!(NegInf) }>(a, b).to_array(), [-1.0, 6.0, 7.0, 8.0]);
814 /// //
815 /// let b = m128::from_array([2.4, f32::NAN, f32::NAN, f32::NAN]);
816 /// //
817 /// assert_eq!(round_m128_s::<{ round_op!(PosInf) }>(a, b).to_array(), [3.0, 6.0, 7.0, 8.0]);
818 /// assert_eq!(round_m128_s::<{ round_op!(Zero) }>(a, b).to_array(), [2.0, 6.0, 7.0, 8.0]);
819 /// ```
820 #[must_use]
821 #[inline(always)]
822 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
823 pub fn round_m128_s<const MODE: i32>(a: m128, b: m128) -> m128 {
824 m128(unsafe { _mm_round_ss(a.0, b.0, MODE) })
825 }
826
827 /// Computes the bitwise AND of 256 bits in `a` and
828 /// `b`, returns 1 if the result is zero, otherwise 0.
829 /// * **Intrinsic:** [`_mm_testz_si128`]
830 /// * **Assembly:** ptest xmm, xmm
831 #[must_use]
832 #[inline(always)]
833 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
834 pub fn testz_m128i(a: m128i, b: m128i) -> i32 {
835 unsafe { _mm_testz_si128(a.0, b.0) }
836 }
837
838 /// Compute the bitwise NOT of `a` and then AND with `b`,
839 /// returns 1 if the result is zero, otherwise 0.
840 /// * **Intrinsic:** [`_mm_testc_si128`]
841 /// * **Assembly:** ptest xmm, xmm
842 #[must_use]
843 #[inline(always)]
844 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
845 pub fn testc_m128i(a: m128i, b: m128i) -> i32 {
846 unsafe { _mm_testc_si128(a.0, b.0) }
847 }
848
849 /// Tests if all bits are 1.
850 /// * **Intrinsic:** [`_mm_test_all_ones`]
851 /// * **Assembly:** pcmpeqd xmm, xmm / ptest xmm, xmm
852 ///
853 /// ```
854 /// # use safe_arch::*;
855 /// let a = m128i::from(0_u128);
856 /// let b = m128i::from(u128::MAX);
857 /// assert_eq!(test_all_ones_m128i(a), 0);
858 /// assert_eq!(test_all_ones_m128i(b), 1);
859 /// ```
860 #[must_use]
861 #[inline(always)]
862 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
863 pub fn test_all_ones_m128i(a: m128i) -> i32 {
864 unsafe { _mm_test_all_ones(a.0) }
865 }
866
867 /// Returns if all masked bits are 0, `(a & mask) as u128 == 0`
868 /// * **Intrinsic:** [`_mm_test_all_zeros`]
869 /// * **Assembly:** ptest xmm, xmm
870 ///
871 /// ```
872 /// # use safe_arch::*;
873 /// let a = m128i::from(0b111_u128);
874 /// let mask = m128i::from(u128::MAX);
875 /// assert_eq!(test_all_zeroes_m128i(a, mask), 0);
876 /// //
877 /// let a = m128i::from(0b0_u128);
878 /// let mask = m128i::from(u128::MAX);
879 /// assert_eq!(test_all_zeroes_m128i(a, mask), 1);
880 /// //
881 /// let a = m128i::from(0b1_0000_u128);
882 /// let mask = m128i::from(0b0_1111_u128);
883 /// assert_eq!(test_all_zeroes_m128i(a, mask), 1);
884 /// ```
885 #[must_use]
886 #[inline(always)]
887 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
888 pub fn test_all_zeroes_m128i(a: m128i, mask: m128i) -> i32 {
889 unsafe { _mm_test_all_zeros(a.0, mask.0) }
890 }
891
892 /// Returns if, among the masked bits, there's both 0s and 1s
893 /// * **Intrinsic:** [`_mm_test_mix_ones_zeros`]
894 /// * **Assembly:** ptest xmm, xmm
895 ///
896 /// * Zero Flag = `(a & mask) as u128 == 0`
897 /// * Carry Flag = `((!a) & mask) as u128 == 0`
898 /// * Return `ZeroFlag == 0 && Carry Flag == 0`
899 ///
900 /// ```
901 /// # use safe_arch::*;
902 /// let a = m128i::from(0b111_u128);
903 /// let mask = m128i::from(u128::MAX);
904 /// assert_eq!(test_mixed_ones_and_zeroes_m128i(a, mask), 1);
905 /// //
906 /// let a = m128i::from(0b0_u128);
907 /// let mask = m128i::from(u128::MAX);
908 /// assert_eq!(test_mixed_ones_and_zeroes_m128i(a, mask), 0);
909 /// //
910 /// let a = m128i::from(0b1_0000_u128);
911 /// let mask = m128i::from(0b0_1111_u128);
912 /// assert_eq!(test_mixed_ones_and_zeroes_m128i(a, mask), 0);
913 /// //
914 /// let a = m128i::from(0b1_0000_u128);
915 /// let mask = m128i::from(0b1_1111_u128);
916 /// assert_eq!(test_mixed_ones_and_zeroes_m128i(a, mask), 1);
917 /// ```
918 #[must_use]
919 #[inline(always)]
920 #[cfg_attr(docsrs, doc(cfg(target_feature = "sse4.1")))]
921 pub fn test_mixed_ones_and_zeroes_m128i(a: m128i, mask: m128i) -> i32 {
922 unsafe { _mm_test_mix_ones_zeros(a.0, mask.0) }
923 }
924