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, int_abs, 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 "pabs.b.128" | "pabs.w.128" | "pabs.d.128" => {
28 let [op] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
29
30 int_abs(this, op, dest)?;
31 }
32 "pshuf.b.128" => {
36 let [left, right] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
37
38 let (left, left_len) = this.project_to_simd(left)?;
39 let (right, right_len) = this.project_to_simd(right)?;
40 let (dest, dest_len) = this.project_to_simd(dest)?;
41
42 assert_eq!(dest_len, left_len);
43 assert_eq!(dest_len, right_len);
44
45 for i in 0..dest_len {
46 let right = this.read_scalar(&this.project_index(&right, i)?)?.to_u8()?;
47 let dest = this.project_index(&dest, i)?;
48
49 let res = if right & 0x80 == 0 {
50 let j = right % 16; this.read_scalar(&this.project_index(&left, j.into())?)?
52 } else {
53 Scalar::from_u8(0)
55 };
56
57 this.write_scalar(res, &dest)?;
58 }
59 }
60 "phadd.w.128" | "phadd.sw.128" | "phadd.d.128" | "phsub.w.128" | "phsub.sw.128"
64 | "phsub.d.128" => {
65 let [left, right] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
66
67 let (which, saturating) = match unprefixed_name {
68 "phadd.w.128" | "phadd.d.128" => (mir::BinOp::Add, false),
69 "phadd.sw.128" => (mir::BinOp::Add, true),
70 "phsub.w.128" | "phsub.d.128" => (mir::BinOp::Sub, false),
71 "phsub.sw.128" => (mir::BinOp::Sub, true),
72 _ => unreachable!(),
73 };
74
75 horizontal_bin_op(this, which, saturating, left, right, dest)?;
76 }
77 "pmadd.ub.sw.128" => {
84 let [left, right] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
85
86 let (left, left_len) = this.project_to_simd(left)?;
87 let (right, right_len) = this.project_to_simd(right)?;
88 let (dest, dest_len) = this.project_to_simd(dest)?;
89
90 assert_eq!(left_len, right_len);
91 assert_eq!(dest_len.strict_mul(2), left_len);
92
93 for i in 0..dest_len {
94 let j1 = i.strict_mul(2);
95 let left1 = this.read_scalar(&this.project_index(&left, j1)?)?.to_u8()?;
96 let right1 = this.read_scalar(&this.project_index(&right, j1)?)?.to_i8()?;
97
98 let j2 = j1.strict_add(1);
99 let left2 = this.read_scalar(&this.project_index(&left, j2)?)?.to_u8()?;
100 let right2 = this.read_scalar(&this.project_index(&right, j2)?)?.to_i8()?;
101
102 let dest = this.project_index(&dest, i)?;
103
104 let mul1 = i16::from(left1).strict_mul(right1.into());
106 let mul2 = i16::from(left2).strict_mul(right2.into());
107 let res = mul1.saturating_add(mul2);
108
109 this.write_scalar(Scalar::from_i16(res), &dest)?;
110 }
111 }
112 "pmul.hr.sw.128" => {
119 let [left, right] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
120
121 pmulhrsw(this, left, right, dest)?;
122 }
123 "psign.b.128" | "psign.w.128" | "psign.d.128" => {
129 let [left, right] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
130
131 psign(this, left, right, dest)?;
132 }
133 _ => return interp_ok(EmulateItemResult::NotSupported),
134 }
135 interp_ok(EmulateItemResult::NeedsReturn)
136 }
137}