RTL Studio Open IDE

Arithmetic & Math

32-Bit Count Leading Zeros

Combinational module that counts the number of leading zeros in a 32-bit input vector using a priority encoder approach.

About this block

Simulate it in the Verilog IDE

Mediumverilogsimsyn

32-Bit Count Leading Zeros is a Verilog reference block in the Arithmetic & Math catalog. Adders, multipliers, dividers, ALUs, and arithmetic datapaths. Marked Medium: more state or a wider datapath, still one workspace in the IDE.

This IP core counts the number of leading zeros in a 32-bit input vector. It implements a priority encoder approach for efficient combinational logic.

This IP provides a robust and simple solution for leading zero counting. It is suitable for use in normalization, floating-point arithmetic, and priority arbitration. The design is fully combinational and requires no clock or reset signals.

  1. Choose Open in IDE. RTL Studio loads the packaged workspace for 32-Bit Count Leading Zeros.
  2. Read the Verilog module in the editor. The catalog line for this block: Combinational module that counts the number of leading zeros in a 32-bit input vector using a priority encoder approach.
  3. Run the packaged testbench with the in-browser Verilator simulator, then open the VCD in the waveform viewer.
  4. Run Yosys in the same IDE for a generic netlist. Sky130 liberty mapping is a separate Pro library option.

RTL Studio is the Verilog simulator and IDE around this block: Slang lint, Verilator, VCD waveforms, and Yosys, in the browser. Simulation guide · Yosys guide.

Features

  • Combinational Logic: No clock or reset required.
  • Priority Encoder: Efficient cascading if-else structure.
  • Edge Case Handling: Explicitly handles all-zero input (output 32).
  • Verified: All critical edge cases and random patterns validated via Icarus Verilog.

Ports

Port NameDirectionWidthDescription
data_inInput3232-bit input data vector
clz_outOutput5Count of leading zeros (0-32)

Architecture

The module uses a cascading if-else structure to determine the first set bit. This approach ensures deterministic combinational delay. The logic checks bits from MSB (31) down to LSB (0). A special case handles the all-zero input condition.

Verification

The testbench validates all critical edge cases and random patterns. It checks for zero input, single bit set, and all bits set scenarios. Simulation uses Icarus Verilog for functional verification.

### Test Cases

Test CaseInput HexExpected OutputResult
TC10x0000000032PASS
TC20x0000000131PASS
TC30x800000000PASS
TC40xFFFFFFFF0PASS
TC50x400000001PASS
TC60x0000000230PASS
TC70x0000000429PASS

### Expected Simulation Output

After Run Simulation, the Log panel should show:

  • TC1 PASS - Input 0 yields 32
  • TC2 PASS - Input 1 yields 31
  • TC3 PASS - Input MSB set yields 0
  • TC4 PASS - Input All Ones yields 0
  • TC5 PASS - Input Bit 30 set yields 1
  • TC6 PASS - Input Bit 1 set yields 30
  • TC7 PASS - Input Bit 2 set yields 29
  • Simulation ends with INFO: PASS.

More in Arithmetic & Math

All Arithmetic & Math blocks