Function ceilPowerOfTwoPromote [src]
Returns the next power of two (if the value is not already a power of two).
Only unsigned integers can be used. Zero is not an allowed input.
Result is a type with 1 more bit than the input type.
Prototype
pub fn ceilPowerOfTwoPromote(comptime T: type, value: T) std.meta.Int(@typeInfo(T).int.signedness, @typeInfo(T).int.bits + 1)
Parameters
T: type
value: T
Example
test ceilPowerOfTwoPromote {
try testCeilPowerOfTwoPromote();
try comptime testCeilPowerOfTwoPromote();
}
Source
pub fn ceilPowerOfTwoPromote(comptime T: type, value: T) std.meta.Int(@typeInfo(T).int.signedness, @typeInfo(T).int.bits + 1) {
comptime assert(@typeInfo(T) == .int);
comptime assert(@typeInfo(T).int.signedness == .unsigned);
assert(value != 0);
const PromotedType = std.meta.Int(@typeInfo(T).int.signedness, @typeInfo(T).int.bits + 1);
const ShiftType = std.math.Log2Int(PromotedType);
return @as(PromotedType, 1) << @as(ShiftType, @intCast(@typeInfo(T).int.bits - @clz(value - 1)));
}