Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 1 | /* |
Nik Dewally | bacae6c | 2024-07-30 16:58:14 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2019-2024, Arm Limited. All rights reserved. |
Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame] | 8 | #include "compute.hpp" |
| 9 | |
| 10 | |
| 11 | using namespace std; |
| 12 | |
| 13 | /********************************************************************************** |
| 14 | Methods of class crc32 follow: |
| 15 | **********************************************************************************/ |
| 16 | |
| 17 | crc32::crc32 (void) |
| 18 | { |
| 19 | shift_reg = 0x55555555; // just give it some default value |
| 20 | } |
| 21 | |
| 22 | void 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. */ |
| 30 | uint32_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 | |
| 46 | uint32_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 | |
| 55 | uint32_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 | |
| 64 | uint32_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 | **********************************************************************************/ |