| 1 |
typedef unsigned long long uint64_t; |
| 2 |
void f(uint64_t *a, uint64_t aa) __attribute__((noinline)); |
| 3 |
void f(uint64_t *a, uint64_t aa) |
| 4 |
{ |
| 5 |
uint64_t new_value = aa; |
| 6 |
uint64_t old_value = *a; |
| 7 |
int bit_size = 32; |
| 8 |
uint64_t mask = (uint64_t)(unsigned)(-1); |
| 9 |
uint64_t tmp = old_value & mask; |
| 10 |
new_value &= mask; |
| 11 |
/* On overflow we need to add 1 in the upper bits */ |
| 12 |
if (tmp > new_value) |
| 13 |
new_value += 1ull<<bit_size; |
| 14 |
/* Add in the upper bits from the old value */ |
| 15 |
new_value += old_value & ~mask; |
| 16 |
*a = new_value; |
| 17 |
} |
| 18 |
int main(void) |
| 19 |
{ |
| 20 |
uint64_t value, new_value, old_value; |
| 21 |
value = 0x100000001; |
| 22 |
old_value = value; |
| 23 |
new_value = (value+1)&(uint64_t)(unsigned)(-1); |
| 24 |
f(&value, new_value); |
| 25 |
if (value != old_value+1) |
| 26 |
__builtin_abort (); |
| 27 |
return 0; |
| 28 |
} |
| 29 |
|