| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:24 -07:00
										 |  |  | #include "../include/generated/autoconf.h"
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | #include "crc32defs.h"
 | 
					
						
							|  |  |  | #include <inttypes.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define ENTRIES_PER_LINE 4
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:24 -07:00
										 |  |  | #if CRC_LE_BITS > 8
 | 
					
						
							|  |  |  | # define LE_TABLE_ROWS (CRC_LE_BITS/8)
 | 
					
						
							|  |  |  | # define LE_TABLE_SIZE 256
 | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:23 -07:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:24 -07:00
										 |  |  | # define LE_TABLE_ROWS 1
 | 
					
						
							|  |  |  | # define LE_TABLE_SIZE (1 << CRC_LE_BITS)
 | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:23 -07:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:24 -07:00
										 |  |  | #if CRC_BE_BITS > 8
 | 
					
						
							|  |  |  | # define BE_TABLE_ROWS (CRC_BE_BITS/8)
 | 
					
						
							|  |  |  | # define BE_TABLE_SIZE 256
 | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:23 -07:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:24 -07:00
										 |  |  | # define BE_TABLE_ROWS 1
 | 
					
						
							|  |  |  | # define BE_TABLE_SIZE (1 << CRC_BE_BITS)
 | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:23 -07:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:24 -07:00
										 |  |  | static uint32_t crc32table_le[LE_TABLE_ROWS][256]; | 
					
						
							|  |  |  | static uint32_t crc32table_be[BE_TABLE_ROWS][256]; | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:25 -07:00
										 |  |  | static uint32_t crc32ctable_le[LE_TABLE_ROWS][256]; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * crc32init_le() - allocate and initialize LE table data | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * crc is the crc of the byte i; other entries are filled in based on the | 
					
						
							|  |  |  |  * fact that crctable[i^j] = crctable[i] ^ crctable[j]. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:25 -07:00
										 |  |  | static void crc32init_le_generic(const uint32_t polynomial, | 
					
						
							|  |  |  | 				 uint32_t (*tab)[256]) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	unsigned i, j; | 
					
						
							|  |  |  | 	uint32_t crc = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:25 -07:00
										 |  |  | 	tab[0][0] = 0; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:23 -07:00
										 |  |  | 	for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) { | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:25 -07:00
										 |  |  | 		crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		for (j = 0; j < LE_TABLE_SIZE; j += 2 * i) | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:25 -07:00
										 |  |  | 			tab[0][i + j] = crc ^ tab[0][j]; | 
					
						
							| 
									
										
										
										
											2010-05-24 14:33:31 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	for (i = 0; i < LE_TABLE_SIZE; i++) { | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:25 -07:00
										 |  |  | 		crc = tab[0][i]; | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:24 -07:00
										 |  |  | 		for (j = 1; j < LE_TABLE_ROWS; j++) { | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:25 -07:00
										 |  |  | 			crc = tab[0][crc & 0xff] ^ (crc >> 8); | 
					
						
							|  |  |  | 			tab[j][i] = crc; | 
					
						
							| 
									
										
										
										
											2010-05-24 14:33:31 -07:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:25 -07:00
										 |  |  | static void crc32init_le(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	crc32init_le_generic(CRCPOLY_LE, crc32table_le); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void crc32cinit_le(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	crc32init_le_generic(CRC32C_POLY_LE, crc32ctable_le); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * crc32init_be() - allocate and initialize BE table data | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void crc32init_be(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	unsigned i, j; | 
					
						
							|  |  |  | 	uint32_t crc = 0x80000000; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-24 14:33:31 -07:00
										 |  |  | 	crc32table_be[0][0] = 0; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 1; i < BE_TABLE_SIZE; i <<= 1) { | 
					
						
							|  |  |  | 		crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0); | 
					
						
							|  |  |  | 		for (j = 0; j < i; j++) | 
					
						
							| 
									
										
										
										
											2010-05-24 14:33:31 -07:00
										 |  |  | 			crc32table_be[0][i + j] = crc ^ crc32table_be[0][j]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for (i = 0; i < BE_TABLE_SIZE; i++) { | 
					
						
							|  |  |  | 		crc = crc32table_be[0][i]; | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:24 -07:00
										 |  |  | 		for (j = 1; j < BE_TABLE_ROWS; j++) { | 
					
						
							| 
									
										
										
										
											2010-05-24 14:33:31 -07:00
										 |  |  | 			crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8); | 
					
						
							|  |  |  | 			crc32table_be[j][i] = crc; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:24 -07:00
										 |  |  | static void output_table(uint32_t (*table)[256], int rows, int len, char *trans) | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-24 14:33:31 -07:00
										 |  |  | 	int i, j; | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:24 -07:00
										 |  |  | 	for (j = 0 ; j < rows; j++) { | 
					
						
							| 
									
										
										
										
											2010-05-24 14:33:31 -07:00
										 |  |  | 		printf("{"); | 
					
						
							|  |  |  | 		for (i = 0; i < len - 1; i++) { | 
					
						
							|  |  |  | 			if (i % ENTRIES_PER_LINE == 0) | 
					
						
							|  |  |  | 				printf("\n"); | 
					
						
							|  |  |  | 			printf("%s(0x%8.8xL), ", trans, table[j][i]); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int main(int argc, char** argv) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	printf("/* this file is generated - do not edit */\n\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (CRC_LE_BITS > 1) { | 
					
						
							|  |  |  | 		crc32init_le(); | 
					
						
							| 
									
										
										
										
											2012-10-04 17:12:15 -07:00
										 |  |  | 		printf("static u32 __cacheline_aligned " | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:24 -07:00
										 |  |  | 		       "crc32table_le[%d][%d] = {", | 
					
						
							|  |  |  | 		       LE_TABLE_ROWS, LE_TABLE_SIZE); | 
					
						
							|  |  |  | 		output_table(crc32table_le, LE_TABLE_ROWS, | 
					
						
							|  |  |  | 			     LE_TABLE_SIZE, "tole"); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		printf("};\n"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (CRC_BE_BITS > 1) { | 
					
						
							|  |  |  | 		crc32init_be(); | 
					
						
							| 
									
										
										
										
											2012-10-04 17:12:15 -07:00
										 |  |  | 		printf("static u32 __cacheline_aligned " | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:24 -07:00
										 |  |  | 		       "crc32table_be[%d][%d] = {", | 
					
						
							|  |  |  | 		       BE_TABLE_ROWS, BE_TABLE_SIZE); | 
					
						
							|  |  |  | 		output_table(crc32table_be, LE_TABLE_ROWS, | 
					
						
							|  |  |  | 			     BE_TABLE_SIZE, "tobe"); | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 		printf("};\n"); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:25 -07:00
										 |  |  | 	if (CRC_LE_BITS > 1) { | 
					
						
							|  |  |  | 		crc32cinit_le(); | 
					
						
							| 
									
										
										
										
											2012-10-04 17:12:15 -07:00
										 |  |  | 		printf("static u32 __cacheline_aligned " | 
					
						
							| 
									
										
										
										
											2012-03-23 15:02:25 -07:00
										 |  |  | 		       "crc32ctable_le[%d][%d] = {", | 
					
						
							|  |  |  | 		       LE_TABLE_ROWS, LE_TABLE_SIZE); | 
					
						
							|  |  |  | 		output_table(crc32ctable_le, LE_TABLE_ROWS, | 
					
						
							|  |  |  | 			     LE_TABLE_SIZE, "tole"); | 
					
						
							|  |  |  | 		printf("};\n"); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2005-04-16 15:20:36 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } |