| ... | ... | @@ -706,23 +706,23 @@ fn testFloorPowerOfTwo() void { |
| 706 | 706 | } |
| 707 | 707 | |
| 708 | 708 | /// Returns the next power of two (if the value is not already a power of two). |
| 709 | /// Only unsigned integers can be used. Zero is not an allowed input. |
| 709 | 710 | /// Result is a type with 1 more bit than the input type. |
| 710 | 711 | pub fn ceilPowerOfTwoPromote(comptime T: type, value: T) @IntType(T.is_signed, T.bit_count + 1) { |
| 711 | | if (T.is_signed) { |
| 712 | | @compileError("signed integers not supported"); |
| 713 | | } |
| 712 | comptime assert(@typeId(T) == builtin.TypeId.Int); |
| 713 | comptime assert(!T.is_signed); |
| 714 | assert(value != 0); |
| 714 | 715 | comptime const promotedType = @IntType(T.is_signed, T.bit_count + 1); |
| 715 | 716 | comptime const shiftType = std.math.Log2Int(promotedType); |
| 716 | | if (value == 0) return promotedType(0); |
| 717 | 717 | return promotedType(1) << @intCast(shiftType, T.bit_count - @clz(T, value - 1)); |
| 718 | 718 | } |
| 719 | 719 | |
| 720 | 720 | /// Returns the next power of two (if the value is not already a power of two). |
| 721 | /// Only unsigned integers can be used. Zero is not an allowed input. |
| 721 | 722 | /// If the value doesn't fit, returns an error. |
| 722 | 723 | pub fn ceilPowerOfTwo(comptime T: type, value: T) (error{Overflow}!T) { |
| 723 | | if (T.is_signed) { |
| 724 | | @compileError("signed integers not supported"); |
| 725 | | } |
| 724 | comptime assert(@typeId(T) == builtin.TypeId.Int); |
| 725 | comptime assert(!T.is_signed); |
| 726 | 726 | comptime const promotedType = @IntType(T.is_signed, T.bit_count + 1); |
| 727 | 727 | comptime const overflowBit = promotedType(1) << T.bit_count; |
| 728 | 728 | var x = ceilPowerOfTwoPromote(T, value); |
| ... | ... | @@ -738,7 +738,6 @@ test "math.ceilPowerOfTwoPromote" { |
| 738 | 738 | } |
| 739 | 739 | |
| 740 | 740 | fn testCeilPowerOfTwoPromote() void { |
| 741 | | testing.expectEqual(u33(0), ceilPowerOfTwoPromote(u32, 0)); |
| 742 | 741 | testing.expectEqual(u33(1), ceilPowerOfTwoPromote(u32, 1)); |
| 743 | 742 | testing.expectEqual(u33(2), ceilPowerOfTwoPromote(u32, 2)); |
| 744 | 743 | testing.expectEqual(u33(64), ceilPowerOfTwoPromote(u32, 63)); |
| ... | ... | @@ -756,7 +755,6 @@ test "math.ceilPowerOfTwo" { |
| 756 | 755 | } |
| 757 | 756 | |
| 758 | 757 | fn testCeilPowerOfTwo() !void { |
| 759 | | testing.expectEqual(u32(0), try ceilPowerOfTwo(u32, 0)); |
| 760 | 758 | testing.expectEqual(u32(1), try ceilPowerOfTwo(u32, 1)); |
| 761 | 759 | testing.expectEqual(u32(2), try ceilPowerOfTwo(u32, 2)); |
| 762 | 760 | testing.expectEqual(u32(64), try ceilPowerOfTwo(u32, 63)); |