OpenCores
URL https://opencores.org/ocsvn/hf-risc/hf-risc/trunk

Subversion Repositories hf-risc

[/] [hf-risc/] [trunk/] [software/] [app/] [test_crc.c] - Blame information for rev 16

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 13 serginhofr
#include <hf-risc.h>
2
 
3
/*
4
crc16 implementation:
5
width=16 poly=0x1021 init=0xffff refin=false refout=false xorout=0x0000 check=0x29b1 name="CRC-16/CCITT-FALSE"
6
*/
7
uint16_t crc16(uint8_t *data, uint32_t len){
8
        uint16_t crc = 0xffff, i;
9
 
10
        while(len--){
11
                crc ^= (uint16_t)*data++ << 8;
12
 
13
                for(i = 0; i < 8; ++i)
14
                        crc = crc << 1 ^ (crc & 0x8000 ? 0x1021 : 0x0000);
15
        }
16
        return crc;
17
}
18
 
19
/*
20
crc32 implementation:
21
width=32 poly=0x04c11db7 init=0xffffffff refin=false refout=false xorout=0x00000000 check=0x0376e6e7 name="CRC-32/MPEG-2"
22
*/
23
uint32_t crc32(uint8_t *data, uint32_t len){
24
        uint32_t crc = ~0, i;
25
 
26
        while(len--){
27
                crc ^= (uint32_t)*data++ << 24;
28
 
29
                for(i = 0; i < 8; ++i)
30
                        crc = crc << 1 ^ (crc & 0x80000000 ? 0x04c11db7 : 0x00000000);
31
        }
32
        return crc;
33
}
34
 
35
/*
36
crc64 implementation:
37
width=64 poly=0x42f0e1eba9ea3693 init=0x0000000000000000 refin=false refout=false xorout=0x0000000000000000 check=0x6c40df5f0b497347 name="CRC-64"
38
*/
39
uint64_t crc64(uint8_t *data, uint32_t len){
40
        uint64_t crc = 0;
41
        uint32_t i;
42
 
43
        while(len--){
44
                crc ^= (uint64_t)*data++ << 56;
45
 
46
                for(i = 0; i < 8; ++i)
47
                        crc = crc << 1 ^ (crc & 0x8000000000000000 ? 0x42f0e1eba9ea3693 : 0x0000000000000000);
48
        }
49
        return crc;
50
}
51
 
52
uint32_t xz_crc32_table[256];
53
 
54
void xz_crc32_init(void)
55
{
56
        const uint32_t poly = 0xEDB88320;
57
 
58
        uint32_t i;
59
        uint32_t j;
60
        uint32_t r;
61
 
62
        for (i = 0; i < 256; ++i) {
63
                r = i;
64
                for (j = 0; j < 8; ++j)
65
                        r = (r >> 1) ^ (poly & ~((r & 1) - 1));
66
 
67
                xz_crc32_table[i] = r;
68
        }
69
 
70
        return;
71
}
72
 
73
uint32_t xz_crc32(const uint8_t *buf, uint32_t size, uint32_t crc)
74
{
75
        crc = ~crc;
76
 
77
        while (size != 0) {
78
                crc = xz_crc32_table[*buf++ ^ (crc & 0xFF)] ^ (crc >> 8);
79
                --size;
80
        }
81
 
82
        return ~crc;
83
}
84
 
85
void main(void){
86
        uint8_t *str = "123456789";
87
        int32_t time;
88
        uint16_t crc1;
89
        uint32_t crc2, ops;
90
        uint64_t crc3;
91
 
92
        time = COUNTER;
93
        crc1 = crc16(str, strlen(str));
94
        time = COUNTER - time;
95
        printf("crc16: %8x, %d cycles\n", crc1, time);
96
        time = COUNTER;
97
        crc2 = crc32(str, strlen(str));
98
        time = COUNTER - time;
99
        printf("crc32: %8x, %d cycles\n", crc2, time);
100
        time = COUNTER;
101
        crc3 = crc64(str, strlen(str));
102
        time = COUNTER - time;
103
        printf("crc64: %8x%8x, %d cycles\n", (uint32_t)(crc3 >> 32), (uint32_t)(crc3 & 0xffffffff), time);
104
 
105
        xz_crc32_init();
106
        time = COUNTER;
107
        crc2 = xz_crc32(str, strlen(str), 0);
108
        time = COUNTER - time;
109
        printf("crc32 (table): %8x, %d cycles\n", crc2, time);
110
 
111
        return;
112
}
113
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.