Linux Audio

Check our new training course

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