From 05e92a51aaaacd0bf326e251763ab97da3d0cd18 Mon Sep 17 00:00:00 2001 From: Ryan Liptak Date: Sun, 9 Jun 2019 22:20:17 -0700 Subject: [PATCH] Use std.math.isPowerOfTwo across std lib --- std/hash_map.zig | 3 +-- std/math/big/int.zig | 2 +- std/segmented_list.zig | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/std/hash_map.zig b/std/hash_map.zig index ab08d44cc0d71401145d119796f31a553e773a0e..73368179541a39d089c5e70b3faa616da83b2460 100644 --- a/std/hash_map.zig +++ b/std/hash_map.zig @@ -157,8 +157,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3 fn ensureCapacityExact(self: *Self, new_capacity: usize) !void { // capacity must always be a power of two to allow for modulo // optimization in the constrainIndex fn - const is_power_of_two = new_capacity & (new_capacity - 1) == 0; - assert(is_power_of_two); + assert(math.isPowerOfTwo(new_capacity)); if (new_capacity <= self.entries.len) { return; diff --git a/std/math/big/int.zig b/std/math/big/int.zig index adc22ffa340c597fe77a411502c5373125c28ffd..46b1bed9a3aae9d2d26d9f236ec948e803f1aa2c 100644 --- a/std/math/big/int.zig +++ b/std/math/big/int.zig @@ -447,7 +447,7 @@ pub const Int = struct { } // Power of two: can do a single pass and use masks to extract digits. - if (base & (base - 1) == 0) { + if (math.isPowerOfTwo(base)) { const base_shift = math.log2_int(Limb, base); for (self.limbs[0..self.len()]) |limb| { diff --git a/std/segmented_list.zig b/std/segmented_list.zig index b3a7065ad83bbe1e820da6871644c3edb84e744d..dc3358cd8594fbe588c1a9620bf35633e8b8bb16 100644 --- a/std/segmented_list.zig +++ b/std/segmented_list.zig @@ -80,9 +80,9 @@ pub fn SegmentedList(comptime T: type, comptime prealloc_item_count: usize) type const prealloc_exp = blk: { // we don't use the prealloc_exp constant when prealloc_item_count is 0. assert(prealloc_item_count != 0); + assert(std.math.isPowerOfTwo(prealloc_item_count)); const value = std.math.log2_int(usize, prealloc_item_count); - assert((1 << value) == prealloc_item_count); // prealloc_item_count must be a power of 2 break :blk @typeOf(1)(value); }; const ShelfIndex = std.math.Log2Int(usize); -- 2.54.0