Re: testing for arithmetic carry

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 8/15/2010 3:01 AM, Dima Sorkin wrote:
Hi.

   Suppose I have the following unsigned integer variables, a0, a1, b0, b1, r0 r1. The operation that is performed is:
         r0 = a0 + b0
         r1 = a1 + b1 + carry from previous addition
         if ( carry in r1 calculation) do something

This is done straight forward on assembly language, assuming 'add' and 'add with carry' are provided by CPU. Is there a way in C to test for carry from last addition ? Some GCC extensions (not ASM(...) ;)?

   There is of course method of testing whether r0 is less then a0 (for detecting first carry), and the second carry can be inferred by two comparison operations. Is GCC optimizing with -O2 able to reduce the comparisons to actual 'add-with-carry' and 'branch-if-carry' operations in assembly ?

Thanks, regards,
   Dima.
I assume that you need this feature for efficiency reasons. In that case your question should be: "Is GCC optimizing with -O2 guaranteed to reduce..."

Given that GCC changes, I wouldn't trust that such a guarantee would be in effect very long.

I think the only way to guarantee a particular behavior at the assembly language level is to write it in assembly language.

--Bob




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux