Hardware interview practice
Compute wrapped performance-counter deltas
Validation takes two snapshots of four free-running 32-bit hardware counters. Write a C++ function that returns modulo-2^32 deltas and the busiest counter.
Starting point
Question code
bool counter_delta(const uint32_t before[4],
const uint32_t after[4], uint32_t delta[4],
unsigned *max_i);Reviewed example
Work through one case
Input
before={100,0xFFFFFFFE,8,2} and after={125,1,17,11}.Expected output
delta={25,3,9,9} and max_i=0.Unsigned subtraction handles the wrapped second counter, and the largest delta is 25 at the lowest matching index 0.
What to cover
Requirements
- Require every pointer; return false without writing outputs if any is null.
- For each index compute delta[i]=after[i]-before[i] using uint32_t modulo arithmetic.
- Set max_i to the index of the largest delta; on a tie choose the lower index.
- Compute all temporary results before writing outputs so aliased before/after and delta arrays still work.
