csledge said:
Hi,
I am trying to compute a 64 bit result from 2 32 bit
registers, How do I get the carry into the higher word ?
Generally, C destroys the carry (you can't get at this result directly). If
you want efficiency, use assembly-language where you can use the carry.
However, if you don't mind a little bit of inefficiency, you can figure out
INDIRECTLY if a carry occurred. On a standard 2's complement machine, note
that for two unsigned integers of the same size, a carry occurred if and
only if the sum is less than both of the operands.
For example, this would work just fine:
unsigned int x, y, z;
....
z = x + y;
if (z < x) ...
If you look at the GMP, for example, in the code that is used if no
assembly-language is available for the target processor, it uses such tests.
So, here is what you're looking for:
void add(unsigned x, unsigned y, unsigned *out_msb, unsigned *out_lsb)
{
unsigned z;
*out_lsb = z = x + y;
if (z < x)
*out_msb = 1;
else
*out_msb = 0;
}