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