@@ -349,6 +349,7 @@ impl<'tcx> AllocMap<'tcx> {
349
349
/// illegal and will likely ICE.
350
350
/// This function exists to allow const eval to detect the difference between evaluation-
351
351
/// local dangling pointers and allocations in constants/statics.
352
+ #[ inline]
352
353
pub fn get ( & self , id : AllocId ) -> Option < AllocKind < ' tcx > > {
353
354
self . id_to_kind . get ( & id) . cloned ( )
354
355
}
@@ -397,6 +398,7 @@ impl<'tcx> AllocMap<'tcx> {
397
398
// Methods to access integers in the target endianness
398
399
////////////////////////////////////////////////////////////////////////////////
399
400
401
+ #[ inline]
400
402
pub fn write_target_uint (
401
403
endianness : layout:: Endian ,
402
404
mut target : & mut [ u8 ] ,
@@ -409,6 +411,7 @@ pub fn write_target_uint(
409
411
}
410
412
}
411
413
414
+ #[ inline]
412
415
pub fn read_target_uint ( endianness : layout:: Endian , mut source : & [ u8 ] ) -> Result < u128 , io:: Error > {
413
416
match endianness {
414
417
layout:: Endian :: Little => source. read_uint128 :: < LittleEndian > ( source. len ( ) ) ,
@@ -420,17 +423,30 @@ pub fn read_target_uint(endianness: layout::Endian, mut source: &[u8]) -> Result
420
423
// Methods to facilitate working with signed integers stored in a u128
421
424
////////////////////////////////////////////////////////////////////////////////
422
425
426
+ /// Truncate `value` to `size` bits and then sign-extend it to 128 bits
427
+ /// (i.e., if it is negative, fill with 1's on the left).
428
+ #[ inline]
423
429
pub fn sign_extend ( value : u128 , size : Size ) -> u128 {
424
430
let size = size. bits ( ) ;
431
+ if size == 0 {
432
+ // Truncated until nothing is left.
433
+ return 0 ;
434
+ }
425
435
// sign extend
426
436
let shift = 128 - size;
427
437
// shift the unsigned value to the left
428
438
// and back to the right as signed (essentially fills with FF on the left)
429
439
( ( ( value << shift) as i128 ) >> shift) as u128
430
440
}
431
441
442
+ /// Truncate `value` to `size` bits.
443
+ #[ inline]
432
444
pub fn truncate ( value : u128 , size : Size ) -> u128 {
433
445
let size = size. bits ( ) ;
446
+ if size == 0 {
447
+ // Truncated until nothing is left.
448
+ return 0 ;
449
+ }
434
450
let shift = 128 - size;
435
451
// truncate (shift left to drop out leftover values, shift right to fill with zeroes)
436
452
( value << shift) >> shift
0 commit comments