Skip to content

Commit

Permalink
Auto merge of #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/rust#95643 (comment)

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

r? rust-lang/libs-api
  • Loading branch information
bors committed Aug 28, 2022
2 parents 1a61b6e + 1acc3b4 commit 7006047
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/intrinsic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,18 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
return;
}

sym::ptr_mask => {
let usize_type = self.context.new_type::<usize>();
let void_ptr_type = self.context.new_type::<*const ()>();

let ptr = args[0].immediate();
let mask = args[1].immediate();

let addr = self.bitcast(ptr, usize_type);
let masked = self.and(addr, mask);
self.bitcast(masked, void_ptr_type)
},

_ if name_str.starts_with("simd_") => {
match generic_simd_intrinsic(self, name, callee_ty, args, ret_ty, llret_ty, span) {
Ok(llval) => llval,
Expand Down

0 comments on commit 7006047

Please sign in to comment.