Intel® Quartus® Prime Software
Intel® Quartus® Prime Design Software, Design Entry, Synthesis, Simulation, Verification, Timing Analysis, System Design (Platform Designer, formerly Qsys)
16556 Discussions

Dividing a negative number

Altera_Forum
Honored Contributor II
1,333 Views

Hi  

 

How can I divide a negative number by 32 ? 

 

without using a dsp ip core. 

 

thanks
0 Kudos
5 Replies
Altera_Forum
Honored Contributor II
576 Views

By dividing the number by 32... 

 

 

Division by a power of two is simply removing that many LSBs from the number, it takes no combinational logic what-so-ever. 

 

32 is 2^5, so you need only remove the lower 5 bits of the number and the division is done. 

 

If you want the output width to stay the same, then for a signed number, simply replicate the MSB to fill the gap. 

 

{{(5){a}},a}  

 

Where "MSB" is the width-1 of the signal you are dividing. 

 

 

Alternatively, do an arithmetic shift: 

 

$signed(a)>>>5  

 

Will do the same thing. 

 

The examples I gave are in Verilog. I realise this is the VHDL forum, but I am not familiar with the language. There will be an equivalent in VHDL.
0 Kudos
Altera_Forum
Honored Contributor II
576 Views

the easiest way, would just be a divide. divide by 2^n infers a bit shift. 

 

signal ip, s : signed(31 downto 0); s <= ip / 32;
0 Kudos
Altera_Forum
Honored Contributor II
576 Views

this is what I did in VHDL: what do you think? 

 

1.shift right 5 bits.  

2.fill this 5MSBs 

 

 

slope(10 downto 0) <= delta_y (15 downto 5) ; 

slope(15 downto 11) <= delta_y(15) & delta_y(15) & delta_y(15) & delta_y(15) & delta_y(15);
0 Kudos
Altera_Forum
Honored Contributor II
576 Views

It will work just fine. Is there any reason you dont want to use the signed data type? then you wouldnt need to pad the sign bit as you can use the resize function: 

 

slope <= resize( delta_t(15 downto 5), slope'length);
0 Kudos
Altera_Forum
Honored Contributor II
576 Views

If you want to round to nearest, you should also "add" the highest order fractional bit that you are shifting out. If delta_y is signed, this requires you to subtract delta_y(4). This is because a 1-bit signed number has two possible values, 0 or -1 decimal. So to add 1 if delta_y(4) is '1', you need to subtract.

0 Kudos
Reply