decode_rs.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * lib/reed_solomon/decode_rs.c
  3. *
  4. * Overview:
  5. * Generic Reed Solomon encoder / decoder library
  6. *
  7. * Copyright 2002, Phil Karn, KA9Q
  8. * May be used under the terms of the GNU General Public License (GPL)
  9. *
  10. * Adaption to the kernel by Thomas Gleixner (tglx@linutronix.de)
  11. *
  12. * $Id: decode_rs.c,v 1.7 2005/11/07 11:14:59 gleixner Exp $
  13. *
  14. */
  15. /* Generic data width independent code which is included by the
  16. * wrappers.
  17. */
  18. {
  19. int deg_lambda, el, deg_omega;
  20. int i, j, r, k, pad;
  21. int nn = rs->nn;
  22. int nroots = rs->nroots;
  23. int fcr = rs->fcr;
  24. int prim = rs->prim;
  25. int iprim = rs->iprim;
  26. uint16_t *alpha_to = rs->alpha_to;
  27. uint16_t *index_of = rs->index_of;
  28. uint16_t u, q, tmp, num1, num2, den, discr_r, syn_error;
  29. /* Err+Eras Locator poly and syndrome poly The maximum value
  30. * of nroots is 8. So the necessary stack size will be about
  31. * 220 bytes max.
  32. */
  33. uint16_t lambda[nroots + 1], syn[nroots];
  34. uint16_t b[nroots + 1], t[nroots + 1], omega[nroots + 1];
  35. uint16_t root[nroots], reg[nroots + 1], loc[nroots];
  36. int count = 0;
  37. uint16_t msk = (uint16_t) rs->nn;
  38. /* Check length parameter for validity */
  39. pad = nn - nroots - len;
  40. BUG_ON(pad < 0 || pad >= nn);
  41. /* Does the caller provide the syndrome ? */
  42. if (s != NULL)
  43. goto decode;
  44. /* form the syndromes; i.e., evaluate data(x) at roots of
  45. * g(x) */
  46. for (i = 0; i < nroots; i++)
  47. syn[i] = (((uint16_t) data[0]) ^ invmsk) & msk;
  48. for (j = 1; j < len; j++) {
  49. for (i = 0; i < nroots; i++) {
  50. if (syn[i] == 0) {
  51. syn[i] = (((uint16_t) data[j]) ^
  52. invmsk) & msk;
  53. } else {
  54. syn[i] = ((((uint16_t) data[j]) ^
  55. invmsk) & msk) ^
  56. alpha_to[rs_modnn(rs, index_of[syn[i]] +
  57. (fcr + i) * prim)];
  58. }
  59. }
  60. }
  61. for (j = 0; j < nroots; j++) {
  62. for (i = 0; i < nroots; i++) {
  63. if (syn[i] == 0) {
  64. syn[i] = ((uint16_t) par[j]) & msk;
  65. } else {
  66. syn[i] = (((uint16_t) par[j]) & msk) ^
  67. alpha_to[rs_modnn(rs, index_of[syn[i]] +
  68. (fcr+i)*prim)];
  69. }
  70. }
  71. }
  72. s = syn;
  73. /* Convert syndromes to index form, checking for nonzero condition */
  74. syn_error = 0;
  75. for (i = 0; i < nroots; i++) {
  76. syn_error |= s[i];
  77. s[i] = index_of[s[i]];
  78. }
  79. if (!syn_error) {
  80. /* if syndrome is zero, data[] is a codeword and there are no
  81. * errors to correct. So return data[] unmodified
  82. */
  83. count = 0;
  84. goto finish;
  85. }
  86. decode:
  87. memset(&lambda[1], 0, nroots * sizeof(lambda[0]));
  88. lambda[0] = 1;
  89. if (no_eras > 0) {
  90. /* Init lambda to be the erasure locator polynomial */
  91. lambda[1] = alpha_to[rs_modnn(rs,
  92. prim * (nn - 1 - eras_pos[0]))];
  93. for (i = 1; i < no_eras; i++) {
  94. u = rs_modnn(rs, prim * (nn - 1 - eras_pos[i]));
  95. for (j = i + 1; j > 0; j--) {
  96. tmp = index_of[lambda[j - 1]];
  97. if (tmp != nn) {
  98. lambda[j] ^=
  99. alpha_to[rs_modnn(rs, u + tmp)];
  100. }
  101. }
  102. }
  103. }
  104. for (i = 0; i < nroots + 1; i++)
  105. b[i] = index_of[lambda[i]];
  106. /*
  107. * Begin Berlekamp-Massey algorithm to determine error+erasure
  108. * locator polynomial
  109. */
  110. r = no_eras;
  111. el = no_eras;
  112. while (++r <= nroots) { /* r is the step number */
  113. /* Compute discrepancy at the r-th step in poly-form */
  114. discr_r = 0;
  115. for (i = 0; i < r; i++) {
  116. if ((lambda[i] != 0) && (s[r - i - 1] != nn)) {
  117. discr_r ^=
  118. alpha_to[rs_modnn(rs,
  119. index_of[lambda[i]] +
  120. s[r - i - 1])];
  121. }
  122. }
  123. discr_r = index_of[discr_r]; /* Index form */
  124. if (discr_r == nn) {
  125. /* 2 lines below: B(x) <-- x*B(x) */
  126. memmove (&b[1], b, nroots * sizeof (b[0]));
  127. b[0] = nn;
  128. } else {
  129. /* 7 lines below: T(x) <-- lambda(x)-discr_r*x*b(x) */
  130. t[0] = lambda[0];
  131. for (i = 0; i < nroots; i++) {
  132. if (b[i] != nn) {
  133. t[i + 1] = lambda[i + 1] ^
  134. alpha_to[rs_modnn(rs, discr_r +
  135. b[i])];
  136. } else
  137. t[i + 1] = lambda[i + 1];
  138. }
  139. if (2 * el <= r + no_eras - 1) {
  140. el = r + no_eras - el;
  141. /*
  142. * 2 lines below: B(x) <-- inv(discr_r) *
  143. * lambda(x)
  144. */
  145. for (i = 0; i <= nroots; i++) {
  146. b[i] = (lambda[i] == 0) ? nn :
  147. rs_modnn(rs, index_of[lambda[i]]
  148. - discr_r + nn);
  149. }
  150. } else {
  151. /* 2 lines below: B(x) <-- x*B(x) */
  152. memmove(&b[1], b, nroots * sizeof(b[0]));
  153. b[0] = nn;
  154. }
  155. memcpy(lambda, t, (nroots + 1) * sizeof(t[0]));
  156. }
  157. }
  158. /* Convert lambda to index form and compute deg(lambda(x)) */
  159. deg_lambda = 0;
  160. for (i = 0; i < nroots + 1; i++) {
  161. lambda[i] = index_of[lambda[i]];
  162. if (lambda[i] != nn)
  163. deg_lambda = i;
  164. }
  165. /* Find roots of error+erasure locator polynomial by Chien search */
  166. memcpy(&reg[1], &lambda[1], nroots * sizeof(reg[0]));
  167. count = 0; /* Number of roots of lambda(x) */
  168. for (i = 1, k = iprim - 1; i <= nn; i++, k = rs_modnn(rs, k + iprim)) {
  169. q = 1; /* lambda[0] is always 0 */
  170. for (j = deg_lambda; j > 0; j--) {
  171. if (reg[j] != nn) {
  172. reg[j] = rs_modnn(rs, reg[j] + j);
  173. q ^= alpha_to[reg[j]];
  174. }
  175. }
  176. if (q != 0)
  177. continue; /* Not a root */
  178. /* store root (index-form) and error location number */
  179. root[count] = i;
  180. loc[count] = k;
  181. /* If we've already found max possible roots,
  182. * abort the search to save time
  183. */
  184. if (++count == deg_lambda)
  185. break;
  186. }
  187. if (deg_lambda != count) {
  188. /*
  189. * deg(lambda) unequal to number of roots => uncorrectable
  190. * error detected
  191. */
  192. count = -EBADMSG;
  193. goto finish;
  194. }
  195. /*
  196. * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
  197. * x**nroots). in index form. Also find deg(omega).
  198. */
  199. deg_omega = deg_lambda - 1;
  200. for (i = 0; i <= deg_omega; i++) {
  201. tmp = 0;
  202. for (j = i; j >= 0; j--) {
  203. if ((s[i - j] != nn) && (lambda[j] != nn))
  204. tmp ^=
  205. alpha_to[rs_modnn(rs, s[i - j] + lambda[j])];
  206. }
  207. omega[i] = index_of[tmp];
  208. }
  209. /*
  210. * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
  211. * inv(X(l))**(fcr-1) and den = lambda_pr(inv(X(l))) all in poly-form
  212. */
  213. for (j = count - 1; j >= 0; j--) {
  214. num1 = 0;
  215. for (i = deg_omega; i >= 0; i--) {
  216. if (omega[i] != nn)
  217. num1 ^= alpha_to[rs_modnn(rs, omega[i] +
  218. i * root[j])];
  219. }
  220. num2 = alpha_to[rs_modnn(rs, root[j] * (fcr - 1) + nn)];
  221. den = 0;
  222. /* lambda[i+1] for i even is the formal derivative
  223. * lambda_pr of lambda[i] */
  224. for (i = min(deg_lambda, nroots - 1) & ~1; i >= 0; i -= 2) {
  225. if (lambda[i + 1] != nn) {
  226. den ^= alpha_to[rs_modnn(rs, lambda[i + 1] +
  227. i * root[j])];
  228. }
  229. }
  230. /* Apply error to data */
  231. if (num1 != 0 && loc[j] >= pad) {
  232. uint16_t cor = alpha_to[rs_modnn(rs,index_of[num1] +
  233. index_of[num2] +
  234. nn - index_of[den])];
  235. /* Store the error correction pattern, if a
  236. * correction buffer is available */
  237. if (corr) {
  238. corr[j] = cor;
  239. } else {
  240. /* If a data buffer is given and the
  241. * error is inside the message,
  242. * correct it */
  243. if (data && (loc[j] < (nn - nroots)))
  244. data[loc[j] - pad] ^= cor;
  245. }
  246. }
  247. }
  248. finish:
  249. if (eras_pos != NULL) {
  250. for (i = 0; i < count; i++)
  251. eras_pos[i] = loc[i] - pad;
  252. }
  253. return count;
  254. }