Skip to main content

miri/intrinsics/x86/
ssse3.rs

1use rustc_span::Symbol;
2
3use super::{pmaddbw, pmulhrsw, pshufb, psign};
4use crate::*;
5
6impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
7pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
8    fn emulate_x86_ssse3_intrinsic(
9        &mut self,
10        link_name: Symbol,
11        args: &[OpTy<'tcx>],
12        dest: &MPlaceTy<'tcx>,
13    ) -> InterpResult<'tcx, EmulateItemResult> {
14        let this = self.eval_context_mut();
15        this.expect_target_feature_for_intrinsic(link_name, "ssse3")?;
16        // Prefix should have already been checked.
17        let unprefixed_name = link_name.as_str().strip_prefix("llvm.x86.ssse3.").unwrap();
18
19        match unprefixed_name {
20            // Used to implement the _mm_shuffle_epi8 intrinsic.
21            // Shuffles bytes from `left` using `right` as pattern.
22            // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_shuffle_epi8
23            "pshuf.b.128" => {
24                let [left, right] = this.check_shim_sig_llvm_intrinsic(link_name, args)?;
25
26                pshufb(this, left, right, dest)?;
27            }
28            // Used to implement the _mm_maddubs_epi16 function.
29            "pmadd.ub.sw.128" => {
30                let [left, right] = this.check_shim_sig_llvm_intrinsic(link_name, args)?;
31
32                pmaddbw(this, left, right, dest)?;
33            }
34            // Used to implement the _mm_mulhrs_epi16 function.
35            // Multiplies packed 16-bit signed integer values, truncates the 32-bit
36            // product to the 18 most significant bits by right-shifting, and then
37            // divides the 18-bit value by 2 (rounding to nearest) by first adding
38            // 1 and then taking the bits `1..=16`.
39            // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_mulhrs_epi16
40            "pmul.hr.sw.128" => {
41                let [left, right] = this.check_shim_sig_llvm_intrinsic(link_name, args)?;
42
43                pmulhrsw(this, left, right, dest)?;
44            }
45            // Used to implement the _mm_sign_epi{8,16,32} functions.
46            // Negates elements from `left` when the corresponding element in
47            // `right` is negative. If an element from `right` is zero, zero
48            // is written to the corresponding output element.
49            // Basically, we multiply `left` with `right.signum()`.
50            "psign.b.128" | "psign.w.128" | "psign.d.128" => {
51                let [left, right] = this.check_shim_sig_llvm_intrinsic(link_name, args)?;
52
53                psign(this, left, right, dest)?;
54            }
55            _ => return interp_ok(EmulateItemResult::NotSupported),
56        }
57        interp_ok(EmulateItemResult::NeedsReturn)
58    }
59}