Debian Codegraph @

gcc/testsuite/gcc.dg/torture/crc-6.c

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