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