Skip to content

Commit 7006047

Browse files
committed
Auto merge of #96946 - WaffleLapkin:ptr_mask, r=scottmcm
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
2 parents 1a61b6e + 1acc3b4 commit 7006047

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/intrinsic/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,18 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
309309
return;
310310
}
311311

312+
sym::ptr_mask => {
313+
let usize_type = self.context.new_type::<usize>();
314+
let void_ptr_type = self.context.new_type::<*const ()>();
315+
316+
let ptr = args[0].immediate();
317+
let mask = args[1].immediate();
318+
319+
let addr = self.bitcast(ptr, usize_type);
320+
let masked = self.and(addr, mask);
321+
self.bitcast(masked, void_ptr_type)
322+
},
323+
312324
_ if name_str.starts_with("simd_") => {
313325
match generic_simd_intrinsic(self, name, callee_ty, args, ret_ty, llret_ty, span) {
314326
Ok(llval) => llval,

0 commit comments

Comments
 (0)