-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrc_util.c
189 lines (147 loc) · 7.71 KB
/
crc_util.c
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
*********************************************************************************************************
* uC/CRC
* ERROR DETECTING CODE (EDC) & ERROR CORRECTING CODE (ECC) CALCULATION UTILITIES
*
* Copyright 2007-2020 Silicon Laboratories Inc. www.silabs.com
*
* SPDX-License-Identifier: APACHE-2.0
*
* This software is subject to an open source license and is distributed by
* Silicon Laboratories Inc. pursuant to the terms of the Apache License,
* Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
*
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*
* uC/CRC UTILITY LIBRARY
*
* Filename : crc_util.c
* Version : V1.10.00
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#define MICRIUM_SOURCE
#include "crc_util.h"
/*
*********************************************************************************************************
* MODULE
*********************************************************************************************************
*/
#ifdef CRC_UTIL_MODULE_PRESENT
/*
*********************************************************************************************************
* EXTERNAL C LANGUAGE LINKAGE
*
* Note(s) : (1) C++ compilers MUST 'extern'ally declare ALL C function prototypes & variable/object
* declarations for correct C language linkage.
*********************************************************************************************************
*/
#ifdef __cplusplus
extern "C" { /* See Note #1. */
#endif
/*
*********************************************************************************************************
* EXTERNAL C LANGUAGE LINKAGE END
*********************************************************************************************************
*/
#ifdef __cplusplus
} /* End of 'extern'al C lang linkage. */
#endif
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
/* Pop cnt algorithms. */
#define CRC_UTIL_POPCNT_METHOD_FAST_MULT 0
#define CRC_UTIL_POPCNT_METHOD_SLOW_MULT 1
#define CRC_UTIL_POPCNT_METHOD CRC_UTIL_POPCNT_METHOD_FAST_MULT
/*
*********************************************************************************************************
* LOCAL CONSTANTS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL DATA TYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL TABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*********************************************************************************************************
* LOCAL FUNCTIONS
*********************************************************************************************************
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* CRCUtil_PopCnt_32()
*
* Description : Compute population count (hamming weight) for value (number of bits set).
*
* Argument(s) : value Value to compute population count on.
*
*
* Return(s) : value's population count.
*
* Caller(s) : various.
*
* Note(s) : (1) Algorithm taken from http://en.wikipedia.org/wiki/Hamming_weight
*********************************************************************************************************
*/
CPU_INT08U CRCUtil_PopCnt_32 (CPU_INT32U value)
{
CPU_INT32U even_cnt;
CPU_INT32U odd_cnt;
CPU_INT32U result;
odd_cnt = (value >> 1u) & CRC_UTIL_POPCNT_MASK01010101_32; /* 2-bits pieces. */
result = value - odd_cnt; /* Same result as result=odd_cnt+(value & 0x55555555). */
even_cnt = result & CRC_UTIL_POPCNT_MASK00110011_32; /* 4-bits pieces. */
odd_cnt = (result >> 2u) & CRC_UTIL_POPCNT_MASK00110011_32;
result = even_cnt + odd_cnt;
even_cnt = result & CRC_UTIL_POPCNT_MASK00001111_32; /* 8-bits pieces. */
odd_cnt = (result >> 4u) & CRC_UTIL_POPCNT_MASK00001111_32;
result = even_cnt + odd_cnt;
#if CRC_UTIL_POPCNT_METHOD == CRC_UTIL_POPCNT_METHOD_SLOW_MULT
result += result >> 8u; /* 16-bits pieces into their lowest 8 bits */
result += result >> 16u; /* Add together both counts. */
result &= 0x3Fu
return (result); /* Mask unwanted bits. */
#else
result = (result * CRC_UTIL_POPCNT_POWERSOF256_32) >> 24u;
return (result);
#endif
}
/*
*********************************************************************************************************
* MODULE END
*********************************************************************************************************
*/
#endif /* End of CRC_UTIL module include. */