miri/shims/x86/
ssse3.rs

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