Skip to content

Commit

Permalink
Rollup merge of rust-lang#58749 - kenta7777:reduce-repetition, r=oli-obk
Browse files Browse the repository at this point in the history
Reduce Repetitions of (n << amt) >> amt

Fixes part of [rust-lang#49937](rust-lang#49937).
  • Loading branch information
Centril authored Feb 27, 2019
2 parents 0ac99f0 + 992694a commit bd2dab8
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/librustc/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::hir::def::Def;
use crate::hir::def_id::DefId;
use crate::hir::map::DefPathData;
use crate::hir::{self, Node};
use crate::mir::interpret::{sign_extend, truncate};
use crate::ich::NodeIdHashingMode;
use crate::traits::{self, ObligationCause};
use crate::ty::{self, Ty, TyCtxt, GenericParamDefKind, TypeFoldable};
Expand Down Expand Up @@ -32,12 +33,12 @@ impl<'tcx> fmt::Display for Discr<'tcx> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.ty.sty {
ty::Int(ity) => {
let bits = ty::tls::with(|tcx| {
Integer::from_attr(&tcx, SignedInt(ity)).size().bits()
let size = ty::tls::with(|tcx| {
Integer::from_attr(&tcx, SignedInt(ity)).size()
});
let x = self.val as i128;
let x = self.val;
// sign extend the raw representation to be an i128
let x = (x << (128 - bits)) >> (128 - bits);
let x = sign_extend(x, size) as i128;
write!(fmt, "{}", x)
},
_ => write!(fmt, "{}", self.val),
Expand All @@ -57,12 +58,12 @@ impl<'tcx> Discr<'tcx> {
_ => bug!("non integer discriminant"),
};

let size = int.size();
let bit_size = int.size().bits();
let shift = 128 - bit_size;
if signed {
let sext = |u| {
let i = u as i128;
(i << shift) >> shift
sign_extend(u, size) as i128
};
let min = sext(1_u128 << (bit_size - 1));
let max = i128::max_value() >> shift;
Expand All @@ -77,7 +78,7 @@ impl<'tcx> Discr<'tcx> {
};
// zero the upper bits
let val = val as u128;
let val = (val << shift) >> shift;
let val = truncate(val, size);
(Self {
val: val as u128,
ty: self.ty,
Expand Down

0 comments on commit bd2dab8

Please sign in to comment.