Skip to main content

miri/intrinsics/x86/
sse41.rs

1use rustc_span::Symbol;
2
3use super::{conditional_dot_product, mpsadbw, packusdw, round_all, round_first, test_bits_masked};
4use crate::*;
5
6impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
7pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
8    fn emulate_x86_sse41_intrinsic(
9        &mut self,
10        link_name: Symbol,
11        args: &[OpTy<'tcx>],
12        dest: &MPlaceTy<'tcx>,
13    ) -> InterpResult<'tcx, EmulateItemResult> {
14        let this = self.eval_context_mut();
15        this.expect_target_feature_for_intrinsic(link_name, "sse4.1")?;
16        // Prefix should have already been checked.
17        let unprefixed_name = link_name.as_str().strip_prefix("llvm.x86.sse41.").unwrap();
18
19        match unprefixed_name {
20            // Used to implement the _mm_insert_ps function.
21            // Takes one element of `right` and inserts it into `left` and
22            // optionally zero some elements. Source index is specified
23            // in bits `6..=7` of `imm`, destination index is specified in
24            // bits `4..=5` if `imm`, and `i`th bit specifies whether element
25            // `i` is zeroed.
26            "insertps" => {
27                let [left, right, imm] = this.check_shim_sig_unadjusted(link_name, args)?;
28
29                let (left, left_len) = this.project_to_simd(left)?;
30                let (right, right_len) = this.project_to_simd(right)?;
31                let (dest, dest_len) = this.project_to_simd(dest)?;
32
33                assert_eq!(dest_len, left_len);
34                assert_eq!(dest_len, right_len);
35                assert!(dest_len <= 4);
36
37                let imm = this.read_scalar(imm)?.to_u8()?;
38                let src_index = u64::from((imm >> 6) & 0b11);
39                let dst_index = u64::from((imm >> 4) & 0b11);
40
41                let src_value = this.read_immediate(&this.project_index(&right, src_index)?)?;
42
43                for i in 0..dest_len {
44                    let dest = this.project_index(&dest, i)?;
45
46                    if imm & (1 << i) != 0 {
47                        // zeroed
48                        this.write_scalar(Scalar::from_u32(0), &dest)?;
49                    } else if i == dst_index {
50                        // copy from `right` at specified index
51                        this.write_immediate(*src_value, &dest)?;
52                    } else {
53                        // copy from `left`
54                        this.copy_op(&this.project_index(&left, i)?, &dest)?;
55                    }
56                }
57            }
58            // Used to implement the _mm_packus_epi32 function.
59            // Concatenates two 32-bit signed integer vectors and converts
60            // the result to a 16-bit unsigned integer vector with saturation.
61            "packusdw" => {
62                let [left, right] = this.check_shim_sig_unadjusted(link_name, args)?;
63
64                packusdw(this, left, right, dest)?;
65            }
66            // Used to implement the _mm_dp_ps and _mm_dp_pd functions.
67            // Conditionally multiplies the packed floating-point elements in
68            // `left` and `right` using the high 4 bits in `imm`, sums the four
69            // products, and conditionally stores the sum in `dest` using the low
70            // 4 bits of `imm`.
71            "dpps" | "dppd" => {
72                let [left, right, imm] = this.check_shim_sig_unadjusted(link_name, args)?;
73
74                conditional_dot_product(this, left, right, imm, dest)?;
75            }
76            // Used to implement the _mm_floor_ss, _mm_ceil_ss and _mm_round_ss
77            // functions. Rounds the first element of `right` according to `rounding`
78            // and copies the remaining elements from `left`.
79            "round.ss" => {
80                let [left, right, rounding] = this.check_shim_sig_unadjusted(link_name, args)?;
81
82                round_first::<rustc_apfloat::ieee::Single>(this, left, right, rounding, dest)?;
83            }
84            // Used to implement the _mm_floor_ps, _mm_ceil_ps and _mm_round_ps
85            // functions. Rounds the elements of `op` according to `rounding`.
86            "round.ps" => {
87                let [op, rounding] = this.check_shim_sig_unadjusted(link_name, args)?;
88
89                round_all::<rustc_apfloat::ieee::Single>(this, op, rounding, dest)?;
90            }
91            // Used to implement the _mm_floor_sd, _mm_ceil_sd and _mm_round_sd
92            // functions. Rounds the first element of `right` according to `rounding`
93            // and copies the remaining elements from `left`.
94            "round.sd" => {
95                let [left, right, rounding] = this.check_shim_sig_unadjusted(link_name, args)?;
96
97                round_first::<rustc_apfloat::ieee::Double>(this, left, right, rounding, dest)?;
98            }
99            // Used to implement the _mm_floor_pd, _mm_ceil_pd and _mm_round_pd
100            // functions. Rounds the elements of `op` according to `rounding`.
101            "round.pd" => {
102                let [op, rounding] = this.check_shim_sig_unadjusted(link_name, args)?;
103
104                round_all::<rustc_apfloat::ieee::Double>(this, op, rounding, dest)?;
105            }
106            // Used to implement the _mm_minpos_epu16 function.
107            // Find the minimum unsigned 16-bit integer in `op` and
108            // returns its value and position.
109            "phminposuw" => {
110                let [op] = this.check_shim_sig_unadjusted(link_name, args)?;
111
112                let (op, op_len) = this.project_to_simd(op)?;
113                let (dest, dest_len) = this.project_to_simd(dest)?;
114
115                // Find minimum
116                let mut min_value = u16::MAX;
117                let mut min_index = 0;
118                for i in 0..op_len {
119                    let op = this.read_scalar(&this.project_index(&op, i)?)?.to_u16()?;
120                    if op < min_value {
121                        min_value = op;
122                        min_index = i;
123                    }
124                }
125
126                // Write value and index
127                this.write_scalar(Scalar::from_u16(min_value), &this.project_index(&dest, 0)?)?;
128                this.write_scalar(
129                    Scalar::from_u16(min_index.try_into().unwrap()),
130                    &this.project_index(&dest, 1)?,
131                )?;
132                // Fill remainder with zeros
133                for i in 2..dest_len {
134                    this.write_scalar(Scalar::from_u16(0), &this.project_index(&dest, i)?)?;
135                }
136            }
137            // Used to implement the _mm_mpsadbw_epu8 function.
138            // Compute the sum of absolute differences of quadruplets of unsigned
139            // 8-bit integers in `left` and `right`, and store the 16-bit results
140            // in `right`. Quadruplets are selected from `left` and `right` with
141            // offsets specified in `imm`.
142            // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_mpsadbw_epu8
143            "mpsadbw" => {
144                let [left, right, imm] = this.check_shim_sig_unadjusted(link_name, args)?;
145
146                mpsadbw(this, left, right, imm, dest)?;
147            }
148            // Used to implement the _mm_testnzc_si128 function.
149            // Tests `(op & mask) != 0 && (op & mask) != mask`
150            "ptestnzc" => {
151                let [op, mask] = this.check_shim_sig_unadjusted(link_name, args)?;
152
153                let (all_zero, masked_set) = test_bits_masked(this, op, mask)?;
154                let res = !all_zero && !masked_set;
155
156                this.write_scalar(Scalar::from_i32(res.into()), dest)?;
157            }
158            _ => return interp_ok(EmulateItemResult::NotSupported),
159        }
160        interp_ok(EmulateItemResult::NeedsReturn)
161    }
162}