Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / md / mktables.c
blobb61d5767aae7c61c3960a09e16c26becb3a1712f
1 /* -*- linux-c -*- ------------------------------------------------------- *
3 * Copyright 2002-2007 H. Peter Anvin - All Rights Reserved
5 * This file is part of the Linux kernel, and is made available under
6 * the terms of the GNU General Public License version 2 or (at your
7 * option) any later version; incorporated herein by reference.
9 * ----------------------------------------------------------------------- */
12 * mktables.c
14 * Make RAID-6 tables. This is a host user space program to be run at
15 * compile time.
18 #include <stdio.h>
19 #include <string.h>
20 #include <inttypes.h>
21 #include <stdlib.h>
22 #include <time.h>
24 static uint8_t gfmul(uint8_t a, uint8_t b)
26 uint8_t v = 0;
28 while (b) {
29 if (b & 1)
30 v ^= a;
31 a = (a << 1) ^ (a & 0x80 ? 0x1d : 0);
32 b >>= 1;
35 return v;
38 static uint8_t gfpow(uint8_t a, int b)
40 uint8_t v = 1;
42 b %= 255;
43 if (b < 0)
44 b += 255;
46 while (b) {
47 if (b & 1)
48 v = gfmul(v, a);
49 a = gfmul(a, a);
50 b >>= 1;
53 return v;
56 int main(int argc, char *argv[])
58 int i, j, k;
59 uint8_t v;
60 uint8_t exptbl[256], invtbl[256];
62 printf("#include \"raid6.h\"\n");
64 /* Compute multiplication table */
65 printf("\nconst u8 __attribute__((aligned(256)))\n"
66 "raid6_gfmul[256][256] =\n"
67 "{\n");
68 for (i = 0; i < 256; i++) {
69 printf("\t{\n");
70 for (j = 0; j < 256; j += 8) {
71 printf("\t\t");
72 for (k = 0; k < 8; k++)
73 printf("0x%02x,%c", gfmul(i, j + k),
74 (k == 7) ? '\n' : ' ');
76 printf("\t},\n");
78 printf("};\n");
80 /* Compute power-of-2 table (exponent) */
81 v = 1;
82 printf("\nconst u8 __attribute__((aligned(256)))\n"
83 "raid6_gfexp[256] =\n" "{\n");
84 for (i = 0; i < 256; i += 8) {
85 printf("\t");
86 for (j = 0; j < 8; j++) {
87 exptbl[i + j] = v;
88 printf("0x%02x,%c", v, (j == 7) ? '\n' : ' ');
89 v = gfmul(v, 2);
90 if (v == 1)
91 v = 0; /* For entry 255, not a real entry */
94 printf("};\n");
96 /* Compute inverse table x^-1 == x^254 */
97 printf("\nconst u8 __attribute__((aligned(256)))\n"
98 "raid6_gfinv[256] =\n" "{\n");
99 for (i = 0; i < 256; i += 8) {
100 printf("\t");
101 for (j = 0; j < 8; j++) {
102 invtbl[i + j] = v = gfpow(i + j, 254);
103 printf("0x%02x,%c", v, (j == 7) ? '\n' : ' ');
106 printf("};\n");
108 /* Compute inv(2^x + 1) (exponent-xor-inverse) table */
109 printf("\nconst u8 __attribute__((aligned(256)))\n"
110 "raid6_gfexi[256] =\n" "{\n");
111 for (i = 0; i < 256; i += 8) {
112 printf("\t");
113 for (j = 0; j < 8; j++)
114 printf("0x%02x,%c", invtbl[exptbl[i + j] ^ 1],
115 (j == 7) ? '\n' : ' ');
117 printf("};\n");
119 return 0;