1
- /*
1
+ /*
2
2
* Copyright (c) 2022 Macronix International Co., Ltd.
3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
@@ -45,10 +45,11 @@ void bch_encode(struct bch_code *bch, unsigned char *data, unsigned int *ecc)
45
45
unsigned int * p ;
46
46
unsigned int * c [16 ];
47
47
unsigned int * t [16 ];
48
-
48
+
49
49
t [0 ] = bch -> mod_tab ;
50
- for (i = 1 ; i < 16 ; i ++ )
50
+ for (i = 1 ; i < 16 ; i ++ ) {
51
51
t [i ] = t [i - 1 ] + 4 * (bch -> ecc_words );
52
+ }
52
53
53
54
memset (bch -> ecc , 0 , bch -> ecc_words * sizeof (* bch -> ecc ));
54
55
@@ -79,7 +80,7 @@ void bch_encode(struct bch_code *bch, unsigned char *data, unsigned int *ecc)
79
80
}
80
81
}
81
82
bch -> ecc [i ] = c [0 ][i ];
82
- for (j = 1 ; j < 16 ; j ++ ) {
83
+ for (j = 1 ; j < 16 ; j ++ ) {
83
84
bch -> ecc [i ] ^= c [j ][i ];
84
85
}
85
86
}
@@ -88,7 +89,7 @@ void bch_encode(struct bch_code *bch, unsigned char *data, unsigned int *ecc)
88
89
for (i = 0 ; i < bch -> ecc_words ; i ++ ) {
89
90
ecc [i ] = bch -> ecc [i ];
90
91
}
91
- }
92
+ }
92
93
}
93
94
94
95
static inline int mod (struct bch_code * bch , unsigned int v )
@@ -105,7 +106,7 @@ static void build_syndrome(struct bch_code *bch)
105
106
unsigned int i , j ;
106
107
unsigned int ecc_bits ;
107
108
unsigned int * ecc ;
108
-
109
+
109
110
memset (bch -> syn , 0 , 2 * bch -> t * sizeof (* bch -> syn ));
110
111
111
112
ecc_bits = bch -> ecc_bits ;
@@ -115,8 +116,9 @@ static void build_syndrome(struct bch_code *bch)
115
116
ecc_bits = i ;
116
117
while (* ecc ) {
117
118
if (* ecc & (unsigned int )1 ) {
118
- for (j = 0 ; j < 2 * bch -> t ; j ++ )
119
- bch -> syn [j ] ^= bch -> a_pow [mod (bch , (j + 1 )* i )];
119
+ for (j = 0 ; j < 2 * bch -> t ; j ++ ) {
120
+ bch -> syn [j ] ^= bch -> a_pow [mod (bch , (j + 1 )* i )];
121
+ }
120
122
}
121
123
* ecc >>= 1 ;
122
124
i ++ ;
@@ -141,21 +143,21 @@ static int build_error_location_poly(struct bch_code *bch)
141
143
142
144
for (i = 0 ; (i < bch -> t ) && (deg <= bch -> t ); i ++ ) {
143
145
if (d ) {
144
- k = 2 * i - pp ;
146
+ k = 2 * i - pp ;
145
147
if (buf_deg + k > deg ) {
146
148
tmp_deg = deg ;
147
149
for (j = 0 ; j <= deg ; j ++ ) {
148
150
bch -> buf2 [j ] = bch -> elp [j ];
149
151
}
150
152
}
151
153
tmp = bch -> n + bch -> a_log [d ] - bch -> a_log [dp ];
152
-
154
+
153
155
for (j = 0 ; j <= buf_deg ; j ++ ) {
154
156
if (bch -> buf [j ]) {
155
- bch -> elp [j + k ] ^= bch -> a_pow [mod (bch , tmp + bch -> a_log [bch -> buf [j ]])];
157
+ bch -> elp [j + k ] ^= bch -> a_pow [mod (bch , tmp + bch -> a_log [bch -> buf [j ]])];
156
158
}
157
159
}
158
- if (buf_deg + k > deg ) {
160
+ if (buf_deg + k > deg ) {
159
161
deg = buf_deg + k ;
160
162
buf_deg = tmp_deg ;
161
163
for (j = 0 ; j <= tmp_deg ; j ++ ) {
@@ -183,8 +185,8 @@ static int chien_search(struct bch_code *bch, unsigned int deg)
183
185
{
184
186
unsigned int i , j , k , nroot = 0 ;
185
187
unsigned int syn , syn0 ;
186
- int * rep = (int * ) bch -> buf ;
187
- int * root = (int * ) bch -> buf2 ;
188
+ int * rep = (int * ) bch -> buf ;
189
+ int * root = (int * ) bch -> buf2 ;
188
190
189
191
k = bch -> n - bch -> a_log [bch -> elp [deg ]];
190
192
for (i = 0 ; i < deg ; i ++ ) {
@@ -205,31 +207,32 @@ static int chien_search(struct bch_code *bch, unsigned int deg)
205
207
return nroot ;
206
208
}
207
209
}
208
- }
209
- return 0 ;
210
+ }
211
+ return 0 ;
210
212
}
211
213
212
214
int bch_decode (struct bch_code * bch , unsigned char * data , unsigned int * ecc )
213
215
{
214
216
unsigned int nbits ;
215
217
unsigned int i , err , nroot ;
216
- int * root = (int * ) bch -> buf2 ;
218
+ int * root = (int * ) bch -> buf2 ;
217
219
218
220
bch_encode (bch , data , NULL );
219
221
220
222
for (i = 0 , err = 0 ; i < bch -> ecc_words ; i ++ ) {
221
223
bch -> ecc [i ] ^= ecc [i ];
222
224
err |= bch -> ecc [i ];
223
225
}
224
- if (!err )
226
+ if (!err ) {
225
227
return 0 ;
228
+ }
226
229
227
230
build_syndrome (bch );
228
231
err = build_error_location_poly (bch );
229
232
if (err <= 0 ) {
230
233
return -1 ;
231
234
}
232
-
235
+
233
236
nroot = chien_search (bch , err );
234
237
if (err != nroot ) {
235
238
return -1 ;
@@ -238,7 +241,7 @@ int bch_decode(struct bch_code *bch, unsigned char *data, unsigned int *ecc)
238
241
for (i = 0 ; i < err ; i ++ ) {
239
242
root [i ] = nbits - 1 - root [i ];
240
243
root [i ] = (root [i ] & ~7 ) | (7 - (root [i ] & 7 ));
241
- data [root [i ]/ 8 ] ^= 1 << root [i ]% 8 ;
244
+ data [root [i ] / 8 ] ^= 1 << root [i ] % 8 ;
242
245
}
243
246
244
247
return err ;
@@ -251,7 +254,7 @@ static void build_gf_table(struct bch_code *bch)
251
254
unsigned int prim_poly [5 ] = {0x11d , 0x211 , 0x409 , 0x805 , 0x1053 };
252
255
253
256
poly = prim_poly [bch -> m - 8 ];
254
- msb = 1 << bch -> m ;
257
+ msb = 1 << bch -> m ;
255
258
bch -> a_pow [0 ] = 1 ;
256
259
bch -> a_log [1 ] = 0 ;
257
260
x = 2 ;
@@ -278,12 +281,12 @@ static void build_mod_tables(struct bch_code *bch, const unsigned int *g)
278
281
279
282
for (i = 0 ; i < 4 ; i ++ ) {
280
283
for (b = 0 ; b < 16 ; b ++ ) {
281
- tab = bch -> mod_tab + (b * 4 + i ) * bch -> ecc_words ;
282
- data = i << (2 * b );
284
+ tab = bch -> mod_tab + (b * 4 + i ) * bch -> ecc_words ;
285
+ data = i << (2 * b );
283
286
while (data ) {
284
287
d = 0 ;
285
288
poly = (data >> 1 );
286
- while (poly ) {
289
+ while (poly ) {
287
290
poly >>= 1 ;
288
291
d ++ ;
289
292
}
@@ -301,10 +304,12 @@ static void build_mod_tables(struct bch_code *bch, const unsigned int *g)
301
304
static void * bch_alloc (size_t size , int * err )
302
305
{
303
306
void * ptr = NULL ;
304
- if (* err == 0 )
307
+ if (* err == 0 ) {
305
308
ptr = malloc (size );
306
- if (ptr == NULL )
309
+ }
310
+ if (ptr == NULL ) {
307
311
* err = 1 ;
312
+ }
308
313
return ptr ;
309
314
}
310
315
@@ -324,36 +329,38 @@ static unsigned int *build_generator_poly(struct bch_code *bch)
324
329
free (g );
325
330
free (x );
326
331
bch_free (bch );
327
- return NULL ;
332
+ return NULL ;
328
333
}
329
-
334
+
330
335
bch -> ecc_bits = 0 ;
331
336
x [0 ] = 1 ;
332
337
for (t = 0 ; t < bch -> t ; t ++ ) {
333
338
for (m = 0 , i = 2 * t + 1 ; m < bch -> m ; m ++ ) {
334
339
x [bch -> ecc_bits + 1 ] = 1 ;
335
340
for (j = bch -> ecc_bits ; j > 0 ; j -- ) {
336
- if (x [j ]) {
341
+ if (x [j ]) {
337
342
x [j ] = bch -> a_pow [mod (bch , bch -> a_log [x [j ]] + i )] ^ x [j - 1 ];
338
343
} else {
339
- x [j ] = x [j - 1 ];
344
+ x [j ] = x [j - 1 ];
340
345
}
341
346
}
342
- if (x [j ])
347
+ if (x [j ]) {
343
348
x [j ] = bch -> a_pow [mod (bch , bch -> a_log [x [j ]] + i )];
349
+ }
344
350
bch -> ecc_bits ++ ;
345
351
i = mod (bch , 2 * i );
346
352
}
347
353
}
348
354
349
355
i = 0 ;
350
356
memset (g , 0 , (bch -> ecc_words + 1 ) * sizeof (* g ));
351
-
357
+
352
358
for (k = bch -> ecc_bits + 1 ; k > 0 ; k = k - n ) {
353
359
n = (k > 32 ) ? 32 : k ;
354
360
for (j = 0 ; j < n ; j ++ ) {
355
- if (x [k - 1 - j ])
361
+ if (x [k - 1 - j ]) {
356
362
g [i ] |= (unsigned int )1 << (31 - j );
363
+ }
357
364
}
358
365
i ++ ;
359
366
}
@@ -380,17 +387,17 @@ struct bch_code *bch_init(unsigned int m, unsigned int t)
380
387
381
388
bch = (struct bch_code * )malloc (sizeof (struct bch_code ));
382
389
383
- if (bch == NULL ) {
390
+ if (bch == NULL ) {
384
391
return NULL ;
385
392
}
386
393
387
394
bch -> m = m ;
388
395
bch -> t = t ;
389
- bch -> n = (1 << m )- 1 ;
396
+ bch -> n = (1 << m ) - 1 ;
390
397
bch -> ecc_words = (m * t + 31 ) / 32 ;
391
398
bch -> len = (bch -> n + 1 ) / 8 ;
392
- bch -> a_pow = bch_alloc ((1 + bch -> n )* sizeof (* bch -> a_pow ), & err );
393
- bch -> a_log = bch_alloc ((1 + bch -> n )* sizeof (* bch -> a_log ), & err );
399
+ bch -> a_pow = bch_alloc ((1 + bch -> n ) * sizeof (* bch -> a_pow ), & err );
400
+ bch -> a_log = bch_alloc ((1 + bch -> n ) * sizeof (* bch -> a_log ), & err );
394
401
bch -> mod_tab = bch_alloc (bch -> ecc_words * 16 * 4 * sizeof (* bch -> mod_tab ), & err );
395
402
bch -> ecc = bch_alloc (bch -> ecc_words * sizeof (* bch -> ecc ), & err );
396
403
bch -> syn = bch_alloc (2 * t * sizeof (* bch -> syn ), & err );
0 commit comments