^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include "../include/linux/crc32poly.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include "../include/generated/autoconf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include "crc32defs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define ENTRIES_PER_LINE 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #if CRC_LE_BITS > 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) # define LE_TABLE_ROWS (CRC_LE_BITS/8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) # define LE_TABLE_SIZE 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) # define LE_TABLE_ROWS 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) # define LE_TABLE_SIZE (1 << CRC_LE_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #if CRC_BE_BITS > 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) # define BE_TABLE_ROWS (CRC_BE_BITS/8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) # define BE_TABLE_SIZE 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) # define BE_TABLE_ROWS 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) # define BE_TABLE_SIZE (1 << CRC_BE_BITS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) static uint32_t crc32table_le[LE_TABLE_ROWS][256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static uint32_t crc32table_be[BE_TABLE_ROWS][256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static uint32_t crc32ctable_le[LE_TABLE_ROWS][256];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * crc32init_le() - allocate and initialize LE table data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * crc is the crc of the byte i; other entries are filled in based on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * fact that crctable[i^j] = crctable[i] ^ crctable[j].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static void crc32init_le_generic(const uint32_t polynomial,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) uint32_t (*tab)[256])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) unsigned i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) uint32_t crc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) tab[0][0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) tab[0][i + j] = crc ^ tab[0][j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) for (i = 0; i < LE_TABLE_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) crc = tab[0][i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) for (j = 1; j < LE_TABLE_ROWS; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) crc = tab[0][crc & 0xff] ^ (crc >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) tab[j][i] = crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static void crc32init_le(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) crc32init_le_generic(CRC32_POLY_LE, crc32table_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static void crc32cinit_le(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) crc32init_le_generic(CRC32C_POLY_LE, crc32ctable_le);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * crc32init_be() - allocate and initialize BE table data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static void crc32init_be(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) unsigned i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) uint32_t crc = 0x80000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) crc32table_be[0][0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) crc = (crc << 1) ^ ((crc & 0x80000000) ? CRC32_POLY_BE : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) for (j = 0; j < i; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) crc32table_be[0][i + j] = crc ^ crc32table_be[0][j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) for (i = 0; i < BE_TABLE_SIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) crc = crc32table_be[0][i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) for (j = 1; j < BE_TABLE_ROWS; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) crc32table_be[j][i] = crc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static void output_table(uint32_t (*table)[256], int rows, int len, char *trans)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) for (j = 0 ; j < rows; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) printf("{");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) for (i = 0; i < len - 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (i % ENTRIES_PER_LINE == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) printf("%s(0x%8.8xL), ", trans, table[j][i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int main(int argc, char** argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) printf("/* this file is generated - do not edit */\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (CRC_LE_BITS > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) crc32init_le();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) printf("static const u32 ____cacheline_aligned "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) "crc32table_le[%d][%d] = {",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) LE_TABLE_ROWS, LE_TABLE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) output_table(crc32table_le, LE_TABLE_ROWS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) LE_TABLE_SIZE, "tole");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) printf("};\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (CRC_BE_BITS > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) crc32init_be();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) printf("static const u32 ____cacheline_aligned "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) "crc32table_be[%d][%d] = {",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) BE_TABLE_ROWS, BE_TABLE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) output_table(crc32table_be, LE_TABLE_ROWS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) BE_TABLE_SIZE, "tobe");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) printf("};\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (CRC_LE_BITS > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) crc32cinit_le();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) printf("static const u32 ____cacheline_aligned "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) "crc32ctable_le[%d][%d] = {",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) LE_TABLE_ROWS, LE_TABLE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) output_table(crc32ctable_le, LE_TABLE_ROWS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) LE_TABLE_SIZE, "tole");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) printf("};\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }