mktables.c 2.9 KB

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