VLSI DV Interview Puzzles · All levels

Static Function Counters Per Specialization

Predict call counters after mixed static-method invocations across different POLY values.

Puzzle

Difficulty: Medium · Puzzle 5 of 6 · Topic: Parameterized Class Puzzles

Predict call counters after mixed static-method invocations across different POLY values.

Code

systemverilog
class crc #(int POLY = 1);
  static int calls = 0;
  static function int calc(int x);
    calls++;
    return x ^ POLY;
  endfunction
endclass

initial begin
  void'(crc#(1)::calc(7));
  void'(crc#(1)::calc(8));
  void'(crc#(3)::calc(9));
  $display("c1=%0d c3=%0d", crc#(1)::calls, crc#(3)::calls);
end

Hint

Static storage belongs to specialization, even for static methods.

Step-by-step solution

diagram
1) Two calls are made on `crc#(1)` => `crc#(1)::calls` becomes 2.
2) One call is made on `crc#(3)` => `crc#(3)::calls` becomes 1.
3) No cross-specialization sharing occurs.

Answer

Answer: Output is `c1=2 c3=1`.

Why candidates get it wrong

Static methods are often mistaken as globally shared across all parameter values.

Interviewer follow-up

How would you accumulate one global call count while still keeping per-POLY counts?

Related topics