@@ -441,36 +441,50 @@ pub struct Align {
441
441
}
442
442
443
443
impl Align {
444
+ #[ inline]
444
445
pub fn from_bits ( bits : u64 ) -> Result < Align , String > {
445
446
Align :: from_bytes ( Size :: from_bits ( bits) . bytes ( ) )
446
447
}
447
448
449
+ #[ inline]
448
450
pub fn from_bytes ( align : u64 ) -> Result < Align , String > {
449
451
// Treat an alignment of 0 bytes like 1-byte alignment.
450
452
if align == 0 {
451
453
return Ok ( Align { pow2 : 0 } ) ;
452
454
}
453
455
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
+
454
466
let mut bytes = align;
455
467
let mut pow2: u8 = 0 ;
456
468
while ( bytes & 1 ) == 0 {
457
469
pow2 += 1 ;
458
470
bytes >>= 1 ;
459
471
}
460
472
if bytes != 1 {
461
- return Err ( format ! ( "`{}` is not a power of 2" , align) ) ;
473
+ return Err ( not_power_of_2 ( align) ) ;
462
474
}
463
475
if pow2 > 29 {
464
- return Err ( format ! ( "`{}` is too large" , align) ) ;
476
+ return Err ( too_large ( align) ) ;
465
477
}
466
478
467
479
Ok ( Align { pow2 } )
468
480
}
469
481
482
+ #[ inline]
470
483
pub fn bytes ( self ) -> u64 {
471
484
1 << self . pow2
472
485
}
473
486
487
+ #[ inline]
474
488
pub fn bits ( self ) -> u64 {
475
489
self . bytes ( ) * 8
476
490
}
@@ -479,12 +493,14 @@ impl Align {
479
493
/// (the largest power of two that the offset is a multiple of).
480
494
///
481
495
/// N.B., for an offset of `0`, this happens to return `2^64`.
496
+ #[ inline]
482
497
pub fn max_for_offset ( offset : Size ) -> Align {
483
498
Align { pow2 : offset. bytes ( ) . trailing_zeros ( ) as u8 }
484
499
}
485
500
486
501
/// Lower the alignment, if necessary, such that the given offset
487
502
/// is aligned to it (the offset is a multiple of the alignment).
503
+ #[ inline]
488
504
pub fn restrict_for_offset ( self , offset : Size ) -> Align {
489
505
self . min ( Align :: max_for_offset ( offset) )
490
506
}
0 commit comments