pub fn log10_int(x: anytype) std.math.Log2Int(@TypeOf(x)) x41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75pub fn log10_int(x: anytype) std.math.Log2Int(@TypeOf(x)) {
const T = @TypeOf(x);
const OutT = std.math.Log2Int(T);
if (@typeInfo(T) != .int or @typeInfo(T).int.signedness != .unsigned)
@compileError("log10_int requires an unsigned integer, found " ++ @typeName(T));
std.debug.assert(x != 0);
const bit_size = @typeInfo(T).int.bits;
if (bit_size <= 8) {
return @as(OutT, @intCast(log10_int_u8(x)));
} else if (bit_size <= 16) {
return @as(OutT, @intCast(less_than_5(x)));
}
var val = x;
var log: u32 = 0;
inline for (0..11) |i| {
// Unnecessary branches should be removed by the compiler
if (bit_size > (1 << (11 - i)) * 5 * @log2(10.0) and val >= pow10((1 << (11 - i)) * 5)) {
const num_digits = (1 << (11 - i)) * 5;
val /= pow10(num_digits);
log += num_digits;
}
}
if (val >= pow10(5)) {
val /= pow10(5);
log += 5;
}
return @as(OutT, @intCast(log + less_than_5(@as(u32, @intCast(val)))));
}
Return the log base 10 of integer value x, rounding down to the nearest integer.