wip between-user padding

This commit is contained in:
2022-02-19 18:18:14 +02:00
committed by Motiejus Jakštys
parent 4e45c6e5a9
commit 2fc925923f
4 changed files with 37 additions and 28 deletions

29
src/padding.zig Normal file
View File

@@ -0,0 +1,29 @@
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);
}