Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce Repetitions of (n << amt) >> amt #58749

Merged
merged 1 commit into from
Feb 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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