Skip to content

Commit

Permalink
Rollup merge of rust-lang#114382 - scottmcm:compare-bytes-intrinsic, …
Browse files Browse the repository at this point in the history
…r=cjgillot

Add a new `compare_bytes` intrinsic instead of calling `memcmp` directly

As discussed in rust-lang#113435, this lets the backends be the place that can have the "don't call the function if n == 0" logic, if it's needed for the target.  (I didn't actually *add* those checks, though, since as I understood it we didn't actually need them on known targets?)

Doing this also let me make it `const` (unstable), which I don't think `extern "C" fn memcmp` can be.

cc `@RalfJung` `@Amanieu`
  • Loading branch information
matthiaskrgr authored Aug 7, 2023
2 parents fe6a477 + b132a7e commit 8a160d6
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/intrinsic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,21 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
}
}

sym::compare_bytes => {
let a = args[0].immediate();
let b = args[1].immediate();
let n = args[2].immediate();

let void_ptr_type = self.context.new_type::<*const ()>();
let a_ptr = self.bitcast(a, void_ptr_type);
let b_ptr = self.bitcast(b, void_ptr_type);

// Here we assume that the `memcmp` provided by the target is a NOP for size 0.
let builtin = self.context.get_builtin_function("memcmp");
let cmp = self.context.new_call(None, builtin, &[a_ptr, b_ptr, n]);
self.sext(cmp, self.type_ix(32))
}

sym::black_box => {
args[0].val.store(self, result);

Expand Down

0 comments on commit 8a160d6

Please sign in to comment.