# 让GCC使用携带逻辑以进行任意精确算术，而无需内联装配?[英] Get GCC To Use Carry Logic For Arbitrary Precision Arithmetic Without Inline Assembly?

### 问题描述

```#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

int main (int argc, char **argv)
{
uint32_t a[4];
uint32_t b[4];
uint32_t c[4];
uint32_t carry = 0;

for (int i = 0; i < 4; ++i)
{
a[i] = strtoul (argv[i+1], NULL, 16);
b[i] = strtoul (argv[i+5], NULL, 16);
}

for (int i = 0; i < 4; ++i)
{
uint32_t aa = a[i];
uint32_t bb = b[i];
uint32_t r = aa + carry;
carry = (r < carry);
r += bb;
carry += (r < bb);
c[i] = r;
}

printf ("%08X%08X%08X%08X + %08X%08X%08X%08X =\n", a[3], a[2], a[1], a[0], b[3], b[2], b[1], b[0]);
printf ("%08X%08X%08X%08X\n", c[3], c[2], c[1], c[0]);

return 0;
}
```

GCC -O3 -std=c99不产生任何adc指令，如objdump检查.我的GCC版本是i686-pc-mingw32-gcc (GCC) 4.5.2.

## 推荐答案

gcc will 如果如果，它可以看到它需要:

，可能是有益的.

```uint32_t aa,bb,rr;
bool msbA, msbB, msbR, carry;
// ...

rr = aa+bb;

msbA = aa >= (1<<31); // equivalent: (aa & (1<<31)) != 0;
msbB = bb >= (1<<31);
msbR = rr >= (1<<31);

carry = (msbA && msbB) || ( !msbR && ( msbA || msbB) );
```