Codiode

32-bit Leading Zero Counter Tree

HardVerilog / SystemVerilogBuild

A flat 32-bit priority scan is a long chain of logic. Splitting the word in halves, recursively, cuts the depth to log2(32) levels.

The module computes the number of leading zeros in a 32-bit input vector. The count starts from the most significant bit (bit 31) and proceeds downwards. To achieve logarithmic delay, the logic must be structured as a tree. The 32-bit input is conceptually divided into eight 4-bit blocks. Each 4-bit block computes its own leading zero count and a zero-flag. These intermediate results are then combined in successive stages to produce the final 6-bit count.

The circuit is purely combinational. There is no clock or reset. The output must reflect the input continuously.

Signal Direction Width Description
in input 32 Data vector to be evaluated
zeros output 6 Number of leading zeros; purely combinational output

Constraints

  • The circuit must be purely combinational
  • Do not use a single 32-bit case statement, for loop, or if-else chain
  • The logic must be constructed hierarchically using 4-bit leading zero counting blocks
  • The output must evaluate to exactly 32 when the input is entirely zero
  • Bit 31 is the most significant bit and represents the first bit checked for a zero

Topics

OptimizationCombinational LogicDatapath

Solve this problem

Write the module in Verilog, SystemVerilog or VHDL. Your submission is compiled and simulated against the problem's testbench. You get the waveform back, not a stored answer.

This problem is part of Codiode Pro. The statement above is free to read.

The circuit builder and code editor need a desktop screen. On a phone, read the problem here and open it on a laptop to solve.

Part of Datapath Design

Problem 12 of 27 in the Datapath Design track.

Related problems

Browse all problems · Verilog practice problems · Learning tracks

Solve this on a laptopSend yourself the link and continue there.