Skip to content

Commit 83dc7fe

Browse files
committed
Inline trivial implementation of rustc_target::abi::Align
1 parent 613ef74 commit 83dc7fe

File tree

1 file changed

+18
-2
lines changed
  • compiler/rustc_target/src/abi

1 file changed

+18
-2
lines changed

compiler/rustc_target/src/abi/mod.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -441,36 +441,50 @@ pub struct Align {
441441
}
442442

443443
impl Align {
444+
#[inline]
444445
pub fn from_bits(bits: u64) -> Result<Align, String> {
445446
Align::from_bytes(Size::from_bits(bits).bytes())
446447
}
447448

449+
#[inline]
448450
pub fn from_bytes(align: u64) -> Result<Align, String> {
449451
// Treat an alignment of 0 bytes like 1-byte alignment.
450452
if align == 0 {
451453
return Ok(Align { pow2: 0 });
452454
}
453455

456+
#[cold]
457+
fn not_power_of_2(align: u64) -> String {
458+
format!("`{}` is not a power of 2", align)
459+
}
460+
461+
#[cold]
462+
fn too_large(align: u64) -> String {
463+
format!("`{}` is too large", align)
464+
}
465+
454466
let mut bytes = align;
455467
let mut pow2: u8 = 0;
456468
while (bytes & 1) == 0 {
457469
pow2 += 1;
458470
bytes >>= 1;
459471
}
460472
if bytes != 1 {
461-
return Err(format!("`{}` is not a power of 2", align));
473+
return Err(not_power_of_2(align));
462474
}
463475
if pow2 > 29 {
464-
return Err(format!("`{}` is too large", align));
476+
return Err(too_large(align));
465477
}
466478

467479
Ok(Align { pow2 })
468480
}
469481

482+
#[inline]
470483
pub fn bytes(self) -> u64 {
471484
1 << self.pow2
472485
}
473486

487+
#[inline]
474488
pub fn bits(self) -> u64 {
475489
self.bytes() * 8
476490
}
@@ -479,12 +493,14 @@ impl Align {
479493
/// (the largest power of two that the offset is a multiple of).
480494
///
481495
/// N.B., for an offset of `0`, this happens to return `2^64`.
496+
#[inline]
482497
pub fn max_for_offset(offset: Size) -> Align {
483498
Align { pow2: offset.bytes().trailing_zeros() as u8 }
484499
}
485500

486501
/// Lower the alignment, if necessary, such that the given offset
487502
/// is aligned to it (the offset is a multiple of the alignment).
503+
#[inline]
488504
pub fn restrict_for_offset(self, offset: Size) -> Align {
489505
self.min(Align::max_for_offset(offset))
490506
}

0 commit comments

Comments
 (0)