mktables.c 3.6 KB

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