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

View File

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