Debian Codegraph @

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

raw
1 /* { dg-do run } */
2 /* { dg-options "-fdump-tree-crc-details -fdisable-tree-phiopt2 -fdisable-tree-phiopt3" } */
3 /* { dg-skip-if "" { *-*-* } { "-O0" "-O1" "-Os" "-flto" } } */
4 /* { dg-require-effective-target int32plus } */
5
6 #include <stddef.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 int i;
38 for (i = 0; i < 16; ++i) {
39 bit_flag = out >> 15;
40 out <<= 1;
41 if (bit_flag)
42 out ^= CRC16;
43 }
44
45 uint16_t crc = 0;
46 i = 0x8000;
47 int j = 0x0001;
48 for (; i != 0; i >>= 1, j <<= 1) {
49 if (i & out) crc |= j;
50 }
51
52 return crc;
53 }
54
55 uint16_t gen_crc16 (const uint8_t *data, uint16_t size) {
56 uint16_t out = 0;
57 int bits_read = 0, bit_flag;
58
59 if (data == NULL)
60 return 0;
61
62 while (size > 0) {
63 bit_flag = out >> 15;
64
65 out <<= 1;
66 out |= (*data >> bits_read) & 1;
67
68 bits_read++;
69 if (bits_read > 7) {
70 bits_read = 0;
71 data++;
72 size--;
73 }
74
75 if (bit_flag)
76 out ^= CRC16;
77 }
78
79 int i;
80 for (i = 0; i < 16; ++i) {
81 bit_flag = out >> 15;
82 out <<= 1;
83 if (bit_flag)
84 out ^= CRC16;
85 }
86
87 uint16_t crc = 0;
88 i = 0x8000;
89 int j = 0x0001;
90 for (; i != 0; i >>= 1, j <<= 1) {
91 if (i & out) crc |= j;
92 }
93
94 return crc;
95 }
96
97 int main ()
98 {
99 if (gen_crc16 ("hello", 5) != 13522)
100 __builtin_abort ();
101
102 for (uint8_t i = 0; i < 255; i++)
103 {
104 uint16_t res1 = gen_crc16_O0 (&i, 1);
105 uint16_t res2 = gen_crc16 (&i, 1);
106 if (res1 != res2)
107 __builtin_abort ();
108 }
109 __builtin_exit (0);
110 }
111 /* { dg-final { scan-tree-dump "function maybe contains CRC calculation." "crc" } } */
112 /* { dg-final { scan-tree-dump "Loop iteration number is 15" "crc" } } */
113 /* { dg-final { scan-tree-dump "Bit forward" "crc" } } */
114 /* { dg-final { scan-tree-dump "Polynomial's value is \\\{\[0, \]*1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1\\\}" "crc" } } */
115 /* { dg-final { scan-tree-dump "calculates CRC!" "crc" } } */
116 /* { dg-final { scan-tree-dump-times "Couldn't generate faster CRC code." 0 "crc" } } */