| ... | ... | @@ -3291,7 +3291,7 @@ pub fn nativeToBig(comptime T: type, x: T) T { |
| 3291 | 3291 | /// - The delta required to align the pointer is not a multiple of the pointee's |
| 3292 | 3292 | /// type. |
| 3293 | 3293 | pub fn alignPointerOffset(ptr: anytype, align_to: usize) ?usize { |
| 3294 | | assert(align_to != 0 and @popCount(align_to) == 1); |
| 3294 | assert(isValidAlign(align_to)); |
| 3295 | 3295 | |
| 3296 | 3296 | const T = @TypeOf(ptr); |
| 3297 | 3297 | const info = @typeInfo(T); |
| ... | ... | @@ -3751,6 +3751,7 @@ pub fn alignForwardLog2(addr: usize, log2_alignment: u8) usize { |
| 3751 | 3751 | /// The alignment must be a power of 2 and greater than 0. |
| 3752 | 3752 | /// Asserts that rounding up the address does not cause integer overflow. |
| 3753 | 3753 | pub fn alignForwardGeneric(comptime T: type, addr: T, alignment: T) T { |
| 3754 | assert(isValidAlignGeneric(T, alignment)); |
| 3754 | 3755 | return alignBackwardGeneric(T, addr + (alignment - 1), alignment); |
| 3755 | 3756 | } |
| 3756 | 3757 | |
| ... | ... | @@ -3846,7 +3847,7 @@ test "alignForward" { |
| 3846 | 3847 | /// Round an address down to the previous (or current) aligned address. |
| 3847 | 3848 | /// Unlike `alignBackward`, `alignment` can be any positive number, not just a power of 2. |
| 3848 | 3849 | pub fn alignBackwardAnyAlign(i: usize, alignment: usize) usize { |
| 3849 | | if (@popCount(alignment) == 1) |
| 3850 | if (isValidAlign(alignment)) |
| 3850 | 3851 | return alignBackward(i, alignment); |
| 3851 | 3852 | assert(alignment != 0); |
| 3852 | 3853 | return i - @mod(i, alignment); |
| ... | ... | @@ -3861,7 +3862,7 @@ pub fn alignBackward(addr: usize, alignment: usize) usize { |
| 3861 | 3862 | /// Round an address down to the previous (or current) aligned address. |
| 3862 | 3863 | /// The alignment must be a power of 2 and greater than 0. |
| 3863 | 3864 | pub fn alignBackwardGeneric(comptime T: type, addr: T, alignment: T) T { |
| 3864 | | assert(@popCount(alignment) == 1); |
| 3865 | assert(isValidAlignGeneric(T, alignment)); |
| 3865 | 3866 | // 000010000 // example alignment |
| 3866 | 3867 | // 000001111 // subtract 1 |
| 3867 | 3868 | // 111110000 // binary not |
| ... | ... | @@ -3871,11 +3872,17 @@ pub fn alignBackwardGeneric(comptime T: type, addr: T, alignment: T) T { |
| 3871 | 3872 | /// Returns whether `alignment` is a valid alignment, meaning it is |
| 3872 | 3873 | /// a positive power of 2. |
| 3873 | 3874 | pub fn isValidAlign(alignment: usize) bool { |
| 3874 | | return @popCount(alignment) == 1; |
| 3875 | return isValidAlignGeneric(usize, alignment); |
| 3876 | } |
| 3877 | |
| 3878 | /// Returns whether `alignment` is a valid alignment, meaning it is |
| 3879 | /// a positive power of 2. |
| 3880 | pub fn isValidAlignGeneric(comptime T: type, alignment: T) bool { |
| 3881 | return alignment > 0 and std.math.isPowerOfTwo(alignment); |
| 3875 | 3882 | } |
| 3876 | 3883 | |
| 3877 | 3884 | pub fn isAlignedAnyAlign(i: usize, alignment: usize) bool { |
| 3878 | | if (@popCount(alignment) == 1) |
| 3885 | if (isValidAlign(alignment)) |
| 3879 | 3886 | return isAligned(i, alignment); |
| 3880 | 3887 | assert(alignment != 0); |
| 3881 | 3888 | return 0 == @mod(i, alignment); |