-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
daafc14
std.math.log_int: implement integer logarithm without using float math
FedericoStra c305efd
std.math: ensure that log_int is tested
FedericoStra c64b9bd
std.math.log_int: improve tests
FedericoStra 8a29847
std.math.log_int: modify the assertion
FedericoStra 55573d8
std.math.log_int: add comments to prove correctness
FedericoStra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
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`. | ||
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); | ||
|
||
// Let's denote by [y] the integer part of y. | ||
|
||
// Throughout the iteration the following invariant is preserved: | ||
// power = base ^ exponent | ||
|
||
// Safety and termination. | ||
// | ||
// We never overflow inside the loop because when we enter the loop we have | ||
// power <= [maxInt(T) / base] | ||
// therefore | ||
// power * base <= maxInt(T) | ||
// is a valid multiplication for type `T` and | ||
// exponent + 1 <= log(base, maxInt(T)) <= log2(maxInt(T)) <= maxInt(Log2Int(T)) | ||
// is a valid addition for type `Log2Int(T)`. | ||
// | ||
// This implies also termination because power is strictly increasing, | ||
// hence it must eventually surpass [x / base] < maxInt(T) and we then exit the loop. | ||
|
||
var exponent: Log2Int(T) = 0; | ||
var power: T = 1; | ||
while (power <= x / base) { | ||
power *= base; | ||
exponent += 1; | ||
} | ||
|
||
// If we never entered the loop we must have | ||
// [x / base] < 1 | ||
// hence | ||
// x <= [x / base] * base < base | ||
// thus the result is 0. We can then return exponent, which is still 0. | ||
// | ||
// Otherwise, if we entered the loop at least once, | ||
// when we exit the loop we have that power is exactly divisible by base and | ||
// power / base <= [x / base] < power | ||
// hence | ||
// power <= [x / base] * base <= x < power * base | ||
// This means that | ||
// base^exponent <= x < base^(exponent+1) | ||
// hence the result is exponent. | ||
|
||
return exponent; | ||
} | ||
|
||
test "math.log_int" { | ||
// Test all unsigned integers with 2, 3, ..., 64 bits. | ||
// We cannot test 0 or 1 bits since base must be > 1. | ||
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 that `log_int(T, base, 1) == 0` | ||
try testing.expectEqual(@as(Log2Int(T), 0), log_int(T, base, 1)); | ||
|
||
// For powers `pow = base^exp > 1` that fit inside T, | ||
// test that `log_int` correctly detects the jump in the logarithm | ||
// from `log(pow-1) == exp-1` to `log(pow) == exp`. | ||
var exp: Log2Int(T) = 0; | ||
var pow: T = 1; | ||
while (pow <= math.maxInt(T) / base) { | ||
exp += 1; | ||
pow *= base; | ||
|
||
try testing.expectEqual(exp - 1, log_int(T, base, pow - 1)); | ||
try testing.expectEqual(exp, log_int(T, base, pow)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
test "math.log_int vs math.log2" { | ||
const types = [_]type{ u2, u3, u4, u8, u16 }; | ||
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 "math.log_int vs math.log10" { | ||
const types = [_]type{ u4, u5, u6, u8, u16 }; | ||
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); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.