aboutsummaryrefslogtreecommitdiff
path: root/lib/std/math.zig
diff options
context:
space:
mode:
authormlugg <mlugg@mlugg.co.uk>2024-08-19 10:26:51 +0100
committermlugg <mlugg@mlugg.co.uk>2024-08-21 01:30:46 +0100
commit018262d537959701566f2dfece66a462c3bbc976 (patch)
treea4d09b23dfd649ffa61313891560b5b33e17810b /lib/std/math.zig
parentceb76b2ba705f362dbd5437ec5804b670298b420 (diff)
downloadzig-018262d537959701566f2dfece66a462c3bbc976.tar.gz
zig-018262d537959701566f2dfece66a462c3bbc976.zip
std: update eval branch quotas after bdbc485
Also, update `std.math.Log2Int[Ceil]` to more efficient implementations that don't use up so much damn quota!
Diffstat (limited to 'lib/std/math.zig')
-rw-r--r--lib/std/math.zig24
1 files changed, 8 insertions, 16 deletions
diff --git a/lib/std/math.zig b/lib/std/math.zig
index 0c00818a1e..f18739095c 100644
--- a/lib/std/math.zig
+++ b/lib/std/math.zig
@@ -749,31 +749,23 @@ test rotl {
try testing.expect(rotl(@Vector(1, u32), @Vector(1, u32){1 << 31}, @as(isize, -1))[0] == @as(u32, 1) << 30);
}
-/// Returns an unsigned int type that can hold the number of bits in T
-/// - 1. Suitable for 0-based bit indices of T.
+/// Returns an unsigned int type that can hold the number of bits in T - 1.
+/// Suitable for 0-based bit indices of T.
pub fn Log2Int(comptime T: type) type {
// comptime ceil log2
if (T == comptime_int) return comptime_int;
- comptime var count = 0;
- comptime var s = @typeInfo(T).Int.bits - 1;
- inline while (s != 0) : (s >>= 1) {
- count += 1;
- }
-
- return std.meta.Int(.unsigned, count);
+ const bits: u16 = @typeInfo(T).Int.bits;
+ const log2_bits = 16 - @clz(bits - 1);
+ return std.meta.Int(.unsigned, log2_bits);
}
/// Returns an unsigned int type that can hold the number of bits in T.
pub fn Log2IntCeil(comptime T: type) type {
// comptime ceil log2
if (T == comptime_int) return comptime_int;
- comptime var count = 0;
- comptime var s = @typeInfo(T).Int.bits;
- inline while (s != 0) : (s >>= 1) {
- count += 1;
- }
-
- return std.meta.Int(.unsigned, count);
+ const bits: u16 = @typeInfo(T).Int.bits;
+ const log2_bits = 16 - @clz(bits);
+ return std.meta.Int(.unsigned, log2_bits);
}
/// Returns the smallest integer type that can hold both from and to.