RTL Design #70 · 2026-12-02 · Leading-zero count · Medium · about 20 min

Left-justify a byte and report the shift

Build normalizer, a one-stage registered block that left-justifies an 8-bit value, the way a floating-point unit does before it rounds.

On a rising edge where in_valid is 1, the block samples din and, one cycle later, presents:

  • out_valid = 1 for that one cycle (it is 0 in every cycle that follows an edge with in_valid = 0),
  • shamt: the number of leading zeros of din, that is how many places it has to move left to put a 1 in bit 7,
  • norm: din shifted left by shamt (zeros come in at the bottom),
  • zero: 1 only if din was 8'h00. An all-zero input has no leading 1 to find, so it reports shamt = 0 and norm = 0.

norm, shamt and zero only have to be right while out_valid is 1. Back-to-back inputs on consecutive edges give back-to-back results. rst_n is a synchronous, active-low reset: after the edge that samples it low, out_valid is 0, and any din sampled on that same edge is discarded.

Example, with in_valid high on one edge:

din out_valid shamt norm zero
8'b0001_0110 1 3 8'b1011_0000 0
8'b0000_0001 1 7 8'b1000_0000 0
8'b0000_0000 1 0 8'b0000_0000 1

Fill in normalizer.sv. Run sends four values.

Write and run your answer in the browser. The hidden tests check it, and it’s free.