xref: /openbmc/linux/lib/reed_solomon/encode_rs.c (revision dc8f923e)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Generic Reed Solomon encoder / decoder library
4  *
5  * Copyright 2002, Phil Karn, KA9Q
6  * May be used under the terms of the GNU General Public License (GPL)
7  *
8  * Adaption to the kernel by Thomas Gleixner (tglx@linutronix.de)
9  *
10  * Generic data width independent code which is included by the wrappers.
11  */
12 {
13 	int i, j, pad;
14 	int nn = rs->nn;
15 	int nroots = rs->nroots;
16 	uint16_t *alpha_to = rs->alpha_to;
17 	uint16_t *index_of = rs->index_of;
18 	uint16_t *genpoly = rs->genpoly;
19 	uint16_t fb;
20 	uint16_t msk = (uint16_t) rs->nn;
21 
22 	/* Check length parameter for validity */
23 	pad = nn - nroots - len;
24 	if (pad < 0 || pad >= nn)
25 		return -ERANGE;
26 
27 	for (i = 0; i < len; i++) {
28 		fb = index_of[((((uint16_t) data[i])^invmsk) & msk) ^ par[0]];
29 		/* feedback term is non-zero */
30 		if (fb != nn) {
31 			for (j = 1; j < nroots; j++) {
32 				par[j] ^= alpha_to[rs_modnn(rs, fb +
33 							 genpoly[nroots - j])];
34 			}
35 		}
36 		/* Shift */
37 		memmove(&par[0], &par[1], sizeof(uint16_t) * (nroots - 1));
38 		if (fb != nn) {
39 			par[nroots - 1] = alpha_to[rs_modnn(rs,
40 							    fb + genpoly[0])];
41 		} else {
42 			par[nroots - 1] = 0;
43 		}
44 	}
45 	return 0;
46 }
47