Skip to content

Commit 9129f43

Browse files
committed
Move unsigned_max etc into Size again
1 parent 459c910 commit 9129f43

File tree

6 files changed

+37
-37
lines changed

6 files changed

+37
-37
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
234234
&r,
235235
)?;
236236
let val = if overflowed {
237-
let num_bits = l.layout.size.bits();
237+
let size = l.layout.size;
238+
let num_bits = size.bits();
238239
if l.layout.abi.is_signed() {
239240
// For signed ints the saturated value depends on the sign of the first
240241
// term since the sign of the second term can be inferred from this and
@@ -259,10 +260,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
259260
// unsigned
260261
if is_add {
261262
// max unsigned
262-
Scalar::from_uint(
263-
u128::MAX >> (128 - num_bits),
264-
Size::from_bits(num_bits),
265-
)
263+
Scalar::from_uint(size.unsigned_max(), Size::from_bits(num_bits))
266264
} else {
267265
// underflow to 0
268266
Scalar::from_uint(0u128, Size::from_bits(num_bits))

compiler/rustc_const_eval/src/interpret/validity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
627627
// At least one value is excluded.
628628
let valid_range = scalar_layout.valid_range;
629629
let WrappingRange { start, end } = valid_range;
630-
let max_value = u128::MAX >> (128 - op.layout.size.bits());
630+
let max_value = op.layout.size.unsigned_max();
631631
assert!(end <= max_value);
632632
// Determine the allowed range
633633
let value = self.read_scalar(op)?;

compiler/rustc_middle/src/ty/layout.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -512,9 +512,9 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
512512
let param_env = self.param_env;
513513
let dl = self.data_layout();
514514
let scalar_unit = |value: Primitive| {
515-
let bits = value.size(dl).bits();
516-
assert!(bits <= 128);
517-
Scalar { value, valid_range: WrappingRange { start: 0, end: (!0 >> (128 - bits)) } }
515+
let size = value.size(dl);
516+
assert!(size.bits() <= 128);
517+
Scalar { value, valid_range: WrappingRange { start: 0, end: size.unsigned_max() } }
518518
};
519519
let scalar = |value: Primitive| tcx.intern_layout(Layout::scalar(self, scalar_unit(value)));
520520

@@ -1266,7 +1266,7 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
12661266
}
12671267
}
12681268

1269-
let tag_mask = !0u128 >> (128 - ity.size().bits());
1269+
let tag_mask = ity.size().unsigned_max();
12701270
let tag = Scalar {
12711271
value: Int(ity, signed),
12721272
valid_range: WrappingRange {

compiler/rustc_middle/src/ty/util.rs

+5-17
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,6 @@ impl<'tcx> fmt::Display for Discr<'tcx> {
4545
}
4646
}
4747

48-
fn signed_min(size: Size) -> i128 {
49-
size.sign_extend(1_u128 << (size.bits() - 1)) as i128
50-
}
51-
52-
fn signed_max(size: Size) -> i128 {
53-
i128::MAX >> (128 - size.bits())
54-
}
55-
56-
fn unsigned_max(size: Size) -> u128 {
57-
u128::MAX >> (128 - size.bits())
58-
}
59-
6048
fn int_size_and_signed<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> (Size, bool) {
6149
let (int, signed) = match *ty.kind() {
6250
Int(ity) => (Integer::from_int_ty(&tcx, ity), true),
@@ -74,8 +62,8 @@ impl<'tcx> Discr<'tcx> {
7462
pub fn checked_add(self, tcx: TyCtxt<'tcx>, n: u128) -> (Self, bool) {
7563
let (size, signed) = int_size_and_signed(tcx, self.ty);
7664
let (val, oflo) = if signed {
77-
let min = signed_min(size);
78-
let max = signed_max(size);
65+
let min = size.signed_min();
66+
let max = size.signed_max();
7967
let val = size.sign_extend(self.val) as i128;
8068
assert!(n < (i128::MAX as u128));
8169
let n = n as i128;
@@ -86,7 +74,7 @@ impl<'tcx> Discr<'tcx> {
8674
let val = size.truncate(val);
8775
(val, oflo)
8876
} else {
89-
let max = unsigned_max(size);
77+
let max = size.unsigned_max();
9078
let val = self.val;
9179
let oflo = val > max - n;
9280
let val = if oflo { n - (max - val) - 1 } else { val + n };
@@ -621,7 +609,7 @@ impl<'tcx> ty::TyS<'tcx> {
621609
let val = match self.kind() {
622610
ty::Int(_) | ty::Uint(_) => {
623611
let (size, signed) = int_size_and_signed(tcx, self);
624-
let val = if signed { signed_max(size) as u128 } else { unsigned_max(size) };
612+
let val = if signed { size.signed_max() as u128 } else { size.unsigned_max() };
625613
Some(val)
626614
}
627615
ty::Char => Some(std::char::MAX as u128),
@@ -640,7 +628,7 @@ impl<'tcx> ty::TyS<'tcx> {
640628
let val = match self.kind() {
641629
ty::Int(_) | ty::Uint(_) => {
642630
let (size, signed) = int_size_and_signed(tcx, self);
643-
let val = if signed { size.truncate(signed_min(size) as u128) } else { 0 };
631+
let val = if signed { size.truncate(size.signed_min() as u128) } else { 0 };
644632
Some(val)
645633
}
646634
ty::Char => Some(0),

compiler/rustc_mir_build/src/build/expr/as_rvalue.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -494,9 +494,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
494494
// Helper to get a `-1` value of the appropriate type
495495
fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
496496
let param_ty = ty::ParamEnv::empty().and(ty);
497-
let bits = self.tcx.layout_of(param_ty).unwrap().size.bits();
498-
let n = (!0u128) >> (128 - bits);
499-
let literal = ty::Const::from_bits(self.tcx, n, param_ty);
497+
let size = self.tcx.layout_of(param_ty).unwrap().size;
498+
let literal = ty::Const::from_bits(self.tcx, size.unsigned_max(), param_ty);
500499

501500
self.literal_operand(span, literal)
502501
}

compiler/rustc_target/src/abi/mod.rs

+22-7
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,21 @@ impl Size {
392392
// Truncate (shift left to drop out leftover values, shift right to fill with zeroes).
393393
(value << shift) >> shift
394394
}
395+
396+
#[inline]
397+
pub fn signed_min(&self) -> i128 {
398+
self.sign_extend(1_u128 << (self.bits() - 1)) as i128
399+
}
400+
401+
#[inline]
402+
pub fn signed_max(&self) -> i128 {
403+
i128::MAX >> (128 - self.bits())
404+
}
405+
406+
#[inline]
407+
pub fn unsigned_max(&self) -> u128 {
408+
u128::MAX >> (128 - self.bits())
409+
}
395410
}
396411

397412
// Panicking addition, subtraction and multiplication for convenience.
@@ -775,7 +790,7 @@ impl WrappingRange {
775790
/// Returns `true` if `size` completely fills the range.
776791
#[inline]
777792
pub fn is_full_for(&self, size: Size) -> bool {
778-
let max_value = u128::MAX >> (128 - size.bits());
793+
let max_value = size.unsigned_max();
779794
debug_assert!(self.start <= max_value && self.end <= max_value);
780795
(self.start == 0 && self.end == max_value) || (self.end + 1 == self.start)
781796
}
@@ -1067,9 +1082,9 @@ impl Niche {
10671082

10681083
pub fn available<C: HasDataLayout>(&self, cx: &C) -> u128 {
10691084
let Scalar { value, valid_range: v } = self.scalar;
1070-
let bits = value.size(cx).bits();
1071-
assert!(bits <= 128);
1072-
let max_value = !0u128 >> (128 - bits);
1085+
let size = value.size(cx);
1086+
assert!(size.bits() <= 128);
1087+
let max_value = size.unsigned_max();
10731088

10741089
// Find out how many values are outside the valid range.
10751090
let niche = v.end.wrapping_add(1)..v.start;
@@ -1080,9 +1095,9 @@ impl Niche {
10801095
assert!(count > 0);
10811096

10821097
let Scalar { value, valid_range: v } = self.scalar;
1083-
let bits = value.size(cx).bits();
1084-
assert!(bits <= 128);
1085-
let max_value = !0u128 >> (128 - bits);
1098+
let size = value.size(cx);
1099+
assert!(size.bits() <= 128);
1100+
let max_value = size.unsigned_max();
10861101

10871102
if count > max_value {
10881103
return None;

0 commit comments

Comments
 (0)