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(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] = 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; // index wraps around
51                        this.read_scalar(&this.project_index(&left, j.into())?)?
52                    } else {
53                        // If the highest bit in `right` is 1, write zero.
54                        Scalar::from_u8(0)
55                    };
56
57                    this.write_scalar(res, &dest)?;
58                }
59            }
60            // Used to implement the _mm_h{add,adds,sub}_epi{16,32} functions.
61            // Horizontally add / add with saturation / subtract adjacent 16/32-bit
62            // integer values in `left` and `right`.
63            "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            // Used to implement the _mm_maddubs_epi16 function.
78            // Multiplies packed 8-bit unsigned integers from `left` and packed
79            // signed 8-bit integers from `right` into 16-bit signed integers. Then,
80            // the saturating sum of the products with indices `2*i` and `2*i+1`
81            // produces the output at index `i`.
82            // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_maddubs_epi16
83            "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                    // Multiplication of a u8 and an i8 into an i16 cannot overflow.
105                    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            // Used to implement the _mm_mulhrs_epi16 function.
113            // Multiplies packed 16-bit signed integer values, truncates the 32-bit
114            // product to the 18 most significant bits by right-shifting, and then
115            // divides the 18-bit value by 2 (rounding to nearest) by first adding
116            // 1 and then taking the bits `1..=16`.
117            // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_mulhrs_epi16
118            "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            // Used to implement the _mm_sign_epi{8,16,32} functions.
124            // Negates elements from `left` when the corresponding element in
125            // `right` is negative. If an element from `right` is zero, zero
126            // is writen to the corresponding output element.
127            // Basically, we multiply `left` with `right.signum()`.
128            "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}