Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
4.7 KiB

  1. /* This program generates several tables used in the FEC algorithms
  2. (this saves a little code space and time over generating them at
  3. runtime)
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #define EXTERN
  8. #include "tabdecls.h"
  9. /* Generate a table of 2^i for 0<=i<=511 (where multiplications are
  10. done in the Galois field) */
  11. void generate_galois_exp() {
  12. int i;
  13. int current = 1;
  14. for (i = 0; i < 512; i++) {
  15. galois_exp[i] = current;
  16. current <<= 1;
  17. if (current & 0x100) {
  18. current ^= 0x11d;
  19. }
  20. }
  21. for (i = 512; i <= 1024; i++) {
  22. galois_exp[i] = 0;
  23. }
  24. }
  25. /* Generate a table of log_2(i) for 1<=i<=255
  26. for 1<=i<=255: galois_exp[galois_log[i]] = i
  27. for 0<=i<=254: galois_log[galois_exp[i]] = i
  28. for 1<=a,b<=255: galois_exp[galois_log[a]+galois_log[b]]
  29. is the galois field product of a and b
  30. */
  31. void generate_galois_log() {
  32. int i;
  33. /* We can't take the log of 0; let's give it a value which is not legal
  34. in this log table. (This out-of-range value will push galois_exp
  35. into the region of the table containing 0's.) */
  36. galois_log[0] = 512;
  37. for (i = 0; i <= 254; i++) {
  38. galois_log[galois_exp[i]] = i;
  39. }
  40. }
  41. /* We can save an array lookup in some inner loops by keeping
  42. the logarithm of the Norpak coefficients */
  43. void generate_lcoeffs() {
  44. int i, j;
  45. for (i = 0; i < 2; i++) {
  46. for (j = 0; j < 26; j++) {
  47. log_norpak_coeffs[i][j] = galois_log[norpak_coeffs[i][j]];
  48. }
  49. }
  50. }
  51. /* This is computing the function P() from my FEC document; it is
  52. used for locating single-byte FEC errors */
  53. void generate_norpak_delta_inv() {
  54. int i;
  55. for (i = 0; i < 256; i++) {
  56. norpak_delta_inv[i] = 255;
  57. }
  58. for (i = 0; i < 26; i++) {
  59. norpak_delta_inv[(log_norpak_coeffs[0][i] + 255 - log_norpak_coeffs[1][i])%255] = i;
  60. }
  61. }
  62. /* For each byte, figure out what hamming value this byte should decode
  63. to (if multiple values are equally close, use an error value of 0xff) */
  64. void generate_decode_hamming() {
  65. int val;
  66. for (val = 0; val < 256; val++) {
  67. int b1 = !!(val&1);
  68. int b2 = !!(val&2);
  69. int b3 = !!(val&4);
  70. int b4 = !!(val&8);
  71. int b5 = !!(val&16);
  72. int b6 = !!(val&32);
  73. int b7 = !!(val&64);
  74. int b8 = !!(val&128);
  75. int a = b8^b6^b2^b1;
  76. int b = b8^b4^b3^b2;
  77. int c = b6^b5^b4^b2;
  78. int d = b8^b7^b6^b5^b4^b3^b2^b1;
  79. if (a == 1 && b == 1 && c == 1 && d == 1) {
  80. decode_hamming_tab[val] = (b8<<3 | b6<<2 | b4<<1 | b2);
  81. } else if (d == 1) {
  82. /* multiple errors */
  83. decode_hamming_tab[val] = 0xff;
  84. } else {
  85. switch (a << 2 | b << 1 | c) {
  86. case 1:
  87. b8 ^= 1;
  88. break;
  89. case 7:
  90. b7 ^= 1;
  91. break;
  92. case 2:
  93. b6 ^= 1;
  94. break;
  95. case 6:
  96. b5 ^= 1;
  97. break;
  98. case 4:
  99. b4 ^= 1;
  100. break;
  101. case 5:
  102. b3 ^= 1;
  103. break;
  104. case 0:
  105. b2 ^= 1;
  106. break;
  107. case 3:
  108. b1 ^= 1;
  109. break;
  110. }
  111. decode_hamming_tab[val] = (b8<<3 | b6<<2 | b4<<1 | b2);
  112. }
  113. }
  114. }
  115. void print_char_table_body(FILE *out, unsigned char *table, int len) {
  116. int i;
  117. fprintf(out, "{\n");
  118. for (i = 0; i < len; i++) {
  119. fprintf(out, " 0x%02x,\n", table[i]);
  120. }
  121. fprintf(out, "}");
  122. }
  123. void print_char_table(FILE *out, const char *name,
  124. unsigned char *table, int len) {
  125. fprintf(out, "unsigned char %s[%d] = ", name, len);
  126. print_char_table_body(out, table, len);
  127. fprintf(out, ";\n\n");
  128. }
  129. void print_short_table_body(FILE *out, unsigned short *table, int len) {
  130. int i;
  131. fprintf(out, "{\n");
  132. for (i = 0; i < len; i++) {
  133. fprintf(out, " 0x%04x,\n", table[i]);
  134. }
  135. fprintf(out, "}");
  136. }
  137. void print_short_table(FILE *out, const char *name,
  138. unsigned short *table, int len) {
  139. fprintf(out, "unsigned short %s[%d] = ", name, len);
  140. print_short_table_body(out, table, len);
  141. fprintf(out, ";\n\n");
  142. }
  143. void print_tables() {
  144. FILE *out;
  145. int i;
  146. out = fopen("tables.c", "w");
  147. if (!out) {
  148. fprintf(stderr, "Failed to open 'tables.c'\n");
  149. exit(1);
  150. }
  151. fprintf(out, "\
  152. /* Warning: This code was automatically generated (by gentabs.c).\n\
  153. DO NOT EDIT! */\n\
  154. #include \"tabdecls.h\"\n\
  155. \n");
  156. print_char_table(out, "galois_exp", galois_exp, 1025);
  157. print_short_table(out, "galois_log", galois_log, 256);
  158. print_char_table(out, "norpak_delta_inv", norpak_delta_inv, 256);
  159. print_char_table(out, "decode_hamming_tab", decode_hamming_tab, 256);
  160. fprintf(out, "\
  161. unsigned char log_norpak_coeffs[2][26] = {\n");
  162. for (i = 0; i < 2; i++) {
  163. print_char_table_body(out, log_norpak_coeffs[i], 26);
  164. fprintf(out, ",\n");
  165. }
  166. fprintf(out, "};\n\n");
  167. }
  168. int main(int argc, char **argv) {
  169. generate_galois_exp();
  170. generate_galois_log();
  171. generate_lcoeffs();
  172. generate_norpak_delta_inv();
  173. generate_decode_hamming();
  174. print_tables();
  175. return 0;
  176. }