zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit ae72a982242fbd46f389f933c18b036e919093bc (tree)
parent ff477361b005f059591e92e079d62e68153365e1
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Thu, 27 Jun 2019 20:37:28 -0400

Merge pull request #2775 from SamTebbs33/alignment-docs

Add doc comments to alignment functions
Diffstat:
Mstd/mem.zig | 6++++++
1 file changed, 6 insertions(+), 0 deletions(-)

diff --git a/std/mem.zig b/std/mem.zig @@ -1481,6 +1481,7 @@ test "subArrayPtr" { } /// Round an address up to the nearest aligned address +/// The alignment must be a power of 2 and greater than 0. pub fn alignForward(addr: usize, alignment: usize) usize { return alignBackward(addr + (alignment - 1), alignment); } @@ -1500,13 +1501,18 @@ test "alignForward" { testing.expect(alignForward(17, 8) == 24); } +/// Round an address up to the previous aligned address +/// The alignment must be a power of 2 and greater than 0. pub fn alignBackward(addr: usize, alignment: usize) usize { + assert(@popCount(usize, alignment) == 1); // 000010000 // example addr // 000001111 // subtract 1 // 111110000 // binary not return addr & ~(alignment - 1); } +/// Given an address and an alignment, return true if the address is a multiple of the alignment +/// The alignment must be a power of 2 and greater than 0. pub fn isAligned(addr: usize, alignment: usize) bool { return alignBackward(addr, alignment) == addr; }