Skip to content

Commit edd4f48

Browse files
authored
Unrolled build for #143477
Rollup merge of #143477 - folkertdev:use-is-multiple-of, r=joshtriplett use `is_multiple_of` and `div_ceil` In tricky logic, these functions are much more informative than the manual implementations. They also catch subtle bugs: - the manual `is_multiple_of` often does not handle division by zero - manual `div_ceil` often does not consider overflow The transformation is free for `is_multiple_of` if the divisor is compile-time known to be non-zero. For `div_ceil` there is a small cost to considering overflow. Here is some assembly https://godbolt.org/z/5zP8KaE1d.
2 parents e804cd4 + ed3711e commit edd4f48

File tree

22 files changed

+33
-34
lines changed

22 files changed

+33
-34
lines changed

compiler/rustc_abi/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,7 @@ impl Size {
527527
/// not a multiple of 8.
528528
pub fn from_bits(bits: impl TryInto<u64>) -> Size {
529529
let bits = bits.try_into().ok().unwrap();
530-
// Avoid potential overflow from `bits + 7`.
531-
Size { raw: bits / 8 + ((bits % 8) + 7) / 8 }
530+
Size { raw: bits.div_ceil(8) }
532531
}
533532

534533
#[inline]

compiler/rustc_borrowck/src/polonius/legacy/location.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,6 @@ impl PoloniusLocationTable {
109109
impl LocationIndex {
110110
fn is_start(self) -> bool {
111111
// even indices are start points; odd indices are mid points
112-
(self.index() % 2) == 0
112+
self.index().is_multiple_of(2)
113113
}
114114
}

compiler/rustc_codegen_llvm/src/abi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl LlvmType for CastTarget {
146146
"total size {:?} cannot be divided into units of zero size",
147147
self.rest.total
148148
);
149-
if self.rest.total.bytes() % self.rest.unit.size.bytes() != 0 {
149+
if !self.rest.total.bytes().is_multiple_of(self.rest.unit.size.bytes()) {
150150
assert_eq!(self.rest.unit.kind, RegKind::Integer, "only int regs can be split");
151151
}
152152
self.rest.total.bytes().div_ceil(self.rest.unit.size.bytes())

compiler/rustc_codegen_llvm/src/va_arg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ fn emit_aapcs_va_arg<'ll, 'tcx>(
172172

173173
let gr_type = target_ty.is_any_ptr() || target_ty.is_integral();
174174
let (reg_off, reg_top, slot_size) = if gr_type {
175-
let nreg = (layout.size.bytes() + 7) / 8;
175+
let nreg = layout.size.bytes().div_ceil(8);
176176
(gr_offs, gr_top, nreg * 8)
177177
} else {
178-
let nreg = (layout.size.bytes() + 15) / 16;
178+
let nreg = layout.size.bytes().div_ceil(16);
179179
(vr_offs, vr_top, nreg * 16)
180180
};
181181

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
537537

538538
#[inline]
539539
fn is_offset_misaligned(offset: u64, align: Align) -> Option<Misalignment> {
540-
if offset % align.bytes() == 0 {
540+
if offset.is_multiple_of(align.bytes()) {
541541
None
542542
} else {
543543
// The biggest power of two through which `offset` is divisible.
@@ -1554,7 +1554,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
15541554
// If the allocation is N-aligned, and the offset is not divisible by N,
15551555
// then `base + offset` has a non-zero remainder after division by `N`,
15561556
// which means `base + offset` cannot be null.
1557-
if offset.bytes() % info.align.bytes() != 0 {
1557+
if !offset.bytes().is_multiple_of(info.align.bytes()) {
15581558
return interp_ok(false);
15591559
}
15601560
// We don't know enough, this might be null.

compiler/rustc_index/src/bit_set.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,13 +1744,13 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
17441744

17451745
#[inline]
17461746
fn num_words<T: Idx>(domain_size: T) -> usize {
1747-
(domain_size.index() + WORD_BITS - 1) / WORD_BITS
1747+
domain_size.index().div_ceil(WORD_BITS)
17481748
}
17491749

17501750
#[inline]
17511751
fn num_chunks<T: Idx>(domain_size: T) -> usize {
17521752
assert!(domain_size.index() > 0);
1753-
(domain_size.index() + CHUNK_BITS - 1) / CHUNK_BITS
1753+
domain_size.index().div_ceil(CHUNK_BITS)
17541754
}
17551755

17561756
#[inline]

compiler/rustc_passes/src/liveness/rwu_table.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl RWUTable {
4444
const WORD_RWU_COUNT: usize = Self::WORD_BITS / Self::RWU_BITS;
4545

4646
pub(super) fn new(live_nodes: usize, vars: usize) -> RWUTable {
47-
let live_node_words = (vars + Self::WORD_RWU_COUNT - 1) / Self::WORD_RWU_COUNT;
47+
let live_node_words = vars.div_ceil(Self::WORD_RWU_COUNT);
4848
Self { live_nodes, vars, live_node_words, words: vec![0u8; live_node_words * live_nodes] }
4949
}
5050

compiler/rustc_query_system/src/query/plumbing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ where
597597
// from disk. Re-hashing results is fairly expensive, so we can't
598598
// currently afford to verify every hash. This subset should still
599599
// give us some coverage of potential bugs though.
600-
let try_verify = prev_fingerprint.split().1.as_u64() % 32 == 0;
600+
let try_verify = prev_fingerprint.split().1.as_u64().is_multiple_of(32);
601601
if std::intrinsics::unlikely(
602602
try_verify || qcx.dep_context().sess().opts.unstable_opts.incremental_verify_ich,
603603
) {

compiler/rustc_serialize/src/leb128.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::serialize::Decoder;
77
/// Returns the length of the longest LEB128 encoding for `T`, assuming `T` is an integer type
88
pub const fn max_leb128_len<T>() -> usize {
99
// The longest LEB128 encoding for an integer uses 7 bits per byte.
10-
(size_of::<T>() * 8 + 6) / 7
10+
(size_of::<T>() * 8).div_ceil(7)
1111
}
1212

1313
/// Returns the length of the longest LEB128 encoding of all supported integer types.

compiler/rustc_span/src/edit_distance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ pub fn edit_distance_with_substrings(a: &str, b: &str, limit: usize) -> Option<u
130130
1 // Exact substring match, but not a total word match so return non-zero
131131
} else if !big_len_diff {
132132
// Not a big difference in length, discount cost of length difference
133-
score + (len_diff + 1) / 2
133+
score + len_diff.div_ceil(2)
134134
} else {
135135
// A big difference in length, add back the difference in length to the score
136136
score + len_diff

0 commit comments

Comments
 (0)