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, 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        // 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_abs_epi{8,16,32} functions.
26            // Calculates the absolute value of packed 8/16/32-bit integers.
27            "pabs.b.128" | "pabs.w.128" | "pabs.d.128" => {
28                let [op] = this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
29
30                int_abs(this, op, dest)?;
31            }
32            // Used to implement the _mm_shuffle_epi8 intrinsic.
33            // Shuffles bytes from `left` using `right` as pattern.
34            // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_shuffle_epi8
35            "pshuf.b.128" => {
36                let [left, right] =
37                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
38
39                let (left, left_len) = this.project_to_simd(left)?;
40                let (right, right_len) = this.project_to_simd(right)?;
41                let (dest, dest_len) = this.project_to_simd(dest)?;
42
43                assert_eq!(dest_len, left_len);
44                assert_eq!(dest_len, right_len);
45
46                for i in 0..dest_len {
47                    let right = this.read_scalar(&this.project_index(&right, i)?)?.to_u8()?;
48                    let dest = this.project_index(&dest, i)?;
49
50                    let res = if right & 0x80 == 0 {
51                        let j = right % 16; // index wraps around
52                        this.read_scalar(&this.project_index(&left, j.into())?)?
53                    } else {
54                        // If the highest bit in `right` is 1, write zero.
55                        Scalar::from_u8(0)
56                    };
57
58                    this.write_scalar(res, &dest)?;
59                }
60            }
61            // Used to implement the _mm_h{add,adds,sub}_epi{16,32} functions.
62            // Horizontally add / add with saturation / subtract adjacent 16/32-bit
63            // integer values in `left` and `right`.
64            "phadd.w.128" | "phadd.sw.128" | "phadd.d.128" | "phsub.w.128" | "phsub.sw.128"
65            | "phsub.d.128" => {
66                let [left, right] =
67                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
68
69                let (which, saturating) = match unprefixed_name {
70                    "phadd.w.128" | "phadd.d.128" => (mir::BinOp::Add, false),
71                    "phadd.sw.128" => (mir::BinOp::Add, true),
72                    "phsub.w.128" | "phsub.d.128" => (mir::BinOp::Sub, false),
73                    "phsub.sw.128" => (mir::BinOp::Sub, true),
74                    _ => unreachable!(),
75                };
76
77                horizontal_bin_op(this, which, saturating, left, right, dest)?;
78            }
79            // Used to implement the _mm_maddubs_epi16 function.
80            // Multiplies packed 8-bit unsigned integers from `left` and packed
81            // signed 8-bit integers from `right` into 16-bit signed integers. Then,
82            // the saturating sum of the products with indices `2*i` and `2*i+1`
83            // produces the output at index `i`.
84            // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_maddubs_epi16
85            "pmadd.ub.sw.128" => {
86                let [left, right] =
87                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
88
89                let (left, left_len) = this.project_to_simd(left)?;
90                let (right, right_len) = this.project_to_simd(right)?;
91                let (dest, dest_len) = this.project_to_simd(dest)?;
92
93                assert_eq!(left_len, right_len);
94                assert_eq!(dest_len.strict_mul(2), left_len);
95
96                for i in 0..dest_len {
97                    let j1 = i.strict_mul(2);
98                    let left1 = this.read_scalar(&this.project_index(&left, j1)?)?.to_u8()?;
99                    let right1 = this.read_scalar(&this.project_index(&right, j1)?)?.to_i8()?;
100
101                    let j2 = j1.strict_add(1);
102                    let left2 = this.read_scalar(&this.project_index(&left, j2)?)?.to_u8()?;
103                    let right2 = this.read_scalar(&this.project_index(&right, j2)?)?.to_i8()?;
104
105                    let dest = this.project_index(&dest, i)?;
106
107                    // Multiplication of a u8 and an i8 into an i16 cannot overflow.
108                    let mul1 = i16::from(left1).strict_mul(right1.into());
109                    let mul2 = i16::from(left2).strict_mul(right2.into());
110                    let res = mul1.saturating_add(mul2);
111
112                    this.write_scalar(Scalar::from_i16(res), &dest)?;
113                }
114            }
115            // Used to implement the _mm_mulhrs_epi16 function.
116            // Multiplies packed 16-bit signed integer values, truncates the 32-bit
117            // product to the 18 most significant bits by right-shifting, and then
118            // divides the 18-bit value by 2 (rounding to nearest) by first adding
119            // 1 and then taking the bits `1..=16`.
120            // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_mulhrs_epi16
121            "pmul.hr.sw.128" => {
122                let [left, right] =
123                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
124
125                pmulhrsw(this, left, right, dest)?;
126            }
127            // Used to implement the _mm_sign_epi{8,16,32} functions.
128            // Negates elements from `left` when the corresponding element in
129            // `right` is negative. If an element from `right` is zero, zero
130            // is writen to the corresponding output element.
131            // Basically, we multiply `left` with `right.signum()`.
132            "psign.b.128" | "psign.w.128" | "psign.d.128" => {
133                let [left, right] =
134                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
135
136                psign(this, left, right, dest)?;
137            }
138            _ => return interp_ok(EmulateItemResult::NotSupported),
139        }
140        interp_ok(EmulateItemResult::NeedsReturn)
141    }
142}