Skip to content
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 5 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/std/math.zig
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ pub const log = @import("math/log.zig").log;
pub const log2 = @import("math/log2.zig").log2;
pub const log10 = @import("math/log10.zig").log10;
pub const log10_int = @import("math/log10.zig").log10_int;
pub const log_int = @import("math/log_int.zig").log_int;
pub const log1p = @import("math/log1p.zig").log1p;
pub const asinh = @import("math/asinh.zig").asinh;
pub const acosh = @import("math/acosh.zig").acosh;
Expand Down
7 changes: 5 additions & 2 deletions lib/std/math/log.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ pub fn log(comptime T: type, base: T, x: T) T {
.ComptimeFloat => {
return @as(comptime_float, @log(@as(f64, x)) / @log(float_base));
},

Copy link
Contributor

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.

// TODO: implement integer log without using float math.
// The present implementation is incorrect, for example
// `log(comptime_int, 9, 59049)` should return `5` and not `4`.
.ComptimeInt => {
return @as(comptime_int, @floor(@log(@as(f64, x)) / @log(float_base)));
},

// TODO implement integer log without using float math
.Int => |IntType| switch (IntType.signedness) {
.signed => @compileError("log not implemented for signed integers"),
.unsigned => return @as(T, @intFromFloat(@floor(@log(@as(f64, @floatFromInt(x))) / @log(float_base)))),
.unsigned => return @as(T, math.log_int(T, base, x)),
},

.Float => {
Expand Down
79 changes: 79 additions & 0 deletions lib/std/math/log_int.zig
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`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make it x > 0 then, if log_int is intended to only support unsigned integers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 log_int(u8, 3, 82) panics? In this case, it does not: it returns @as(u3, 4).


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);
}
}
}