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. if (pad < 0 || pad >= nn)
  41. return -ERANGE;
  42. /* Does the caller provide the syndrome ? */
  43. if (s != NULL)
  44. goto decode;
  45. /* form the syndromes; i.e., evaluate data(x) at roots of
  46. * g(x) */
  47. for (i = 0; i < nroots; i++)
  48. syn[i] = (((uint16_t) data[0]) ^ invmsk) & msk;
  49. for (j = 1; j < len; j++) {
  50. for (i = 0; i < nroots; i++) {
  51. if (syn[i] == 0) {
  52. syn[i] = (((uint16_t) data[j]) ^
  53. invmsk) & msk;
  54. } else {
  55. syn[i] = ((((uint16_t) data[j]) ^
  56. invmsk) & msk) ^
  57. alpha_to[rs_modnn(rs, index_of[syn[i]] +
  58. (fcr + i) * prim)];
  59. }
  60. }
  61. }
  62. for (j = 0; j < nroots; j++) {
  63. for (i = 0; i < nroots; i++) {
  64. if (syn[i] == 0) {
  65. syn[i] = ((uint16_t) par[j]) & msk;
  66. } else {
  67. syn[i] = (((uint16_t) par[j]) & msk) ^
  68. alpha_to[rs_modnn(rs, index_of[syn[i]] +
  69. (fcr+i)*prim)];
  70. }
  71. }
  72. }
  73. s = syn;
  74. /* Convert syndromes to index form, checking for nonzero condition */
  75. syn_error = 0;
  76. for (i = 0; i < nroots; i++) {
  77. syn_error |= s[i];
  78. s[i] = index_of[s[i]];
  79. }
  80. if (!syn_error) {
  81. /* if syndrome is zero, data[] is a codeword and there are no
  82. * errors to correct. So return data[] unmodified
  83. */
  84. count = 0;
  85. goto finish;
  86. }
  87. decode:
  88. memset(&lambda[1], 0, nroots * sizeof(lambda[0]));
  89. lambda[0] = 1;
  90. if (no_eras > 0) {
  91. /* Init lambda to be the erasure locator polynomial */
  92. lambda[1] = alpha_to[rs_modnn(rs,
  93. prim * (nn - 1 - eras_pos[0]))];
  94. for (i = 1; i < no_eras; i++) {
  95. u = rs_modnn(rs, prim * (nn - 1 - eras_pos[i]));
  96. for (j = i + 1; j > 0; j--) {
  97. tmp = index_of[lambda[j - 1]];
  98. if (tmp != nn) {
  99. lambda[j] ^=
  100. alpha_to[rs_modnn(rs, u + tmp)];
  101. }
  102. }
  103. }
  104. }
  105. for (i = 0; i < nroots + 1; i++)
  106. b[i] = index_of[lambda[i]];
  107. /*
  108. * Begin Berlekamp-Massey algorithm to determine error+erasure
  109. * locator polynomial
  110. */
  111. r = no_eras;
  112. el = no_eras;
  113. while (++r <= nroots) { /* r is the step number */
  114. /* Compute discrepancy at the r-th step in poly-form */
  115. discr_r = 0;
  116. for (i = 0; i < r; i++) {
  117. if ((lambda[i] != 0) && (s[r - i - 1] != nn)) {
  118. discr_r ^=
  119. alpha_to[rs_modnn(rs,
  120. index_of[lambda[i]] +
  121. s[r - i - 1])];
  122. }
  123. }
  124. discr_r = index_of[discr_r]; /* Index form */
  125. if (discr_r == nn) {
  126. /* 2 lines below: B(x) <-- x*B(x) */
  127. memmove (&b[1], b, nroots * sizeof (b[0]));
  128. b[0] = nn;
  129. } else {
  130. /* 7 lines below: T(x) <-- lambda(x)-discr_r*x*b(x) */
  131. t[0] = lambda[0];
  132. for (i = 0; i < nroots; i++) {
  133. if (b[i] != nn) {
  134. t[i + 1] = lambda[i + 1] ^
  135. alpha_to[rs_modnn(rs, discr_r +
  136. b[i])];
  137. } else
  138. t[i + 1] = lambda[i + 1];
  139. }
  140. if (2 * el <= r + no_eras - 1) {
  141. el = r + no_eras - el;
  142. /*
  143. * 2 lines below: B(x) <-- inv(discr_r) *
  144. * lambda(x)
  145. */
  146. for (i = 0; i <= nroots; i++) {
  147. b[i] = (lambda[i] == 0) ? nn :
  148. rs_modnn(rs, index_of[lambda[i]]
  149. - discr_r + nn);
  150. }
  151. } else {
  152. /* 2 lines below: B(x) <-- x*B(x) */
  153. memmove(&b[1], b, nroots * sizeof(b[0]));
  154. b[0] = nn;
  155. }
  156. memcpy(lambda, t, (nroots + 1) * sizeof(t[0]));
  157. }
  158. }
  159. /* Convert lambda to index form and compute deg(lambda(x)) */
  160. deg_lambda = 0;
  161. for (i = 0; i < nroots + 1; i++) {
  162. lambda[i] = index_of[lambda[i]];
  163. if (lambda[i] != nn)
  164. deg_lambda = i;
  165. }
  166. /* Find roots of error+erasure locator polynomial by Chien search */
  167. memcpy(&reg[1], &lambda[1], nroots * sizeof(reg[0]));
  168. count = 0; /* Number of roots of lambda(x) */
  169. for (i = 1, k = iprim - 1; i <= nn; i++, k = rs_modnn(rs, k + iprim)) {
  170. q = 1; /* lambda[0] is always 0 */
  171. for (j = deg_lambda; j > 0; j--) {
  172. if (reg[j] != nn) {
  173. reg[j] = rs_modnn(rs, reg[j] + j);
  174. q ^= alpha_to[reg[j]];
  175. }
  176. }
  177. if (q != 0)
  178. continue; /* Not a root */
  179. /* store root (index-form) and error location number */
  180. root[count] = i;
  181. loc[count] = k;
  182. /* If we've already found max possible roots,
  183. * abort the search to save time
  184. */
  185. if (++count == deg_lambda)
  186. break;
  187. }
  188. if (deg_lambda != count) {
  189. /*
  190. * deg(lambda) unequal to number of roots => uncorrectable
  191. * error detected
  192. */
  193. count = -1;
  194. goto finish;
  195. }
  196. /*
  197. * Compute err+eras evaluator poly omega(x) = s(x)*lambda(x) (modulo
  198. * x**nroots). in index form. Also find deg(omega).
  199. */
  200. deg_omega = deg_lambda - 1;
  201. for (i = 0; i <= deg_omega; i++) {
  202. tmp = 0;
  203. for (j = i; j >= 0; j--) {
  204. if ((s[i - j] != nn) && (lambda[j] != nn))
  205. tmp ^=
  206. alpha_to[rs_modnn(rs, s[i - j] + lambda[j])];
  207. }
  208. omega[i] = index_of[tmp];
  209. }
  210. /*
  211. * Compute error values in poly-form. num1 = omega(inv(X(l))), num2 =
  212. * inv(X(l))**(fcr-1) and den = lambda_pr(inv(X(l))) all in poly-form
  213. */
  214. for (j = count - 1; j >= 0; j--) {
  215. num1 = 0;
  216. for (i = deg_omega; i >= 0; i--) {
  217. if (omega[i] != nn)
  218. num1 ^= alpha_to[rs_modnn(rs, omega[i] +
  219. i * root[j])];
  220. }
  221. num2 = alpha_to[rs_modnn(rs, root[j] * (fcr - 1) + nn)];
  222. den = 0;
  223. /* lambda[i+1] for i even is the formal derivative
  224. * lambda_pr of lambda[i] */
  225. for (i = min(deg_lambda, nroots - 1) & ~1; i >= 0; i -= 2) {
  226. if (lambda[i + 1] != nn) {
  227. den ^= alpha_to[rs_modnn(rs, lambda[i + 1] +
  228. i * root[j])];
  229. }
  230. }
  231. /* Apply error to data */
  232. if (num1 != 0 && loc[j] >= pad) {
  233. uint16_t cor = alpha_to[rs_modnn(rs,index_of[num1] +
  234. index_of[num2] +
  235. nn - index_of[den])];
  236. /* Store the error correction pattern, if a
  237. * correction buffer is available */
  238. if (corr) {
  239. corr[j] = cor;
  240. } else {
  241. /* If a data buffer is given and the
  242. * error is inside the message,
  243. * correct it */
  244. if (data && (loc[j] < (nn - nroots)))
  245. data[loc[j] - pad] ^= cor;
  246. }
  247. }
  248. }
  249. finish:
  250. if (eras_pos != NULL) {
  251. for (i = 0; i < count; i++)
  252. eras_pos[i] = loc[i] - pad;
  253. }
  254. return count;
  255. }