| 1 |
/* { dg-do run } */ |
| 2 |
/* { dg-options "-fdump-tree-crc -fdisable-tree-phiopt2 -fdisable-tree-phiopt3" } */ |
| 3 |
/* { dg-skip-if "" { *-*-* } { "-O0" "-O1" "-Os" "-flto" } } */ |
| 4 |
/* { dg-require-effective-target int32plus } */ |
| 5 |
|
| 6 |
#include <stdio.h> |
| 7 |
#include <stdint.h> |
| 8 |
#include <stdlib.h> |
| 9 |
|
| 10 |
#define CRC16 0x8005 |
| 11 |
|
| 12 |
__attribute__ ((noinline,optimize(0))) |
| 13 |
uint16_t gen_crc16_O0 (const uint8_t *data, uint16_t size) { |
| 14 |
uint16_t out = 0; |
| 15 |
int bits_read = 0, bit_flag; |
| 16 |
|
| 17 |
if (data == NULL) |
| 18 |
return 0; |
| 19 |
|
| 20 |
while (size > 0) { |
| 21 |
bit_flag = out >> 15; |
| 22 |
|
| 23 |
out <<= 1; |
| 24 |
out |= (*data >> bits_read) & 1; |
| 25 |
|
| 26 |
bits_read++; |
| 27 |
if (bits_read > 7) { |
| 28 |
bits_read = 0; |
| 29 |
data++; |
| 30 |
size--; |
| 31 |
} |
| 32 |
|
| 33 |
if (bit_flag) |
| 34 |
out ^= CRC16; |
| 35 |
|
| 36 |
} |
| 37 |
|
| 38 |
int i; |
| 39 |
for (i = 0; i < 16; ++i) { |
| 40 |
bit_flag = out >> 15; |
| 41 |
out <<= 1; |
| 42 |
if (bit_flag) |
| 43 |
out ^= CRC16; |
| 44 |
} |
| 45 |
|
| 46 |
uint16_t crc = 0; |
| 47 |
i = 0x8000; |
| 48 |
int j = 0x0001; |
| 49 |
for (; i != 0; i >>= 1, j <<= 1) { |
| 50 |
if (i & out) crc |= j; |
| 51 |
} |
| 52 |
|
| 53 |
return crc; |
| 54 |
} |
| 55 |
|
| 56 |
uint16_t gen_crc16 (const uint8_t *data, uint16_t size) { |
| 57 |
uint16_t out = 0; |
| 58 |
int bits_read = 0, bit_flag; |
| 59 |
|
| 60 |
if (data == NULL) |
| 61 |
return 0; |
| 62 |
|
| 63 |
while (size > 0) { |
| 64 |
bit_flag = out >> 15; |
| 65 |
|
| 66 |
out <<= 1; |
| 67 |
out |= (*data >> bits_read) & 1; |
| 68 |
|
| 69 |
bits_read++; |
| 70 |
if (bits_read > 7) { |
| 71 |
bits_read = 0; |
| 72 |
data++; |
| 73 |
size--; |
| 74 |
} |
| 75 |
|
| 76 |
if (bit_flag) |
| 77 |
out ^= CRC16; |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
int i; |
| 82 |
for (i = 0; i < 16; ++i) { |
| 83 |
bit_flag = out >> 15; |
| 84 |
out <<= 1; |
| 85 |
if (bit_flag) |
| 86 |
out ^= CRC16; |
| 87 |
} |
| 88 |
|
| 89 |
uint16_t crc = 0; |
| 90 |
i = 0x8000; |
| 91 |
int j = 0x0001; |
| 92 |
for (; i != 0; i >>= 1, j <<= 1) { |
| 93 |
if (i & out) crc |= j; |
| 94 |
} |
| 95 |
|
| 96 |
return crc; |
| 97 |
} |
| 98 |
|
| 99 |
int main () |
| 100 |
{ |
| 101 |
for (uint8_t i = 0; i < 255; i++) |
| 102 |
{ |
| 103 |
uint16_t res1 = gen_crc16_O0 (&i, 1); |
| 104 |
uint16_t res2 = gen_crc16 (&i, 1); |
| 105 |
if (res1 != res2) |
| 106 |
__builtin_abort (); |
| 107 |
} |
| 108 |
__builtin_exit (0); |
| 109 |
} |
| 110 |
/* { dg-final { scan-tree-dump "function maybe contains CRC calculation." "crc"} } */ |
| 111 |
/* { dg-final { scan-tree-dump "Loop iteration number is 15" "crc"} } */ |
| 112 |
/* { dg-final { scan-tree-dump "Bit forward" "crc"} } */ |
| 113 |
/* { dg-final { scan-tree-dump "calculates CRC!" "crc"} } */ |
| 114 |
/* { dg-final { scan-tree-dump-times "Couldn't generate faster CRC code." 0 "crc"} } */ |