miri/shims/x86/
bmi.rs

1use rustc_middle::ty::Ty;
2use rustc_span::Symbol;
3use rustc_target::callconv::{Conv, FnAbi};
4
5use crate::*;
6
7impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
8pub(super) trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
9    fn emulate_x86_bmi_intrinsic(
10        &mut self,
11        link_name: Symbol,
12        abi: &FnAbi<'tcx, Ty<'tcx>>,
13        args: &[OpTy<'tcx>],
14        dest: &MPlaceTy<'tcx>,
15    ) -> InterpResult<'tcx, EmulateItemResult> {
16        let this = self.eval_context_mut();
17
18        // Prefix should have already been checked.
19        let unprefixed_name = link_name.as_str().strip_prefix("llvm.x86.bmi.").unwrap();
20
21        // The intrinsics are suffixed with the bit size of their operands.
22        let (is_64_bit, unprefixed_name) = if unprefixed_name.ends_with("64") {
23            (true, unprefixed_name.strip_suffix(".64").unwrap_or(""))
24        } else {
25            (false, unprefixed_name.strip_suffix(".32").unwrap_or(""))
26        };
27
28        // All intrinsics of the "bmi" namespace belong to the "bmi2" ISA extension.
29        // The exception is "bextr", which belongs to "bmi1".
30        let target_feature = if unprefixed_name == "bextr" { "bmi1" } else { "bmi2" };
31        this.expect_target_feature_for_intrinsic(link_name, target_feature)?;
32
33        if is_64_bit && this.tcx.sess.target.arch != "x86_64" {
34            return interp_ok(EmulateItemResult::NotSupported);
35        }
36
37        let [left, right] = this.check_shim(abi, Conv::C, link_name, args)?;
38        let left = this.read_scalar(left)?;
39        let right = this.read_scalar(right)?;
40
41        let left = if is_64_bit { left.to_u64()? } else { u64::from(left.to_u32()?) };
42        let right = if is_64_bit { right.to_u64()? } else { u64::from(right.to_u32()?) };
43
44        let result = match unprefixed_name {
45            // Extract a contigous range of bits from an unsigned integer.
46            // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_bextr_u32
47            "bextr" => {
48                let start = u32::try_from(right & 0xff).unwrap();
49                let len = u32::try_from((right >> 8) & 0xff).unwrap();
50                let shifted = left.checked_shr(start).unwrap_or(0);
51                // Keep the `len` lowest bits of `shifted`, or all bits if `len` is too big.
52                if len >= 64 { shifted } else { shifted & 1u64.wrapping_shl(len).wrapping_sub(1) }
53            }
54            // Create a copy of an unsigned integer with bits above a certain index cleared.
55            // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_bzhi_u32
56            "bzhi" => {
57                let index = u32::try_from(right & 0xff).unwrap();
58                // Keep the `index` lowest bits of `left`, or all bits if `index` is too big.
59                if index >= 64 { left } else { left & 1u64.wrapping_shl(index).wrapping_sub(1) }
60            }
61            // Extract bit values of an unsigned integer at positions marked by a mask.
62            // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_pext_u32
63            "pext" => {
64                let mut mask = right;
65                let mut i = 0u32;
66                let mut result = 0;
67                // Iterate over the mask one 1-bit at a time, from
68                // the least significant bit to the most significant bit.
69                while mask != 0 {
70                    // Extract the bit marked by the mask's least significant set bit
71                    // and put it at position `i` of the result.
72                    result |= u64::from(left & (1 << mask.trailing_zeros()) != 0) << i;
73                    i = i.wrapping_add(1);
74                    // Clear the least significant set bit.
75                    mask &= mask.wrapping_sub(1);
76                }
77                result
78            }
79            // Deposit bit values of an unsigned integer to positions marked by a mask.
80            // https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#text=_pdep_u32
81            "pdep" => {
82                let mut mask = right;
83                let mut set = left;
84                let mut result = 0;
85                // Iterate over the mask one 1-bit at a time, from
86                // the least significant bit to the most significant bit.
87                while mask != 0 {
88                    // Put rightmost bit of `set` at the position of the current `mask` bit.
89                    result |= (set & 1) << mask.trailing_zeros();
90                    // Go to next bit of `set`.
91                    set >>= 1;
92                    // Clear the least significant set bit.
93                    mask &= mask.wrapping_sub(1);
94                }
95                result
96            }
97            _ => return interp_ok(EmulateItemResult::NotSupported),
98        };
99
100        let result = if is_64_bit {
101            Scalar::from_u64(result)
102        } else {
103            Scalar::from_u32(u32::try_from(result).unwrap())
104        };
105        this.write_scalar(result, dest)?;
106
107        interp_ok(EmulateItemResult::NeedsReturn)
108    }
109}