mktables.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #ident "$Id: mktables.c,v 1.2 2002/12/12 22:41:27 hpa Exp $"
  2. /* ----------------------------------------------------------------------- *
  3. *
  4. * Copyright 2002 H. Peter Anvin - All Rights Reserved
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
  9. * Bostom MA 02111-1307, USA; either version 2 of the License, or
  10. * (at your option) any later version; incorporated herein by reference.
  11. *
  12. * ----------------------------------------------------------------------- */
  13. /*
  14. * mktables.c
  15. *
  16. * Make RAID-6 tables. This is a host user space program to be run at
  17. * compile time.
  18. */
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <inttypes.h>
  22. #include <stdlib.h>
  23. #include <time.h>
  24. static uint8_t gfmul(uint8_t a, uint8_t b)
  25. {
  26. uint8_t v = 0;
  27. while ( b ) {
  28. if ( b & 1 ) v ^= a;
  29. a = (a << 1) ^ (a & 0x80 ? 0x1d : 0);
  30. b >>= 1;
  31. }
  32. return v;
  33. }
  34. static uint8_t gfpow(uint8_t a, int b)
  35. {
  36. uint8_t v = 1;
  37. b %= 255;
  38. if ( b < 0 )
  39. b += 255;
  40. while ( b ) {
  41. if ( b & 1 ) v = gfmul(v,a);
  42. a = gfmul(a,a);
  43. b >>= 1;
  44. }
  45. return v;
  46. }
  47. int main(int argc, char *argv[])
  48. {
  49. int i, j, k;
  50. uint8_t v;
  51. uint8_t exptbl[256], invtbl[256];
  52. printf("#include \"raid6.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, ", gfmul(i,j+k));
  63. }
  64. printf("\n");
  65. }
  66. printf("\t},\n");
  67. }
  68. printf("};\n");
  69. /* Compute power-of-2 table (exponent) */
  70. v = 1;
  71. printf("\nconst u8 __attribute__((aligned(256)))\n"
  72. "raid6_gfexp[256] =\n"
  73. "{\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, ", v);
  79. v = gfmul(v,2);
  80. if ( v == 1 ) v = 0; /* For entry 255, not a real entry */
  81. }
  82. printf("\n");
  83. }
  84. printf("};\n");
  85. /* Compute inverse table x^-1 == x^254 */
  86. printf("\nconst u8 __attribute__((aligned(256)))\n"
  87. "raid6_gfinv[256] =\n"
  88. "{\n");
  89. for ( i = 0 ; i < 256 ; i += 8 ) {
  90. printf("\t");
  91. for ( j = 0 ; j < 8 ; j++ ) {
  92. invtbl[i+j] = v = gfpow(i+j,254);
  93. printf("0x%02x, ", v);
  94. }
  95. printf("\n");
  96. }
  97. printf("};\n");
  98. /* Compute inv(2^x + 1) (exponent-xor-inverse) table */
  99. printf("\nconst u8 __attribute__((aligned(256)))\n"
  100. "raid6_gfexi[256] =\n"
  101. "{\n");
  102. for ( i = 0 ; i < 256 ; i += 8 ) {
  103. printf("\t");
  104. for ( j = 0 ; j < 8 ; j++ ) {
  105. printf("0x%02x, ", invtbl[exptbl[i+j]^1]);
  106. }
  107. printf("\n");
  108. }
  109. printf("};\n\n");
  110. return 0;
  111. }