File tree Expand file tree Collapse file tree 1 file changed +11
-24
lines changed Expand file tree Collapse file tree 1 file changed +11
-24
lines changed Original file line number Diff line number Diff line change 33// See the license.txt file in the project root for more information.
44
55using System . Text ;
6+ using System . Numerics ;
67
78namespace LibObjectFile . Dwarf
89{
@@ -31,35 +32,21 @@ public static uint SizeOfUInt(DwarfAddressSize addressSize)
3132
3233 public static uint SizeOfULEB128 ( ulong value )
3334 {
34- if ( value == 0 ) return 1 ;
35+ // bits_to_encode = (data != 0) ? 64 - CLZ(x) : 1 = 64 - CLZ(data | 1)
36+ // bytes = ceil(bits_to_encode / 7.0); = (6 + bits_to_encode) / 7
37+ uint x = 6 + 64 - ( uint ) BitOperations . LeadingZeroCount ( value | 1UL ) ;
3538
36- uint sizeOf = 0 ;
37- while ( value != 0 )
38- {
39- value >>= 7 ;
40- sizeOf ++ ;
41- }
42-
43- return sizeOf ;
39+ // Division by 7 is done by (x * 37) >> 8 where 37 = ceil(256 / 7).
40+ // This works for 0 <= x < 256 / (7 * 37 - 256), i.e. 0 <= x <= 85.
41+ return ( x * 37 ) >> 8 ;
4442 }
4543
4644 public static uint SizeOfILEB128 ( long value )
4745 {
48- if ( value == 0 ) return 1 ;
49- uint sizeOf = 0 ;
50- while ( true )
51- {
52- sizeOf ++ ;
53- var b = ( byte ) value ;
54- value >>= 7 ;
55- bool isSignBitSet = ( b & 0x40 ) != 0 ;
56- if ( ( value == 0 && ! isSignBitSet ) || ( value == - 1 && isSignBitSet ) )
57- {
58- break ;
59- }
60- }
61-
62- return sizeOf ;
46+ // The same as SizeOfULEB128 calculation but we have to account for the sign bit.
47+ value ^= value >> 63 ;
48+ uint x = 1 + 6 + 64 - ( uint ) BitOperations . LeadingZeroCount ( ( ulong ) value | 1UL ) ;
49+ return ( x * 37 ) >> 8 ;
6350 }
6451
6552 public static DwarfAttributeEncoding GetAttributeEncoding ( DwarfAttributeKindEx kind )
You can’t perform that action at this time.
0 commit comments