[493c477] | 1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
[2dd068] | 2 | |
---|
[b52d27] | 3 | /** |
---|
| 4 | * @file gfops.h |
---|
| 5 | * |
---|
| 6 | * Operations in GF, where GF is a finite field of size less than 2^16 |
---|
| 7 | * represented by a root of Conway polynomial. Uses look up tables for addition. |
---|
| 8 | * |
---|
| 9 | * @sa gf_tabutil.h |
---|
| 10 | **/ |
---|
[2dd068] | 11 | #ifndef INCL_GFOPS_H |
---|
| 12 | #define INCL_GFOPS_H |
---|
| 13 | |
---|
[e4fe2b] | 14 | // #include "config.h" |
---|
[25c3c2b] | 15 | |
---|
[bb20e7] | 16 | #ifndef NOSTREAMIO |
---|
[1dc616] | 17 | #ifdef HAVE_IOSTREAM |
---|
| 18 | #include <iostream> |
---|
[181148] | 19 | #define OSTREAM std::ostream |
---|
[1dc616] | 20 | #elif defined(HAVE_IOSTREAM_H) |
---|
[2dd068] | 21 | #include <iostream.h> |
---|
[181148] | 22 | #define OSTREAM ostream |
---|
[1dc616] | 23 | #endif |
---|
[bb20e7] | 24 | #endif /* NOSTREAMIO */ |
---|
[2dd068] | 25 | |
---|
[650f2d8] | 26 | #include "cf_assert.h" |
---|
[bb20e7] | 27 | |
---|
[2dd068] | 28 | #include "cf_defs.h" |
---|
[25c3c2b] | 29 | #include "canonicalform.h" |
---|
[2dd068] | 30 | |
---|
[a3f0fea] | 31 | EXTERN_VAR int gf_q; |
---|
| 32 | EXTERN_VAR int gf_p; |
---|
| 33 | EXTERN_VAR int gf_n; |
---|
| 34 | EXTERN_VAR int gf_q1; |
---|
| 35 | EXTERN_VAR int gf_m1; |
---|
| 36 | EXTERN_VAR char gf_name; |
---|
[2dd068] | 37 | |
---|
[a3f0fea] | 38 | EXTERN_VAR unsigned short * gf_table; |
---|
[2dd068] | 39 | |
---|
[a3f0fea] | 40 | EXTERN_INST_VAR CanonicalForm gf_mipo; |
---|
[2dd068] | 41 | |
---|
[2adb1c] | 42 | //{{{ predicates |
---|
| 43 | inline bool gf_iszero ( int a ) |
---|
| 44 | { |
---|
| 45 | return gf_q == a; |
---|
| 46 | } |
---|
| 47 | |
---|
[8710ff0] | 48 | inline bool gf_iszero ( long a ) |
---|
| 49 | { |
---|
| 50 | return gf_q == a; |
---|
| 51 | } |
---|
| 52 | |
---|
[2adb1c] | 53 | inline bool gf_isone ( int a ) |
---|
| 54 | { |
---|
| 55 | return 0 == a; |
---|
| 56 | } |
---|
[8710ff0] | 57 | |
---|
| 58 | inline bool gf_isone ( long a ) |
---|
| 59 | { |
---|
| 60 | return 0 == a; |
---|
| 61 | } |
---|
[2adb1c] | 62 | //}}} |
---|
| 63 | |
---|
| 64 | //{{{ conversion functions |
---|
[2dd068] | 65 | inline int gf_int2gf ( int i ) |
---|
| 66 | { |
---|
| 67 | while ( i < 0 ) |
---|
[2bf04b] | 68 | i += gf_p; |
---|
[2dd068] | 69 | while ( i >= gf_p ) |
---|
[2bf04b] | 70 | i -= gf_p; |
---|
[2dd068] | 71 | if ( i == 0 ) |
---|
[2bf04b] | 72 | return gf_q; |
---|
[2dd068] | 73 | int c = 0; |
---|
| 74 | while ( i > 1 ) { |
---|
[2bf04b] | 75 | c = gf_table[c]; |
---|
| 76 | i--; |
---|
[2dd068] | 77 | } |
---|
| 78 | return c; |
---|
| 79 | } |
---|
[8710ff0] | 80 | |
---|
| 81 | inline long gf_int2gf ( long i ) |
---|
| 82 | { |
---|
| 83 | while ( i < 0 ) |
---|
[2bf04b] | 84 | i += gf_p; |
---|
[8710ff0] | 85 | while ( i >= gf_p ) |
---|
[2bf04b] | 86 | i -= gf_p; |
---|
[8710ff0] | 87 | if ( i == 0 ) |
---|
[2bf04b] | 88 | return gf_q; |
---|
[8710ff0] | 89 | long c = 0; |
---|
| 90 | while ( i > 1 ) { |
---|
[2bf04b] | 91 | c = gf_table[c]; |
---|
| 92 | i--; |
---|
[8710ff0] | 93 | } |
---|
| 94 | return c; |
---|
| 95 | } |
---|
[2adb1c] | 96 | //}}} |
---|
[2dd068] | 97 | |
---|
[2adb1c] | 98 | //{{{ zero and one |
---|
[2dd068] | 99 | inline int gf_zero() |
---|
| 100 | { |
---|
| 101 | return gf_q; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | inline int gf_one() |
---|
| 105 | { |
---|
| 106 | return 0; |
---|
| 107 | } |
---|
[2adb1c] | 108 | //}}} |
---|
[2dd068] | 109 | |
---|
[987a48] | 110 | //{{{ inline int gf_sign ( int a ) |
---|
[2adb1c] | 111 | // docu: see imm_sign() |
---|
[987a48] | 112 | inline |
---|
| 113 | int gf_sign ( int a ) |
---|
[2dd068] | 114 | { |
---|
| 115 | if ( gf_iszero( a ) ) |
---|
[2bf04b] | 116 | return 0; |
---|
[2dd068] | 117 | else |
---|
[2bf04b] | 118 | return 1; |
---|
[2dd068] | 119 | } |
---|
[987a48] | 120 | //}}} |
---|
[2dd068] | 121 | |
---|
[2adb1c] | 122 | //{{{ arithmetic operators |
---|
[2dd068] | 123 | inline int gf_neg ( int a ) |
---|
| 124 | { |
---|
| 125 | // -z^a=z^a*(-1)=z^a*gf_m1; |
---|
| 126 | if ( a == gf_q ) return a; |
---|
| 127 | int i = a + gf_m1; |
---|
| 128 | if ( i >= gf_q1 ) |
---|
[2bf04b] | 129 | i -= gf_q1; |
---|
[2dd068] | 130 | return i; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | inline int gf_add ( int a, int b ) |
---|
| 134 | { |
---|
| 135 | // z^a+z^b=z^b*(z^(a-b)+1), if a>=b; |
---|
| 136 | // =z^a*(z^(b-a)+1), if a<b; |
---|
| 137 | if ( a == gf_q ) return b; |
---|
| 138 | if ( b == gf_q ) return a; |
---|
| 139 | int zb, zab, r; |
---|
| 140 | if ( a >= b ) { |
---|
[2bf04b] | 141 | zb = b; |
---|
| 142 | zab = a - b; |
---|
[2dd068] | 143 | } |
---|
| 144 | else { |
---|
[2bf04b] | 145 | zb = a; |
---|
| 146 | zab = b - a; |
---|
[2dd068] | 147 | } |
---|
| 148 | if ( gf_table[zab] == gf_q ) |
---|
[2bf04b] | 149 | r = gf_q; /*if z^(a-b)+1 =0*/ |
---|
[2dd068] | 150 | else { |
---|
[2bf04b] | 151 | r= zb + gf_table[zab]; |
---|
| 152 | if ( r >= gf_q1 ) |
---|
| 153 | r -= gf_q1; |
---|
[2dd068] | 154 | } |
---|
| 155 | return r; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | inline int gf_sub ( int a, int b ) |
---|
| 159 | { |
---|
| 160 | return gf_add( a, gf_neg( b ) ); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | inline int gf_mul ( int a, int b ) |
---|
| 164 | { |
---|
| 165 | if ( a == gf_q || b == gf_q ) |
---|
[2bf04b] | 166 | return gf_q; |
---|
[2dd068] | 167 | else { |
---|
[2bf04b] | 168 | int i = a + b; |
---|
| 169 | if ( i >= gf_q1 ) i -= gf_q1; |
---|
| 170 | return i; |
---|
[2dd068] | 171 | } |
---|
| 172 | } |
---|
| 173 | |
---|
[8710ff0] | 174 | inline long gf_mul ( long a, int b ) |
---|
| 175 | { |
---|
| 176 | if ( a == gf_q || b == gf_q ) |
---|
[2bf04b] | 177 | return gf_q; |
---|
[8710ff0] | 178 | else { |
---|
[2bf04b] | 179 | long i = a + b; |
---|
| 180 | if ( i >= gf_q1 ) i -= gf_q1; |
---|
| 181 | return i; |
---|
[8710ff0] | 182 | } |
---|
| 183 | } |
---|
| 184 | |
---|
[2dd068] | 185 | inline int gf_div ( int a, int b ) |
---|
| 186 | { |
---|
| 187 | ASSERT( b != gf_q, "divide by zero" ); |
---|
| 188 | if ( a == gf_q ) |
---|
[2bf04b] | 189 | return gf_q; |
---|
[2dd068] | 190 | else { |
---|
[2bf04b] | 191 | int s = a - b; |
---|
| 192 | if (s < 0) |
---|
| 193 | s += gf_q1; |
---|
| 194 | return s; |
---|
[2dd068] | 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | inline int gf_inv ( int a ) |
---|
| 199 | { |
---|
| 200 | ASSERT( a != gf_q, "divide by zero" ); |
---|
| 201 | return gf_q1 - a; |
---|
| 202 | } |
---|
[2adb1c] | 203 | //}}} |
---|
[2dd068] | 204 | |
---|
[2adb1c] | 205 | //{{{ input/output |
---|
[bb20e7] | 206 | #ifndef NOSTREAMIO |
---|
[181148] | 207 | inline void gf_print ( OSTREAM & os, int a ) |
---|
[2dd068] | 208 | { |
---|
| 209 | if ( a == gf_q ) |
---|
[2bf04b] | 210 | os << "0"; |
---|
[2dd068] | 211 | else if ( a == 0 ) |
---|
[2bf04b] | 212 | os << "1"; |
---|
[2dd068] | 213 | else if ( a == 1 ) |
---|
[2bf04b] | 214 | os << gf_name; |
---|
[2dd068] | 215 | else |
---|
[2bf04b] | 216 | os << gf_name << "^" << a; |
---|
[2dd068] | 217 | } |
---|
[bb20e7] | 218 | #endif /* NOSTREAMIO */ |
---|
[2adb1c] | 219 | //}}} |
---|
[2dd068] | 220 | |
---|
[2adb1c] | 221 | //{{{ exponentation |
---|
[2dd068] | 222 | inline int gf_power ( int a, int n ) |
---|
| 223 | { |
---|
| 224 | if ( n == 0 ) |
---|
[2bf04b] | 225 | return 0; |
---|
[2dd068] | 226 | else if ( n == 1 ) |
---|
[2bf04b] | 227 | return a; |
---|
[2dd068] | 228 | else |
---|
[2bf04b] | 229 | return gf_mul( a, gf_power( a, n-1 ) ); |
---|
[2dd068] | 230 | } |
---|
[8710ff0] | 231 | |
---|
| 232 | inline long gf_power ( long a, int n ) |
---|
| 233 | { |
---|
| 234 | if ( n == 0 ) |
---|
[2bf04b] | 235 | return 0; |
---|
[8710ff0] | 236 | else if ( n == 1 ) |
---|
[2bf04b] | 237 | return a; |
---|
[8710ff0] | 238 | else |
---|
[2bf04b] | 239 | return gf_mul( a, gf_power( a, n-1 ) ); |
---|
[8710ff0] | 240 | } |
---|
[2adb1c] | 241 | //}}} |
---|
[2dd068] | 242 | |
---|
| 243 | void gf_setcharacteristic ( int p, int n, char name ); |
---|
| 244 | |
---|
[25c3c2b] | 245 | // Singular needs this |
---|
| 246 | /*BEGINPUBLIC*/ |
---|
| 247 | |
---|
[8710ff0] | 248 | long gf_gf2ff ( long a ); |
---|
[2dd068] | 249 | int gf_gf2ff ( int a ); |
---|
| 250 | |
---|
[8710ff0] | 251 | bool gf_isff ( long a ); |
---|
[2dd068] | 252 | bool gf_isff ( int a ); |
---|
| 253 | |
---|
[25c3c2b] | 254 | /*ENDPUBLIC*/ |
---|
| 255 | |
---|
[493c477] | 256 | #endif /* ! INCL_GFOPS_H */ |
---|