2019-03-02 21:46:04 +00:00
|
|
|
const std = @import("../std.zig");
|
2022-11-29 20:08:57 +00:00
|
|
|
const builtin = @import("builtin");
|
2017-12-24 03:08:53 +00:00
|
|
|
const math = std.math;
|
2019-02-08 23:18:47 +00:00
|
|
|
const expect = std.testing.expect;
|
2017-06-16 09:26:10 +01:00
|
|
|
|
2019-05-01 07:15:57 +01:00
|
|
|
/// Returns the base-2 logarithm of x.
|
|
|
|
///
|
|
|
|
/// Special Cases:
|
|
|
|
/// - log2(+inf) = +inf
|
|
|
|
/// - log2(0) = -inf
|
|
|
|
/// - log2(x) = nan if x < 0
|
|
|
|
/// - log2(nan) = nan
|
2020-07-11 12:09:04 +01:00
|
|
|
pub fn log2(x: anytype) @TypeOf(x) {
|
2019-12-09 20:56:19 +00:00
|
|
|
const T = @TypeOf(x);
|
2020-02-24 21:03:30 +00:00
|
|
|
switch (@typeInfo(T)) {
|
|
|
|
.ComptimeFloat => {
|
2022-04-26 18:13:55 +01:00
|
|
|
return @as(comptime_float, @log2(x));
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 06:32:15 +01:00
|
|
|
},
|
2022-04-26 18:13:55 +01:00
|
|
|
.Float => return @log2(x),
|
2020-02-24 21:03:30 +00:00
|
|
|
.ComptimeInt => comptime {
|
2017-08-19 07:29:18 +01:00
|
|
|
var x_shifted = x;
|
2022-06-07 18:58:49 +01:00
|
|
|
// First, calculate floorPowerOfTwo(x)
|
|
|
|
var shift_amt = 1;
|
|
|
|
while (x_shifted >> (shift_amt << 1) != 0) shift_amt <<= 1;
|
|
|
|
|
|
|
|
// Answer is in the range [shift_amt, 2 * shift_amt - 1]
|
|
|
|
// We can find it in O(log(N)) using binary search.
|
|
|
|
var result = 0;
|
|
|
|
while (shift_amt != 0) : (shift_amt >>= 1) {
|
|
|
|
if (x_shifted >> shift_amt != 0) {
|
|
|
|
x_shifted >>= shift_amt;
|
|
|
|
result += shift_amt;
|
|
|
|
}
|
|
|
|
}
|
2017-08-19 07:29:18 +01:00
|
|
|
return result;
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 06:32:15 +01:00
|
|
|
},
|
2021-04-02 13:52:47 +01:00
|
|
|
.Int => |IntType| switch (IntType.signedness) {
|
2021-11-24 21:47:33 +00:00
|
|
|
.signed => @compileError("log2 not implemented for signed integers"),
|
2021-04-02 13:52:47 +01:00
|
|
|
.unsigned => return math.log2_int(T, x),
|
bit shifting safety
* add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7
* shift operations shift amount parameter type is
integer with log2 bit width of other param
- This enforces not violating undefined behavior on
shift amount >= bit width with the type system
* clean up math.log, math.ln, math.log2, math.log10
closes #403
2017-08-19 06:32:15 +01:00
|
|
|
},
|
2017-06-16 09:26:10 +01:00
|
|
|
else => @compileError("log2 not implemented for " ++ @typeName(T)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 23:14:43 +00:00
|
|
|
test log2 {
|
2022-11-29 20:08:57 +00:00
|
|
|
// https://github.com/ziglang/zig/issues/13703
|
|
|
|
if (builtin.cpu.arch == .aarch64 and builtin.os.tag == .windows) return error.SkipZigTest;
|
|
|
|
|
2022-04-26 18:13:55 +01:00
|
|
|
try expect(log2(@as(f32, 0.2)) == @log2(0.2));
|
|
|
|
try expect(log2(@as(f64, 0.2)) == @log2(0.2));
|
2022-06-07 18:58:49 +01:00
|
|
|
comptime {
|
|
|
|
try expect(log2(1) == 0);
|
|
|
|
try expect(log2(15) == 3);
|
|
|
|
try expect(log2(16) == 4);
|
|
|
|
try expect(log2(1 << 4073) == 4073);
|
|
|
|
}
|
2017-06-20 12:01:04 +01:00
|
|
|
}
|