I'd have to go look at the rule book, but I think the general idea
is that types are promoted to the "largest" referenced on the
right hand side of the expression. In your case they are both uint8
so no implicit conversion gets done. I will go further and assume
that your declared "sum" should have been "carry", in which case
I don't know how you got a negative sign extension in your result.
If carry was signed I could make a case for it though.
I always like to err on the side of explicitness in casts and
ordering just so I don't have to try to remember all the rules.
I even, probably as a result of dealing with always-signed Java,
go so far as to mask results to the precision I want in order to
avoid unintentional sign extension.
In your repaired example you could probably get away with uint16
casts as two 8 bit values will not multiply to larger than 16 bits.
Also you might consider making all your i's and j's unsigned as
the arithmetic is often a bit faster.
MS
and including
王悦 wrote:
> Just a NOTICE, hope to help:)
>
> *Be very careful when using integers of different types in an arithmetic
> expression. NesC compiler doesn't seem to convert them into one
> appropriate type during evaluation.
> *
> Because TinyOS doesn't have a full-featured debugging mechanism(though I
> appreciate printf, which is really a gift), I used to work in VS2005
> before I transplant the code onto micaz. And recently I wrote something
> like this:
>
> int8_t i, j;
> uint8_t input[SIZE];
> uint32_t sum = 0;
>
> for(i = SIZE-1; i >= 0; i--){
> for(j = 0; j < SIZE; j++){
> carry += input[j] * input[i>=j+1?i-j-1:SIZE+i-j-1];
> }
> carry >>= 8;
> }
>
> It works fine in VS2005, yet has trouble on micaz. input[j] *
> input[i>=j+1?i-j-1:SIZE+i-j-1] can produce negative product though both
> operands are unsigned int. I fixed it like this:
>
> carry += (uint32_t)input[j] * (uint32_t)input[i>=j+1?i-j-1:SIZE+i-j-1];
>
> While VS2005 is tolerant, one should still pay attention to coding
> style in case of compiler confusion.
>
> --
> Best Regards
>
> Wyatt
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tinyos-help mailing list
> [email protected]
> https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help
_______________________________________________
Tinyos-help mailing list
[email protected]
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help