arduino-audio-tools
gf.hpp
1 /* Author: Mike Lubinets (aka mersinvald)
2  * Date: 29.12.15
3  *
4  * See LICENSE */
5 
6 #ifndef GF_H
7 #define GF_H
8 #include <stdint.h>
9 #include <string.h>
10 #include "poly.hpp"
11 
12 #if !defined DEBUG && !defined __CC_ARM
13 #include <assert.h>
14 #else
15 #define assert(dummy)
16 #endif
17 
19 namespace RS {
20 
21 namespace gf {
22 
23 
24 /* GF tables pre-calculated for 0x11d primitive polynomial */
25 
26 const uint8_t exp[255] = {
27  0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x1d, 0x3a, 0x74, 0xe8, 0xcd, 0x87, 0x13, 0x26, 0x4c,
28  0x98, 0x2d, 0x5a, 0xb4, 0x75, 0xea, 0xc9, 0x8f, 0x3, 0x6, 0xc, 0x18, 0x30, 0x60, 0xc0, 0x9d,
29  0x27, 0x4e, 0x9c, 0x25, 0x4a, 0x94, 0x35, 0x6a, 0xd4, 0xb5, 0x77, 0xee, 0xc1, 0x9f, 0x23, 0x46,
30  0x8c, 0x5, 0xa, 0x14, 0x28, 0x50, 0xa0, 0x5d, 0xba, 0x69, 0xd2, 0xb9, 0x6f, 0xde, 0xa1, 0x5f,
31  0xbe, 0x61, 0xc2, 0x99, 0x2f, 0x5e, 0xbc, 0x65, 0xca, 0x89, 0xf, 0x1e, 0x3c, 0x78, 0xf0, 0xfd,
32  0xe7, 0xd3, 0xbb, 0x6b, 0xd6, 0xb1, 0x7f, 0xfe, 0xe1, 0xdf, 0xa3, 0x5b, 0xb6, 0x71, 0xe2, 0xd9,
33  0xaf, 0x43, 0x86, 0x11, 0x22, 0x44, 0x88, 0xd, 0x1a, 0x34, 0x68, 0xd0, 0xbd, 0x67, 0xce, 0x81,
34  0x1f, 0x3e, 0x7c, 0xf8, 0xed, 0xc7, 0x93, 0x3b, 0x76, 0xec, 0xc5, 0x97, 0x33, 0x66, 0xcc, 0x85,
35  0x17, 0x2e, 0x5c, 0xb8, 0x6d, 0xda, 0xa9, 0x4f, 0x9e, 0x21, 0x42, 0x84, 0x15, 0x2a, 0x54, 0xa8,
36  0x4d, 0x9a, 0x29, 0x52, 0xa4, 0x55, 0xaa, 0x49, 0x92, 0x39, 0x72, 0xe4, 0xd5, 0xb7, 0x73, 0xe6,
37  0xd1, 0xbf, 0x63, 0xc6, 0x91, 0x3f, 0x7e, 0xfc, 0xe5, 0xd7, 0xb3, 0x7b, 0xf6, 0xf1, 0xff, 0xe3,
38  0xdb, 0xab, 0x4b, 0x96, 0x31, 0x62, 0xc4, 0x95, 0x37, 0x6e, 0xdc, 0xa5, 0x57, 0xae, 0x41, 0x82,
39  0x19, 0x32, 0x64, 0xc8, 0x8d, 0x7, 0xe, 0x1c, 0x38, 0x70, 0xe0, 0xdd, 0xa7, 0x53, 0xa6, 0x51,
40  0xa2, 0x59, 0xb2, 0x79, 0xf2, 0xf9, 0xef, 0xc3, 0x9b, 0x2b, 0x56, 0xac, 0x45, 0x8a, 0x9, 0x12,
41  0x24, 0x48, 0x90, 0x3d, 0x7a, 0xf4, 0xf5, 0xf7, 0xf3, 0xfb, 0xeb, 0xcb, 0x8b, 0xb, 0x16, 0x2c,
42  0x58, 0xb0, 0x7d, 0xfa, 0xe9, 0xcf, 0x83, 0x1b, 0x36, 0x6c, 0xd8, 0xad, 0x47, 0x8e
43 };
44 
45 const uint8_t log[256] = {
46  0x0, 0x0, 0x1, 0x19, 0x2, 0x32, 0x1a, 0xc6, 0x3, 0xdf, 0x33, 0xee, 0x1b, 0x68, 0xc7, 0x4b, 0x4,
47  0x64, 0xe0, 0xe, 0x34, 0x8d, 0xef, 0x81, 0x1c, 0xc1, 0x69, 0xf8, 0xc8, 0x8, 0x4c, 0x71, 0x5,
48  0x8a, 0x65, 0x2f, 0xe1, 0x24, 0xf, 0x21, 0x35, 0x93, 0x8e, 0xda, 0xf0, 0x12, 0x82, 0x45, 0x1d,
49  0xb5, 0xc2, 0x7d, 0x6a, 0x27, 0xf9, 0xb9, 0xc9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, 0x2c, 0xd7, 0x4f,
61  0xae, 0xd5, 0xe9, 0xe6, 0xe7, 0xad, 0xe8, 0x74, 0xd6, 0xf4, 0xea, 0xa8, 0x50, 0x58, 0xaf
62 };
63 
64 
65 
66 /* ################################
67  * # OPERATIONS OVER GALOIS FIELDS #
68  * ################################ */
69 
70 /* @brief Addition in Galois Fields
71  * @param x - left operand
72  * @param y - right operand
73  * @return x + y */
74 inline uint8_t add(uint8_t x, uint8_t y) {
75  return x^y;
76 }
77 
78 /* ##### GF subtraction ###### */
79 /* @brief Subtraction in Galois Fields
80  * @param x - left operand
81  * @param y - right operand
82  * @return x - y */
83 inline uint8_t sub(uint8_t x, uint8_t y) {
84  return x^y;
85 }
86 
87 /* @brief Multiplication in Galois Fields
88  * @param x - left operand
89  * @param y - right operand
90  * @return x * y */
91 inline uint8_t mul(uint16_t x, uint16_t y){
92  if (x == 0 || y == 0)
93  return 0;
94  return exp[(log[x] + log[y]) % 255];
95 }
96 
97 /* @brief Division in Galois Fields
98  * @param x - dividend
99  * @param y - divisor
100  * @return x / y */
101 inline uint8_t div(uint8_t x, uint8_t y){
102  assert(y != 0);
103  if(x == 0) return 0;
104  return exp[(log[x] + 255 - log[y]) % 255];
105 }
106 
107 /* @brief X in power Y w
108  * @param x - operand
109  * @param power - power
110  * @return x^power */
111 inline uint8_t pow(uint8_t x, intmax_t power){
112  intmax_t i = log[x];
113  i *= power;
114  i %= 255;
115  if(i < 0) i = i + 255;
116  return exp[i % 255];
117 }
118 
119 /* @brief Inversion in Galois Fields
120  * @param x - number
121  * @return inversion of x */
122 inline uint8_t inverse(uint8_t x){
123  return exp[(255 - log[x]) % 255]; /* == div(1, x); */
124 }
125 
126 /* ##########################
127  * # POLYNOMIALS OPERATIONS #
128  * ########################## */
129 
130 /* @brief Multiplication polynomial by scalar
131  * @param &p - source polynomial
132  * @param &newp - destination polynomial
133  * @param x - scalar */
134 inline void
135 poly_scale(const Poly *p, Poly *newp, uint16_t x) {
136  newp->length = p->length;
137  for(uint16_t i = 0; i < p->length; i++){
138  newp->at(i) = mul(p->at(i), x);
139  }
140 }
141 
142 /* @brief Addition of two polynomials
143  * @param &p - right operand polynomial
144  * @param &q - left operand polynomial
145  * @param &newp - destination polynomial */
146 inline void
147 poly_add(const Poly *p, const Poly *q, Poly *newp) {
148  newp->length = poly_max(p->length, q->length);
149  memset(newp->ptr(), 0, newp->length * sizeof(uint8_t));
150 
151  for(uint8_t i = 0; i < p->length; i++){
152  newp->at(i + newp->length - p->length) = p->at(i);
153  }
154 
155  for(uint8_t i = 0; i < q->length; i++){
156  newp->at(i + newp->length - q->length) ^= q->at(i);
157  }
158 }
159 
160 
161 /* @brief Multiplication of two polynomials
162  * @param &p - right operand polynomial
163  * @param &q - left operand polynomial
164  * @param &newp - destination polynomial */
165 inline void
166 poly_mul(const Poly *p, const Poly *q, Poly *newp) {
167  newp->length = p->length + q->length - 1;
168  memset(newp->ptr(), 0, newp->length * sizeof(uint8_t));
169  /* Compute the polynomial multiplication (just like the outer product of two vectors,
170  * we multiply each coefficients of p with all coefficients of q) */
171  for(uint8_t j = 0; j < q->length; j++){
172  for(uint8_t i = 0; i < p->length; i++){
173  newp->at(i+j) ^= mul(p->at(i), q->at(j)); /* == r[i + j] = gf_add(r[i+j], gf_mul(p[i], q[j])) */
174  }
175  }
176 }
177 
178 /* @brief Division of two polynomials
179  * @param &p - right operand polynomial
180  * @param &q - left operand polynomial
181  * @param &newp - destination polynomial */
182 inline void
183 poly_div(const Poly *p, const Poly *q, Poly *newp) {
184  if(p->ptr() != newp->ptr()) {
185  memcpy(newp->ptr(), p->ptr(), p->length*sizeof(uint8_t));
186  }
187 
188  newp->length = p->length;
189 
190  uint8_t coef;
191 
192  for(int i = 0; i < (p->length-(q->length-1)); i++){
193  coef = newp->at(i);
194  if(coef != 0){
195  for(uint8_t j = 1; j < q->length; j++){
196  if(q->at(j) != 0)
197  newp->at(i+j) ^= mul(q->at(j), coef);
198  }
199  }
200  }
201 
202  size_t sep = p->length-(q->length-1);
203  memmove(newp->ptr(), newp->ptr()+sep, (newp->length-sep) * sizeof(uint8_t));
204  newp->length = newp->length-sep;
205 }
206 
207 /* @brief Evaluation of polynomial in x
208  * @param &p - polynomial to evaluate
209  * @param x - evaluation point */
210 inline int8_t
211 poly_eval(const Poly *p, uint16_t x) {
212  uint8_t y = p->at(0);
213  for(uint8_t i = 1; i < p->length; i++){
214  y = mul(y, x) ^ p->at(i);
215  }
216  return y;
217 }
218 
219 } /* end of gf namespace */
220 
221 }
222 #endif // GF_H
223 
AudioTools internal: Reed-Solomon.
Definition: gf.hpp:19
Definition: poly.hpp:20