Skip to content

Commit 5dd98a4

Browse files
authored
Rollup merge of rust-lang#114382 - scottmcm:compare-bytes-intrinsic, 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`
2 parents 3f92261 + 659fabd commit 5dd98a4

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/intrinsics/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,20 @@ fn codegen_regular_intrinsic_call<'tcx>(
11551155
ret.write_cvalue(fx, CValue::by_val(is_eq_value, ret.layout()));
11561156
}
11571157

1158+
sym::compare_bytes => {
1159+
intrinsic_args!(fx, args => (lhs_ptr, rhs_ptr, bytes_val); intrinsic);
1160+
let lhs_ptr = lhs_ptr.load_scalar(fx);
1161+
let rhs_ptr = rhs_ptr.load_scalar(fx);
1162+
let bytes_val = bytes_val.load_scalar(fx);
1163+
1164+
let params = vec![AbiParam::new(fx.pointer_type); 3];
1165+
let returns = vec![AbiParam::new(types::I32)];
1166+
let args = &[lhs_ptr, rhs_ptr, bytes_val];
1167+
// Here we assume that the `memcmp` provided by the target is a NOP for size 0.
1168+
let cmp = fx.lib_call("memcmp", params, returns, args)[0];
1169+
ret.write_cvalue(fx, CValue::by_val(cmp, ret.layout()));
1170+
}
1171+
11581172
sym::const_allocate => {
11591173
intrinsic_args!(fx, args => (_size, _align); intrinsic);
11601174

0 commit comments

Comments
 (0)