const std = @import("std"); // rounds up a u12 to the nearest factor of 4 and returns the difference // (padding) pub fn roundUpPadding(comptime T: type, comptime nbits: u8, n: T) T { return roundUp(T, nbits, n) - n; } // rounds up a u12 to the nearest factor of 4. pub fn roundUp(comptime T: type, comptime nbits: u8, n: T) T { const factor = comptime (1 << nbits) - 1; return ((n + factor) & ~@as(T, factor)); } const testing = std.testing; test "padding" { try testing.expectEqual(roundUpPadding(u12, 2, 0), 0); try testing.expectEqual(roundUpPadding(u12, 2, 1), 3); try testing.expectEqual(roundUpPadding(u12, 2, 2), 2); try testing.expectEqual(roundUpPadding(u12, 2, 3), 1); try testing.expectEqual(roundUpPadding(u12, 2, 4), 0); try testing.expectEqual(roundUpPadding(u12, 2, 40), 0); try testing.expectEqual(roundUpPadding(u12, 2, 41), 3); try testing.expectEqual(roundUpPadding(u12, 2, 42), 2); try testing.expectEqual(roundUpPadding(u12, 2, 43), 1); try testing.expectEqual(roundUpPadding(u12, 2, 44), 0); try testing.expectEqual(roundUpPadding(u12, 2, 4091), 1); try testing.expectEqual(roundUpPadding(u12, 2, 4092), 0); }