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, pmulhrsw, 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 let unprefixed_name = link_name.as_str().strip_prefix("llvm.x86.ssse3.").unwrap();
23
24 match unprefixed_name {
25 "pshuf.b.128" => {
29 let [left, right] =
30 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
31
32 let (left, left_len) = this.project_to_simd(left)?;
33 let (right, right_len) = this.project_to_simd(right)?;
34 let (dest, dest_len) = this.project_to_simd(dest)?;
35
36 assert_eq!(dest_len, left_len);
37 assert_eq!(dest_len, right_len);
38
39 for i in 0..dest_len {
40 let right = this.read_scalar(&this.project_index(&right, i)?)?.to_u8()?;
41 let dest = this.project_index(&dest, i)?;
42
43 let res = if right & 0x80 == 0 {
44 let j = right % 16; this.read_scalar(&this.project_index(&left, j.into())?)?
46 } else {
47 Scalar::from_u8(0)
49 };
50
51 this.write_scalar(res, &dest)?;
52 }
53 }
54 "phadd.sw.128" | "phsub.sw.128" => {
58 let [left, right] =
59 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
60
61 let which = match unprefixed_name {
62 "phadd.sw.128" => mir::BinOp::Add,
63 "phsub.sw.128" => mir::BinOp::Sub,
64 _ => unreachable!(),
65 };
66
67 horizontal_bin_op(this, which, true, left, right, dest)?;
68 }
69 "pmadd.ub.sw.128" => {
76 let [left, right] =
77 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
78
79 let (left, left_len) = this.project_to_simd(left)?;
80 let (right, right_len) = this.project_to_simd(right)?;
81 let (dest, dest_len) = this.project_to_simd(dest)?;
82
83 assert_eq!(left_len, right_len);
84 assert_eq!(dest_len.strict_mul(2), left_len);
85
86 for i in 0..dest_len {
87 let j1 = i.strict_mul(2);
88 let left1 = this.read_scalar(&this.project_index(&left, j1)?)?.to_u8()?;
89 let right1 = this.read_scalar(&this.project_index(&right, j1)?)?.to_i8()?;
90
91 let j2 = j1.strict_add(1);
92 let left2 = this.read_scalar(&this.project_index(&left, j2)?)?.to_u8()?;
93 let right2 = this.read_scalar(&this.project_index(&right, j2)?)?.to_i8()?;
94
95 let dest = this.project_index(&dest, i)?;
96
97 let mul1 = i16::from(left1).strict_mul(right1.into());
99 let mul2 = i16::from(left2).strict_mul(right2.into());
100 let res = mul1.saturating_add(mul2);
101
102 this.write_scalar(Scalar::from_i16(res), &dest)?;
103 }
104 }
105 "pmul.hr.sw.128" => {
112 let [left, right] =
113 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
114
115 pmulhrsw(this, left, right, dest)?;
116 }
117 "psign.b.128" | "psign.w.128" | "psign.d.128" => {
123 let [left, right] =
124 this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
125
126 psign(this, left, right, dest)?;
127 }
128 _ => return interp_ok(EmulateItemResult::NotSupported),
129 }
130 interp_ok(EmulateItemResult::NeedsReturn)
131 }
132}