Btrfs progs v4.17.1
[btrfs-progs-unstable/devel.git] / kernel-lib / mktables.c
blob85f621fe9156c020f7f088a9c9eb5d2b58a6cb25
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.
19 * Btrfs-progs port, with following minor fixes:
20 * 1) Use "kerncompat.h"
21 * 2) Get rid of __KERNEL__ related macros
24 #include <stdio.h>
25 #include <string.h>
26 #include <inttypes.h>
27 #include <stdlib.h>
28 #include <time.h>
30 static uint8_t gfmul(uint8_t a, uint8_t b)
32 uint8_t v = 0;
34 while (b) {
35 if (b & 1)
36 v ^= a;
37 a = (a << 1) ^ (a & 0x80 ? 0x1d : 0);
38 b >>= 1;
41 return v;
44 static uint8_t gfpow(uint8_t a, int b)
46 uint8_t v = 1;
48 b %= 255;
49 if (b < 0)
50 b += 255;
52 while (b) {
53 if (b & 1)
54 v = gfmul(v, a);
55 a = gfmul(a, a);
56 b >>= 1;
59 return v;
62 int main(int argc, char *argv[])
64 int i, j, k;
65 uint8_t v;
66 uint8_t exptbl[256], invtbl[256];
68 printf("#include \"kerncompat.h\"\n");
70 /* Compute multiplication table */
71 printf("\nconst u8 __attribute__((aligned(256)))\n"
72 "raid6_gfmul[256][256] =\n"
73 "{\n");
74 for (i = 0; i < 256; i++) {
75 printf("\t{\n");
76 for (j = 0; j < 256; j += 8) {
77 printf("\t\t");
78 for (k = 0; k < 8; k++)
79 printf("0x%02x,%c", gfmul(i, j + k),
80 (k == 7) ? '\n' : ' ');
82 printf("\t},\n");
84 printf("};\n");
86 /* Compute vector multiplication table */
87 printf("\nconst u8 __attribute__((aligned(256)))\n"
88 "raid6_vgfmul[256][32] =\n"
89 "{\n");
90 for (i = 0; i < 256; i++) {
91 printf("\t{\n");
92 for (j = 0; j < 16; j += 8) {
93 printf("\t\t");
94 for (k = 0; k < 8; k++)
95 printf("0x%02x,%c", gfmul(i, j + k),
96 (k == 7) ? '\n' : ' ');
98 for (j = 0; j < 16; j += 8) {
99 printf("\t\t");
100 for (k = 0; k < 8; k++)
101 printf("0x%02x,%c", gfmul(i, (j + k) << 4),
102 (k == 7) ? '\n' : ' ');
104 printf("\t},\n");
106 printf("};\n");
108 /* Compute power-of-2 table (exponent) */
109 v = 1;
110 printf("\nconst u8 __attribute__((aligned(256)))\n"
111 "raid6_gfexp[256] =\n" "{\n");
112 for (i = 0; i < 256; i += 8) {
113 printf("\t");
114 for (j = 0; j < 8; j++) {
115 exptbl[i + j] = v;
116 printf("0x%02x,%c", v, (j == 7) ? '\n' : ' ');
117 v = gfmul(v, 2);
118 if (v == 1)
119 v = 0; /* For entry 255, not a real entry */
122 printf("};\n");
124 /* Compute inverse table x^-1 == x^254 */
125 printf("\nconst u8 __attribute__((aligned(256)))\n"
126 "raid6_gfinv[256] =\n" "{\n");
127 for (i = 0; i < 256; i += 8) {
128 printf("\t");
129 for (j = 0; j < 8; j++) {
130 invtbl[i + j] = v = gfpow(i + j, 254);
131 printf("0x%02x,%c", v, (j == 7) ? '\n' : ' ');
134 printf("};\n");
136 /* Compute inv(2^x + 1) (exponent-xor-inverse) table */
137 printf("\nconst u8 __attribute__((aligned(256)))\n"
138 "raid6_gfexi[256] =\n" "{\n");
139 for (i = 0; i < 256; i += 8) {
140 printf("\t");
141 for (j = 0; j < 8; j++)
142 printf("0x%02x,%c", invtbl[exptbl[i + j] ^ 1],
143 (j == 7) ? '\n' : ' ');
145 printf("};\n");
147 return 0;