Skip to content

Commit

Permalink
Inline all methods on abi::Size
Browse files Browse the repository at this point in the history
This save 3 seconds on the test program.
  • Loading branch information
wesleywiser committed Jun 27, 2018
1 parent 63ab0cb commit 429bc8d
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/librustc_target/abi/mod.rs
Expand Up @@ -229,11 +229,13 @@ pub struct Size {
impl Size {
pub const ZERO: Size = Self::from_bytes(0);

#[inline]
pub fn from_bits(bits: u64) -> Size {
// Avoid potential overflow from `bits + 7`.
Size::from_bytes(bits / 8 + ((bits % 8) + 7) / 8)
}

#[inline]
pub const fn from_bytes(bytes: u64) -> Size {
Size {
raw: bytes
Expand All @@ -245,22 +247,26 @@ impl Size {
self.raw
}

#[inline]
pub fn bits(self) -> u64 {
self.bytes().checked_mul(8).unwrap_or_else(|| {
panic!("Size::bits: {} bytes in bits doesn't fit in u64", self.bytes())
})
}

#[inline]
pub fn abi_align(self, align: Align) -> Size {
let mask = align.abi() - 1;
Size::from_bytes((self.bytes() + mask) & !mask)
}

#[inline]
pub fn is_abi_aligned(self, align: Align) -> bool {
let mask = align.abi() - 1;
self.bytes() & mask == 0
}

#[inline]
pub fn checked_add<C: HasDataLayout>(self, offset: Size, cx: C) -> Option<Size> {
let dl = cx.data_layout();

Expand All @@ -273,6 +279,7 @@ impl Size {
}
}

#[inline]
pub fn checked_mul<C: HasDataLayout>(self, count: u64, cx: C) -> Option<Size> {
let dl = cx.data_layout();

Expand All @@ -290,6 +297,7 @@ impl Size {

impl Add for Size {
type Output = Size;
#[inline]
fn add(self, other: Size) -> Size {
Size::from_bytes(self.bytes().checked_add(other.bytes()).unwrap_or_else(|| {
panic!("Size::add: {} + {} doesn't fit in u64", self.bytes(), other.bytes())
Expand All @@ -299,6 +307,7 @@ impl Add for Size {

impl Sub for Size {
type Output = Size;
#[inline]
fn sub(self, other: Size) -> Size {
Size::from_bytes(self.bytes().checked_sub(other.bytes()).unwrap_or_else(|| {
panic!("Size::sub: {} - {} would result in negative size", self.bytes(), other.bytes())
Expand All @@ -308,13 +317,15 @@ impl Sub for Size {

impl Mul<Size> for u64 {
type Output = Size;
#[inline]
fn mul(self, size: Size) -> Size {
size * self
}
}

impl Mul<u64> for Size {
type Output = Size;
#[inline]
fn mul(self, count: u64) -> Size {
match self.bytes().checked_mul(count) {
Some(bytes) => Size::from_bytes(bytes),
Expand All @@ -326,6 +337,7 @@ impl Mul<u64> for Size {
}

impl AddAssign for Size {
#[inline]
fn add_assign(&mut self, other: Size) {
*self = *self + other;
}
Expand Down

0 comments on commit 429bc8d

Please sign in to comment.