Align a Device Buffer Address to a Power-of-Two Boundary
Last updated: November 29, 2026
Quick Overview
Write a function that rounds a byte address up to the next multiple of a power-of-two alignment and returns both the aligned address and the padding bytes added. Reject alignments that are not positive powers of two.
AMD
November 29, 20260
0
0 solved
Write a function that rounds a byte address up to the next multiple of a power-of-two alignment and returns both the aligned address and the padding bytes added. Reject alignments that are not positive powers of two.
A fundamentals check in the style of AMD ROCm runtime and driver interviews, where buffers handed to DMA engines, page tables and HBM allocators must sit on 256-byte, 4 KiB or 2 MiB boundaries. The interviewer wants the bitmask trick, the reasoning behind it, and validation of the alignment argument.
What the Interviewer Expects
- Validate alignment with the x & (x - 1) == 0 test
- Use (addr + a - 1) & ~(a - 1) and explain why it works
- Return padding as aligned minus original
- Note that an already aligned address stays unchanged
- Mention overflow at the top of a fixed-width address space
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you align down instead of up?
- In C with a 64-bit uintptr_t, when can addr + a - 1 overflow and how do you guard it?
- How does a bump allocator use this to carve sub-allocations from one large HBM block?
- Why do GPUs care about 2 MiB alignment for large allocations?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
Input: non-negative integer addr and alignment. Output: (aligned, padding) where aligned is the smallest multiple of alignment that is at least addr, and padding = aligned - addr. Raise ...
High-Level Design
For a power of two a, the low log2(a) bits of a multiple of a are zero, so ~(a - 1) is a mask that clears them (align down). Adding a - 1 first pushes any address with nonzero low bits past ...