miri/shims/x86/
sse3.rs

1use rustc_abi::CanonAbi;
2use rustc_middle::mir;
3use rustc_middle::ty::Ty;
4use rustc_span::Symbol;
5use rustc_target::callconv::FnAbi;
6
7use super::horizontal_bin_op;
8use crate::*;
9
10impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
11pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
12    fn emulate_x86_sse3_intrinsic(
13        &mut self,
14        link_name: Symbol,
15        abi: &FnAbi<'tcx, Ty<'tcx>>,
16        args: &[OpTy<'tcx>],
17        dest: &MPlaceTy<'tcx>,
18    ) -> InterpResult<'tcx, EmulateItemResult> {
19        let this = self.eval_context_mut();
20        this.expect_target_feature_for_intrinsic(link_name, "sse3")?;
21        // Prefix should have already been checked.
22        let unprefixed_name = link_name.as_str().strip_prefix("llvm.x86.sse3.").unwrap();
23
24        match unprefixed_name {
25            // Used to implement the _mm_h{add,sub}_p{s,d} functions.
26            // Horizontally add/subtract adjacent floating point values
27            // in `left` and `right`.
28            "hadd.ps" | "hadd.pd" | "hsub.ps" | "hsub.pd" => {
29                let [left, right] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
30
31                let which = match unprefixed_name {
32                    "hadd.ps" | "hadd.pd" => mir::BinOp::Add,
33                    "hsub.ps" | "hsub.pd" => mir::BinOp::Sub,
34                    _ => unreachable!(),
35                };
36
37                horizontal_bin_op(this, which, /*saturating*/ false, left, right, dest)?;
38            }
39            // Used to implement the _mm_lddqu_si128 function.
40            // Reads a 128-bit vector from an unaligned pointer. This intrinsic
41            // is expected to perform better than a regular unaligned read when
42            // the data crosses a cache line, but for Miri this is just a regular
43            // unaligned read.
44            "ldu.dq" => {
45                let [src_ptr] = this.check_shim(abi, CanonAbi::C, link_name, args)?;
46                let src_ptr = this.read_pointer(src_ptr)?;
47                let dest = dest.force_mplace(this)?;
48
49                this.mem_copy(src_ptr, dest.ptr(), dest.layout.size, /*nonoverlapping*/ true)?;
50            }
51            _ => return interp_ok(EmulateItemResult::NotSupported),
52        }
53        interp_ok(EmulateItemResult::NeedsReturn)
54    }
55}