| 1 |
/* { dg-do compile } */ |
| 2 |
/* { dg-options "-fdump-tree-crc-details -O2" } */ |
| 3 |
/* { dg-require-effective-target int32plus } */ |
| 4 |
|
| 5 |
// File - wifi. Sizeof check is added for the test. |
| 6 |
|
| 7 |
#include <stdlib.h> |
| 8 |
#include <stdbool.h> |
| 9 |
#include <stdio.h> |
| 10 |
typedef unsigned int u32; |
| 11 |
typedef unsigned char u8; |
| 12 |
u32 WIFI_CRC32Table[256]; |
| 13 |
bool initialized; |
| 14 |
u32 reflect (u32 ref, char ch) |
| 15 |
{ |
| 16 |
if (sizeof (u32) < 4) |
| 17 |
exit (0); |
| 18 |
|
| 19 |
u32 value = 0; |
| 20 |
|
| 21 |
for (int i = 1; i < (ch + 1); i++) |
| 22 |
{ |
| 23 |
if (ref & 1) |
| 24 |
value |= 1 << (ch - i); |
| 25 |
ref >>= 1; |
| 26 |
} |
| 27 |
|
| 28 |
return value; |
| 29 |
} |
| 30 |
|
| 31 |
u32 WIFI_calcCRC32 (u8 *data, int len) |
| 32 |
{ |
| 33 |
if (sizeof (u32) < 4) |
| 34 |
exit (0); |
| 35 |
|
| 36 |
u32 crc = 0xFFFFFFFF; |
| 37 |
|
| 38 |
while (len--) |
| 39 |
crc = (crc >> 8) ^ WIFI_CRC32Table[(crc & 0xFF) ^ *data++]; |
| 40 |
|
| 41 |
return (crc ^ 0xFFFFFFFF); |
| 42 |
} |
| 43 |
|
| 44 |
void WIFI_initCRC32Table () |
| 45 |
{ |
| 46 |
if (sizeof (u32) < 4) |
| 47 |
exit (0); |
| 48 |
|
| 49 |
initialized = false; |
| 50 |
if (initialized) return; |
| 51 |
initialized = true; |
| 52 |
|
| 53 |
u32 polynomial = 0x04C11DB7; |
| 54 |
|
| 55 |
for (int i = 0; i < 0x100; i++) |
| 56 |
{ |
| 57 |
WIFI_CRC32Table[i] = reflect (i, 8) << 24; |
| 58 |
for (int j = 0; j < 8; j++) |
| 59 |
WIFI_CRC32Table[i] = (WIFI_CRC32Table[i] << 1) |
| 60 |
^ (WIFI_CRC32Table[i] & (1 << 31) ? polynomial |
| 61 |
: 0); |
| 62 |
WIFI_CRC32Table[i] = reflect (WIFI_CRC32Table[i], 32); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
int main () |
| 67 |
{ |
| 68 |
if (sizeof (u32) < 4) |
| 69 |
exit (0); |
| 70 |
|
| 71 |
WIFI_initCRC32Table (); |
| 72 |
WIFI_calcCRC32 ("dfsf", 4); |
| 73 |
} |
| 74 |
|
| 75 |
/* { dg-final { scan-tree-dump "calculates CRC!" "crc" } } */ |