On Sat, 3 Dec 2005, Gerhard Fiedler wrote: > sergio masci wrote: > > > XCSB doesn't go the whole hog but it does track this much better than C. > > > byte_var = byte_var + byte_var // see (1) > > (1) ... add operation performed as two bytes > > This is intuitive, and not significantly different from C. The result is > the same. > > > byte_var = byte_var + word_var // see (2) > > (2) ... add operation performed as two bytes > > This is surprising, and may lead to a wrong result (for signed variables). > IMO this should at least trigger a compiler warning. > > > word_var = byte_var + byte_var // see (3) > > (3) ... add operation performed as two words > > This is not different from C. > > > byte_var = (byte_var + byte_var) >> k // see (4) > > (4) ... add operation performed as two words > > shift operation performed on word result > > Considering 1, this is surprising. In any case, it's safe and not different > from C. > > > byte_var = (ubyte)(byte_var + byte_var) >> k // see (5) > > (5) ... add operation performed as two bytes > > shift operation performed on byte result > > This is intuitive and not significantly different from C. The result is the > same. > > Overall, these examples don't seem to show a significant difference from C, > with the exception of (2), of which I'm not sure it really makes more sense > than the way C does it. > > > > Also comparing signed and unsigned works correctly > > > > e.g. > > if unsigned_byte_var > signed_byte_var then > > > > always true if unsigned_byte_var > 127 > > always false if signed_byte_var < 0 > > otherwise true or false as expected > > endif > > This is the same in C. > > (And as always, when talking about C in a generic fashion, I think ANSI C89 > is probably a good candidate.) > > Gerhard > Thanks for the comments. I can see how you would consider both as doing the same however whereas C is promoting "signed byte" and "unsigned byte" to "int" before applying the operator and then (possibly) optimising the generated code afterwards, XCSB applies the operator to the byte values directly and generates additional code to cope with overflow. I got carried away in my enthusiasm and forgot about the promotion to "int" in the conditional in C. But what I wrote also holds true for ints and longs in XCSB whereas it does not in C e.g. if unsigned_int_var > signed_int_var then always true if unsigned_int_var > 32767 always false if signed_int_var < 0 otherwise true or false as expected endif and in anycase although the byte version has the same net effect under both C and XCSB, the XCSB version does not need to generate zero and sign extended ints prior to the comparison :-P Regards Sergio Masci http://www.xcprod.com/titan/XCSB - optimising PIC compiler FREE for personal non-commercial use . -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist