Debian Codegraph @

mednafen/cdrom/galois.cpp

raw symbols SCIP
1 /* dvdisaster: Additional error correction for optical media.
2 * Copyright (C) 2004-2007 Carsten Gnoerlich.
3 * Project home page: http://www.dvdisaster.com
4 * Email: [email protected] -or- [email protected]
5 *
6 * The Reed-Solomon error correction draws a lot of inspiration - and even code -
7 * from Phil Karn's excellent Reed-Solomon library: http://www.ka9q.net/code/fec/
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA,
22 * or direct your browser at http://www.gnu.org.
23 */
24
25 #include "dvdisaster.h"cxx . . $ `<file>/mednafen/cdrom/dvdisaster.h`/
26
27 #include "galois-inlines.h"cxx . . $ `<file>/mednafen/cdrom/galois-inlines.h`/
28
29 /***
30 *** Galois field arithmetic.
31 ***
32 * Calculations are done over the extension field GF(2**n).
33 * Be careful not to overgeneralize these arithmetics;
34 * they only work for the case of GF(p**n) with p being prime.
35 */
36
37 /* Initialize the Galois field tables */
38
39
40 GaloisTablescxx . . $ GaloisTables#* CreateGaloisTablescxx . . $ CreateGaloisTables(d23602e4c396cb3b).(int32cxx . . $ int32# gf_generator)
41 {
42 GaloisTablescxx . . $ GaloisTables# *gt = (GaloisTablescxx . . $ GaloisTables# *)calloccxx . . $ calloc(95acc9b79bb49189).(1, sizeof(GaloisTablescxx . . $ GaloisTables#));
43 int32cxx . . $ int32# b,log;
44
45 /* Allocate the tables.
46 The encoder uses a special version of alpha_to which has the mod_fieldmax()
47 folded into the table. */
48
49 gt->gfGeneratorcxx . . $ _GaloisTables#gfGenerator. = gf_generator;
50
51 gt->indexOfcxx . . $ _GaloisTables#indexOf. = (int32cxx . . $ int32# *)calloccxx . . $ calloc(95acc9b79bb49189).(GF_FIELDSIZEcxx . . $ `mednafen/cdrom/dvdisaster.h:83:9`!, sizeof(int32cxx . . $ int32#));
52 gt->alphaTocxx . . $ _GaloisTables#alphaTo. = (int32cxx . . $ int32# *)calloccxx . . $ calloc(95acc9b79bb49189).(GF_FIELDSIZEcxx . . $ `mednafen/cdrom/dvdisaster.h:83:9`!, sizeof(int32cxx . . $ int32#));
53 gt->encAlphaTocxx . . $ _GaloisTables#encAlphaTo. = (int32cxx . . $ int32# *)calloccxx . . $ calloc(95acc9b79bb49189).(2*GF_FIELDSIZEcxx . . $ `mednafen/cdrom/dvdisaster.h:83:9`!, sizeof(int32cxx . . $ int32#));
54
55 /* create the log/ilog values */
56
57 for(b=1, log=0; log<GF_FIELDMAXcxx . . $ `mednafen/cdrom/dvdisaster.h:84:9`!; log++)
58 { gt->indexOfcxx . . $ _GaloisTables#indexOf.[b] = log;
59 gt->alphaTocxx . . $ _GaloisTables#alphaTo.[log] = b;
60 b = b << 1;
61 if(b & GF_FIELDSIZEcxx . . $ `mednafen/cdrom/dvdisaster.h:83:9`!)
62 b = b ^ gf_generator;
63 }
64
65 if(b!=1)
66 {
67 printf("Failed to create the Galois field log tables!\n");
68 exitcxx . . $ exit(44ed87f96534fad7).(1);
69 }
70
71 /* we're even closed using infinity (makes things easier) */
72
73 gt->indexOfcxx . . $ _GaloisTables#indexOf.[0] = GF_ALPHA0cxx . . $ `mednafen/cdrom/dvdisaster.h:85:9`!; /* log(0) = inf */
74 gt->alphaTocxx . . $ _GaloisTables#alphaTo.[GF_ALPHA0cxx . . $ `mednafen/cdrom/dvdisaster.h:85:9`!] = 0; /* and the other way around */
75
76 for(b=0; b<2*GF_FIELDSIZEcxx . . $ `mednafen/cdrom/dvdisaster.h:83:9`!; b++)
77 gt->encAlphaTocxx . . $ _GaloisTables#encAlphaTo.[b] = gt->alphaTocxx . . $ _GaloisTables#alphaTo.[mod_fieldmaxcxx . . $ mod_fieldmax(7864480464b09eea).(b)];
78
79 return gt;
80 }
81
82 void FreeGaloisTablescxx . . $ FreeGaloisTables(96bf5e7dfcec2c6b).(GaloisTablescxx . . $ GaloisTables# *gt)
83 {
84 if(gt->indexOfcxx . . $ _GaloisTables#indexOf.) freecxx . . $ free(bd21765a0afc8e3c).(gt->indexOfcxx . . $ _GaloisTables#indexOf.);
85 if(gt->alphaTocxx . . $ _GaloisTables#alphaTo.) freecxx . . $ free(bd21765a0afc8e3c).(gt->alphaTocxx . . $ _GaloisTables#alphaTo.);
86 if(gt->encAlphaTocxx . . $ _GaloisTables#encAlphaTo.) freecxx . . $ free(bd21765a0afc8e3c).(gt->encAlphaTocxx . . $ _GaloisTables#encAlphaTo.);
87
88 freecxx . . $ free(bd21765a0afc8e3c).(gt);
89 }
90
91 /***
92 *** Create the the Reed-Solomon generator polynomial
93 *** and some auxiliary data structures.
94 */
95
96 ReedSolomonTablescxx . . $ ReedSolomonTables# *CreateReedSolomonTablescxx . . $ CreateReedSolomonTables(3ca6ffdd841c0c54).(GaloisTablescxx . . $ GaloisTables# *gt,
97 int32cxx . . $ int32# first_consecutive_root,
98 int32cxx . . $ int32# prim_elem,
99 int nroots_in)
100 { ReedSolomonTablescxx . . $ ReedSolomonTables# *rt = (ReedSolomonTablescxx . . $ ReedSolomonTables# *)calloccxx . . $ calloc(95acc9b79bb49189).(1, sizeof(ReedSolomonTablescxx . . $ ReedSolomonTables#));
101 int32cxx . . $ int32# i,j,root;
102
103 rt->gfTablescxx . . $ _ReedSolomonTables#gfTables. = gt;
104 rt->fcrcxx . . $ _ReedSolomonTables#fcr. = first_consecutive_root;
105 rt->primElemcxx . . $ _ReedSolomonTables#primElem. = prim_elem;
106 rt->nrootscxx . . $ _ReedSolomonTables#nroots. = nroots_in;
107 rt->ndatacxx . . $ _ReedSolomonTables#ndata. = GF_FIELDMAXcxx . . $ `mednafen/cdrom/dvdisaster.h:84:9`! - rt->nrootscxx . . $ _ReedSolomonTables#nroots.;
108
109 rt->gpolycxx . . $ _ReedSolomonTables#gpoly. = (int32cxx . . $ int32# *)calloccxx . . $ calloc(95acc9b79bb49189).((rt->nrootscxx . . $ _ReedSolomonTables#nroots.+1), sizeof(int32cxx . . $ int32#));
110
111 /* Create the RS code generator polynomial */
112
113 rt->gpolycxx . . $ _ReedSolomonTables#gpoly.[0] = 1;
114
115 for(i=0, root=first_consecutive_root*prim_elem; i<rt->nrootscxx . . $ _ReedSolomonTables#nroots.; i++, root+=prim_elem)
116 { rt->gpolycxx . . $ _ReedSolomonTables#gpoly.[i+1] = 1;
117
118 /* Multiply gpoly by alpha**(root+x) */
119
120 for(j=i; j>0; j--)
121 {
122 if(rt->gpolycxx . . $ _ReedSolomonTables#gpoly.[j] != 0)
123 rt->gpolycxx . . $ _ReedSolomonTables#gpoly.[j] = rt->gpolycxx . . $ _ReedSolomonTables#gpoly.[j-1] ^ gt->alphaTocxx . . $ _GaloisTables#alphaTo.[mod_fieldmaxcxx . . $ mod_fieldmax(7864480464b09eea).(gt->indexOfcxx . . $ _GaloisTables#indexOf.[rt->gpolycxx . . $ _ReedSolomonTables#gpoly.[j]] + root)];
124 else
125 rt->gpolycxx . . $ _ReedSolomonTables#gpoly.[j] = rt->gpolycxx . . $ _ReedSolomonTables#gpoly.[j-1];
126 }
127
128 rt->gpolycxx . . $ _ReedSolomonTables#gpoly.[0] = gt->alphaTocxx . . $ _GaloisTables#alphaTo.[mod_fieldmaxcxx . . $ mod_fieldmax(7864480464b09eea).(gt->indexOfcxx . . $ _GaloisTables#indexOf.[rt->gpolycxx . . $ _ReedSolomonTables#gpoly.[0]] + root)];
129 }
130
131 /* Store the polynomials index for faster encoding */
132
133 for(i=0; i<=rt->nrootscxx . . $ _ReedSolomonTables#nroots.; i++)
134 rt->gpolycxx . . $ _ReedSolomonTables#gpoly.[i] = gt->indexOfcxx . . $ _GaloisTables#indexOf.[rt->gpolycxx . . $ _ReedSolomonTables#gpoly.[i]];
135
136 #if 0
137 /* for the precalculated unrolled loops only */
138
139 for(i=gt->nroots-1; i>0; i--)
140 PrintCLI(
141 " par_idx[((++spk)&%d)] ^= enc_alpha_to[feedback + %3d];\n",
142 nroots-1,gt->gpoly[i]);
143
144 PrintCLI(" par_idx[sp] = enc_alpha_to[feedback + %3d];\n",
145 gt->gpoly[0]);
146 #endif
147
148 return rt;
149 }
150
151 void FreeReedSolomonTablescxx . . $ FreeReedSolomonTables(3061629492dcb198).(ReedSolomonTablescxx . . $ ReedSolomonTables# *rt)
152 {
153 if(rt->gpolycxx . . $ _ReedSolomonTables#gpoly.) freecxx . . $ free(bd21765a0afc8e3c).(rt->gpolycxx . . $ _ReedSolomonTables#gpoly.);
154
155 freecxx . . $ free(bd21765a0afc8e3c).(rt);
156 }