Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
14 years ago

Adder in LPM seems to be weird

Hello all,

I have met a something weird in an adder in LPM and need your help or suggestion.

My signal is two 16 bits from NCOs which are signed numbered. And the adder is just summing those two numbers.

To prevent an overflow, I used a carry out bit (cout). The final output bit-width is 17 bits (1 bit cout + 16 bit of the summed data) and later on, I will take first 16 bits from the MSB (1 bit cout + first 15 bits of the summed data).

I ran a simulation but the output is wrong whenever datab port data is negative (result = dataa + datab; the result is wrong whenever datab is negative).

Since I am using two sine and cosine waves, the sum of those is a something smooth wave but if you look at the screen capture which is also attached, there is abrupt change.

Could anyone let me know how can I resolve this?

Thanks,

4 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Try sign extending each input to 17 bits (just duplicate the high order bit) and use a 17-bit adder. Notice how expected value - simulated value = 65536 or 2^16. That probably means your use of carry_out is incorrect.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thank you very much, alias.

    Then the carry out bit in LPM_ADD_SUB does not work properly?

    Regards,
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I'm not sure on this (you can check the documentation), but i think the LPM's do unsigned arithmetic. With 2's complement, the output is the same (signed or unsigned) but the carry out is different. Carry out can be thought of as overflow which happens at the 0xffff<->0x0000 boundary for unsigned, but at the 0x8000<->0x7fff boundary for signed. So you can't use an unsigned carry out as the extra bit for a signed add or subtract.

    Edit for correction:

    Neither carry out nor overflow are equivalent to an extra high order bit for signed arithmetic. Carry out is the same for signed or unsigned, it does not matter which the LPM is using except for the overflow output.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    Then the carry out bit in LPM_ADD_SUB does not work properly?

    --- Quote End ---

    carry out is not shown in any of your simulation results. How will you know that it's working uncorrectly.

    The problem is that you are adding two signed 16 Bit numbers and get a result, that exceeds the 16 Bit number range. But don't consider this overflow in your code and get's overflows as shown in your waveform. carry out could inform you about the overflow, but you're ignoring it.

    As suggested, the adder input data has to be either extended to 17 bits or scaled down one bit (by an arithmetical right shift) to avoid an overflow.

    Checking the expectable results by pencil and paper or a pocket calculator can help to understand how binary arithmetic works.