Continue to Site

# how to determine the relevent msb bit in a 32 bit stream

Status
Not open for further replies.

#### Ashish Agrawal

##### Member level 3
Hi
I have a 32 bit stream. However only some of the bits are used.
For example in 0000_0000_0000_0000_0000_0000_0000_1010 only last 4 bits are relevent. So relevent msb is 3.

I have to write a synthesizable verilog code to determine the relevent msb.

Such a circuit is called a "priority encoder". If it is a school homework, try to solve it yourself before searching for the solution.

I have a 32 bit stream. However only some of the bits are used.
For example in 0000_0000_0000_0000_0000_0000_0000_1010 only last 4 bits are relevent. So relevent msb is 3.

Hint: log2(x)

Hint: log2(x)

A log2 function is normally used to determine the number of bits needed to represent a number, and it is often not suitable for synthesis.

The OP asks for synthesizable code. "Priority encoder" circuits are normally synthesizable.

A log2 function is normally used to determine the number of bits needed to represent a number, and it is often not suitable for synthesis.

The OP asks for synthesizable code. "Priority encoder" circuits are normally synthesizable.

log2 is synthesizable as well, and not just for computing constants. Maybe you should take another look at the code for log2 and re-evaluate your statement that log2 is 'often not suitable for synthesis'. In fact, look at it to say in what way log2 even differs from a priority encoder.

For a single clock cycle input to output delay, Quartus 14.0, targeting a Cyclone IV GX, registering the inputs and outputs surrounding the call to the log2 function (in order to produce a meaningful Fmax for comparison) produces the following results:
31 bits => 252 LE 32.86 MHz
30 bits => 247 LE 35.12 MHz
29 bits => 231 LE 365.65 MHz
16 bits => 38 LE 225.78 MHz
12 bits => 18 LE 550.36 MHz
8 bits => 11 LE 728.86 MHz

I suspect that 'priority encoder' should produce similar results but will leave that for you to produce.

Kevin Jennings

Last edited:

log2 is synthesizable as well, and not just for computing constants. Maybe you should take another look at the code for log2 and re-evaluate your statement that log2 is 'often not suitable for synthesis'. In fact, look at it to say in what way log2 even differs from a priority encoder.
I now see that the original request was for verilog code. I live in the VHDL world, and the only standardized log2 function is in the non-synthesizable math_real package.

I agree that a log2 function and a "priority encoder" have the same function.

Status
Not open for further replies.