1b1928704201034c785a26296a49f69355eb56a05Nick Lewycky/* The guts of the Reed-Solomon encoder, meant to be #included
2b1928704201034c785a26296a49f69355eb56a05Nick Lewycky * into a function body with the following typedefs, macros and variables supplied
3b1928704201034c785a26296a49f69355eb56a05Nick Lewycky * according to the code parameters:
4b1928704201034c785a26296a49f69355eb56a05Nick Lewycky
5b1928704201034c785a26296a49f69355eb56a05Nick Lewycky * data_t - a typedef for the data symbol
6b1928704201034c785a26296a49f69355eb56a05Nick Lewycky * data_t data[] - array of NN-NROOTS-PAD and type data_t to be encoded
7b1928704201034c785a26296a49f69355eb56a05Nick Lewycky * data_t parity[] - an array of NROOTS and type data_t to be written with parity symbols
8b1928704201034c785a26296a49f69355eb56a05Nick Lewycky * NROOTS - the number of roots in the RS code generator polynomial,
9b1928704201034c785a26296a49f69355eb56a05Nick Lewycky *          which is the same as the number of parity symbols in a block.
10b1928704201034c785a26296a49f69355eb56a05Nick Lewycky            Integer variable or literal.
11b1928704201034c785a26296a49f69355eb56a05Nick Lewycky	    *
12b1928704201034c785a26296a49f69355eb56a05Nick Lewycky * NN - the total number of symbols in a RS block. Integer variable or literal.
13b1928704201034c785a26296a49f69355eb56a05Nick Lewycky * PAD - the number of pad symbols in a block. Integer variable or literal.
14b1928704201034c785a26296a49f69355eb56a05Nick Lewycky * ALPHA_TO - The address of an array of NN elements to convert Galois field
15b1928704201034c785a26296a49f69355eb56a05Nick Lewycky *            elements in index (log) form to polynomial form. Read only.
16b1928704201034c785a26296a49f69355eb56a05Nick Lewycky * INDEX_OF - The address of an array of NN elements to convert Galois field
17b1928704201034c785a26296a49f69355eb56a05Nick Lewycky *            elements in polynomial form to index (log) form. Read only.
18b1928704201034c785a26296a49f69355eb56a05Nick Lewycky * MODNN - a function to reduce its argument modulo NN. May be inline or a macro.
1936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines * GENPOLY - an array of NROOTS+1 elements containing the generator polynomial in index form
20b1928704201034c785a26296a49f69355eb56a05Nick Lewycky
2106cb8ed00696eb14d1b831921452e50ec0568ea2Chandler Carruth * The memset() and memmove() functions are used. The appropriate header
22b1928704201034c785a26296a49f69355eb56a05Nick Lewycky * file declaring these functions (usually <string.h>) must be included by the calling
23b1928704201034c785a26296a49f69355eb56a05Nick Lewycky * program.
24b1928704201034c785a26296a49f69355eb56a05Nick Lewycky
2536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines * Copyright 2004, Phil Karn, KA9Q
2636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines * May be used under the terms of the GNU Lesser General Public License (LGPL)
270b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth */
2836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
290b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth
3036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#undef A0
310b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#define A0 (NN) /* Special reserved value encoding zero in index form */
32d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth
33a204ef3168c8804808c716115ba915c89d8849b9Nick Lewycky{
3406cb8ed00696eb14d1b831921452e50ec0568ea2Chandler Carruth  int i, j;
3539c41c3c93e0d223792acb093adce21a714b01c6Bill Wendling  data_t feedback;
36a11c3e25015a62c817e60ec4f955a7f3f3bb6c67Rafael Espindola
3706cb8ed00696eb14d1b831921452e50ec0568ea2Chandler Carruth  memset(parity,0,NROOTS*sizeof(data_t));
3806cb8ed00696eb14d1b831921452e50ec0568ea2Chandler Carruth
39c4e6b540f05932eea37ca10b6c1fded522777954Nick Lewycky  for(i=0;i<NN-NROOTS-PAD;i++){
40dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    feedback = INDEX_OF[data[i] ^ parity[0]];
41b1928704201034c785a26296a49f69355eb56a05Nick Lewycky    if(feedback != A0){      /* feedback term is non-zero */
42b1928704201034c785a26296a49f69355eb56a05Nick Lewycky#ifdef UNNORMALIZED
43b1928704201034c785a26296a49f69355eb56a05Nick Lewycky      /* This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
44b1928704201034c785a26296a49f69355eb56a05Nick Lewycky       * always be for the polynomials constructed by init_rs()
45dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines       */
46dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      feedback = MODNN(NN - GENPOLY[NROOTS] + feedback);
47a204ef3168c8804808c716115ba915c89d8849b9Nick Lewycky#endif
48a204ef3168c8804808c716115ba915c89d8849b9Nick Lewycky      for(j=1;j<NROOTS;j++)
49a204ef3168c8804808c716115ba915c89d8849b9Nick Lewycky	parity[j] ^= ALPHA_TO[MODNN(feedback + GENPOLY[NROOTS-j])];
504c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    }
514c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar    /* Shift */
52a204ef3168c8804808c716115ba915c89d8849b9Nick Lewycky    memmove(&parity[0],&parity[1],sizeof(data_t)*(NROOTS-1));
53a204ef3168c8804808c716115ba915c89d8849b9Nick Lewycky    if(feedback != A0)
54a204ef3168c8804808c716115ba915c89d8849b9Nick Lewycky      parity[NROOTS-1] = ALPHA_TO[MODNN(feedback + GENPOLY[0])];
55a204ef3168c8804808c716115ba915c89d8849b9Nick Lewycky    else
56a204ef3168c8804808c716115ba915c89d8849b9Nick Lewycky      parity[NROOTS-1] = 0;
57a204ef3168c8804808c716115ba915c89d8849b9Nick Lewycky  }
58a204ef3168c8804808c716115ba915c89d8849b9Nick Lewycky}
59a204ef3168c8804808c716115ba915c89d8849b9Nick Lewycky