@@ -2027,7 +2027,13 @@ test "sliceAsBytes and bytesAsSlice back" {
2027
2027
/// Round an address up to the nearest aligned address
2028
2028
/// The alignment must be a power of 2 and greater than 0.
2029
2029
pub fn alignForward (addr : usize , alignment : usize ) usize {
2030
- return alignBackward (addr + (alignment - 1 ), alignment );
2030
+ return alignForwardGeneric (usize , addr , alignment );
2031
+ }
2032
+
2033
+ /// Round an address up to the nearest aligned address
2034
+ /// The alignment must be a power of 2 and greater than 0.
2035
+ pub fn alignForwardGeneric (comptime T : type , addr : T , alignment : T ) T {
2036
+ return alignBackwardGeneric (T , addr + (alignment - 1 ), alignment );
2031
2037
}
2032
2038
2033
2039
test "alignForward" {
@@ -2048,8 +2054,14 @@ test "alignForward" {
2048
2054
/// Round an address up to the previous aligned address
2049
2055
/// The alignment must be a power of 2 and greater than 0.
2050
2056
pub fn alignBackward (addr : usize , alignment : usize ) usize {
2051
- assert (@popCount (usize , alignment ) == 1 );
2052
- // 000010000 // example addr
2057
+ return alignBackwardGeneric (usize , addr , alignment );
2058
+ }
2059
+
2060
+ /// Round an address up to the previous aligned address
2061
+ /// The alignment must be a power of 2 and greater than 0.
2062
+ pub fn alignBackwardGeneric (comptime T : type , addr : T , alignment : T ) T {
2063
+ assert (@popCount (T , alignment ) == 1 );
2064
+ // 000010000 // example alignment
2053
2065
// 000001111 // subtract 1
2054
2066
// 111110000 // binary not
2055
2067
return addr & ~ (alignment - 1 );
0 commit comments