Skip to content

Commit

Permalink
Auto merge of rust-lang#96946 - WaffleLapkin:ptr_mask, r=scottmcm
Browse files Browse the repository at this point in the history
Add pointer masking convenience functions

This PR adds the following public API:
```rust
impl<T: ?Sized> *const T {
    fn mask(self, mask: usize) -> *const T;
}

impl<T: ?Sized> *mut T {
    fn mask(self, mask: usize) -> *const T;
}

// mod intrinsics
fn mask<T>(ptr: *const T, mask: usize) -> *const T
```
This is equivalent to `ptr.map_addr(|a| a & mask)` but also uses a cool llvm intrinsic.

Proposed in rust-lang#95643 (comment)

cc `@Gankra` `@scottmcm` `@RalfJung`

r? rust-lang/libs-api
  • Loading branch information
bors committed Aug 28, 2022
2 parents 1239a02 + 5f357c2 commit f48af91
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,13 @@ fn codegen_regular_intrinsic_call<'tcx>(
ret.write_cvalue(fx, CValue::by_val(res, base.layout()));
}

sym::ptr_mask => {
intrinsic_args!(fx, args => (ptr, mask); intrinsic);
let ptr = ptr.load_scalar(fx);
let mask = mask.load_scalar(fx);
fx.bcx.ins().band(ptr, mask);
}

sym::transmute => {
intrinsic_args!(fx, args => (from); intrinsic);

Expand Down

0 comments on commit f48af91

Please sign in to comment.