Hardware interview practice
Count coalesced memory segments
A validation model must count the 32-byte memory segments touched by one warp's active four-byte loads. Write the C++ function that returns the number of distinct aligned segments.
Starting point
Question code
int segment_count(const uint32_t addr[32], uint32_t active_mask);Reviewed example
Work through one case
Input
Activate aligned addresses 0x100, 0x104, and 0x11C in three warp lanes.Expected output
segment_count returns 1.Masking each address to a 32-byte base produces the same segment address 0x100.
What to cover
Requirements
- Each active lane performs one four-byte load and every active address is four-byte aligned.
- A segment is identified by addr[lane] & ~uint32_t(31); count each distinct segment once.
- Return zero for active_mask=0; addr may be NULL only in that case, otherwise return -1 for NULL or misalignment.
- Use fixed storage only, with no allocation and no reads from inactive lanes.
