From 018262d537959701566f2dfece66a462c3bbc976 Mon Sep 17 00:00:00 2001 From: mlugg Date: Mon, 19 Aug 2024 10:26:51 +0100 Subject: [PATCH] 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! --- lib/std/crypto/aes/soft.zig | 2 ++ lib/std/crypto/blake2.zig | 4 ++-- lib/std/enums.zig | 2 +- lib/std/hash/xxhash.zig | 2 +- lib/std/math.zig | 24 ++++++++---------------- lib/std/math/hypot.zig | 1 + lib/std/math/nextafter.zig | 2 +- lib/std/unicode.zig | 1 + 8 files changed, 17 insertions(+), 21 deletions(-) diff --git a/lib/std/crypto/aes/soft.zig b/lib/std/crypto/aes/soft.zig index 9e7af0606d..fd0dfaf001 100644 --- a/lib/std/crypto/aes/soft.zig +++ b/lib/std/crypto/aes/soft.zig @@ -629,6 +629,8 @@ fn generateSbox(invert: bool) [256]u8 { // Generate lookup tables. fn generateTable(invert: bool) [4][256]u32 { + @setEvalBranchQuota(50000); + var table: [4][256]u32 = undefined; for (generateSbox(invert), 0..) |value, index| { diff --git a/lib/std/crypto/blake2.zig b/lib/std/crypto/blake2.zig index 255011de87..1a285080b5 100644 --- a/lib/std/crypto/blake2.zig +++ b/lib/std/crypto/blake2.zig @@ -786,7 +786,7 @@ test "blake2b384 streaming" { test "comptime blake2b384" { comptime { - @setEvalBranchQuota(10000); + @setEvalBranchQuota(20000); var block = [_]u8{0} ** Blake2b384.block_length; var out: [Blake2b384.digest_length]u8 = undefined; @@ -878,7 +878,7 @@ test "blake2b512 keyed" { test "comptime blake2b512" { comptime { - @setEvalBranchQuota(10000); + @setEvalBranchQuota(12000); var block = [_]u8{0} ** Blake2b512.block_length; var out: [Blake2b512.digest_length]u8 = undefined; diff --git a/lib/std/enums.zig b/lib/std/enums.zig index aea194683d..1cc7bde8d2 100644 --- a/lib/std/enums.zig +++ b/lib/std/enums.zig @@ -6,7 +6,7 @@ const testing = std.testing; const EnumField = std.builtin.Type.EnumField; /// Increment this value when adding APIs that add single backwards branches. -const eval_branch_quota_cushion = 5; +const eval_branch_quota_cushion = 10; /// Returns a struct with a field matching each unique named enum element. /// If the enum is extern and has multiple names for the same value, only diff --git a/lib/std/hash/xxhash.zig b/lib/std/hash/xxhash.zig index 88ec3ba372..1d7c8399fc 100644 --- a/lib/std/hash/xxhash.zig +++ b/lib/std/hash/xxhash.zig @@ -890,7 +890,7 @@ test "xxhash32 smhasher" { } }; try Test.do(); - @setEvalBranchQuota(75000); + @setEvalBranchQuota(85000); comptime try Test.do(); } 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. diff --git a/lib/std/math/hypot.zig b/lib/std/math/hypot.zig index cc0dc17ab1..ddc9408aba 100644 --- a/lib/std/math/hypot.zig +++ b/lib/std/math/hypot.zig @@ -114,6 +114,7 @@ test "hypot.precise" { } test "hypot.special" { + @setEvalBranchQuota(2000); inline for (.{ f16, f32, f64, f128 }) |T| { try expect(math.isNan(hypot(nan(T), 0.0))); try expect(math.isNan(hypot(0.0, nan(T)))); diff --git a/lib/std/math/nextafter.zig b/lib/std/math/nextafter.zig index 717cbf4700..b88648229b 100644 --- a/lib/std/math/nextafter.zig +++ b/lib/std/math/nextafter.zig @@ -144,7 +144,7 @@ test "int" { } test "float" { - @setEvalBranchQuota(3000); + @setEvalBranchQuota(4000); // normal -> normal try expect(nextAfter(f16, 0x1.234p0, 2.0) == 0x1.238p0); diff --git a/lib/std/unicode.zig b/lib/std/unicode.zig index a8fa1454a5..4c6ec1294b 100644 --- a/lib/std/unicode.zig +++ b/lib/std/unicode.zig @@ -535,6 +535,7 @@ fn testUtf16CountCodepoints() !void { } test "utf16 count codepoints" { + @setEvalBranchQuota(2000); try testUtf16CountCodepoints(); try comptime testUtf16CountCodepoints(); }