[493c477] | 1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
[341696] | 2 | /* $Id$ */ |
---|
[2dd068] | 3 | |
---|
| 4 | #ifndef INCL_GFOPS_H |
---|
| 5 | #define INCL_GFOPS_H |
---|
| 6 | |
---|
[e4fe2b] | 7 | // #include "config.h" |
---|
[25c3c2b] | 8 | |
---|
[bb20e7] | 9 | #ifndef NOSTREAMIO |
---|
[1dc616] | 10 | #ifdef HAVE_IOSTREAM |
---|
| 11 | #include <iostream> |
---|
[181148] | 12 | #define OSTREAM std::ostream |
---|
[1dc616] | 13 | #elif defined(HAVE_IOSTREAM_H) |
---|
[2dd068] | 14 | #include <iostream.h> |
---|
[181148] | 15 | #define OSTREAM ostream |
---|
[1dc616] | 16 | #endif |
---|
[bb20e7] | 17 | #endif /* NOSTREAMIO */ |
---|
[2dd068] | 18 | |
---|
[650f2d8] | 19 | #include "cf_assert.h" |
---|
[bb20e7] | 20 | |
---|
[2dd068] | 21 | #include "cf_defs.h" |
---|
[25c3c2b] | 22 | #include "canonicalform.h" |
---|
[2dd068] | 23 | |
---|
| 24 | extern int gf_q; |
---|
| 25 | extern int gf_p; |
---|
| 26 | extern int gf_n; |
---|
| 27 | extern int gf_q1; |
---|
| 28 | extern int gf_m1; |
---|
| 29 | extern char gf_name; |
---|
| 30 | |
---|
| 31 | extern unsigned short * gf_table; |
---|
| 32 | |
---|
[25c3c2b] | 33 | extern CanonicalForm gf_mipo; |
---|
[2dd068] | 34 | |
---|
[2adb1c] | 35 | //{{{ predicates |
---|
| 36 | inline bool gf_iszero ( int a ) |
---|
| 37 | { |
---|
| 38 | return gf_q == a; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | inline bool gf_isone ( int a ) |
---|
| 42 | { |
---|
| 43 | return 0 == a; |
---|
| 44 | } |
---|
| 45 | //}}} |
---|
| 46 | |
---|
| 47 | //{{{ conversion functions |
---|
[2dd068] | 48 | inline int gf_int2gf ( int i ) |
---|
| 49 | { |
---|
| 50 | while ( i < 0 ) |
---|
| 51 | i += gf_p; |
---|
| 52 | while ( i >= gf_p ) |
---|
| 53 | i -= gf_p; |
---|
| 54 | if ( i == 0 ) |
---|
| 55 | return gf_q; |
---|
| 56 | int c = 0; |
---|
| 57 | while ( i > 1 ) { |
---|
| 58 | c = gf_table[c]; |
---|
| 59 | i--; |
---|
| 60 | } |
---|
| 61 | return c; |
---|
| 62 | } |
---|
[2adb1c] | 63 | //}}} |
---|
[2dd068] | 64 | |
---|
[2adb1c] | 65 | //{{{ zero and one |
---|
[2dd068] | 66 | inline int gf_zero() |
---|
| 67 | { |
---|
| 68 | return gf_q; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | inline int gf_one() |
---|
| 72 | { |
---|
| 73 | return 0; |
---|
| 74 | } |
---|
[2adb1c] | 75 | //}}} |
---|
[2dd068] | 76 | |
---|
[987a48] | 77 | //{{{ inline int gf_sign ( int a ) |
---|
[2adb1c] | 78 | // docu: see imm_sign() |
---|
[987a48] | 79 | inline |
---|
| 80 | int gf_sign ( int a ) |
---|
[2dd068] | 81 | { |
---|
| 82 | if ( gf_iszero( a ) ) |
---|
| 83 | return 0; |
---|
| 84 | else |
---|
| 85 | return 1; |
---|
| 86 | } |
---|
[987a48] | 87 | //}}} |
---|
[2dd068] | 88 | |
---|
[2adb1c] | 89 | //{{{ arithmetic operators |
---|
[2dd068] | 90 | inline int gf_neg ( int a ) |
---|
| 91 | { |
---|
| 92 | // -z^a=z^a*(-1)=z^a*gf_m1; |
---|
| 93 | if ( a == gf_q ) return a; |
---|
| 94 | int i = a + gf_m1; |
---|
| 95 | if ( i >= gf_q1 ) |
---|
| 96 | i -= gf_q1; |
---|
| 97 | return i; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | inline int gf_add ( int a, int b ) |
---|
| 101 | { |
---|
| 102 | // z^a+z^b=z^b*(z^(a-b)+1), if a>=b; |
---|
| 103 | // =z^a*(z^(b-a)+1), if a<b; |
---|
| 104 | if ( a == gf_q ) return b; |
---|
| 105 | if ( b == gf_q ) return a; |
---|
| 106 | int zb, zab, r; |
---|
| 107 | if ( a >= b ) { |
---|
| 108 | zb = b; |
---|
| 109 | zab = a - b; |
---|
| 110 | } |
---|
| 111 | else { |
---|
| 112 | zb = a; |
---|
| 113 | zab = b - a; |
---|
| 114 | } |
---|
| 115 | if ( gf_table[zab] == gf_q ) |
---|
| 116 | r = gf_q; /*if z^(a-b)+1 =0*/ |
---|
| 117 | else { |
---|
| 118 | r= zb + gf_table[zab]; |
---|
| 119 | if ( r >= gf_q1 ) |
---|
| 120 | r -= gf_q1; |
---|
| 121 | } |
---|
| 122 | return r; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | inline int gf_sub ( int a, int b ) |
---|
| 126 | { |
---|
| 127 | return gf_add( a, gf_neg( b ) ); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | inline int gf_mul ( int a, int b ) |
---|
| 131 | { |
---|
| 132 | if ( a == gf_q || b == gf_q ) |
---|
| 133 | return gf_q; |
---|
| 134 | else { |
---|
| 135 | int i = a + b; |
---|
| 136 | if ( i >= gf_q1 ) i -= gf_q1; |
---|
| 137 | return i; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | inline int gf_div ( int a, int b ) |
---|
| 142 | { |
---|
| 143 | ASSERT( b != gf_q, "divide by zero" ); |
---|
| 144 | if ( a == gf_q ) |
---|
| 145 | return gf_q; |
---|
| 146 | else { |
---|
| 147 | int s = a - b; |
---|
| 148 | if (s < 0) |
---|
| 149 | s += gf_q1; |
---|
| 150 | return s; |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | inline int gf_inv ( int a ) |
---|
| 155 | { |
---|
| 156 | ASSERT( a != gf_q, "divide by zero" ); |
---|
| 157 | return gf_q1 - a; |
---|
| 158 | } |
---|
[2adb1c] | 159 | //}}} |
---|
[2dd068] | 160 | |
---|
[2adb1c] | 161 | //{{{ input/output |
---|
[bb20e7] | 162 | #ifndef NOSTREAMIO |
---|
[181148] | 163 | inline void gf_print ( OSTREAM & os, int a ) |
---|
[2dd068] | 164 | { |
---|
| 165 | if ( a == gf_q ) |
---|
| 166 | os << "0"; |
---|
| 167 | else if ( a == 0 ) |
---|
| 168 | os << "1"; |
---|
| 169 | else if ( a == 1 ) |
---|
| 170 | os << gf_name; |
---|
| 171 | else |
---|
| 172 | os << gf_name << "^" << a; |
---|
| 173 | } |
---|
[bb20e7] | 174 | #endif /* NOSTREAMIO */ |
---|
[2adb1c] | 175 | //}}} |
---|
[2dd068] | 176 | |
---|
[2adb1c] | 177 | //{{{ exponentation |
---|
[2dd068] | 178 | inline int gf_power ( int a, int n ) |
---|
| 179 | { |
---|
| 180 | if ( n == 0 ) |
---|
| 181 | return 0; |
---|
| 182 | else if ( n == 1 ) |
---|
| 183 | return a; |
---|
| 184 | else |
---|
| 185 | return gf_mul( a, gf_power( a, n-1 ) ); |
---|
| 186 | } |
---|
[2adb1c] | 187 | //}}} |
---|
[2dd068] | 188 | |
---|
| 189 | void gf_setcharacteristic ( int p, int n, char name ); |
---|
| 190 | |
---|
[25c3c2b] | 191 | // Singular needs this |
---|
| 192 | /*BEGINPUBLIC*/ |
---|
| 193 | |
---|
[2dd068] | 194 | int gf_gf2ff ( int a ); |
---|
| 195 | |
---|
| 196 | bool gf_isff ( int a ); |
---|
| 197 | |
---|
[25c3c2b] | 198 | /*ENDPUBLIC*/ |
---|
| 199 | |
---|
[493c477] | 200 | #endif /* ! INCL_GFOPS_H */ |
---|