blob: f898be264374ea43cee1f2499411bf70580b8ccf [file] [log] [blame]
Karl Zhang3de5ab12021-05-31 11:45:48 +08001/*
Nik Dewallybacae6c2024-07-30 16:58:14 +01002 * Copyright (c) 2019-2024, Arm Limited. All rights reserved.
Karl Zhang3de5ab12021-05-31 11:45:48 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Karl Zhang3de5ab12021-05-31 11:45:48 +08008#include "compute.hpp"
9
10
11using namespace std;
12
13/**********************************************************************************
14 Methods of class crc32 follow:
15**********************************************************************************/
16
17crc32::crc32 (void)
18{
19 shift_reg = 0x55555555; // just give it some default value
20}
21
22void crc32::seed_lfsr (uint32_t init_value)
23{
24 shift_reg = init_value;
25}
26
27/* lfsr_1b() performs one shift of the LFSR, factoring in a single bit of info,
28 that single bit must be in the low-order bit of the parameter. It returns
29 the LFSR value, which may be ignored. */
30uint32_t crc32::lfsr_1b (uint32_t a_bit)
31{
32 bool odd;
33
34 odd = ((shift_reg ^ a_bit) & 1) == 1;
35 shift_reg >>= 1;
36 if (odd) {
37 shift_reg ^= polynomial;
38 }
39 if (shift_reg == 0) {
40 // Theoretically should never happen, but precaution...
41 seed_lfsr (0x55555555);
42 }
43 return shift_reg;
44}
45
46uint32_t crc32::crc (uint8_t a_byte)
47{
48 for (int i = 0; i < 8; i++) {
49 lfsr_1b ((uint32_t) a_byte);
50 a_byte >>= 1;
51 }
52 return shift_reg;
53}
54
55uint32_t crc32::crc (uint16_t a_halfword)
56{
57 for (int i = 0; i < 16; i++) {
58 lfsr_1b ((uint32_t) a_halfword);
59 a_halfword >>= 1;
60 }
61 return shift_reg;
62}
63
64uint32_t crc32::crc (uint32_t a_word)
65{
66 for (int i = 0; i < 32; i++) {
67 lfsr_1b ((uint32_t) a_word);
68 a_word >>= 1;
69 }
70 return shift_reg;
71}
72
73/**********************************************************************************
74 End of methods of class crc32.
75**********************************************************************************/