mktables.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 power-of-2 table (exponent) */
  72. v = 1;
  73. printf("\nconst u8 __attribute__((aligned(256)))\n"
  74. "raid6_gfexp[256] =\n" "{\n");
  75. for (i = 0; i < 256; i += 8) {
  76. printf("\t");
  77. for (j = 0; j < 8; j++) {
  78. exptbl[i + j] = v;
  79. printf("0x%02x,%c", v, (j == 7) ? '\n' : ' ');
  80. v = gfmul(v, 2);
  81. if (v == 1)
  82. v = 0; /* For entry 255, not a real entry */
  83. }
  84. }
  85. printf("};\n");
  86. printf("#ifdef __KERNEL__\n");
  87. printf("EXPORT_SYMBOL(raid6_gfexp);\n");
  88. printf("#endif\n");
  89. /* Compute inverse table x^-1 == x^254 */
  90. printf("\nconst u8 __attribute__((aligned(256)))\n"
  91. "raid6_gfinv[256] =\n" "{\n");
  92. for (i = 0; i < 256; i += 8) {
  93. printf("\t");
  94. for (j = 0; j < 8; j++) {
  95. invtbl[i + j] = v = gfpow(i + j, 254);
  96. printf("0x%02x,%c", v, (j == 7) ? '\n' : ' ');
  97. }
  98. }
  99. printf("};\n");
  100. printf("#ifdef __KERNEL__\n");
  101. printf("EXPORT_SYMBOL(raid6_gfinv);\n");
  102. printf("#endif\n");
  103. /* Compute inv(2^x + 1) (exponent-xor-inverse) table */
  104. printf("\nconst u8 __attribute__((aligned(256)))\n"
  105. "raid6_gfexi[256] =\n" "{\n");
  106. for (i = 0; i < 256; i += 8) {
  107. printf("\t");
  108. for (j = 0; j < 8; j++)
  109. printf("0x%02x,%c", invtbl[exptbl[i + j] ^ 1],
  110. (j == 7) ? '\n' : ' ');
  111. }
  112. printf("};\n");
  113. printf("#ifdef __KERNEL__\n");
  114. printf("EXPORT_SYMBOL(raid6_gfexi);\n");
  115. printf("#endif\n");
  116. return 0;
  117. }