FastCRCsw.cpp
Go to the documentation of this file.
1 /* FastCRC library code is placed under the MIT license
2  * Copyright (c) 2014,2015,2016 Frank Bosing
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 //
26 // Thanks to:
27 // - Catalogue of parametrised CRC algorithms, CRC RevEng
28 // http://reveng.sourceforge.net/crc-catalogue/
29 //
30 // - Danjel McGougan (CRC-Table-Generator)
31 //
32 
33 //
34 // modify from FastCRC library @ 2018/11/20
35 //
36 
37 #include "FastCRC.h"
38 #include "FastCRC_tables.hpp"
39 
40 // ================= 16-BIT CRC ===================
44 
45 #define crc_n4(crc, data, table) \
46  crc ^= data; \
47  crc = (table[(crc & 0xff) + 0x300]) ^ (table[((crc >> 8) & 0xff) + 0x200]) ^ \
48  (table[((data >> 16) & 0xff) + 0x100]) ^ (table[data >> 24]);
49 
58  uint16_t crc = seed_;
59 
60  while (((uintptr_t)data & 3) && len) {
61  crc = (crc >> 8) ^ crc_table_mcrf4xx[(crc & 0xff) ^ *data++];
62  len--;
63  }
64 
65  while (len >= 16) {
66  len -= 16;
67  crc_n4(crc, ((uint32_t *)data)[0], crc_table_mcrf4xx);
68  crc_n4(crc, ((uint32_t *)data)[1], crc_table_mcrf4xx);
69  crc_n4(crc, ((uint32_t *)data)[2], crc_table_mcrf4xx);
70  crc_n4(crc, ((uint32_t *)data)[3], crc_table_mcrf4xx);
71  data += 16;
72  }
73 
74  while (len--) {
75  crc = (crc >> 8) ^ crc_table_mcrf4xx[(crc & 0xff) ^ *data++];
76  }
77 
78  // seed = crc;
79  return crc;
80 }
81 
82 // ================= 32-BIT CRC ===================
86 
87 #define crc_n4d(crc, data, table) \
88  crc ^= data; \
89  crc = (table[(crc & 0xff) + 0x300]) ^ (table[((crc >> 8) & 0xff) + 0x200]) ^ \
90  (table[((crc >> 16) & 0xff) + 0x100]) ^ (table[(crc >> 24) & 0xff]);
91 
92 #define crcsm_n4d(crc, data, table) \
93  crc ^= data; \
94  crc = (crc >> 8) ^ (table[crc & 0xff]); \
95  crc = (crc >> 8) ^ (table[crc & 0xff]); \
96  crc = (crc >> 8) ^ (table[crc & 0xff]); \
97  crc = (crc >> 8) ^ (table[crc & 0xff]);
98 
105 #if CRC_BIGTABLES
106 #define CRC_TABLE_CRC32 crc_table_crc32_big
107 #else
108 #define CRC_TABLE_CRC32 crc_table_crc32
109 #endif
110 
112  uint32_t crc = seed_ ^ 0xffffffff;
113 
114  while (((uintptr_t)data & 3) && len) {
115  crc = (crc >> 8) ^ CRC_TABLE_CRC32[(crc & 0xff) ^ *data++];
116  len--;
117  }
118 
119  while (len >= 16) {
120  len -= 16;
121 #if CRC_BIGTABLES
122  crc_n4d(crc, ((uint32_t *)data)[0], CRC_TABLE_CRC32);
123  crc_n4d(crc, ((uint32_t *)data)[1], CRC_TABLE_CRC32);
124  crc_n4d(crc, ((uint32_t *)data)[2], CRC_TABLE_CRC32);
125  crc_n4d(crc, ((uint32_t *)data)[3], CRC_TABLE_CRC32);
126 #else
127  crcsm_n4d(crc, ((uint32_t *)data)[0], CRC_TABLE_CRC32);
128  crcsm_n4d(crc, ((uint32_t *)data)[1], CRC_TABLE_CRC32);
129  crcsm_n4d(crc, ((uint32_t *)data)[2], CRC_TABLE_CRC32);
130  crcsm_n4d(crc, ((uint32_t *)data)[3], CRC_TABLE_CRC32);
131 #endif
132  data += 16;
133  }
134 
135  while (len--) {
136  crc = (crc >> 8) ^ CRC_TABLE_CRC32[(crc & 0xff) ^ *data++];
137  }
138 
139  // seed = crc;
140  crc ^= 0xffffffff;
141 
142  return crc;
143 }
FastCRC16(uint16_t seed)
Definition: FastCRCsw.cpp:43
uint16_t seed_
Definition: FastCRC.h:62
uint32_t crc32_calc(const uint8_t *data, uint16_t len)
Definition: FastCRCsw.cpp:111
unsigned short uint16_t
Definition: stdint.h:126
FastCRC32(uint32_t seed)
Definition: FastCRCsw.cpp:85
unsigned char uint8_t
Definition: stdint.h:125
uint16_t mcrf4xx_calc(const uint8_t *data, const uint16_t datalen)
Definition: FastCRCsw.cpp:57
unsigned int uint32_t
Definition: stdint.h:127
_W64 unsigned int uintptr_t
Definition: stdint.h:165
#define crcsm_n4d(crc, data, table)
Definition: FastCRCsw.cpp:92
#define crc_n4(crc, data, table)
Definition: FastCRCsw.cpp:45
const uint16_t crc_table_mcrf4xx[1024]
#define crc_n4d(crc, data, table)
Definition: FastCRCsw.cpp:87
#define CRC_TABLE_CRC32
Definition: FastCRCsw.cpp:108


livox_ros_driver
Author(s): Livox Dev Team
autogenerated on Mon Mar 15 2021 02:40:46