miri/shims/x86/
avx512.rs

1use rustc_abi::CanonAbi;
2use rustc_middle::ty::Ty;
3use rustc_span::Symbol;
4use rustc_target::callconv::FnAbi;
5
6use super::psadbw;
7use crate::*;
8
9impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
10pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
11    fn emulate_x86_avx512_intrinsic(
12        &mut self,
13        link_name: Symbol,
14        abi: &FnAbi<'tcx, Ty<'tcx>>,
15        args: &[OpTy<'tcx>],
16        dest: &MPlaceTy<'tcx>,
17    ) -> InterpResult<'tcx, EmulateItemResult> {
18        let this = self.eval_context_mut();
19        // Prefix should have already been checked.
20        let unprefixed_name = link_name.as_str().strip_prefix("llvm.x86.avx512.").unwrap();
21
22        match unprefixed_name {
23            // Used by the ternarylogic functions.
24            "pternlog.d.128" | "pternlog.d.256" | "pternlog.d.512" => {
25                this.expect_target_feature_for_intrinsic(link_name, "avx512f")?;
26                if matches!(unprefixed_name, "pternlog.d.128" | "pternlog.d.256") {
27                    this.expect_target_feature_for_intrinsic(link_name, "avx512vl")?;
28                }
29
30                let [a, b, c, imm8] =
31                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
32
33                assert_eq!(dest.layout, a.layout);
34                assert_eq!(dest.layout, b.layout);
35                assert_eq!(dest.layout, c.layout);
36
37                // The signatures of these operations are:
38                //
39                // ```
40                // fn vpternlogd(a: i32x16, b: i32x16, c: i32x16, imm8: i32) -> i32x16;
41                // fn vpternlogd256(a: i32x8, b: i32x8, c: i32x8, imm8: i32) -> i32x8;
42                // fn vpternlogd128(a: i32x4, b: i32x4, c: i32x4, imm8: i32) -> i32x4;
43                // ```
44                //
45                // The element type is always a 32-bit integer, the width varies.
46
47                let (a, _a_len) = this.project_to_simd(a)?;
48                let (b, _b_len) = this.project_to_simd(b)?;
49                let (c, _c_len) = this.project_to_simd(c)?;
50                let (dest, dest_len) = this.project_to_simd(dest)?;
51
52                // Compute one lane with ternary table.
53                let tern = |xa: u32, xb: u32, xc: u32, imm: u32| -> u32 {
54                    let mut out = 0u32;
55                    // At each bit position, select bit from imm8 at index = (a << 2) | (b << 1) | c
56                    for bit in 0..32 {
57                        let ia = (xa >> bit) & 1;
58                        let ib = (xb >> bit) & 1;
59                        let ic = (xc >> bit) & 1;
60                        let idx = (ia << 2) | (ib << 1) | ic;
61                        let v = (imm >> idx) & 1;
62                        out |= v << bit;
63                    }
64                    out
65                };
66
67                let imm8 = this.read_scalar(imm8)?.to_u32()? & 0xFF;
68                for i in 0..dest_len {
69                    let a_lane = this.project_index(&a, i)?;
70                    let b_lane = this.project_index(&b, i)?;
71                    let c_lane = this.project_index(&c, i)?;
72                    let d_lane = this.project_index(&dest, i)?;
73
74                    let va = this.read_scalar(&a_lane)?.to_u32()?;
75                    let vb = this.read_scalar(&b_lane)?.to_u32()?;
76                    let vc = this.read_scalar(&c_lane)?.to_u32()?;
77
78                    let r = tern(va, vb, vc, imm8);
79                    this.write_scalar(Scalar::from_u32(r), &d_lane)?;
80                }
81            }
82            // Used to implement the _mm512_sad_epu8 function.
83            "psad.bw.512" => {
84                this.expect_target_feature_for_intrinsic(link_name, "avx512bw")?;
85
86                let [left, right] =
87                    this.check_shim_sig_lenient(abi, CanonAbi::C, link_name, args)?;
88
89                psadbw(this, left, right, dest)?
90            }
91            _ => return interp_ok(EmulateItemResult::NotSupported),
92        }
93        interp_ok(EmulateItemResult::NeedsReturn)
94    }
95}