Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

verilog arithmetic

Status
Not open for further replies.

KingMoshe

Member level 2
Joined
Aug 10, 2021
Messages
48
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
362
Hi All,
What will happen if I will do the following operation?

reg [15:0] NUM1;
reg [15:0] NUM2;
NUM1 <= NUM2*16'd1064/16'd100;

The result of "NUM2*16'd1064/16'd100" not always integer.
Please see that if NUM1 = 100 , so NUM1 * 1064 = 106400 and 16 bit register is too small, do the actions (*1064/100) occur together or in two stages?

Thanks,
Moshe.
 

A note on synthesis here -


Not an expert here but <= are non blocking statements, evaluated at end of time step.



Regards, Dana.
 

Expression are evaluated left to right, bit width is generally determined by the operands (self-determined) and in some cases by the left hand side (context-determined), e.g. if it's assigned to a wider variable. Because right hand side is also 16 bit wide, the multiplication is trunacted to 16 bit.

The original expression is synthesized this way
1641137313042.png


To avoid truncation of the multiplication, you can extend the width of the first constant

Code:
NUM1 <= NUM2*27'd1064/16'd100;

Synthesis result changes respectively

1641137010447.png


Notice that the division operation is very ineffective in hardware synthesis. You probably want to replace it with fixed point multiplication.

Code:
NUM1 <= NUM2*26'd681>>6;
 

Expression get evaluated by the rules described in section 11 of the IEEE 1800-2017 LRM. (There is no difference between Verilog and SystemVerilog here) Operands get extended to the maximum width of all other operands in the context of the expression before any operations get evaluated. Since all your operands are 16 bits, nothing gets extended.

Multiplication and division have the same precedence and left associativity, That means NUM2*16'd1064 gets evaluated first in a 16-bit context, so the upper 10 bits of that result could get truncated. Then that result gets divided by 16'd100, remainder truncated.
 

Upper = most significant. See also RTL schematic in post #3.
 

I'm substituting NUM2*1064/100 by NUM2*(64*1064/100)/64 to save the divider.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top