Skip to main content

miri/shims/x86/
mod.rs

1use rustc_abi::{CanonAbi, FieldIdx, Size};
2use rustc_apfloat::Float;
3use rustc_apfloat::ieee::Single;
4use rustc_middle::ty::Ty;
5use rustc_middle::{mir, ty};
6use rustc_span::Symbol;
7use rustc_target::callconv::FnAbi;
8use rustc_target::spec::Arch;
9
10use self::helpers::bool_to_simd_element;
11use crate::*;
12
13mod aesni;
14mod avx;
15mod avx2;
16mod avx512;
17mod bmi;
18mod gfni;
19mod sha;
20mod sse;
21mod sse2;
22mod sse3;
23mod sse41;
24mod sse42;
25mod ssse3;
26
27impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
28pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
29    fn emulate_x86_intrinsic(
30        &mut self,
31        link_name: Symbol,
32        abi: &FnAbi<'tcx, Ty<'tcx>>,
33        args: &[OpTy<'tcx>],
34        dest: &MPlaceTy<'tcx>,
35    ) -> InterpResult<'tcx, EmulateItemResult> {
36        let this = self.eval_context_mut();
37        // Prefix should have already been checked.
38        let unprefixed_name = link_name.as_str().strip_prefix("llvm.x86.").unwrap();
39        match unprefixed_name {
40            // Used to implement the `_addcarry_u{32, 64}` and the `_subborrow_u{32, 64}` functions.
41            // Computes a + b or a - b with input and output carry/borrow. The input carry/borrow is an 8-bit
42            // value, which is interpreted as 1 if it is non-zero. The output carry/borrow is an 8-bit value that will be 0 or 1.
43            // https://www.intel.com/content/www/us/en/docs/cpp-compiler/developer-guide-reference/2021-8/addcarry-u32-addcarry-u64.html
44            // https://www.intel.com/content/www/us/en/docs/cpp-compiler/developer-guide-reference/2021-8/subborrow-u32-subborrow-u64.html
45            "addcarry.32" | "addcarry.64" | "subborrow.32" | "subborrow.64" => {
46                if unprefixed_name.ends_with("64") && this.tcx.sess.target.arch != Arch::X86_64 {
47                    return interp_ok(EmulateItemResult::NotSupported);
48                }
49
50                let [cb_in, a, b] =
51                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
52                let op = if unprefixed_name.starts_with("add") {
53                    mir::BinOp::AddWithOverflow
54                } else {
55                    mir::BinOp::SubWithOverflow
56                };
57
58                let (sum, cb_out) = carrying_add(this, cb_in, a, b, op)?;
59                this.write_scalar(cb_out, &this.project_field(dest, FieldIdx::ZERO)?)?;
60                this.write_immediate(*sum, &this.project_field(dest, FieldIdx::ONE)?)?;
61            }
62
63            // Used to implement the `_mm_pause` function.
64            // The intrinsic is used to hint the processor that the code is in a spin-loop.
65            // It is compiled down to a `pause` instruction. When SSE2 is not available,
66            // the instruction behaves like a no-op, so it is always safe to call the
67            // intrinsic.
68            "sse2.pause" => {
69                let [] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
70                // Only exhibit the spin-loop hint behavior when SSE2 is enabled.
71                if this.tcx.sess.unstable_target_features.contains(&Symbol::intern("sse2")) {
72                    this.yield_active_thread();
73                }
74            }
75
76            "pclmulqdq" | "pclmulqdq.256" | "pclmulqdq.512" => {
77                let mut len = 2; // in units of 64bits
78                this.expect_target_feature_for_intrinsic(link_name, "pclmulqdq")?;
79                if unprefixed_name.ends_with(".256") {
80                    this.expect_target_feature_for_intrinsic(link_name, "vpclmulqdq")?;
81                    len = 4;
82                } else if unprefixed_name.ends_with(".512") {
83                    this.expect_target_feature_for_intrinsic(link_name, "vpclmulqdq")?;
84                    this.expect_target_feature_for_intrinsic(link_name, "avx512f")?;
85                    len = 8;
86                }
87
88                let [left, right, imm] =
89                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
90
91                pclmulqdq(this, left, right, imm, dest, len)?;
92            }
93
94            name if name.starts_with("bmi.") => {
95                return bmi::EvalContextExt::emulate_x86_bmi_intrinsic(
96                    this, link_name, abi, args, dest,
97                );
98            }
99            // The GFNI extension does not get its own namespace.
100            // Check for instruction names instead.
101            name if name.starts_with("vgf2p8affine") || name.starts_with("vgf2p8mulb") => {
102                return gfni::EvalContextExt::emulate_x86_gfni_intrinsic(
103                    this, link_name, abi, args, dest,
104                );
105            }
106            name if name.starts_with("sha") => {
107                return sha::EvalContextExt::emulate_x86_sha_intrinsic(
108                    this, link_name, abi, args, dest,
109                );
110            }
111            name if name.starts_with("sse.") => {
112                return sse::EvalContextExt::emulate_x86_sse_intrinsic(
113                    this, link_name, abi, args, dest,
114                );
115            }
116            name if name.starts_with("sse2.") => {
117                return sse2::EvalContextExt::emulate_x86_sse2_intrinsic(
118                    this, link_name, abi, args, dest,
119                );
120            }
121            name if name.starts_with("sse3.") => {
122                return sse3::EvalContextExt::emulate_x86_sse3_intrinsic(
123                    this, link_name, abi, args, dest,
124                );
125            }
126            name if name.starts_with("ssse3.") => {
127                return ssse3::EvalContextExt::emulate_x86_ssse3_intrinsic(
128                    this, link_name, abi, args, dest,
129                );
130            }
131            name if name.starts_with("sse41.") => {
132                return sse41::EvalContextExt::emulate_x86_sse41_intrinsic(
133                    this, link_name, abi, args, dest,
134                );
135            }
136            name if name.starts_with("sse42.") => {
137                return sse42::EvalContextExt::emulate_x86_sse42_intrinsic(
138                    this, link_name, abi, args, dest,
139                );
140            }
141            name if name.starts_with("aesni.") => {
142                return aesni::EvalContextExt::emulate_x86_aesni_intrinsic(
143                    this, link_name, abi, args, dest,
144                );
145            }
146            name if name.starts_with("avx.") => {
147                return avx::EvalContextExt::emulate_x86_avx_intrinsic(
148                    this, link_name, abi, args, dest,
149                );
150            }
151            name if name.starts_with("avx2.") => {
152                return avx2::EvalContextExt::emulate_x86_avx2_intrinsic(
153                    this, link_name, abi, args, dest,
154                );
155            }
156            name if name.starts_with("avx512.") => {
157                return avx512::EvalContextExt::emulate_x86_avx512_intrinsic(
158                    this, link_name, abi, args, dest,
159                );
160            }
161
162            _ => return interp_ok(EmulateItemResult::NotSupported),
163        }
164        interp_ok(EmulateItemResult::NeedsReturn)
165    }
166}
167
168#[derive(Copy, Clone)]
169enum FloatBinOp {
170    /// Comparison
171    ///
172    /// The semantics of this operator is a case distinction: we compare the two operands,
173    /// and then we return one of the four booleans `gt`, `lt`, `eq`, `unord` depending on
174    /// which class they fall into.
175    ///
176    /// AVX supports all 16 combinations, SSE only a subset
177    ///
178    /// <https://www.felixcloutier.com/x86/cmpss>
179    /// <https://www.felixcloutier.com/x86/cmpps>
180    /// <https://www.felixcloutier.com/x86/cmpsd>
181    /// <https://www.felixcloutier.com/x86/cmppd>
182    Cmp {
183        /// Result when lhs < rhs
184        gt: bool,
185        /// Result when lhs > rhs
186        lt: bool,
187        /// Result when lhs == rhs
188        eq: bool,
189        /// Result when lhs is NaN or rhs is NaN
190        unord: bool,
191    },
192    /// Minimum value (with SSE semantics)
193    ///
194    /// <https://www.felixcloutier.com/x86/minss>
195    /// <https://www.felixcloutier.com/x86/minps>
196    /// <https://www.felixcloutier.com/x86/minsd>
197    /// <https://www.felixcloutier.com/x86/minpd>
198    Min,
199    /// Maximum value (with SSE semantics)
200    ///
201    /// <https://www.felixcloutier.com/x86/maxss>
202    /// <https://www.felixcloutier.com/x86/maxps>
203    /// <https://www.felixcloutier.com/x86/maxsd>
204    /// <https://www.felixcloutier.com/x86/maxpd>
205    Max,
206}
207
208impl FloatBinOp {
209    /// Convert from the `imm` argument used to specify the comparison
210    /// operation in intrinsics such as `llvm.x86.sse.cmp.ss`.
211    fn cmp_from_imm<'tcx>(
212        ecx: &crate::MiriInterpCx<'tcx>,
213        imm: i8,
214        intrinsic: Symbol,
215    ) -> InterpResult<'tcx, Self> {
216        // Only bits 0..=4 are used, remaining should be zero.
217        if imm & !0b1_1111 != 0 {
218            panic!("invalid `imm` parameter of {intrinsic}: 0x{imm:x}");
219        }
220        // Bit 4 specifies whether the operation is quiet or signaling, which
221        // we do not care in Miri.
222        // Bits 0..=2 specifies the operation.
223        // `gt` indicates the result to be returned when the LHS is strictly
224        // greater than the RHS, and so on.
225        let (gt, lt, eq, mut unord) = match imm & 0b111 {
226            // Equal
227            0x0 => (false, false, true, false),
228            // Less-than
229            0x1 => (false, true, false, false),
230            // Less-or-equal
231            0x2 => (false, true, true, false),
232            // Unordered (either is NaN)
233            0x3 => (false, false, false, true),
234            // Not equal
235            0x4 => (true, true, false, true),
236            // Not less-than
237            0x5 => (true, false, true, true),
238            // Not less-or-equal
239            0x6 => (true, false, false, true),
240            // Ordered (neither is NaN)
241            0x7 => (true, true, true, false),
242            _ => unreachable!(),
243        };
244        // When bit 3 is 1 (only possible in AVX), unord is toggled.
245        if imm & 0b1000 != 0 {
246            ecx.expect_target_feature_for_intrinsic(intrinsic, "avx")?;
247            unord = !unord;
248        }
249        interp_ok(Self::Cmp { gt, lt, eq, unord })
250    }
251}
252
253/// Performs `which` scalar operation on `left` and `right` and returns
254/// the result.
255fn bin_op_float<'tcx, F: rustc_apfloat::Float>(
256    which: FloatBinOp,
257    left: &ImmTy<'tcx>,
258    right: &ImmTy<'tcx>,
259) -> InterpResult<'tcx, Scalar> {
260    match which {
261        FloatBinOp::Cmp { gt, lt, eq, unord } => {
262            let left = left.to_scalar().to_float::<F>()?;
263            let right = right.to_scalar().to_float::<F>()?;
264
265            let res = match left.partial_cmp(&right) {
266                None => unord,
267                Some(std::cmp::Ordering::Less) => lt,
268                Some(std::cmp::Ordering::Equal) => eq,
269                Some(std::cmp::Ordering::Greater) => gt,
270            };
271            interp_ok(bool_to_simd_element(res, Size::from_bits(F::BITS)))
272        }
273        FloatBinOp::Min => {
274            let left_scalar = left.to_scalar();
275            let left = left_scalar.to_float::<F>()?;
276            let right_scalar = right.to_scalar();
277            let right = right_scalar.to_float::<F>()?;
278            // SSE semantics to handle zero and NaN. Note that `x == F::ZERO`
279            // is true when `x` is either +0 or -0.
280            if (left == F::ZERO && right == F::ZERO)
281                || left.is_nan()
282                || right.is_nan()
283                || left >= right
284            {
285                interp_ok(right_scalar)
286            } else {
287                interp_ok(left_scalar)
288            }
289        }
290        FloatBinOp::Max => {
291            let left_scalar = left.to_scalar();
292            let left = left_scalar.to_float::<F>()?;
293            let right_scalar = right.to_scalar();
294            let right = right_scalar.to_float::<F>()?;
295            // SSE semantics to handle zero and NaN. Note that `x == F::ZERO`
296            // is true when `x` is either +0 or -0.
297            if (left == F::ZERO && right == F::ZERO)
298                || left.is_nan()
299                || right.is_nan()
300                || left <= right
301            {
302                interp_ok(right_scalar)
303            } else {
304                interp_ok(left_scalar)
305            }
306        }
307    }
308}
309
310/// Performs `which` operation on the first component of `left` and `right`
311/// and copies the other components from `left`. The result is stored in `dest`.
312fn bin_op_simd_float_first<'tcx, F: rustc_apfloat::Float>(
313    ecx: &mut crate::MiriInterpCx<'tcx>,
314    which: FloatBinOp,
315    left: &OpTy<'tcx>,
316    right: &OpTy<'tcx>,
317    dest: &MPlaceTy<'tcx>,
318) -> InterpResult<'tcx, ()> {
319    let (left, left_len) = ecx.project_to_simd(left)?;
320    let (right, right_len) = ecx.project_to_simd(right)?;
321    let (dest, dest_len) = ecx.project_to_simd(dest)?;
322
323    assert_eq!(dest_len, left_len);
324    assert_eq!(dest_len, right_len);
325
326    let res0 = bin_op_float::<F>(
327        which,
328        &ecx.read_immediate(&ecx.project_index(&left, 0)?)?,
329        &ecx.read_immediate(&ecx.project_index(&right, 0)?)?,
330    )?;
331    ecx.write_scalar(res0, &ecx.project_index(&dest, 0)?)?;
332
333    for i in 1..dest_len {
334        ecx.copy_op(&ecx.project_index(&left, i)?, &ecx.project_index(&dest, i)?)?;
335    }
336
337    interp_ok(())
338}
339
340/// Performs `which` operation on each component of `left` and
341/// `right`, storing the result is stored in `dest`.
342fn bin_op_simd_float_all<'tcx, F: rustc_apfloat::Float>(
343    ecx: &mut crate::MiriInterpCx<'tcx>,
344    which: FloatBinOp,
345    left: &OpTy<'tcx>,
346    right: &OpTy<'tcx>,
347    dest: &MPlaceTy<'tcx>,
348) -> InterpResult<'tcx, ()> {
349    let (left, left_len) = ecx.project_to_simd(left)?;
350    let (right, right_len) = ecx.project_to_simd(right)?;
351    let (dest, dest_len) = ecx.project_to_simd(dest)?;
352
353    assert_eq!(dest_len, left_len);
354    assert_eq!(dest_len, right_len);
355
356    for i in 0..dest_len {
357        let left = ecx.read_immediate(&ecx.project_index(&left, i)?)?;
358        let right = ecx.read_immediate(&ecx.project_index(&right, i)?)?;
359        let dest = ecx.project_index(&dest, i)?;
360
361        let res = bin_op_float::<F>(which, &left, &right)?;
362        ecx.write_scalar(res, &dest)?;
363    }
364
365    interp_ok(())
366}
367
368#[derive(Copy, Clone)]
369enum FloatUnaryOp {
370    /// Approximation of 1/x
371    ///
372    /// <https://www.felixcloutier.com/x86/rcpss>
373    /// <https://www.felixcloutier.com/x86/rcpps>
374    Rcp,
375    /// Approximation of 1/sqrt(x)
376    ///
377    /// <https://www.felixcloutier.com/x86/rsqrtss>
378    /// <https://www.felixcloutier.com/x86/rsqrtps>
379    Rsqrt,
380}
381
382/// Performs `which` scalar operation on `op` and returns the result.
383fn unary_op_f32<'tcx>(
384    ecx: &mut crate::MiriInterpCx<'tcx>,
385    which: FloatUnaryOp,
386    op: &ImmTy<'tcx>,
387) -> InterpResult<'tcx, Scalar> {
388    match which {
389        FloatUnaryOp::Rcp => {
390            let op = op.to_scalar().to_f32()?;
391            let div = (Single::from_u128(1).value / op).value;
392            // Apply a relative error with a magnitude on the order of 2^-12 to simulate the
393            // inaccuracy of RCP.
394            let res = math::apply_random_float_error(ecx, div, -12);
395            interp_ok(Scalar::from_f32(res))
396        }
397        FloatUnaryOp::Rsqrt => {
398            let op = op.to_scalar().to_f32()?;
399            let rsqrt = (Single::from_u128(1).value / math::sqrt(op)).value;
400            // Apply a relative error with a magnitude on the order of 2^-12 to simulate the
401            // inaccuracy of RSQRT.
402            let res = math::apply_random_float_error(ecx, rsqrt, -12);
403            interp_ok(Scalar::from_f32(res))
404        }
405    }
406}
407
408/// Performs `which` operation on the first component of `op` and copies
409/// the other components. The result is stored in `dest`.
410fn unary_op_ss<'tcx>(
411    ecx: &mut crate::MiriInterpCx<'tcx>,
412    which: FloatUnaryOp,
413    op: &OpTy<'tcx>,
414    dest: &MPlaceTy<'tcx>,
415) -> InterpResult<'tcx, ()> {
416    let (op, op_len) = ecx.project_to_simd(op)?;
417    let (dest, dest_len) = ecx.project_to_simd(dest)?;
418
419    assert_eq!(dest_len, op_len);
420
421    let res0 = unary_op_f32(ecx, which, &ecx.read_immediate(&ecx.project_index(&op, 0)?)?)?;
422    ecx.write_scalar(res0, &ecx.project_index(&dest, 0)?)?;
423
424    for i in 1..dest_len {
425        ecx.copy_op(&ecx.project_index(&op, i)?, &ecx.project_index(&dest, i)?)?;
426    }
427
428    interp_ok(())
429}
430
431/// Performs `which` operation on each component of `op`, storing the
432/// result is stored in `dest`.
433fn unary_op_ps<'tcx>(
434    ecx: &mut crate::MiriInterpCx<'tcx>,
435    which: FloatUnaryOp,
436    op: &OpTy<'tcx>,
437    dest: &MPlaceTy<'tcx>,
438) -> InterpResult<'tcx, ()> {
439    let (op, op_len) = ecx.project_to_simd(op)?;
440    let (dest, dest_len) = ecx.project_to_simd(dest)?;
441
442    assert_eq!(dest_len, op_len);
443
444    for i in 0..dest_len {
445        let op = ecx.read_immediate(&ecx.project_index(&op, i)?)?;
446        let dest = ecx.project_index(&dest, i)?;
447
448        let res = unary_op_f32(ecx, which, &op)?;
449        ecx.write_scalar(res, &dest)?;
450    }
451
452    interp_ok(())
453}
454
455enum ShiftOp {
456    /// Shift left, logically (shift in zeros) -- same as shift left, arithmetically
457    Left,
458    /// Shift right, logically (shift in zeros)
459    RightLogic,
460    /// Shift right, arithmetically (shift in sign)
461    RightArith,
462}
463
464/// Shifts each element of `left` by a scalar amount. The shift amount
465/// is determined by the lowest 64 bits of `right` (which is a 128-bit vector).
466///
467/// For logic shifts, when right is larger than BITS - 1, zero is produced.
468/// For arithmetic right-shifts, when right is larger than BITS - 1, the sign
469/// bit is copied to all bits.
470fn shift_simd_by_scalar<'tcx>(
471    ecx: &mut crate::MiriInterpCx<'tcx>,
472    left: &OpTy<'tcx>,
473    right: &OpTy<'tcx>,
474    which: ShiftOp,
475    dest: &MPlaceTy<'tcx>,
476) -> InterpResult<'tcx, ()> {
477    let (left, left_len) = ecx.project_to_simd(left)?;
478    let (dest, dest_len) = ecx.project_to_simd(dest)?;
479
480    assert_eq!(dest_len, left_len);
481    // `right` may have a different length, and we only care about its
482    // lowest 64bit anyway.
483
484    // Get the 64-bit shift operand and convert it to the type expected
485    // by checked_{shl,shr} (u32).
486    // It is ok to saturate the value to u32::MAX because any value
487    // above BITS - 1 will produce the same result.
488    let shift = u32::try_from(extract_first_u64(ecx, right)?).unwrap_or(u32::MAX);
489
490    for i in 0..dest_len {
491        let left = ecx.read_scalar(&ecx.project_index(&left, i)?)?;
492        let dest = ecx.project_index(&dest, i)?;
493
494        let res = match which {
495            ShiftOp::Left => {
496                let left = left.to_uint(dest.layout.size)?;
497                let res = left.checked_shl(shift).unwrap_or(0);
498                // `truncate` is needed as left-shift can make the absolute value larger.
499                Scalar::from_uint(dest.layout.size.truncate(res), dest.layout.size)
500            }
501            ShiftOp::RightLogic => {
502                let left = left.to_uint(dest.layout.size)?;
503                let res = left.checked_shr(shift).unwrap_or(0);
504                // No `truncate` needed as right-shift can only make the absolute value smaller.
505                Scalar::from_uint(res, dest.layout.size)
506            }
507            ShiftOp::RightArith => {
508                let left = left.to_int(dest.layout.size)?;
509                // On overflow, copy the sign bit to the remaining bits
510                let res = left.checked_shr(shift).unwrap_or(left >> 127);
511                // No `truncate` needed as right-shift can only make the absolute value smaller.
512                Scalar::from_int(res, dest.layout.size)
513            }
514        };
515        ecx.write_scalar(res, &dest)?;
516    }
517
518    interp_ok(())
519}
520
521/// Takes a 128-bit vector, transmutes it to `[u64; 2]` and extracts
522/// the first value.
523fn extract_first_u64<'tcx>(
524    ecx: &crate::MiriInterpCx<'tcx>,
525    op: &OpTy<'tcx>,
526) -> InterpResult<'tcx, u64> {
527    // Transmute vector to `[u64; 2]`
528    let array_layout = ecx.layout_of(Ty::new_array(ecx.tcx.tcx, ecx.tcx.types.u64, 2))?;
529    let op = op.transmute(array_layout, ecx)?;
530
531    // Get the first u64 from the array
532    ecx.read_scalar(&ecx.project_index(&op, 0)?)?.to_u64()
533}
534
535// Rounds the first element of `right` according to `rounding`
536// and copies the remaining elements from `left`.
537fn round_first<'tcx, F: rustc_apfloat::Float>(
538    ecx: &mut crate::MiriInterpCx<'tcx>,
539    left: &OpTy<'tcx>,
540    right: &OpTy<'tcx>,
541    rounding: &OpTy<'tcx>,
542    dest: &MPlaceTy<'tcx>,
543) -> InterpResult<'tcx, ()> {
544    let (left, left_len) = ecx.project_to_simd(left)?;
545    let (right, right_len) = ecx.project_to_simd(right)?;
546    let (dest, dest_len) = ecx.project_to_simd(dest)?;
547
548    assert_eq!(dest_len, left_len);
549    assert_eq!(dest_len, right_len);
550
551    let rounding = rounding_from_imm(ecx.read_scalar(rounding)?.to_i32()?)?;
552
553    let op0: F = ecx.read_scalar(&ecx.project_index(&right, 0)?)?.to_float()?;
554    let res = op0.round_to_integral(rounding).value;
555    ecx.write_scalar(
556        Scalar::from_uint(res.to_bits(), Size::from_bits(F::BITS)),
557        &ecx.project_index(&dest, 0)?,
558    )?;
559
560    for i in 1..dest_len {
561        ecx.copy_op(&ecx.project_index(&left, i)?, &ecx.project_index(&dest, i)?)?;
562    }
563
564    interp_ok(())
565}
566
567// Rounds all elements of `op` according to `rounding`.
568fn round_all<'tcx, F: rustc_apfloat::Float>(
569    ecx: &mut crate::MiriInterpCx<'tcx>,
570    op: &OpTy<'tcx>,
571    rounding: &OpTy<'tcx>,
572    dest: &MPlaceTy<'tcx>,
573) -> InterpResult<'tcx, ()> {
574    let (op, op_len) = ecx.project_to_simd(op)?;
575    let (dest, dest_len) = ecx.project_to_simd(dest)?;
576
577    assert_eq!(dest_len, op_len);
578
579    let rounding = rounding_from_imm(ecx.read_scalar(rounding)?.to_i32()?)?;
580
581    for i in 0..dest_len {
582        let op: F = ecx.read_scalar(&ecx.project_index(&op, i)?)?.to_float()?;
583        let res = op.round_to_integral(rounding).value;
584        ecx.write_scalar(
585            Scalar::from_uint(res.to_bits(), Size::from_bits(F::BITS)),
586            &ecx.project_index(&dest, i)?,
587        )?;
588    }
589
590    interp_ok(())
591}
592
593/// Gets equivalent `rustc_apfloat::Round` from rounding mode immediate of
594/// `round.{ss,sd,ps,pd}` intrinsics.
595fn rounding_from_imm<'tcx>(rounding: i32) -> InterpResult<'tcx, rustc_apfloat::Round> {
596    // The fourth bit of `rounding` only affects the SSE status
597    // register, which cannot be accessed from Miri (or from Rust,
598    // for that matter), so we can ignore it.
599    match rounding & !0b1000 {
600        // When the third bit is 0, the rounding mode is determined by the
601        // first two bits.
602        0b000 => interp_ok(rustc_apfloat::Round::NearestTiesToEven),
603        0b001 => interp_ok(rustc_apfloat::Round::TowardNegative),
604        0b010 => interp_ok(rustc_apfloat::Round::TowardPositive),
605        0b011 => interp_ok(rustc_apfloat::Round::TowardZero),
606        // When the third bit is 1, the rounding mode is determined by the
607        // SSE status register. Since we do not support modifying it from
608        // Miri (or Rust), we assume it to be at its default mode (round-to-nearest).
609        0b100..=0b111 => interp_ok(rustc_apfloat::Round::NearestTiesToEven),
610        rounding => panic!("invalid rounding mode 0x{rounding:02x}"),
611    }
612}
613
614/// Converts each element of `op` from floating point to signed integer.
615///
616/// When the input value is NaN or out of range, fall back to minimum value.
617///
618/// If `op` has more elements than `dest`, extra elements are ignored. If `op`
619/// has less elements than `dest`, the rest is filled with zeros.
620fn convert_float_to_int<'tcx>(
621    ecx: &mut crate::MiriInterpCx<'tcx>,
622    op: &OpTy<'tcx>,
623    rnd: rustc_apfloat::Round,
624    dest: &MPlaceTy<'tcx>,
625) -> InterpResult<'tcx, ()> {
626    let (op, op_len) = ecx.project_to_simd(op)?;
627    let (dest, dest_len) = ecx.project_to_simd(dest)?;
628
629    // Output must be *signed* integers.
630    assert!(matches!(dest.layout.field(ecx, 0).ty.kind(), ty::Int(_)));
631
632    for i in 0..op_len.min(dest_len) {
633        let op = ecx.read_immediate(&ecx.project_index(&op, i)?)?;
634        let dest = ecx.project_index(&dest, i)?;
635
636        let res = ecx.float_to_int_checked(&op, dest.layout, rnd)?.unwrap_or_else(|| {
637            // Fallback to minimum according to SSE/AVX semantics.
638            ImmTy::from_int(dest.layout.size.signed_int_min(), dest.layout)
639        });
640        ecx.write_immediate(*res, &dest)?;
641    }
642    // Fill remainder with zeros
643    for i in op_len..dest_len {
644        let dest = ecx.project_index(&dest, i)?;
645        ecx.write_scalar(Scalar::from_int(0, dest.layout.size), &dest)?;
646    }
647
648    interp_ok(())
649}
650
651/// Splits `op` (which must be a SIMD vector) into 128-bit chunks.
652///
653/// Returns a tuple where:
654/// * The first element is the number of 128-bit chunks (let's call it `N`).
655/// * The second element is the number of elements per chunk (let's call it `M`).
656/// * The third element is the `op` vector split into chunks, i.e, it's
657///   type is `[[T; M]; N]` where `T` is the element type of `op`.
658fn split_simd_to_128bit_chunks<'tcx, P: Projectable<'tcx, Provenance>>(
659    ecx: &mut crate::MiriInterpCx<'tcx>,
660    op: &P,
661) -> InterpResult<'tcx, (u64, u64, P)> {
662    let simd_layout = op.layout();
663    let (simd_len, element_ty) = simd_layout.ty.simd_size_and_type(ecx.tcx.tcx);
664
665    assert_eq!(simd_layout.size.bits() % 128, 0);
666    let num_chunks = simd_layout.size.bits() / 128;
667    let items_per_chunk = simd_len.strict_div(num_chunks);
668
669    // Transmute to `[[T; items_per_chunk]; num_chunks]`
670    let chunked_layout = ecx
671        .layout_of(Ty::new_array(
672            ecx.tcx.tcx,
673            Ty::new_array(ecx.tcx.tcx, element_ty, items_per_chunk),
674            num_chunks,
675        ))
676        .unwrap();
677    let chunked_op = op.transmute(chunked_layout, ecx)?;
678
679    interp_ok((num_chunks, items_per_chunk, chunked_op))
680}
681
682/// Horizontally performs `which` operation on adjacent values of
683/// `left` and `right` SIMD vectors and stores the result in `dest`.
684/// "Horizontal" means that the i-th output element is calculated
685/// from the elements 2*i and 2*i+1 of the concatenation of `left` and
686/// `right`.
687///
688/// Each 128-bit chunk is treated independently (i.e., the value for
689/// the is i-th 128-bit chunk of `dest` is calculated with the i-th
690/// 128-bit chunks of `left` and `right`).
691fn horizontal_bin_op<'tcx>(
692    ecx: &mut crate::MiriInterpCx<'tcx>,
693    which: mir::BinOp,
694    saturating: bool,
695    left: &OpTy<'tcx>,
696    right: &OpTy<'tcx>,
697    dest: &MPlaceTy<'tcx>,
698) -> InterpResult<'tcx, ()> {
699    assert_eq!(left.layout, dest.layout);
700    assert_eq!(right.layout, dest.layout);
701
702    let (num_chunks, items_per_chunk, left) = split_simd_to_128bit_chunks(ecx, left)?;
703    let (_, _, right) = split_simd_to_128bit_chunks(ecx, right)?;
704    let (_, _, dest) = split_simd_to_128bit_chunks(ecx, dest)?;
705
706    let middle = items_per_chunk / 2;
707    for i in 0..num_chunks {
708        let left = ecx.project_index(&left, i)?;
709        let right = ecx.project_index(&right, i)?;
710        let dest = ecx.project_index(&dest, i)?;
711
712        for j in 0..items_per_chunk {
713            // `j` is the index in `dest`
714            // `k` is the index of the 2-item chunk in `src`
715            let (k, src) = if j < middle { (j, &left) } else { (j.strict_sub(middle), &right) };
716            // `base_i` is the index of the first item of the 2-item chunk in `src`
717            let base_i = k.strict_mul(2);
718            let lhs = ecx.read_immediate(&ecx.project_index(src, base_i)?)?;
719            let rhs = ecx.read_immediate(&ecx.project_index(src, base_i.strict_add(1))?)?;
720
721            let res = if saturating {
722                Immediate::from(ecx.saturating_arith(which, &lhs, &rhs)?)
723            } else {
724                *ecx.binary_op(which, &lhs, &rhs)?
725            };
726
727            ecx.write_immediate(res, &ecx.project_index(&dest, j)?)?;
728        }
729    }
730
731    interp_ok(())
732}
733
734/// Conditionally multiplies the packed floating-point elements in
735/// `left` and `right` using the high 4 bits in `imm`, sums the calculated
736/// products (up to 4), and conditionally stores the sum in `dest` using
737/// the low 4 bits of `imm`.
738///
739/// Each 128-bit chunk is treated independently (i.e., the value for
740/// the is i-th 128-bit chunk of `dest` is calculated with the i-th
741/// 128-bit blocks of `left` and `right`).
742fn conditional_dot_product<'tcx>(
743    ecx: &mut crate::MiriInterpCx<'tcx>,
744    left: &OpTy<'tcx>,
745    right: &OpTy<'tcx>,
746    imm: &OpTy<'tcx>,
747    dest: &MPlaceTy<'tcx>,
748) -> InterpResult<'tcx, ()> {
749    assert_eq!(left.layout, dest.layout);
750    assert_eq!(right.layout, dest.layout);
751
752    let (num_chunks, items_per_chunk, left) = split_simd_to_128bit_chunks(ecx, left)?;
753    let (_, _, right) = split_simd_to_128bit_chunks(ecx, right)?;
754    let (_, _, dest) = split_simd_to_128bit_chunks(ecx, dest)?;
755
756    let element_layout = left.layout.field(ecx, 0).field(ecx, 0);
757    assert!(items_per_chunk <= 4);
758
759    // `imm` is a `u8` for SSE4.1 or an `i32` for AVX :/
760    let imm = ecx.read_scalar(imm)?.to_uint(imm.layout.size)?;
761
762    for i in 0..num_chunks {
763        let left = ecx.project_index(&left, i)?;
764        let right = ecx.project_index(&right, i)?;
765        let dest = ecx.project_index(&dest, i)?;
766
767        // Calculate dot product
768        // Elements are floating point numbers, but we can use `from_int`
769        // for the initial value because the representation of 0.0 is all zero bits.
770        let mut sum = ImmTy::from_int(0u8, element_layout);
771        for j in 0..items_per_chunk {
772            if imm & (1 << j.strict_add(4)) != 0 {
773                let left = ecx.read_immediate(&ecx.project_index(&left, j)?)?;
774                let right = ecx.read_immediate(&ecx.project_index(&right, j)?)?;
775
776                let mul = ecx.binary_op(mir::BinOp::Mul, &left, &right)?;
777                sum = ecx.binary_op(mir::BinOp::Add, &sum, &mul)?;
778            }
779        }
780
781        // Write to destination (conditioned to imm)
782        for j in 0..items_per_chunk {
783            let dest = ecx.project_index(&dest, j)?;
784
785            if imm & (1 << j) != 0 {
786                ecx.write_immediate(*sum, &dest)?;
787            } else {
788                ecx.write_scalar(Scalar::from_int(0u8, element_layout.size), &dest)?;
789            }
790        }
791    }
792
793    interp_ok(())
794}
795
796/// Calculates two booleans.
797///
798/// The first is true when all the bits of `op & mask` are zero.
799/// The second is true when `(op & mask) == mask`
800fn test_bits_masked<'tcx>(
801    ecx: &crate::MiriInterpCx<'tcx>,
802    op: &OpTy<'tcx>,
803    mask: &OpTy<'tcx>,
804) -> InterpResult<'tcx, (bool, bool)> {
805    assert_eq!(op.layout, mask.layout);
806
807    let (op, op_len) = ecx.project_to_simd(op)?;
808    let (mask, mask_len) = ecx.project_to_simd(mask)?;
809
810    assert_eq!(op_len, mask_len);
811
812    let mut all_zero = true;
813    let mut masked_set = true;
814    for i in 0..op_len {
815        let op = ecx.project_index(&op, i)?;
816        let mask = ecx.project_index(&mask, i)?;
817
818        let op = ecx.read_scalar(&op)?.to_uint(op.layout.size)?;
819        let mask = ecx.read_scalar(&mask)?.to_uint(mask.layout.size)?;
820        all_zero &= (op & mask) == 0;
821        masked_set &= (op & mask) == mask;
822    }
823
824    interp_ok((all_zero, masked_set))
825}
826
827/// Calculates two booleans.
828///
829/// The first is true when the highest bit of each element of `op & mask` is zero.
830/// The second is true when the highest bit of each element of `!op & mask` is zero.
831fn test_high_bits_masked<'tcx>(
832    ecx: &crate::MiriInterpCx<'tcx>,
833    op: &OpTy<'tcx>,
834    mask: &OpTy<'tcx>,
835) -> InterpResult<'tcx, (bool, bool)> {
836    assert_eq!(op.layout, mask.layout);
837
838    let (op, op_len) = ecx.project_to_simd(op)?;
839    let (mask, mask_len) = ecx.project_to_simd(mask)?;
840
841    assert_eq!(op_len, mask_len);
842
843    let high_bit_offset = op.layout.field(ecx, 0).size.bits().strict_sub(1);
844
845    let mut direct = true;
846    let mut negated = true;
847    for i in 0..op_len {
848        let op = ecx.project_index(&op, i)?;
849        let mask = ecx.project_index(&mask, i)?;
850
851        let op = ecx.read_scalar(&op)?.to_uint(op.layout.size)?;
852        let mask = ecx.read_scalar(&mask)?.to_uint(mask.layout.size)?;
853        direct &= (op & mask) >> high_bit_offset == 0;
854        negated &= (!op & mask) >> high_bit_offset == 0;
855    }
856
857    interp_ok((direct, negated))
858}
859
860/// Compute the sum of absolute differences of quadruplets of unsigned
861/// 8-bit integers in `left` and `right`, and store the 16-bit results
862/// in `right`. Quadruplets are selected from `left` and `right` with
863/// offsets specified in `imm`.
864///
865/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_maddubs_epi16>
866/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm256_mpsadbw_epu8>
867///
868/// Each 128-bit chunk is treated independently (i.e., the value for
869/// the is i-th 128-bit chunk of `dest` is calculated with the i-th
870/// 128-bit chunks of `left` and `right`).
871fn mpsadbw<'tcx>(
872    ecx: &mut crate::MiriInterpCx<'tcx>,
873    left: &OpTy<'tcx>,
874    right: &OpTy<'tcx>,
875    imm: &OpTy<'tcx>,
876    dest: &MPlaceTy<'tcx>,
877) -> InterpResult<'tcx, ()> {
878    assert_eq!(left.layout, right.layout);
879    assert_eq!(left.layout.size, dest.layout.size);
880
881    let (num_chunks, op_items_per_chunk, left) = split_simd_to_128bit_chunks(ecx, left)?;
882    let (_, _, right) = split_simd_to_128bit_chunks(ecx, right)?;
883    let (_, dest_items_per_chunk, dest) = split_simd_to_128bit_chunks(ecx, dest)?;
884
885    assert_eq!(op_items_per_chunk, dest_items_per_chunk.strict_mul(2));
886
887    let imm = ecx.read_scalar(imm)?.to_uint(imm.layout.size)?;
888    // Bit 2 of `imm` specifies the offset for indices of `left`.
889    // The offset is 0 when the bit is 0 or 4 when the bit is 1.
890    let left_offset = u64::try_from((imm >> 2) & 1).unwrap().strict_mul(4);
891    // Bits 0..=1 of `imm` specify the offset for indices of
892    // `right` in blocks of 4 elements.
893    let right_offset = u64::try_from(imm & 0b11).unwrap().strict_mul(4);
894
895    for i in 0..num_chunks {
896        let left = ecx.project_index(&left, i)?;
897        let right = ecx.project_index(&right, i)?;
898        let dest = ecx.project_index(&dest, i)?;
899
900        for j in 0..dest_items_per_chunk {
901            let left_offset = left_offset.strict_add(j);
902            let mut res: u16 = 0;
903            for k in 0..4 {
904                let left = ecx
905                    .read_scalar(&ecx.project_index(&left, left_offset.strict_add(k))?)?
906                    .to_u8()?;
907                let right = ecx
908                    .read_scalar(&ecx.project_index(&right, right_offset.strict_add(k))?)?
909                    .to_u8()?;
910                res = res.strict_add(left.abs_diff(right).into());
911            }
912            ecx.write_scalar(Scalar::from_u16(res), &ecx.project_index(&dest, j)?)?;
913        }
914    }
915
916    interp_ok(())
917}
918
919/// Compute the absolute differences of packed unsigned 8-bit integers
920/// in `left` and `right`, then horizontally sum each consecutive 8
921/// differences to produce unsigned 16-bit integers, and pack
922/// these unsigned 16-bit integers in the low 16 bits of 64-bit elements
923/// in `dest`.
924///
925/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_sad_epu8>
926/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm256_sad_epu8>
927/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm512_sad_epu8>
928fn psadbw<'tcx>(
929    ecx: &mut crate::MiriInterpCx<'tcx>,
930    left: &OpTy<'tcx>,
931    right: &OpTy<'tcx>,
932    dest: &MPlaceTy<'tcx>,
933) -> InterpResult<'tcx, ()> {
934    let (left, left_len) = ecx.project_to_simd(left)?;
935    let (right, right_len) = ecx.project_to_simd(right)?;
936    let (dest, dest_len) = ecx.project_to_simd(dest)?;
937
938    // fn psadbw(a: u8x16, b: u8x16) -> u64x2;
939    // fn psadbw(a: u8x32, b: u8x32) -> u64x4;
940    // fn vpsadbw(a: u8x64, b: u8x64) -> u64x8;
941    assert_eq!(left_len, right_len);
942    assert_eq!(left_len, left.layout.layout.size().bytes());
943    assert_eq!(dest_len, left_len.strict_div(8));
944
945    for i in 0..dest_len {
946        let dest = ecx.project_index(&dest, i)?;
947
948        let mut acc: u16 = 0;
949        for j in 0..8 {
950            let src_index = i.strict_mul(8).strict_add(j);
951
952            let left = ecx.project_index(&left, src_index)?;
953            let left = ecx.read_scalar(&left)?.to_u8()?;
954
955            let right = ecx.project_index(&right, src_index)?;
956            let right = ecx.read_scalar(&right)?.to_u8()?;
957
958            acc = acc.strict_add(left.abs_diff(right).into());
959        }
960
961        ecx.write_scalar(Scalar::from_u64(acc.into()), &dest)?;
962    }
963
964    interp_ok(())
965}
966
967/// Multiply packed signed 16-bit integers in `left` and `right`, producing intermediate signed 32-bit integers.
968/// Horizontally add adjacent pairs of intermediate 32-bit integers, and pack the results in `dest`.
969///
970/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_madd_epi16>
971/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm256_madd_epi16>
972/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm512_madd_epi16>
973fn pmaddwd<'tcx>(
974    ecx: &mut crate::MiriInterpCx<'tcx>,
975    left: &OpTy<'tcx>,
976    right: &OpTy<'tcx>,
977    dest: &MPlaceTy<'tcx>,
978) -> InterpResult<'tcx, ()> {
979    let (left, left_len) = ecx.project_to_simd(left)?;
980    let (right, right_len) = ecx.project_to_simd(right)?;
981    let (dest, dest_len) = ecx.project_to_simd(dest)?;
982
983    // fn  pmaddwd(a: i16x8,  b: i16x8)  -> i32x4;
984    // fn  pmaddwd(a: i16x16, b: i16x16) -> i32x8;
985    // fn vpmaddwd(a: i16x32, b: i16x32) -> i32x16;
986    assert_eq!(left_len, right_len);
987    assert_eq!(dest_len.strict_mul(2), left_len);
988
989    for i in 0..dest_len {
990        let j1 = i.strict_mul(2);
991        let left1 = ecx.read_scalar(&ecx.project_index(&left, j1)?)?.to_i16()?;
992        let right1 = ecx.read_scalar(&ecx.project_index(&right, j1)?)?.to_i16()?;
993
994        let j2 = j1.strict_add(1);
995        let left2 = ecx.read_scalar(&ecx.project_index(&left, j2)?)?.to_i16()?;
996        let right2 = ecx.read_scalar(&ecx.project_index(&right, j2)?)?.to_i16()?;
997
998        let dest = ecx.project_index(&dest, i)?;
999
1000        // Multiplications are i16*i16->i32, which will not overflow.
1001        let mul1 = i32::from(left1).strict_mul(right1.into());
1002        let mul2 = i32::from(left2).strict_mul(right2.into());
1003        // However, this addition can overflow in the most extreme case
1004        // (-0x8000)*(-0x8000)+(-0x8000)*(-0x8000) = 0x80000000
1005        let res = mul1.wrapping_add(mul2);
1006
1007        ecx.write_scalar(Scalar::from_i32(res), &dest)?;
1008    }
1009
1010    interp_ok(())
1011}
1012
1013/// Multiplies packed 8-bit unsigned integers from `left` and packed
1014/// signed 8-bit integers from `right` into 16-bit signed integers. Then,
1015/// the saturating sum of the products with indices `2*i` and `2*i+1`
1016/// produces the output at index `i`.
1017///
1018/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_maddubs_epi16>
1019/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm256_maddubs_epi16>
1020/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm512_maddubs_epi16>
1021fn pmaddbw<'tcx>(
1022    ecx: &mut crate::MiriInterpCx<'tcx>,
1023    left: &OpTy<'tcx>,
1024    right: &OpTy<'tcx>,
1025    dest: &MPlaceTy<'tcx>,
1026) -> InterpResult<'tcx, ()> {
1027    let (left, left_len) = ecx.project_to_simd(left)?;
1028    let (right, right_len) = ecx.project_to_simd(right)?;
1029    let (dest, dest_len) = ecx.project_to_simd(dest)?;
1030
1031    // fn pmaddubsw128(a: u8x16, b: i8x16) -> i16x8;
1032    // fn pmaddubsw(   a: u8x32, b: i8x32) -> i16x16;
1033    // fn vpmaddubsw(  a: u8x64, b: i8x64) -> i16x32;
1034    assert_eq!(left_len, right_len);
1035    assert_eq!(dest_len.strict_mul(2), left_len);
1036
1037    for i in 0..dest_len {
1038        let j1 = i.strict_mul(2);
1039        let left1 = ecx.read_scalar(&ecx.project_index(&left, j1)?)?.to_u8()?;
1040        let right1 = ecx.read_scalar(&ecx.project_index(&right, j1)?)?.to_i8()?;
1041
1042        let j2 = j1.strict_add(1);
1043        let left2 = ecx.read_scalar(&ecx.project_index(&left, j2)?)?.to_u8()?;
1044        let right2 = ecx.read_scalar(&ecx.project_index(&right, j2)?)?.to_i8()?;
1045
1046        let dest = ecx.project_index(&dest, i)?;
1047
1048        // Multiplication of a u8 and an i8 into an i16 cannot overflow.
1049        let mul1 = i16::from(left1).strict_mul(right1.into());
1050        let mul2 = i16::from(left2).strict_mul(right2.into());
1051        let res = mul1.saturating_add(mul2);
1052
1053        ecx.write_scalar(Scalar::from_i16(res), &dest)?;
1054    }
1055
1056    interp_ok(())
1057}
1058
1059/// Shuffle elements in `values` across lanes using the corresponding index in
1060/// `indices`, and store the results in `dest`.
1061///
1062/// This helper is shared by both the 32-bit-lane and 64-bit-lane AVX
1063/// permute-by-index intrinsics. The element type is taken from `values` and
1064/// `dest`, while the index lanes are interpreted at their full width (`i32` or
1065/// `i64`, depending on the intrinsic).
1066///
1067/// For a vector with `N` lanes, only the low `log2(N)` bits of each index are
1068/// used. Equivalently, lane `i` of the result is copied from
1069/// `values[indices[i] & (N - 1)]`.
1070///
1071/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm256_permutevar8x32_epi32>
1072/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm256_permutevar8x32_ps>
1073/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm512_permutexvar_epi32>
1074/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm512_permutexvar_epi64>
1075fn permute<'tcx>(
1076    ecx: &mut crate::MiriInterpCx<'tcx>,
1077    values: &OpTy<'tcx>,
1078    indices: &OpTy<'tcx>,
1079    dest: &MPlaceTy<'tcx>,
1080) -> InterpResult<'tcx, ()> {
1081    let (values, values_len) = ecx.project_to_simd(values)?;
1082    let (indices, indices_len) = ecx.project_to_simd(indices)?;
1083    let (dest, dest_len) = ecx.project_to_simd(dest)?;
1084
1085    // fn permd(a: u32x8, b: u32x8) -> u32x8;
1086    // fn permps(a: __m256, b: i32x8) -> __m256;
1087    // fn vpermd(a: i32x16, idx: i32x16) -> i32x16;
1088    // fn vpermq(a: i64x8, b: i64x8) -> i64x8;
1089    assert_eq!(dest_len, values_len);
1090    assert_eq!(dest_len, indices_len);
1091
1092    // Only use the lower 3 bits to index into a vector with 8 lanes,
1093    // or the lower 4 bits when indexing into a 16-lane vector.
1094    assert!(dest_len.is_power_of_two());
1095    let mask = u128::from(dest_len).strict_sub(1);
1096
1097    for i in 0..dest_len {
1098        let dest = ecx.project_index(&dest, i)?;
1099        let index_place = ecx.project_index(&indices, i)?;
1100        let index = ecx.read_scalar(&index_place)?.to_uint(index_place.layout.size)?;
1101        // `mask` is at most `dest_len - 1` which fits in a `u64`, so this cannot fail.
1102        let element = ecx.project_index(&values, u64::try_from(index & mask).unwrap())?;
1103
1104        ecx.copy_op(&element, &dest)?;
1105    }
1106
1107    interp_ok(())
1108}
1109
1110/// Shuffle elements from *two* source registers (`left` and `right`) using
1111/// the corresponding index in `indices`, and store the results in `dest`.
1112///
1113/// For a vector with `N` lanes, the low `log2(N)` bits of each index select a
1114/// lane within a source vector. Bit `log2(N)` selects the source vector (`0` =>
1115/// `left`, `1` => `right`), and all higher bits are ignored.
1116/// Equivalently, lane `i` of the result is copied from
1117/// `src[indices[i] & (N - 1)]` where
1118/// `src = if indices[i] & N == 0 { left } else { right }`.
1119///
1120/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm512_permutex2var_epi64>
1121fn permute2<'tcx>(
1122    ecx: &mut crate::MiriInterpCx<'tcx>,
1123    left: &OpTy<'tcx>,
1124    indices: &OpTy<'tcx>,
1125    right: &OpTy<'tcx>,
1126    dest: &MPlaceTy<'tcx>,
1127) -> InterpResult<'tcx, ()> {
1128    let (left, left_len) = ecx.project_to_simd(left)?;
1129    let (indices, indices_len) = ecx.project_to_simd(indices)?;
1130    let (right, right_len) = ecx.project_to_simd(right)?;
1131    let (dest, dest_len) = ecx.project_to_simd(dest)?;
1132
1133    assert_eq!(dest_len, left_len);
1134    assert_eq!(dest_len, indices_len);
1135    assert_eq!(dest_len, right_len);
1136
1137    // Use the low bits to select a lane within either input vector, and the next bit to
1138    // choose between the two vectors.
1139    assert!(dest_len.is_power_of_two());
1140    let lane_mask = u128::from(dest_len).strict_sub(1);
1141    let vector_select_bit = u128::from(dest_len);
1142
1143    for i in 0..dest_len {
1144        let dest = ecx.project_index(&dest, i)?;
1145        let index_place = ecx.project_index(&indices, i)?;
1146        let index = ecx.read_scalar(&index_place)?.to_uint(index_place.layout.size)?;
1147        // `lane_mask` is at most `dest_len - 1` which fits in a `u64`, so this cannot fail.
1148        let lane = u64::try_from(index & lane_mask).unwrap();
1149        let src = if index & vector_select_bit == 0 { &left } else { &right };
1150        let element = ecx.project_index(src, lane)?;
1151
1152        ecx.copy_op(&element, &dest)?;
1153    }
1154
1155    interp_ok(())
1156}
1157
1158/// Multiplies packed 16-bit signed integer values, truncates the 32-bit
1159/// product to the 18 most significant bits by right-shifting, and then
1160/// divides the 18-bit value by 2 (rounding to nearest) by first adding
1161/// 1 and then taking the bits `1..=16`.
1162///
1163/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_mulhrs_epi16>
1164/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm256_mulhrs_epi16>
1165fn pmulhrsw<'tcx>(
1166    ecx: &mut crate::MiriInterpCx<'tcx>,
1167    left: &OpTy<'tcx>,
1168    right: &OpTy<'tcx>,
1169    dest: &MPlaceTy<'tcx>,
1170) -> InterpResult<'tcx, ()> {
1171    let (left, left_len) = ecx.project_to_simd(left)?;
1172    let (right, right_len) = ecx.project_to_simd(right)?;
1173    let (dest, dest_len) = ecx.project_to_simd(dest)?;
1174
1175    assert_eq!(dest_len, left_len);
1176    assert_eq!(dest_len, right_len);
1177
1178    for i in 0..dest_len {
1179        let left = ecx.read_scalar(&ecx.project_index(&left, i)?)?.to_i16()?;
1180        let right = ecx.read_scalar(&ecx.project_index(&right, i)?)?.to_i16()?;
1181        let dest = ecx.project_index(&dest, i)?;
1182
1183        let res = (i32::from(left).strict_mul(right.into()) >> 14).strict_add(1) >> 1;
1184
1185        // The result of this operation can overflow a signed 16-bit integer.
1186        // When `left` and `right` are -0x8000, the result is 0x8000.
1187        #[expect(clippy::as_conversions)]
1188        let res = res as i16;
1189
1190        ecx.write_scalar(Scalar::from_i16(res), &dest)?;
1191    }
1192
1193    interp_ok(())
1194}
1195
1196/// Perform a carry-less multiplication of two 64-bit integers, selected from `left` and `right` according to `imm8`,
1197/// and store the results in `dst`.
1198///
1199/// `left` and `right` are both vectors of type `len` x i64. Only bits 0 and 4 of `imm8` matter;
1200/// they select the element of `left` and `right`, respectively.
1201///
1202/// `len` is the SIMD vector length (in counts of `i64` values). It is expected to be one of
1203/// `2`, `4`, or `8`.
1204///
1205/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_clmulepi64_si128>
1206fn pclmulqdq<'tcx>(
1207    ecx: &mut MiriInterpCx<'tcx>,
1208    left: &OpTy<'tcx>,
1209    right: &OpTy<'tcx>,
1210    imm8: &OpTy<'tcx>,
1211    dest: &MPlaceTy<'tcx>,
1212    len: u64,
1213) -> InterpResult<'tcx, ()> {
1214    assert_eq!(left.layout, right.layout);
1215    assert_eq!(left.layout.size, dest.layout.size);
1216    assert!([2u64, 4, 8].contains(&len));
1217
1218    // Transmute the input into arrays of `[u64; len]`.
1219    // Transmute the output into an array of `[u128, len / 2]`.
1220
1221    let src_layout = ecx.layout_of(Ty::new_array(ecx.tcx.tcx, ecx.tcx.types.u64, len))?;
1222    let dest_layout = ecx.layout_of(Ty::new_array(ecx.tcx.tcx, ecx.tcx.types.u128, len / 2))?;
1223
1224    let left = left.transmute(src_layout, ecx)?;
1225    let right = right.transmute(src_layout, ecx)?;
1226    let dest = dest.transmute(dest_layout, ecx)?;
1227
1228    let imm8 = ecx.read_scalar(imm8)?.to_u8()?;
1229
1230    for i in 0..(len / 2) {
1231        let lo = i.strict_mul(2);
1232        let hi = i.strict_mul(2).strict_add(1);
1233
1234        // select the 64-bit integer from left that the user specified (low or high)
1235        let index = if (imm8 & 0x01) == 0 { lo } else { hi };
1236        let left = ecx.read_scalar(&ecx.project_index(&left, index)?)?.to_u64()?;
1237
1238        // select the 64-bit integer from right that the user specified (low or high)
1239        let index = if (imm8 & 0x10) == 0 { lo } else { hi };
1240        let right = ecx.read_scalar(&ecx.project_index(&right, index)?)?.to_u64()?;
1241
1242        let result = left.widening_carryless_mul(right);
1243
1244        let dest = ecx.project_index(&dest, i)?;
1245        ecx.write_scalar(Scalar::from_u128(result), &dest)?;
1246    }
1247
1248    interp_ok(())
1249}
1250
1251/// Shuffles bytes from `left` using `right` as pattern. Each 16-byte block is shuffled independently.
1252///
1253/// `left` and `right` are both vectors of type `len` x i8.
1254///
1255/// If the highest bit of a byte in `right` is not set, the corresponding byte in `dest` is taken
1256/// from the current 16-byte block of `left` at the position indicated by the lowest 4 bits of this
1257/// byte in `right`. If the highest bit of a byte in `right` is set, the corresponding byte in
1258/// `dest` is set to `0`.
1259///
1260/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_shuffle_epi8>
1261/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm256_shuffle_epi8>
1262/// <https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm512_shuffle_epi8>
1263fn pshufb<'tcx>(
1264    ecx: &mut crate::MiriInterpCx<'tcx>,
1265    left: &OpTy<'tcx>,
1266    right: &OpTy<'tcx>,
1267    dest: &MPlaceTy<'tcx>,
1268) -> InterpResult<'tcx, ()> {
1269    let (left, left_len) = ecx.project_to_simd(left)?;
1270    let (right, right_len) = ecx.project_to_simd(right)?;
1271    let (dest, dest_len) = ecx.project_to_simd(dest)?;
1272
1273    assert_eq!(dest_len, left_len);
1274    assert_eq!(dest_len, right_len);
1275
1276    for i in 0..dest_len {
1277        let right = ecx.read_scalar(&ecx.project_index(&right, i)?)?.to_u8()?;
1278        let dest = ecx.project_index(&dest, i)?;
1279
1280        let res = if right & 0x80 == 0 {
1281            // Shuffle each 128-bit (16-byte) block independently.
1282            let block_offset = i & !15; // round down to previous multiple of 16
1283            let j = block_offset.strict_add((right % 16).into());
1284            ecx.read_scalar(&ecx.project_index(&left, j)?)?
1285        } else {
1286            // If the highest bit in `right` is 1, write zero.
1287            Scalar::from_u8(0)
1288        };
1289
1290        ecx.write_scalar(res, &dest)?;
1291    }
1292
1293    interp_ok(())
1294}
1295
1296/// Packs two N-bit integer vectors to a single N/2-bit integers.
1297///
1298/// The conversion from N-bit to N/2-bit should be provided by `f`.
1299///
1300/// Each 128-bit chunk is treated independently (i.e., the value for
1301/// the is i-th 128-bit chunk of `dest` is calculated with the i-th
1302/// 128-bit chunks of `left` and `right`).
1303fn pack_generic<'tcx>(
1304    ecx: &mut crate::MiriInterpCx<'tcx>,
1305    left: &OpTy<'tcx>,
1306    right: &OpTy<'tcx>,
1307    dest: &MPlaceTy<'tcx>,
1308    f: impl Fn(Scalar) -> InterpResult<'tcx, Scalar>,
1309) -> InterpResult<'tcx, ()> {
1310    assert_eq!(left.layout, right.layout);
1311    assert_eq!(left.layout.size, dest.layout.size);
1312
1313    let (num_chunks, op_items_per_chunk, left) = split_simd_to_128bit_chunks(ecx, left)?;
1314    let (_, _, right) = split_simd_to_128bit_chunks(ecx, right)?;
1315    let (_, dest_items_per_chunk, dest) = split_simd_to_128bit_chunks(ecx, dest)?;
1316
1317    assert_eq!(dest_items_per_chunk, op_items_per_chunk.strict_mul(2));
1318
1319    for i in 0..num_chunks {
1320        let left = ecx.project_index(&left, i)?;
1321        let right = ecx.project_index(&right, i)?;
1322        let dest = ecx.project_index(&dest, i)?;
1323
1324        for j in 0..op_items_per_chunk {
1325            let left = ecx.read_scalar(&ecx.project_index(&left, j)?)?;
1326            let right = ecx.read_scalar(&ecx.project_index(&right, j)?)?;
1327            let left_dest = ecx.project_index(&dest, j)?;
1328            let right_dest = ecx.project_index(&dest, j.strict_add(op_items_per_chunk))?;
1329
1330            let left_res = f(left)?;
1331            let right_res = f(right)?;
1332
1333            ecx.write_scalar(left_res, &left_dest)?;
1334            ecx.write_scalar(right_res, &right_dest)?;
1335        }
1336    }
1337
1338    interp_ok(())
1339}
1340
1341/// Converts two 16-bit integer vectors to a single 8-bit integer
1342/// vector with signed saturation.
1343///
1344/// Each 128-bit chunk is treated independently (i.e., the value for
1345/// the is i-th 128-bit chunk of `dest` is calculated with the i-th
1346/// 128-bit chunks of `left` and `right`).
1347fn packsswb<'tcx>(
1348    ecx: &mut crate::MiriInterpCx<'tcx>,
1349    left: &OpTy<'tcx>,
1350    right: &OpTy<'tcx>,
1351    dest: &MPlaceTy<'tcx>,
1352) -> InterpResult<'tcx, ()> {
1353    pack_generic(ecx, left, right, dest, |op| {
1354        let op = op.to_i16()?;
1355        let res = i8::try_from(op).unwrap_or(if op < 0 { i8::MIN } else { i8::MAX });
1356        interp_ok(Scalar::from_i8(res))
1357    })
1358}
1359
1360/// Converts two 16-bit signed integer vectors to a single 8-bit
1361/// unsigned integer vector with saturation.
1362///
1363/// Each 128-bit chunk is treated independently (i.e., the value for
1364/// the is i-th 128-bit chunk of `dest` is calculated with the i-th
1365/// 128-bit chunks of `left` and `right`).
1366fn packuswb<'tcx>(
1367    ecx: &mut crate::MiriInterpCx<'tcx>,
1368    left: &OpTy<'tcx>,
1369    right: &OpTy<'tcx>,
1370    dest: &MPlaceTy<'tcx>,
1371) -> InterpResult<'tcx, ()> {
1372    pack_generic(ecx, left, right, dest, |op| {
1373        let op = op.to_i16()?;
1374        let res = u8::try_from(op).unwrap_or(if op < 0 { 0 } else { u8::MAX });
1375        interp_ok(Scalar::from_u8(res))
1376    })
1377}
1378
1379/// Converts two 32-bit integer vectors to a single 16-bit integer
1380/// vector with signed saturation.
1381///
1382/// Each 128-bit chunk is treated independently (i.e., the value for
1383/// the is i-th 128-bit chunk of `dest` is calculated with the i-th
1384/// 128-bit chunks of `left` and `right`).
1385fn packssdw<'tcx>(
1386    ecx: &mut crate::MiriInterpCx<'tcx>,
1387    left: &OpTy<'tcx>,
1388    right: &OpTy<'tcx>,
1389    dest: &MPlaceTy<'tcx>,
1390) -> InterpResult<'tcx, ()> {
1391    pack_generic(ecx, left, right, dest, |op| {
1392        let op = op.to_i32()?;
1393        let res = i16::try_from(op).unwrap_or(if op < 0 { i16::MIN } else { i16::MAX });
1394        interp_ok(Scalar::from_i16(res))
1395    })
1396}
1397
1398/// Converts two 32-bit integer vectors to a single 16-bit integer
1399/// vector with unsigned saturation.
1400///
1401/// Each 128-bit chunk is treated independently (i.e., the value for
1402/// the is i-th 128-bit chunk of `dest` is calculated with the i-th
1403/// 128-bit chunks of `left` and `right`).
1404fn packusdw<'tcx>(
1405    ecx: &mut crate::MiriInterpCx<'tcx>,
1406    left: &OpTy<'tcx>,
1407    right: &OpTy<'tcx>,
1408    dest: &MPlaceTy<'tcx>,
1409) -> InterpResult<'tcx, ()> {
1410    pack_generic(ecx, left, right, dest, |op| {
1411        let op = op.to_i32()?;
1412        let res = u16::try_from(op).unwrap_or(if op < 0 { 0 } else { u16::MAX });
1413        interp_ok(Scalar::from_u16(res))
1414    })
1415}
1416
1417/// Negates elements from `left` when the corresponding element in
1418/// `right` is negative. If an element from `right` is zero, zero
1419/// is written to the corresponding output element.
1420/// In other words, multiplies `left` with `right.signum()`.
1421fn psign<'tcx>(
1422    ecx: &mut crate::MiriInterpCx<'tcx>,
1423    left: &OpTy<'tcx>,
1424    right: &OpTy<'tcx>,
1425    dest: &MPlaceTy<'tcx>,
1426) -> InterpResult<'tcx, ()> {
1427    let (left, left_len) = ecx.project_to_simd(left)?;
1428    let (right, right_len) = ecx.project_to_simd(right)?;
1429    let (dest, dest_len) = ecx.project_to_simd(dest)?;
1430
1431    assert_eq!(dest_len, left_len);
1432    assert_eq!(dest_len, right_len);
1433
1434    for i in 0..dest_len {
1435        let dest = ecx.project_index(&dest, i)?;
1436        let left = ecx.read_immediate(&ecx.project_index(&left, i)?)?;
1437        let right = ecx.read_scalar(&ecx.project_index(&right, i)?)?.to_int(dest.layout.size)?;
1438
1439        let res =
1440            ecx.binary_op(mir::BinOp::Mul, &left, &ImmTy::from_int(right.signum(), dest.layout))?;
1441
1442        ecx.write_immediate(*res, &dest)?;
1443    }
1444
1445    interp_ok(())
1446}
1447
1448/// Calcultates either `a + b + cb_in` or `a - b - cb_in` depending on the value
1449/// of `op` and returns both the sum and the overflow bit. `op` is expected to be
1450/// either one of `mir::BinOp::AddWithOverflow` and `mir::BinOp::SubWithOverflow`.
1451fn carrying_add<'tcx>(
1452    ecx: &mut crate::MiriInterpCx<'tcx>,
1453    cb_in: &OpTy<'tcx>,
1454    a: &OpTy<'tcx>,
1455    b: &OpTy<'tcx>,
1456    op: mir::BinOp,
1457) -> InterpResult<'tcx, (ImmTy<'tcx>, Scalar)> {
1458    assert!(op == mir::BinOp::AddWithOverflow || op == mir::BinOp::SubWithOverflow);
1459
1460    let cb_in = ecx.read_scalar(cb_in)?.to_u8()? != 0;
1461    let a = ecx.read_immediate(a)?;
1462    let b = ecx.read_immediate(b)?;
1463
1464    let (sum, overflow1) = ecx.binary_op(op, &a, &b)?.to_pair(ecx);
1465    let (sum, overflow2) =
1466        ecx.binary_op(op, &sum, &ImmTy::from_uint(cb_in, a.layout))?.to_pair(ecx);
1467    let cb_out = overflow1.to_scalar().to_bool()? | overflow2.to_scalar().to_bool()?;
1468
1469    interp_ok((sum, Scalar::from_u8(cb_out.into())))
1470}