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