rssoft  0.0.0
Reed-Solomon codes library with soft decision decoding
/shared/development/google_code/rssoft/library/lib/GFq.h
Go to the documentation of this file.
00001 /*
00002      Copyright 2013 Edouard Griffiths <f4exb at free dot fr>
00003 
00004      This file is part of RSSoft. A Reed-Solomon Soft Decoding library
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 51 Franklin Street, Boston, MA  02110-1301  USA
00019 
00020          Original from Arash Partow (see original copytight notice).
00021          Modified and included in RSSoft in gf sub-namespace.
00022 
00023          Galois Field GF(q=2^m) class
00024 
00025 */
00026 /*
00027  *********************************************************************
00028  *                                                                   *
00029  *        Galois Field Arithmetic Library (version 0.0.1)            *
00030  *                                                                   *
00031  * Class: Galois Field                                               *
00032  * Author: Arash Partow - 2000                                       *
00033  * URL: http://www.partow.net/projects/galois/index.html             *
00034  *                                                                   *
00035  * Copyright Notice:                                                 *
00036  * Free use of this library is permitted under the guidelines and    *
00037  * in accordance with the most current version of the Common Public  *
00038  * License.                                                          *
00039  * http://www.opensource.org/licenses/cpl.php                        *
00040  *                                                                   *
00041  *********************************************************************
00042  */
00043 
00044 #ifndef __GFQ_H__
00045 #define __GFQ_H__
00046 
00047 #include "GF2_Polynomial.h"
00048 #include <iostream>
00049 #include <vector>
00050 #include <string.h>
00051 
00052 namespace rssoft
00053 {
00054 namespace gf
00055 {
00056 typedef int GFq_Symbol;        
00057 const GFq_Symbol GFERROR = -1; 
00058 
00064 class GFq
00065 {
00066 
00067 public:
00068         GFq(const int pwr, const GF2_Polynomial& primitive_poly);
00069         GFq(const GFq& gf);
00070         ~GFq();
00071 
00072         GFq& operator=(const GFq& gf);
00073         bool operator==(const GFq& gf) const;
00074         bool operator!=(const GFq& gf) const;
00075 
00081         inline unsigned int index(const GFq_Symbol value) const
00082         {
00083                 return index_of[value];
00084         }
00085 
00091         inline GFq_Symbol alpha(const unsigned int power) const
00092         {
00093                 return alpha_to[power];
00094         }
00095 
00100         inline unsigned int size() const
00101         {
00102                 return field_size;
00103         }
00104 
00109         inline unsigned int pwr() const
00110         {
00111                 return power;
00112         }
00113 
00114         inline GFq_Symbol add(const GFq_Symbol& a, const GFq_Symbol& b) const
00115         {
00116                 return (a ^ b);
00117         }
00118 
00119         inline GFq_Symbol sub(const GFq_Symbol& a, const GFq_Symbol& b) const
00120         {
00121                 return (a ^ b);
00122         }
00123 
00124         inline GFq_Symbol mul(const GFq_Symbol& a, const GFq_Symbol& b) const
00125         {
00126 #if !defined(NO_GFLUT)
00127                 return mul_table[a][b];
00128 #else
00129                 if ((a == 0) || (b == 0))
00130                 {
00131                         return 0;
00132                 }
00133                 else
00134                 {
00135                         return alpha_to[fast_modulus(index_of[a] + index_of[b])];
00136                 }
00137 #endif
00138         }
00139 
00140         inline GFq_Symbol div(const GFq_Symbol& a, const GFq_Symbol& b) const
00141         {
00142 #if !defined(NO_GFLUT)
00143                 return div_table[a][b];
00144 #else
00145                 if ((a == 0) || (b == 0))
00146                 {
00147                         return 0;
00148                 }
00149                 else
00150                 {
00151                         return alpha_to[fast_modulus(index_of[a] - index_of[b] + field_size)];
00152                 }
00153 #endif
00154         }
00155 
00156         inline GFq_Symbol exp(const GFq_Symbol& a, const int& n) const
00157         {
00158                 if (n == 0)
00159                 {
00160                         return 1;
00161                 }
00162                 else if (a == 0)
00163         {
00164             return 0;
00165         }
00166         else
00167         {
00168                 unsigned int log_a = index_of[a];
00169                 unsigned int log_a_pwr_n = log_a * n;
00170                 return alpha_to[log_a_pwr_n % field_size];
00171         }
00172         }
00173 
00174 /* Just does not work
00175         inline GFq_Symbol exp(const GFq_Symbol& a, const int& n) const
00176         {
00177         if (n == 0)
00178         {
00179             return 1;
00180         }
00181 #if !defined(NO_GFLUT)
00182                 if (n < 0)
00183                 {
00184                         int b = n;
00185                         while (b < 0)
00186                         {
00187                                 b += field_size; // b could be negative
00188                         }
00189 
00190                         if (b == 0)
00191                         {
00192                                 return 1;
00193                         }
00194                         return exp_table[a][b];
00195                 }
00196                 else
00197                 {
00198                         return exp_table[a][n & field_size];
00199                 }
00200 #else
00201                 if (a != 0)
00202                 {
00203                         if (n < 0)
00204                         {
00205                                 int b = n;
00206                                 while(b < 0)
00207                                 {
00208                                         b += field_size; // b could be negative
00209                                 }
00210                                 if (b == 0)
00211                                 {
00212                                         return 1;
00213                                 }
00214                                 return alpha_to[fast_modulus(index_of[a] * b)];
00215                         }
00216                         else if (n == 0)
00217                         {
00218                                 return 1;
00219                         }
00220                         else
00221                         {
00222                                 return alpha_to[fast_modulus(index_of[a] * n)];
00223                         }
00224                 }
00225                 else
00226                 {
00227                         return 0;
00228                 }
00229 #endif
00230         }
00231 */
00232 
00233         inline GFq_Symbol inverse(const GFq_Symbol& val) const
00234         {
00235 #if !defined(NO_GFLUT)
00236                 return mul_inverse[val];
00237 #else
00238                 return alpha_to[fast_modulus(field_size - index_of[val])];
00239 #endif
00240         }
00241 
00242         friend std::ostream& operator <<(std::ostream& os, const GFq& gf);
00243 
00244 private:
00245 
00246         void generate_field();
00247         GFq_Symbol fast_modulus(GFq_Symbol x) const;
00248         GFq_Symbol gen_mul(const GFq_Symbol& a, const GFq_Symbol& b) const;
00249         GFq_Symbol gen_div(const GFq_Symbol& a, const GFq_Symbol& b) const;
00250         GFq_Symbol gen_exp(const GFq_Symbol& a, const unsigned int& n) const;
00251         GFq_Symbol gen_inverse(const GFq_Symbol& val) const;
00252 
00253         unsigned int power;                   
00254         unsigned int field_size;              
00255     const GF2_Polynomial& primitive_poly; 
00256         unsigned int prim_poly_hash;          
00257         GFq_Symbol* alpha_to;                 
00258         GFq_Symbol* index_of;                 
00259         GFq_Symbol* mul_inverse;              
00260         GFq_Symbol** mul_table;               
00261         GFq_Symbol** div_table;               
00262         GFq_Symbol** exp_table;               
00263 
00264 };
00265 
00266 } // namespace gf
00267 } // namespace rssoft
00268 
00269 #endif // __GFQ_H__
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines