This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: testing for arithmetic carry


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 Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]