1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
/* { dg-do run } */
/* { dg-options "-fdump-tree-crc-details" } */
/* { dg-skip-if "" { *-*-* } { "-O0" "-O1" "-Os" "-O3" "-flto"} } */
#include <stdint.h>
#include <stdlib.h>
typedef uint8_t crc;
#define WIDTH (8 * sizeof(crc))
#define TOPBIT (1 << (WIDTH - 1))
__attribute__ ((noinline,optimize(0)))
crc crcSlow_O0 (uint8_t const message[], int nBytes) {
crc remainder = 0;
/*
* Perform modulo-2 division, a byte at a time.
*/
for (int byte = 0; byte < nBytes; ++byte) {
remainder ^= (message[byte] << (WIDTH - 8));
for (uint8_t bit = 8; bit > 0; --bit) {
if (remainder & TOPBIT) {
remainder = (remainder << 1) ^ 1234;
} else {
remainder = (remainder << 1);
}
}
}
return (remainder);
}
crc crcSlow (uint8_t const message[], int nBytes) {
crc remainder = 0;
/*
* Perform modulo-2 division, a byte at a time.
*/
for (int byte = 0; byte < nBytes; ++byte) {
remainder ^= (message[byte] << (WIDTH - 8));
for (uint8_t bit = 8; bit > 0; --bit) {
if (remainder & TOPBIT) {
remainder = (remainder << 1) ^ 1234;
} else {
remainder = (remainder << 1);
}
}
}
return (remainder);
}
int main ()
{
for (crc i = 0; i < 255; i++)
{
crc res1 = crcSlow_O0 (&i, 1);
crc res2 = crcSlow (&i, 1);
if (res1 != res2)
__builtin_abort ();
}
__builtin_exit (0);
}
/* { dg-final { scan-tree-dump "crcSlow function maybe contains CRC calculation." "crc"} } */
/* { dg-final { scan-tree-dump "Loop iteration number is 7" "crc"} } */
/* { dg-final { scan-tree-dump "Bit forward" "crc"} } */
/* { dg-final { scan-tree-dump "Polynomial's value is \\\{1, 1, 0, 1, 0, 0, 1, 0\\\}" "crc" } } */
/* { dg-final { scan-tree-dump "calculates CRC!" "crc"} } */
/* { dg-final { scan-tree-dump-times "Couldn't generate faster CRC code." 0 "crc"} } */
|