|
| 1 | +use rustc_middle::mir; |
| 2 | +use rustc_span::Symbol; |
| 3 | +use rustc_target::spec::abi::Abi; |
| 4 | + |
| 5 | +use super::horizontal_bin_op; |
| 6 | +use crate::*; |
| 7 | +use shims::foreign_items::EmulateByNameResult; |
| 8 | + |
| 9 | +impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {} |
| 10 | +pub(super) trait EvalContextExt<'mir, 'tcx: 'mir>: |
| 11 | + crate::MiriInterpCxExt<'mir, 'tcx> |
| 12 | +{ |
| 13 | + fn emulate_x86_ssse3_intrinsic( |
| 14 | + &mut self, |
| 15 | + link_name: Symbol, |
| 16 | + abi: Abi, |
| 17 | + args: &[OpTy<'tcx, Provenance>], |
| 18 | + dest: &PlaceTy<'tcx, Provenance>, |
| 19 | + ) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> { |
| 20 | + let this = self.eval_context_mut(); |
| 21 | + // Prefix should have already been checked. |
| 22 | + let unprefixed_name = link_name.as_str().strip_prefix("llvm.x86.ssse3.").unwrap(); |
| 23 | + |
| 24 | + match unprefixed_name { |
| 25 | + // Used to implement the _mm_abs_epi{8,16,32} functions. |
| 26 | + // Calculates the absolute value of packed 8/16/32-bit integers. |
| 27 | + "pabs.b.128" | "pabs.w.128" | "pabs.d.128" => { |
| 28 | + let [op] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; |
| 29 | + |
| 30 | + let (op, op_len) = this.operand_to_simd(op)?; |
| 31 | + let (dest, dest_len) = this.place_to_simd(dest)?; |
| 32 | + |
| 33 | + assert_eq!(op_len, dest_len); |
| 34 | + |
| 35 | + for i in 0..dest_len { |
| 36 | + let op = this.read_scalar(&this.project_index(&op, i)?)?; |
| 37 | + let dest = this.project_index(&dest, i)?; |
| 38 | + |
| 39 | + // Converting to a host "i128" works since the input is always signed. |
| 40 | + let res = op.to_int(dest.layout.size)?.unsigned_abs(); |
| 41 | + |
| 42 | + this.write_scalar(Scalar::from_uint(res, dest.layout.size), &dest)?; |
| 43 | + } |
| 44 | + } |
| 45 | + // Used to implement the _mm_shuffle_epi8 intrinsic. |
| 46 | + // Shuffles bytes from `left` using `right` as pattern. |
| 47 | + // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_shuffle_epi8 |
| 48 | + "pshuf.b.128" => { |
| 49 | + let [left, right] = |
| 50 | + this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; |
| 51 | + |
| 52 | + let (left, left_len) = this.operand_to_simd(left)?; |
| 53 | + let (right, right_len) = this.operand_to_simd(right)?; |
| 54 | + let (dest, dest_len) = this.place_to_simd(dest)?; |
| 55 | + |
| 56 | + assert_eq!(dest_len, left_len); |
| 57 | + assert_eq!(dest_len, right_len); |
| 58 | + |
| 59 | + for i in 0..dest_len { |
| 60 | + let right = this.read_scalar(&this.project_index(&right, i)?)?.to_u8()?; |
| 61 | + let dest = this.project_index(&dest, i)?; |
| 62 | + |
| 63 | + let res = if right & 0x80 == 0 { |
| 64 | + let j = right % 16; // index wraps around |
| 65 | + this.read_scalar(&this.project_index(&left, j.into())?)? |
| 66 | + } else { |
| 67 | + // If the highest bit in `right` is 1, write zero. |
| 68 | + Scalar::from_u8(0) |
| 69 | + }; |
| 70 | + |
| 71 | + this.write_scalar(res, &dest)?; |
| 72 | + } |
| 73 | + } |
| 74 | + // Used to implement the _mm_h{add,adds,sub}_epi{16,32} functions. |
| 75 | + // Horizontally add / add with saturation / subtract adjacent 16/32-bit |
| 76 | + // integer values in `left` and `right`. |
| 77 | + "phadd.w.128" | "phadd.sw.128" | "phadd.d.128" | "phsub.w.128" | "phsub.sw.128" |
| 78 | + | "phsub.d.128" => { |
| 79 | + let [left, right] = |
| 80 | + this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; |
| 81 | + |
| 82 | + let (which, saturating) = match unprefixed_name { |
| 83 | + "phadd.w.128" | "phadd.d.128" => (mir::BinOp::Add, false), |
| 84 | + "phadd.sw.128" => (mir::BinOp::Add, true), |
| 85 | + "phsub.w.128" | "phsub.d.128" => (mir::BinOp::Sub, false), |
| 86 | + "phsub.sw.128" => (mir::BinOp::Sub, true), |
| 87 | + _ => unreachable!(), |
| 88 | + }; |
| 89 | + |
| 90 | + horizontal_bin_op(this, which, saturating, left, right, dest)?; |
| 91 | + } |
| 92 | + // Used to implement the _mm_maddubs_epi16 function. |
| 93 | + // Multiplies packed 8-bit unsigned integers from `left` and packed |
| 94 | + // signed 8-bit integers from `right` into 16-bit signed integers. Then, |
| 95 | + // the saturating sum of the products with indices `2*i` and `2*i+1` |
| 96 | + // produces the output at index `i`. |
| 97 | + // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_maddubs_epi16 |
| 98 | + "pmadd.ub.sw.128" => { |
| 99 | + let [left, right] = |
| 100 | + this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; |
| 101 | + |
| 102 | + let (left, left_len) = this.operand_to_simd(left)?; |
| 103 | + let (right, right_len) = this.operand_to_simd(right)?; |
| 104 | + let (dest, dest_len) = this.place_to_simd(dest)?; |
| 105 | + |
| 106 | + assert_eq!(left_len, right_len); |
| 107 | + assert_eq!(dest_len.checked_mul(2).unwrap(), left_len); |
| 108 | + |
| 109 | + for i in 0..dest_len { |
| 110 | + let j1 = i.checked_mul(2).unwrap(); |
| 111 | + let left1 = this.read_scalar(&this.project_index(&left, j1)?)?.to_u8()?; |
| 112 | + let right1 = this.read_scalar(&this.project_index(&right, j1)?)?.to_i8()?; |
| 113 | + |
| 114 | + let j2 = j1.checked_add(1).unwrap(); |
| 115 | + let left2 = this.read_scalar(&this.project_index(&left, j2)?)?.to_u8()?; |
| 116 | + let right2 = this.read_scalar(&this.project_index(&right, j2)?)?.to_i8()?; |
| 117 | + |
| 118 | + let dest = this.project_index(&dest, i)?; |
| 119 | + |
| 120 | + // Multiplication of a u8 and an i8 into an i16 cannot overflow. |
| 121 | + let mul1 = i16::from(left1).checked_mul(right1.into()).unwrap(); |
| 122 | + let mul2 = i16::from(left2).checked_mul(right2.into()).unwrap(); |
| 123 | + let res = mul1.saturating_add(mul2); |
| 124 | + |
| 125 | + this.write_scalar(Scalar::from_i16(res), &dest)?; |
| 126 | + } |
| 127 | + } |
| 128 | + // Used to implement the _mm_mulhrs_epi16 function. |
| 129 | + // Multiplies packed 16-bit signed integer values, truncates the 32-bit |
| 130 | + // product to the 18 most significant bits by right-shifting, and then |
| 131 | + // divides the 18-bit value by 2 (rounding to nearest) by first adding |
| 132 | + // 1 and then taking the bits `1..=16`. |
| 133 | + // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_mm_mulhrs_epi16 |
| 134 | + "pmul.hr.sw.128" => { |
| 135 | + let [left, right] = |
| 136 | + this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; |
| 137 | + |
| 138 | + let (left, left_len) = this.operand_to_simd(left)?; |
| 139 | + let (right, right_len) = this.operand_to_simd(right)?; |
| 140 | + let (dest, dest_len) = this.place_to_simd(dest)?; |
| 141 | + |
| 142 | + assert_eq!(dest_len, left_len); |
| 143 | + assert_eq!(dest_len, right_len); |
| 144 | + |
| 145 | + for i in 0..dest_len { |
| 146 | + let left = this.read_scalar(&this.project_index(&left, i)?)?.to_i16()?; |
| 147 | + let right = this.read_scalar(&this.project_index(&right, i)?)?.to_i16()?; |
| 148 | + let dest = this.project_index(&dest, i)?; |
| 149 | + |
| 150 | + let res = (i32::from(left).checked_mul(right.into()).unwrap() >> 14) |
| 151 | + .checked_add(1) |
| 152 | + .unwrap() |
| 153 | + >> 1; |
| 154 | + |
| 155 | + // The result of this operation can overflow a signed 16-bit integer. |
| 156 | + // When `left` and `right` are -0x8000, the result is 0x8000. |
| 157 | + #[allow(clippy::cast_possible_truncation)] |
| 158 | + let res = res as i16; |
| 159 | + |
| 160 | + this.write_scalar(Scalar::from_i16(res), &dest)?; |
| 161 | + } |
| 162 | + } |
| 163 | + // Used to implement the _mm_sign_epi{8,16,32} functions. |
| 164 | + // Negates elements from `left` when the corresponding element in |
| 165 | + // `right` is negative. If an element from `right` is zero, zero |
| 166 | + // is writen to the corresponding output element. |
| 167 | + // Basically, we multiply `left` with `right.signum()`. |
| 168 | + "psign.b.128" | "psign.w.128" | "psign.d.128" => { |
| 169 | + let [left, right] = |
| 170 | + this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?; |
| 171 | + |
| 172 | + let (left, left_len) = this.operand_to_simd(left)?; |
| 173 | + let (right, right_len) = this.operand_to_simd(right)?; |
| 174 | + let (dest, dest_len) = this.place_to_simd(dest)?; |
| 175 | + |
| 176 | + assert_eq!(dest_len, left_len); |
| 177 | + assert_eq!(dest_len, right_len); |
| 178 | + |
| 179 | + for i in 0..dest_len { |
| 180 | + let dest = this.project_index(&dest, i)?; |
| 181 | + let left = this.read_immediate(&this.project_index(&left, i)?)?; |
| 182 | + let right = this |
| 183 | + .read_scalar(&this.project_index(&right, i)?)? |
| 184 | + .to_int(dest.layout.size)?; |
| 185 | + |
| 186 | + let res = this.wrapping_binary_op( |
| 187 | + mir::BinOp::Mul, |
| 188 | + &left, |
| 189 | + &ImmTy::from_int(right.signum(), dest.layout), |
| 190 | + )?; |
| 191 | + |
| 192 | + this.write_immediate(*res, &dest)?; |
| 193 | + } |
| 194 | + } |
| 195 | + _ => return Ok(EmulateByNameResult::NotSupported), |
| 196 | + } |
| 197 | + Ok(EmulateByNameResult::NeedsJumping) |
| 198 | + } |
| 199 | +} |
0 commit comments