xref: /linux/lib/reed_solomon/encode_rs.c (revision 3eb66e91a25497065c5322b1268cbc3953642227)
1dc8f923eSThomas 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 {
13*21633981SThomas Gleixner 	struct rs_codec *rs = rsc->codec;
141da177e4SLinus Torvalds 	int i, j, pad;
151da177e4SLinus Torvalds 	int nn = rs->nn;
161da177e4SLinus Torvalds 	int nroots = rs->nroots;
171da177e4SLinus Torvalds 	uint16_t *alpha_to = rs->alpha_to;
181da177e4SLinus Torvalds 	uint16_t *index_of = rs->index_of;
191da177e4SLinus Torvalds 	uint16_t *genpoly = rs->genpoly;
201da177e4SLinus Torvalds 	uint16_t fb;
211da177e4SLinus Torvalds 	uint16_t msk = (uint16_t) rs->nn;
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds 	/* Check length parameter for validity */
241da177e4SLinus Torvalds 	pad = nn - nroots - len;
251da177e4SLinus Torvalds 	if (pad < 0 || pad >= nn)
261da177e4SLinus Torvalds 		return -ERANGE;
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds 	for (i = 0; i < len; i++) {
291da177e4SLinus Torvalds 		fb = index_of[((((uint16_t) data[i])^invmsk) & msk) ^ par[0]];
301da177e4SLinus Torvalds 		/* feedback term is non-zero */
311da177e4SLinus Torvalds 		if (fb != nn) {
321da177e4SLinus Torvalds 			for (j = 1; j < nroots; j++) {
331da177e4SLinus Torvalds 				par[j] ^= alpha_to[rs_modnn(rs, fb +
341da177e4SLinus Torvalds 							 genpoly[nroots - j])];
351da177e4SLinus Torvalds 			}
361da177e4SLinus Torvalds 		}
371da177e4SLinus Torvalds 		/* Shift */
381da177e4SLinus Torvalds 		memmove(&par[0], &par[1], sizeof(uint16_t) * (nroots - 1));
391da177e4SLinus Torvalds 		if (fb != nn) {
401da177e4SLinus Torvalds 			par[nroots - 1] = alpha_to[rs_modnn(rs,
411da177e4SLinus Torvalds 							    fb + genpoly[0])];
421da177e4SLinus Torvalds 		} else {
431da177e4SLinus Torvalds 			par[nroots - 1] = 0;
441da177e4SLinus Torvalds 		}
451da177e4SLinus Torvalds 	}
461da177e4SLinus Torvalds 	return 0;
471da177e4SLinus Torvalds }
48