[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
## Re: [avr-gcc-list] Operation problem

**From**: |
Dave Hansen |

**Subject**: |
Re: [avr-gcc-list] Operation problem |

**Date**: |
Sat, 11 Aug 2007 16:16:22 -0400 |

From: "Gerard Marull Paretas" <address@hidden>

> Paulo Marques:

`> I think you just answered your own question: if all the variables
``involved
`are uint16_t, then that is the precision used for all he expression and it
will overflow on the multiplication.
The same happens whether if currentsong.length is uint32_t or uint16_t.

Yes. The target of the assignment plays no part in the calculation.
Let's simplify some things. Consider the the following code:
uint16_t w1=40000, w2=40000;
uint32_t dw1=40000, dw2=40000;
uint32_t r1, r2, r3, r4;
r1 = w1 + w2;
r2 = dw1 + dw2;
r3 = dw1 + w2;
r4 = (uint32_t)w1 + w2;

`I haven't tried it, but I have a prediction: r1 will be 14464, but r2, r3,
``and r4 will all be 80000.
`

`The reason is simple: the calculation in each statement is separate from the
``assignment. The calculation occurs first, and the result is then assigned
``to the target.
`

`The first expression (w1+w2) is the addition of 2 uint16_t values, the
``result of which is a uint16_t. Since the addition overflows, only the
``lowest 16 bits are kept. That result is stored in r1.
`

`The second expression is the addition of two uint32_t values. The result is
``a uint32_t, and no overflow occurs.
`

`The third expression involves a uint32_t and a uint16_t. The promotion
``rules of C require the uint16_t value be promoted to uint32_t before the
``addition takes place. After that, it's just like the second expression.
`

`The third expression has an explicit cast of a uint16_t (w1) to uint32_t.
``The conversion of w1 takes place before the addition, and the integer
``promotions then require that w2 be converted to uint32_t as well before the
``addition takes place. After that, it's just like the third expression.
`

`If this is still fuzzy, try compiling the code above (or something like it),
``and look at the generated assembly.
`
Regards,
-=Dave
_________________________________________________________________

`Find a local pizza place, movie theater, and moreĀ
.then map the best route!
``http://maps.live.com/default.aspx?v=2&ss=yp.bars~yp.pizza~yp.movie%20theater&cp=42.358996~-71.056691&style=r&lvl=13&tilt=-90&dir=0&alt=-1000&scene=950607&encType=1&FORM=MGAC01
`