miri/intrinsics/x86/avx2.rs
1use rustc_middle::mir;
2use rustc_span::Symbol;
3
4use super::{
5 ShiftOp, horizontal_bin_op, mpsadbw, packssdw, packsswb, packusdw, packuswb, permute, pmaddbw,
6 pmaddwd, pmulhrsw, psadbw, pshufb, psign, shift_simd_by_scalar,
7};
8use crate::*;
9
10impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
11pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
12 fn emulate_x86_avx2_intrinsic(
13 &mut self,
14 link_name: Symbol,
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, "avx2")?;
20 // Prefix should have already been checked.
21 let unprefixed_name = link_name.as_str().strip_prefix("llvm.x86.avx2.").unwrap();
22
23 match unprefixed_name {
24 // Used to implement the _mm256_h{adds,subs}_epi16 functions.
25 // Horizontally add / subtract with saturation adjacent 16-bit
26 // integer values in `left` and `right`.
27 "phadd.sw" | "phsub.sw" => {
28 let [left, right] = this.check_shim_sig_unadjusted(link_name, args)?;
29
30 let which = match unprefixed_name {
31 "phadd.sw" => mir::BinOp::Add,
32 "phsub.sw" => mir::BinOp::Sub,
33 _ => unreachable!(),
34 };
35
36 horizontal_bin_op(this, which, /*saturating*/ true, left, right, dest)?;
37 }
38 // Used to implement `_mm{,_mask}_{i32,i64}gather_{epi32,epi64,pd,ps}` functions
39 // Gathers elements from `slice` using `offsets * scale` as indices.
40 // When the highest bit of the corresponding element of `mask` is 0,
41 // the value is copied from `src` instead.
42 "gather.d.d" | "gather.d.d.256" | "gather.d.q" | "gather.d.q.256" | "gather.q.d"
43 | "gather.q.d.256" | "gather.q.q" | "gather.q.q.256" | "gather.d.pd"
44 | "gather.d.pd.256" | "gather.q.pd" | "gather.q.pd.256" | "gather.d.ps"
45 | "gather.d.ps.256" | "gather.q.ps" | "gather.q.ps.256" => {
46 let [src, slice, offsets, mask, scale] =
47 this.check_shim_sig_unadjusted(link_name, args)?;
48
49 assert_eq!(dest.layout, src.layout);
50
51 let (src, _) = this.project_to_simd(src)?;
52 let (offsets, offsets_len) = this.project_to_simd(offsets)?;
53 let (mask, mask_len) = this.project_to_simd(mask)?;
54 let (dest, dest_len) = this.project_to_simd(dest)?;
55
56 // There are cases like dest: i32x4, offsets: i64x2
57 // If dest has more elements than offset, extra dest elements are filled with zero.
58 // If offsets has more elements than dest, extra offsets are ignored.
59 let actual_len = dest_len.min(offsets_len);
60
61 assert_eq!(dest_len, mask_len);
62
63 let mask_item_size = mask.layout.field(this, 0).size;
64 let high_bit_offset = mask_item_size.bits().strict_sub(1);
65
66 let scale = this.read_scalar(scale)?.to_i8()?;
67 if !matches!(scale, 1 | 2 | 4 | 8) {
68 panic!("invalid gather scale {scale}");
69 }
70 let scale = i64::from(scale);
71
72 let slice = this.read_pointer(slice)?;
73 for i in 0..actual_len {
74 let mask = this.project_index(&mask, i)?;
75 let dest = this.project_index(&dest, i)?;
76
77 if this.read_scalar(&mask)?.to_uint(mask_item_size)? >> high_bit_offset != 0 {
78 let offset = this.project_index(&offsets, i)?;
79 let offset =
80 i64::try_from(this.read_scalar(&offset)?.to_int(offset.layout.size)?)
81 .unwrap();
82 let ptr = slice.wrapping_signed_offset(offset.strict_mul(scale), &this.tcx);
83 // Unaligned copy, which is what we want.
84 this.mem_copy(
85 ptr,
86 dest.ptr(),
87 dest.layout.size,
88 /*nonoverlapping*/ true,
89 )?;
90 } else {
91 this.copy_op(&this.project_index(&src, i)?, &dest)?;
92 }
93 }
94 for i in actual_len..dest_len {
95 let dest = this.project_index(&dest, i)?;
96 this.write_scalar(Scalar::from_int(0, dest.layout.size), &dest)?;
97 }
98 }
99 // Used to implement the _mm256_maddubs_epi16 function.
100 "pmadd.ub.sw" => {
101 let [left, right] = this.check_shim_sig_unadjusted(link_name, args)?;
102
103 pmaddbw(this, left, right, dest)?;
104 }
105 // Used to implement the _mm256_mpsadbw_epu8 function.
106 // Compute the sum of absolute differences of quadruplets of unsigned
107 // 8-bit integers in `left` and `right`, and store the 16-bit results
108 // in `right`. Quadruplets are selected from `left` and `right` with
109 // offsets specified in `imm`.
110 // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm256_mpsadbw_epu8
111 "mpsadbw" => {
112 let [left, right, imm] = this.check_shim_sig_unadjusted(link_name, args)?;
113
114 mpsadbw(this, left, right, imm, dest)?;
115 }
116 // Used to implement the _mm256_mulhrs_epi16 function.
117 // Multiplies packed 16-bit signed integer values, truncates the 32-bit
118 // product to the 18 most significant bits by right-shifting, and then
119 // divides the 18-bit value by 2 (rounding to nearest) by first adding
120 // 1 and then taking the bits `1..=16`.
121 // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm256_mulhrs_epi16
122 "pmul.hr.sw" => {
123 let [left, right] = this.check_shim_sig_unadjusted(link_name, args)?;
124
125 pmulhrsw(this, left, right, dest)?;
126 }
127 // Used to implement the _mm256_packs_epi16 function.
128 // Converts two 16-bit integer vectors to a single 8-bit integer
129 // vector with signed saturation.
130 "packsswb" => {
131 let [left, right] = this.check_shim_sig_unadjusted(link_name, args)?;
132
133 packsswb(this, left, right, dest)?;
134 }
135 // Used to implement the _mm256_packs_epi32 function.
136 // Converts two 32-bit integer vectors to a single 16-bit integer
137 // vector with signed saturation.
138 "packssdw" => {
139 let [left, right] = this.check_shim_sig_unadjusted(link_name, args)?;
140
141 packssdw(this, left, right, dest)?;
142 }
143 // Used to implement the _mm256_packus_epi16 function.
144 // Converts two 16-bit signed integer vectors to a single 8-bit
145 // unsigned integer vector with saturation.
146 "packuswb" => {
147 let [left, right] = this.check_shim_sig_unadjusted(link_name, args)?;
148
149 packuswb(this, left, right, dest)?;
150 }
151 // Used to implement the _mm256_packus_epi32 function.
152 // Concatenates two 32-bit signed integer vectors and converts
153 // the result to a 16-bit unsigned integer vector with saturation.
154 "packusdw" => {
155 let [left, right] = this.check_shim_sig_unadjusted(link_name, args)?;
156
157 packusdw(this, left, right, dest)?;
158 }
159 // Used to implement _mm256_permutevar8x32_epi32 and _mm256_permutevar8x32_ps.
160 "permd" | "permps" => {
161 let [left, right] = this.check_shim_sig_unadjusted(link_name, args)?;
162
163 permute(this, left, right, dest)?;
164 }
165 // Used to implement the _mm256_sad_epu8 function.
166 "psad.bw" => {
167 let [left, right] = this.check_shim_sig_unadjusted(link_name, args)?;
168
169 psadbw(this, left, right, dest)?
170 }
171 // Used to implement the _mm256_shuffle_epi8 intrinsic.
172 // Shuffles bytes from `left` using `right` as pattern.
173 // Each 128-bit block is shuffled independently.
174 "pshuf.b" => {
175 let [left, right] = this.check_shim_sig_unadjusted(link_name, args)?;
176
177 pshufb(this, left, right, dest)?;
178 }
179 // Used to implement the _mm256_sign_epi{8,16,32} functions.
180 // Negates elements from `left` when the corresponding element in
181 // `right` is negative. If an element from `right` is zero, zero
182 // is written to the corresponding output element.
183 // Basically, we multiply `left` with `right.signum()`.
184 "psign.b" | "psign.w" | "psign.d" => {
185 let [left, right] = this.check_shim_sig_unadjusted(link_name, args)?;
186
187 psign(this, left, right, dest)?;
188 }
189 // Used to implement the _mm256_{sll,srl,sra}_epi{16,32,64} functions
190 // (except _mm256_sra_epi64, which is not available in AVX2).
191 // Shifts N-bit packed integers in left by the amount in right.
192 // `right` is as 128-bit vector. but it is interpreted as a single
193 // 64-bit integer (remaining bits are ignored).
194 // For logic shifts, when right is larger than N - 1, zero is produced.
195 // For arithmetic shifts, when right is larger than N - 1, the sign bit
196 // is copied to remaining bits.
197 "psll.w" | "psrl.w" | "psra.w" | "psll.d" | "psrl.d" | "psra.d" | "psll.q"
198 | "psrl.q" => {
199 let [left, right] = this.check_shim_sig_unadjusted(link_name, args)?;
200
201 let which = match unprefixed_name {
202 "psll.w" | "psll.d" | "psll.q" => ShiftOp::Left,
203 "psrl.w" | "psrl.d" | "psrl.q" => ShiftOp::RightLogic,
204 "psra.w" | "psra.d" => ShiftOp::RightArith,
205 _ => unreachable!(),
206 };
207
208 shift_simd_by_scalar(this, left, right, which, dest)?;
209 }
210 // Used to implement the _mm256_madd_epi16 function.
211 // Multiplies packed signed 16-bit integers in `left` and `right`, producing
212 // intermediate signed 32-bit integers. Horizontally add adjacent pairs of
213 // intermediate 32-bit integers, and pack the results in `dest`.
214 "pmadd.wd" => {
215 let [left, right] = this.check_shim_sig_unadjusted(link_name, args)?;
216
217 pmaddwd(this, left, right, dest)?;
218 }
219 _ => return interp_ok(EmulateItemResult::NotSupported),
220 }
221 interp_ok(EmulateItemResult::NeedsReturn)
222 }
223}