1
Fork 0

add roundUp assertion

This commit is contained in:
Motiejus Jakštys 2022-02-19 22:12:49 +02:00 committed by Motiejus Jakštys
parent 4dbc7b03bc
commit 330db487ac
1 changed files with 2 additions and 0 deletions

View File

@ -1,4 +1,5 @@
const std = @import("std");
const assert = std.debug.assert;
// rounds up a u12 to the nearest factor of 4 and returns the difference
// (padding)
@ -8,6 +9,7 @@ pub fn roundUpPadding(comptime T: type, comptime nbits: u8, n: T) T {
// rounds up a u12 to the nearest factor of 4.
pub fn roundUp(comptime T: type, comptime nbits: u8, n: T) T {
comptime assert(nbits < @bitSizeOf(T));
const factor = comptime (1 << nbits) - 1;
return ((n + factor) & ~@as(T, factor));
}