|
1 |
| -use crate::InterpResult; |
| 1 | +use rustc_middle::mir; |
| 2 | +use rustc_target::abi::Size; |
| 3 | + |
| 4 | +use crate::*; |
| 5 | +use helpers::bool_to_simd_element; |
2 | 6 |
|
3 | 7 | pub(super) mod sse;
|
4 | 8 | pub(super) mod sse2;
|
@@ -43,3 +47,155 @@ impl FloatCmpOp {
|
43 | 47 | }
|
44 | 48 | }
|
45 | 49 | }
|
| 50 | + |
| 51 | +#[derive(Copy, Clone)] |
| 52 | +enum FloatBinOp { |
| 53 | + /// Arithmetic operation |
| 54 | + Arith(mir::BinOp), |
| 55 | + /// Comparison |
| 56 | + Cmp(FloatCmpOp), |
| 57 | + /// Minimum value (with SSE semantics) |
| 58 | + /// |
| 59 | + /// <https://www.felixcloutier.com/x86/minss> |
| 60 | + /// <https://www.felixcloutier.com/x86/minps> |
| 61 | + /// <https://www.felixcloutier.com/x86/minsd> |
| 62 | + /// <https://www.felixcloutier.com/x86/minpd> |
| 63 | + Min, |
| 64 | + /// Maximum value (with SSE semantics) |
| 65 | + /// |
| 66 | + /// <https://www.felixcloutier.com/x86/maxss> |
| 67 | + /// <https://www.felixcloutier.com/x86/maxps> |
| 68 | + /// <https://www.felixcloutier.com/x86/maxsd> |
| 69 | + /// <https://www.felixcloutier.com/x86/maxpd> |
| 70 | + Max, |
| 71 | +} |
| 72 | + |
| 73 | +/// Performs `which` scalar operation on `left` and `right` and returns |
| 74 | +/// the result. |
| 75 | +fn bin_op_float<'tcx, F: rustc_apfloat::Float>( |
| 76 | + this: &crate::MiriInterpCx<'_, 'tcx>, |
| 77 | + which: FloatBinOp, |
| 78 | + left: &ImmTy<'tcx, Provenance>, |
| 79 | + right: &ImmTy<'tcx, Provenance>, |
| 80 | +) -> InterpResult<'tcx, Scalar<Provenance>> { |
| 81 | + match which { |
| 82 | + FloatBinOp::Arith(which) => { |
| 83 | + let (res, _overflow, _ty) = this.overflowing_binary_op(which, left, right)?; |
| 84 | + Ok(res) |
| 85 | + } |
| 86 | + FloatBinOp::Cmp(which) => { |
| 87 | + let left = left.to_scalar().to_float::<F>()?; |
| 88 | + let right = right.to_scalar().to_float::<F>()?; |
| 89 | + // FIXME: Make sure that these operations match the semantics |
| 90 | + // of cmpps/cmpss/cmppd/cmpsd |
| 91 | + let res = match which { |
| 92 | + FloatCmpOp::Eq => left == right, |
| 93 | + FloatCmpOp::Lt => left < right, |
| 94 | + FloatCmpOp::Le => left <= right, |
| 95 | + FloatCmpOp::Unord => left.is_nan() || right.is_nan(), |
| 96 | + FloatCmpOp::Neq => left != right, |
| 97 | + FloatCmpOp::Nlt => !(left < right), |
| 98 | + FloatCmpOp::Nle => !(left <= right), |
| 99 | + FloatCmpOp::Ord => !left.is_nan() && !right.is_nan(), |
| 100 | + }; |
| 101 | + Ok(bool_to_simd_element(res, Size::from_bits(F::BITS))) |
| 102 | + } |
| 103 | + FloatBinOp::Min => { |
| 104 | + let left_scalar = left.to_scalar(); |
| 105 | + let left = left_scalar.to_float::<F>()?; |
| 106 | + let right_scalar = right.to_scalar(); |
| 107 | + let right = right_scalar.to_float::<F>()?; |
| 108 | + // SSE semantics to handle zero and NaN. Note that `x == F::ZERO` |
| 109 | + // is true when `x` is either +0 or -0. |
| 110 | + if (left == F::ZERO && right == F::ZERO) |
| 111 | + || left.is_nan() |
| 112 | + || right.is_nan() |
| 113 | + || left >= right |
| 114 | + { |
| 115 | + Ok(right_scalar) |
| 116 | + } else { |
| 117 | + Ok(left_scalar) |
| 118 | + } |
| 119 | + } |
| 120 | + FloatBinOp::Max => { |
| 121 | + let left_scalar = left.to_scalar(); |
| 122 | + let left = left_scalar.to_float::<F>()?; |
| 123 | + let right_scalar = right.to_scalar(); |
| 124 | + let right = right_scalar.to_float::<F>()?; |
| 125 | + // SSE semantics to handle zero and NaN. Note that `x == F::ZERO` |
| 126 | + // is true when `x` is either +0 or -0. |
| 127 | + if (left == F::ZERO && right == F::ZERO) |
| 128 | + || left.is_nan() |
| 129 | + || right.is_nan() |
| 130 | + || left <= right |
| 131 | + { |
| 132 | + Ok(right_scalar) |
| 133 | + } else { |
| 134 | + Ok(left_scalar) |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +/// Performs `which` operation on the first component of `left` and `right` |
| 141 | +/// and copies the other components from `left`. The result is stored in `dest`. |
| 142 | +fn bin_op_simd_float_first<'tcx, F: rustc_apfloat::Float>( |
| 143 | + this: &mut crate::MiriInterpCx<'_, 'tcx>, |
| 144 | + which: FloatBinOp, |
| 145 | + left: &OpTy<'tcx, Provenance>, |
| 146 | + right: &OpTy<'tcx, Provenance>, |
| 147 | + dest: &PlaceTy<'tcx, Provenance>, |
| 148 | +) -> InterpResult<'tcx, ()> { |
| 149 | + let (left, left_len) = this.operand_to_simd(left)?; |
| 150 | + let (right, right_len) = this.operand_to_simd(right)?; |
| 151 | + let (dest, dest_len) = this.place_to_simd(dest)?; |
| 152 | + |
| 153 | + assert_eq!(dest_len, left_len); |
| 154 | + assert_eq!(dest_len, right_len); |
| 155 | + |
| 156 | + let res0 = bin_op_float::<F>( |
| 157 | + this, |
| 158 | + which, |
| 159 | + &this.read_immediate(&this.project_index(&left, 0)?)?, |
| 160 | + &this.read_immediate(&this.project_index(&right, 0)?)?, |
| 161 | + )?; |
| 162 | + this.write_scalar(res0, &this.project_index(&dest, 0)?)?; |
| 163 | + |
| 164 | + for i in 1..dest_len { |
| 165 | + this.copy_op( |
| 166 | + &this.project_index(&left, i)?, |
| 167 | + &this.project_index(&dest, i)?, |
| 168 | + /*allow_transmute*/ false, |
| 169 | + )?; |
| 170 | + } |
| 171 | + |
| 172 | + Ok(()) |
| 173 | +} |
| 174 | + |
| 175 | +/// Performs `which` operation on each component of `left` and |
| 176 | +/// `right`, storing the result is stored in `dest`. |
| 177 | +fn bin_op_simd_float_all<'tcx, F: rustc_apfloat::Float>( |
| 178 | + this: &mut crate::MiriInterpCx<'_, 'tcx>, |
| 179 | + which: FloatBinOp, |
| 180 | + left: &OpTy<'tcx, Provenance>, |
| 181 | + right: &OpTy<'tcx, Provenance>, |
| 182 | + dest: &PlaceTy<'tcx, Provenance>, |
| 183 | +) -> InterpResult<'tcx, ()> { |
| 184 | + let (left, left_len) = this.operand_to_simd(left)?; |
| 185 | + let (right, right_len) = this.operand_to_simd(right)?; |
| 186 | + let (dest, dest_len) = this.place_to_simd(dest)?; |
| 187 | + |
| 188 | + assert_eq!(dest_len, left_len); |
| 189 | + assert_eq!(dest_len, right_len); |
| 190 | + |
| 191 | + for i in 0..dest_len { |
| 192 | + let left = this.read_immediate(&this.project_index(&left, i)?)?; |
| 193 | + let right = this.read_immediate(&this.project_index(&right, i)?)?; |
| 194 | + let dest = this.project_index(&dest, i)?; |
| 195 | + |
| 196 | + let res = bin_op_float::<F>(this, which, &left, &right)?; |
| 197 | + this.write_scalar(res, &dest)?; |
| 198 | + } |
| 199 | + |
| 200 | + Ok(()) |
| 201 | +} |
0 commit comments