-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
std.math.log_int: implement integer logarithm without using float math #17143
Changes from 1 commit
daafc14
c305efd
c64b9bd
8a29847
55573d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const std = @import("../std.zig"); | ||
const math = std.math; | ||
const testing = std.testing; | ||
const assert = std.debug.assert; | ||
const Log2Int = math.Log2Int; | ||
|
||
/// Returns the logarithm of `x` for the provided `base`, rounding down to the nearest integer. | ||
FedericoStra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// Asserts that `base > 1` and `x != 0`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed: 8a29847 |
||
pub fn log_int(comptime T: type, base: T, x: T) Log2Int(T) { | ||
if (@typeInfo(T) != .Int or @typeInfo(T).Int.signedness != .unsigned) | ||
@compileError("log_int requires an unsigned integer, found " ++ @typeName(T)); | ||
|
||
assert(base > 1 and x != 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiousity: Did you test, if the assertion fires, if either base or x is known at comptime and satisfies the subexpressions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand the question. Do you mean whether an expression such as |
||
|
||
var exponent: Log2Int(T) = 0; | ||
var power: T = 1; | ||
while (power <= x / base) { | ||
power *= base; | ||
exponent += 1; | ||
} | ||
|
||
return exponent; | ||
} | ||
|
||
test "log" { | ||
FedericoStra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// test all unsigned integers with 2, 3, ..., 64 bits | ||
inline for (2..64 + 1) |bits| { | ||
const T = @Type(std.builtin.Type{ | ||
.Int = std.builtin.Type.Int{ .signedness = .unsigned, .bits = @intCast(bits) }, | ||
}); | ||
|
||
// for base = 2, 3, ..., min(maxInt(T),1024) | ||
var base: T = 1; | ||
while (base < math.maxInt(T) and base <= 1024) { | ||
base += 1; | ||
|
||
// test `log(1) == 0` | ||
FedericoStra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try testing.expectEqual(@as(Log2Int(T), 0), log_int(T, base, 1)); | ||
|
||
// for powers `pow = base^exp` that fit inside T | ||
var exp: Log2Int(T) = 0; | ||
var pow: T = 1; | ||
while (pow < math.maxInt(T) / base) { | ||
exp += 1; | ||
pow *= base; | ||
|
||
// test that `log_int` correctly detects the threshold | ||
FedericoStra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try testing.expectEqual(exp - 1, log_int(T, base, pow - 1)); | ||
try testing.expectEqual(exp, log_int(T, base, pow)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
test "log2" { | ||
const types = [_]type{ u2, u3, u4, u8, u16, u24 }; | ||
inline for (types) |T| { | ||
var n: T = 0; | ||
while (n < math.maxInt(T)) { | ||
n += 1; | ||
const special = math.log2_int(T, n); | ||
const general = log_int(T, 2, n); | ||
try testing.expectEqual(special, general); | ||
} | ||
} | ||
} | ||
|
||
test "log10" { | ||
const types = [_]type{ u4, u5, u6, u8, u16, u24 }; | ||
inline for (types) |T| { | ||
var n: T = 0; | ||
while (n < math.maxInt(T)) { | ||
n += 1; | ||
const special = math.log10_int(n); | ||
const general = log_int(T, 10, n); | ||
try testing.expectEqual(special, general); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add, that we should special case comptime-known divisions of power of two with shifts and/or make an issue.