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.

VHDL Integer Division Problem

Status
Not open for further replies.

Kamran Saleh

Newbie level 2
Joined
Oct 9, 2011
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,295

Dear Friends,

I have used the following VHDL expression to divide an integer by a constant power of 2 and simulated the code with Modelsim 6.0:

signal a,b: integer range -1024 to 1023;

a <= b / 128;

it works properly with positive numbers. surprisingly, when b is a negative number, some times the result is not correct. for example if b=-256 then a = -2 (correct). but when b = -300 then a = -2 instead of -3. In other words the simulator rounds the result toward zero instead of calculating the FLOOR of the result.

if i define a and b as std_logic_vector and use a right shift instead of the '/' operator, the result is -3 (OK).
My question: is there any way for dividing an integer by a constant power of 2 numbers?
 

In other words the simulator rounds the result toward zero instead of calculating the FLOOR of the result.
Do you know a language specification that makes you expect floor as result of integer division?

I see the following language specfication in IEEE Std 1076, 7.2.6
Integer division satisfies the following identity:
(–A)/B = –(A/B) = A/(–B)

P.S.: You get in fact a different result on right shift of signed numbers in 2s complement representation. While VHDL has an explicite specification for the behaviour of negative integers in division, you'll find that the result is usually specified "implementation dependent" with programming languages like C. This may give the appearance, that the usually observed behaviour caused by 2s complement representation is "natural". But it isn't. In other words, the "integer division problem" reveals as a problem of inappropriate expectations.
 
Last edited:

You have to remember, integers are not bit vectors in VHDL. so their behaviour is defined by the language, not the bitwise arithmatic.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top