inline fn log10_int_u8(x: u8) u32 x: u8100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115inline fn log10_int_u8(x: u8) u32 {
// For better performance, avoid branches by assembling the solution
// in the bits above the low 8 bits.
// Adding c1 to val gives 10 in the top bits for val < 10, 11 for val >= 10
const C1: u32 = 0b11_00000000 - 10; // 758
// Adding c2 to val gives 01 in the top bits for val < 100, 10 for val >= 100
const C2: u32 = 0b10_00000000 - 100; // 412
// Value of top bits:
// +c1 +c2 1&2
// 0..=9 10 01 00 = 0
// 10..=99 11 01 01 = 1
// 100..=255 11 10 10 = 2
return ((x + C1) & (x + C2)) >> 8;
}