xref: /linux/lib/reed_solomon/encode_rs.c (revision dc8f923eaee24061c557cb3221abeea3b95072c4)
1*dc8f923eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Generic Reed Solomon encoder / decoder library
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright 2002, Phil Karn, KA9Q
61da177e4SLinus Torvalds  * May be used under the terms of the GNU General Public License (GPL)
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Adaption to the kernel by Thomas Gleixner (tglx@linutronix.de)
91da177e4SLinus Torvalds  *
103413e189SThomas Gleixner  * Generic data width independent code which is included by the wrappers.
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds {
131da177e4SLinus Torvalds 	int i, j, pad;
141da177e4SLinus Torvalds 	int nn = rs->nn;
151da177e4SLinus Torvalds 	int nroots = rs->nroots;
161da177e4SLinus Torvalds 	uint16_t *alpha_to = rs->alpha_to;
171da177e4SLinus Torvalds 	uint16_t *index_of = rs->index_of;
181da177e4SLinus Torvalds 	uint16_t *genpoly = rs->genpoly;
191da177e4SLinus Torvalds 	uint16_t fb;
201da177e4SLinus Torvalds 	uint16_t msk = (uint16_t) rs->nn;
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds 	/* Check length parameter for validity */
231da177e4SLinus Torvalds 	pad = nn - nroots - len;
241da177e4SLinus Torvalds 	if (pad < 0 || pad >= nn)
251da177e4SLinus Torvalds 		return -ERANGE;
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds 	for (i = 0; i < len; i++) {
281da177e4SLinus Torvalds 		fb = index_of[((((uint16_t) data[i])^invmsk) & msk) ^ par[0]];
291da177e4SLinus Torvalds 		/* feedback term is non-zero */
301da177e4SLinus Torvalds 		if (fb != nn) {
311da177e4SLinus Torvalds 			for (j = 1; j < nroots; j++) {
321da177e4SLinus Torvalds 				par[j] ^= alpha_to[rs_modnn(rs, fb +
331da177e4SLinus Torvalds 							 genpoly[nroots - j])];
341da177e4SLinus Torvalds 			}
351da177e4SLinus Torvalds 		}
361da177e4SLinus Torvalds 		/* Shift */
371da177e4SLinus Torvalds 		memmove(&par[0], &par[1], sizeof(uint16_t) * (nroots - 1));
381da177e4SLinus Torvalds 		if (fb != nn) {
391da177e4SLinus Torvalds 			par[nroots - 1] = alpha_to[rs_modnn(rs,
401da177e4SLinus Torvalds 							    fb + genpoly[0])];
411da177e4SLinus Torvalds 		} else {
421da177e4SLinus Torvalds 			par[nroots - 1] = 0;
431da177e4SLinus Torvalds 		}
441da177e4SLinus Torvalds 	}
451da177e4SLinus Torvalds 	return 0;
461da177e4SLinus Torvalds }
47