Programmable Devices
CPLDs, FPGAs, SoC FPGAs, Configuration, and Transceivers
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
21615 Discussions

judgement on whether or not there is an overflow

Altera_Forum
Honored Contributor II
1,669 Views

I have 3 numbers x, y, z to be added together, each with the format of s1.14. Then the sum will be in the format of s3.14. How to make judgement on whether or not there is an overflow? If it is judged based on the first 3 MSB and they are ALL either zeros or ones, is there is no overflow? why? Does the overflow in the middle cares (e.g. I have more than 3 addition)?

0 Kudos
5 Replies
Altera_Forum
Honored Contributor II
968 Views

the rule is : 

For an N bit number 

N + N = N+1 bits required. 

 

So N + N + N requires N+2 bits. Which is what you have, hence it will never overflow.
0 Kudos
Altera_Forum
Honored Contributor II
968 Views

Sum is just an intermediate variable for the calculation, and the final output has the same number of bits as input. Besides, there is also an output (a flag) indicating whether there is an overflow in the final result. Then, how to make a judgement on whether or not there is an overflow, and why?

0 Kudos
Altera_Forum
Honored Contributor II
968 Views

 

--- Quote Start ---  

Sum is just an intermediate variable for the calculation, and the final output has the same number of bits as input. Besides, there is also an output (a flag) indicating whether there is an overflow in the final result. Then, how to make a judgement on whether or not there is an overflow, and why? 

--- Quote End ---  

 

 

in general if your internal bitwidth is say 20 bits and you want only 16 LSBs out then you check bit(18:15) if it is different from sign bit then you clip output to either +32767 or -32767 and raise the flag, otherwise you pass the 16 bits direct out. 

 

if data_int(19 downto 15) = "00000" or data_int(19 downto 15) = "11111" then dout <= data_int(15 downto 0); clip <= '0'; elsif if data_int(19) = '0' then dout <= 32767; clip <= '1'; else dout <= -32767; clip <= '1'; end if;
0 Kudos
Altera_Forum
Honored Contributor II
968 Views

If the final result doesn't have an overflow, does an overflow in the middle of the calculation matters?

0 Kudos
Altera_Forum
Honored Contributor II
968 Views

 

--- Quote Start ---  

If the final result doesn't have an overflow, does an overflow in the middle of the calculation matters? 

--- Quote End ---  

 

 

Yes it does. 

Normally you should not allow that by applying enough width for internal bit growth. 

If your application permits you may clip internally instead in the same way as my example but adjusted for relevant widths
0 Kudos
Reply