miri/shims/x86/
ssse3.rs

1use rustc_abi::CanonAbi;
2use rustc_middle::mir;
3use rustc_middle::ty::Ty;
4use rustc_span::Symbol;
5use rustc_target::callconv::FnAbi;
6
7use super::{horizontal_bin_op, pmaddbw, pmulhrsw, pshufb, psign};
8use crate::*;
9
10impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
11pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
12    fn emulate_x86_ssse3_intrinsic(
13        &mut self,
14        link_name: Symbol,
15        abi: &FnAbi<'tcx, Ty<'tcx>>,
16        args: &[OpTy<'tcx>],
17        dest: &MPlaceTy<'tcx>,
18    ) -> InterpResult<'tcx, EmulateItemResult> {
19        let this = self.eval_context_mut();
20        this.expect_target_feature_for_intrinsic(link_name, "ssse3")?;
21        // Prefix should have already been checked.
22        let unprefixed_name = link_name.as_str().strip_prefix("llvm.x86.ssse3.").unwrap();
23
24        match unprefixed_name {
25            // Used to implement the _mm_shuffle_epi8 intrinsic.
26            // Shuffles bytes from `left` using `right` as pattern.
27            // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_shuffle_epi8
28            "pshuf.b.128" => {
29                let [left, right] =
30                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
31
32                pshufb(this, left, right, dest)?;
33            }
34            // Used to implement the _mm_h{adds,subs}_epi16 functions.
35            // Horizontally add / subtract with saturation adjacent 16-bit
36            // integer values in `left` and `right`.
37            "phadd.sw.128" | "phsub.sw.128" => {
38                let [left, right] =
39                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
40
41                let which = match unprefixed_name {
42                    "phadd.sw.128" => mir::BinOp::Add,
43                    "phsub.sw.128" => mir::BinOp::Sub,
44                    _ => unreachable!(),
45                };
46
47                horizontal_bin_op(this, which, /*saturating*/ true, left, right, dest)?;
48            }
49            // Used to implement the _mm_maddubs_epi16 function.
50            "pmadd.ub.sw.128" => {
51                let [left, right] =
52                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
53
54                pmaddbw(this, left, right, dest)?;
55            }
56            // Used to implement the _mm_mulhrs_epi16 function.
57            // Multiplies packed 16-bit signed integer values, truncates the 32-bit
58            // product to the 18 most significant bits by right-shifting, and then
59            // divides the 18-bit value by 2 (rounding to nearest) by first adding
60            // 1 and then taking the bits `1..=16`.
61            // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_mulhrs_epi16
62            "pmul.hr.sw.128" => {
63                let [left, right] =
64                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
65
66                pmulhrsw(this, left, right, dest)?;
67            }
68            // Used to implement the _mm_sign_epi{8,16,32} functions.
69            // Negates elements from `left` when the corresponding element in
70            // `right` is negative. If an element from `right` is zero, zero
71            // is writen to the corresponding output element.
72            // Basically, we multiply `left` with `right.signum()`.
73            "psign.b.128" | "psign.w.128" | "psign.d.128" => {
74                let [left, right] =
75                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
76
77                psign(this, left, right, dest)?;
78            }
79            _ => return interp_ok(EmulateItemResult::NotSupported),
80        }
81        interp_ok(EmulateItemResult::NeedsReturn)
82    }
83}