xref: /linux/lib/raid6/mktables.c (revision f5e70d0fe3ea990cfb3fc8d7f76a719adcb1e0b5)
1*f5e70d0fSDavid Woodhouse /* -*- linux-c -*- ------------------------------------------------------- *
2*f5e70d0fSDavid Woodhouse  *
3*f5e70d0fSDavid Woodhouse  *   Copyright 2002-2007 H. Peter Anvin - All Rights Reserved
4*f5e70d0fSDavid Woodhouse  *
5*f5e70d0fSDavid Woodhouse  *   This file is part of the Linux kernel, and is made available under
6*f5e70d0fSDavid Woodhouse  *   the terms of the GNU General Public License version 2 or (at your
7*f5e70d0fSDavid Woodhouse  *   option) any later version; incorporated herein by reference.
8*f5e70d0fSDavid Woodhouse  *
9*f5e70d0fSDavid Woodhouse  * ----------------------------------------------------------------------- */
10*f5e70d0fSDavid Woodhouse 
11*f5e70d0fSDavid Woodhouse /*
12*f5e70d0fSDavid Woodhouse  * mktables.c
13*f5e70d0fSDavid Woodhouse  *
14*f5e70d0fSDavid Woodhouse  * Make RAID-6 tables.  This is a host user space program to be run at
15*f5e70d0fSDavid Woodhouse  * compile time.
16*f5e70d0fSDavid Woodhouse  */
17*f5e70d0fSDavid Woodhouse 
18*f5e70d0fSDavid Woodhouse #include <stdio.h>
19*f5e70d0fSDavid Woodhouse #include <string.h>
20*f5e70d0fSDavid Woodhouse #include <inttypes.h>
21*f5e70d0fSDavid Woodhouse #include <stdlib.h>
22*f5e70d0fSDavid Woodhouse #include <time.h>
23*f5e70d0fSDavid Woodhouse 
24*f5e70d0fSDavid Woodhouse static uint8_t gfmul(uint8_t a, uint8_t b)
25*f5e70d0fSDavid Woodhouse {
26*f5e70d0fSDavid Woodhouse 	uint8_t v = 0;
27*f5e70d0fSDavid Woodhouse 
28*f5e70d0fSDavid Woodhouse 	while (b) {
29*f5e70d0fSDavid Woodhouse 		if (b & 1)
30*f5e70d0fSDavid Woodhouse 			v ^= a;
31*f5e70d0fSDavid Woodhouse 		a = (a << 1) ^ (a & 0x80 ? 0x1d : 0);
32*f5e70d0fSDavid Woodhouse 		b >>= 1;
33*f5e70d0fSDavid Woodhouse 	}
34*f5e70d0fSDavid Woodhouse 
35*f5e70d0fSDavid Woodhouse 	return v;
36*f5e70d0fSDavid Woodhouse }
37*f5e70d0fSDavid Woodhouse 
38*f5e70d0fSDavid Woodhouse static uint8_t gfpow(uint8_t a, int b)
39*f5e70d0fSDavid Woodhouse {
40*f5e70d0fSDavid Woodhouse 	uint8_t v = 1;
41*f5e70d0fSDavid Woodhouse 
42*f5e70d0fSDavid Woodhouse 	b %= 255;
43*f5e70d0fSDavid Woodhouse 	if (b < 0)
44*f5e70d0fSDavid Woodhouse 		b += 255;
45*f5e70d0fSDavid Woodhouse 
46*f5e70d0fSDavid Woodhouse 	while (b) {
47*f5e70d0fSDavid Woodhouse 		if (b & 1)
48*f5e70d0fSDavid Woodhouse 			v = gfmul(v, a);
49*f5e70d0fSDavid Woodhouse 		a = gfmul(a, a);
50*f5e70d0fSDavid Woodhouse 		b >>= 1;
51*f5e70d0fSDavid Woodhouse 	}
52*f5e70d0fSDavid Woodhouse 
53*f5e70d0fSDavid Woodhouse 	return v;
54*f5e70d0fSDavid Woodhouse }
55*f5e70d0fSDavid Woodhouse 
56*f5e70d0fSDavid Woodhouse int main(int argc, char *argv[])
57*f5e70d0fSDavid Woodhouse {
58*f5e70d0fSDavid Woodhouse 	int i, j, k;
59*f5e70d0fSDavid Woodhouse 	uint8_t v;
60*f5e70d0fSDavid Woodhouse 	uint8_t exptbl[256], invtbl[256];
61*f5e70d0fSDavid Woodhouse 
62*f5e70d0fSDavid Woodhouse 	printf("#include <linux/raid/pq.h>\n");
63*f5e70d0fSDavid Woodhouse 
64*f5e70d0fSDavid Woodhouse 	/* Compute multiplication table */
65*f5e70d0fSDavid Woodhouse 	printf("\nconst u8  __attribute__((aligned(256)))\n"
66*f5e70d0fSDavid Woodhouse 		"raid6_gfmul[256][256] =\n"
67*f5e70d0fSDavid Woodhouse 		"{\n");
68*f5e70d0fSDavid Woodhouse 	for (i = 0; i < 256; i++) {
69*f5e70d0fSDavid Woodhouse 		printf("\t{\n");
70*f5e70d0fSDavid Woodhouse 		for (j = 0; j < 256; j += 8) {
71*f5e70d0fSDavid Woodhouse 			printf("\t\t");
72*f5e70d0fSDavid Woodhouse 			for (k = 0; k < 8; k++)
73*f5e70d0fSDavid Woodhouse 				printf("0x%02x,%c", gfmul(i, j + k),
74*f5e70d0fSDavid Woodhouse 				       (k == 7) ? '\n' : ' ');
75*f5e70d0fSDavid Woodhouse 		}
76*f5e70d0fSDavid Woodhouse 		printf("\t},\n");
77*f5e70d0fSDavid Woodhouse 	}
78*f5e70d0fSDavid Woodhouse 	printf("};\n");
79*f5e70d0fSDavid Woodhouse 	printf("#ifdef __KERNEL__\n");
80*f5e70d0fSDavid Woodhouse 	printf("EXPORT_SYMBOL(raid6_gfmul);\n");
81*f5e70d0fSDavid Woodhouse 	printf("#endif\n");
82*f5e70d0fSDavid Woodhouse 
83*f5e70d0fSDavid Woodhouse 	/* Compute power-of-2 table (exponent) */
84*f5e70d0fSDavid Woodhouse 	v = 1;
85*f5e70d0fSDavid Woodhouse 	printf("\nconst u8 __attribute__((aligned(256)))\n"
86*f5e70d0fSDavid Woodhouse 	       "raid6_gfexp[256] =\n" "{\n");
87*f5e70d0fSDavid Woodhouse 	for (i = 0; i < 256; i += 8) {
88*f5e70d0fSDavid Woodhouse 		printf("\t");
89*f5e70d0fSDavid Woodhouse 		for (j = 0; j < 8; j++) {
90*f5e70d0fSDavid Woodhouse 			exptbl[i + j] = v;
91*f5e70d0fSDavid Woodhouse 			printf("0x%02x,%c", v, (j == 7) ? '\n' : ' ');
92*f5e70d0fSDavid Woodhouse 			v = gfmul(v, 2);
93*f5e70d0fSDavid Woodhouse 			if (v == 1)
94*f5e70d0fSDavid Woodhouse 				v = 0;	/* For entry 255, not a real entry */
95*f5e70d0fSDavid Woodhouse 		}
96*f5e70d0fSDavid Woodhouse 	}
97*f5e70d0fSDavid Woodhouse 	printf("};\n");
98*f5e70d0fSDavid Woodhouse 	printf("#ifdef __KERNEL__\n");
99*f5e70d0fSDavid Woodhouse 	printf("EXPORT_SYMBOL(raid6_gfexp);\n");
100*f5e70d0fSDavid Woodhouse 	printf("#endif\n");
101*f5e70d0fSDavid Woodhouse 
102*f5e70d0fSDavid Woodhouse 	/* Compute inverse table x^-1 == x^254 */
103*f5e70d0fSDavid Woodhouse 	printf("\nconst u8 __attribute__((aligned(256)))\n"
104*f5e70d0fSDavid Woodhouse 	       "raid6_gfinv[256] =\n" "{\n");
105*f5e70d0fSDavid Woodhouse 	for (i = 0; i < 256; i += 8) {
106*f5e70d0fSDavid Woodhouse 		printf("\t");
107*f5e70d0fSDavid Woodhouse 		for (j = 0; j < 8; j++) {
108*f5e70d0fSDavid Woodhouse 			invtbl[i + j] = v = gfpow(i + j, 254);
109*f5e70d0fSDavid Woodhouse 			printf("0x%02x,%c", v, (j == 7) ? '\n' : ' ');
110*f5e70d0fSDavid Woodhouse 		}
111*f5e70d0fSDavid Woodhouse 	}
112*f5e70d0fSDavid Woodhouse 	printf("};\n");
113*f5e70d0fSDavid Woodhouse 	printf("#ifdef __KERNEL__\n");
114*f5e70d0fSDavid Woodhouse 	printf("EXPORT_SYMBOL(raid6_gfinv);\n");
115*f5e70d0fSDavid Woodhouse 	printf("#endif\n");
116*f5e70d0fSDavid Woodhouse 
117*f5e70d0fSDavid Woodhouse 	/* Compute inv(2^x + 1) (exponent-xor-inverse) table */
118*f5e70d0fSDavid Woodhouse 	printf("\nconst u8 __attribute__((aligned(256)))\n"
119*f5e70d0fSDavid Woodhouse 	       "raid6_gfexi[256] =\n" "{\n");
120*f5e70d0fSDavid Woodhouse 	for (i = 0; i < 256; i += 8) {
121*f5e70d0fSDavid Woodhouse 		printf("\t");
122*f5e70d0fSDavid Woodhouse 		for (j = 0; j < 8; j++)
123*f5e70d0fSDavid Woodhouse 			printf("0x%02x,%c", invtbl[exptbl[i + j] ^ 1],
124*f5e70d0fSDavid Woodhouse 			       (j == 7) ? '\n' : ' ');
125*f5e70d0fSDavid Woodhouse 	}
126*f5e70d0fSDavid Woodhouse 	printf("};\n");
127*f5e70d0fSDavid Woodhouse 	printf("#ifdef __KERNEL__\n");
128*f5e70d0fSDavid Woodhouse 	printf("EXPORT_SYMBOL(raid6_gfexi);\n");
129*f5e70d0fSDavid Woodhouse 	printf("#endif\n");
130*f5e70d0fSDavid Woodhouse 
131*f5e70d0fSDavid Woodhouse 	return 0;
132*f5e70d0fSDavid Woodhouse }
133