| 1 |
/* { dg-do compile } */ |
| 2 |
/* { dg-options "-fdump-tree-crc-details" } */ |
| 3 |
/* { dg-skip-if "" { *-*-* } { "-O0" "-O1" "-g" "-Os" "-flto" } } */ |
| 4 |
/* { dg-require-effective-target int32plus } */ |
| 5 |
|
| 6 |
/* This is a CRC, even though it may seem it's not. |
| 7 |
'j' is initialized with 4 (100) and increased by 2 each time. |
| 8 |
Thus, first bit value is never 1. For each iteration 'j & 1' yields 0, |
| 9 |
and it doesn't affect the CRC result. */ |
| 10 |
|
| 11 |
#include <stdint.h> |
| 12 |
|
| 13 |
uint16_t crc (uint8_t data, uint16_t crc) { |
| 14 |
uint8_t x16 = 0, carry = 0; |
| 15 |
for (uint8_t i = 0, j=4; i < 8; i++, j+=2) { |
| 16 |
x16 = (uint8_t) (((uint8_t) crc & 1) ^ (j & 1)); |
| 17 |
data >>= 1; |
| 18 |
if (x16 == 1) { |
| 19 |
crc ^= 0x4002; |
| 20 |
carry = 1; |
| 21 |
} else |
| 22 |
carry = 0; |
| 23 |
crc >>= 1; |
| 24 |
if (carry) |
| 25 |
crc |= 0x8000; |
| 26 |
else |
| 27 |
crc &= 0x7fff; |
| 28 |
} |
| 29 |
return crc; |
| 30 |
} |
| 31 |
|
| 32 |
/* { dg-final { scan-tree-dump "calculates CRC!" "crc" } } */ |