Forum Discussion
judgement on whether or not there is an overflow
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)?
5 Replies
- Altera_Forum
Honored Contributor
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. - Altera_Forum
Honored Contributor
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?
- Altera_Forum
Honored Contributor
--- 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; - Altera_Forum
Honored Contributor
If the final result doesn't have an overflow, does an overflow in the middle of the calculation matters?
- Altera_Forum
Honored Contributor
--- 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