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