[35aab3] | 1 | #ifndef MODULOP_H |
---|
| 2 | #define MODULOP_H |
---|
| 3 | /**************************************** |
---|
| 4 | * Computer Algebra System SINGULAR * |
---|
| 5 | ****************************************/ |
---|
[341696] | 6 | /* $Id$ */ |
---|
[35aab3] | 7 | /* |
---|
| 8 | * ABSTRACT: numbers modulo p (<=32003) |
---|
| 9 | */ |
---|
[2d805a] | 10 | #include <coeffs/coeffs.h> |
---|
[35aab3] | 11 | |
---|
| 12 | // defines are in struct.h |
---|
| 13 | // define if a*b is with mod instead of tables |
---|
| 14 | //#define HAVE_MULT_MOD |
---|
| 15 | // define if a/b is with mod instead of tables |
---|
| 16 | //#define HAVE_DIV_MOD |
---|
| 17 | // define if an if should be used |
---|
| 18 | //#define HAVE_GENERIC_ADD |
---|
| 19 | |
---|
| 20 | // enable large primes (32003 < p < 2^31-) |
---|
| 21 | #define NV_OPS |
---|
| 22 | #define NV_MAX_PRIME 32003 |
---|
| 23 | |
---|
| 24 | extern int npGen; |
---|
| 25 | |
---|
[1cce47] | 26 | BOOLEAN npInitChar(coeffs r, void* p); |
---|
[8c484e] | 27 | |
---|
[7d90aa] | 28 | BOOLEAN npGreaterZero (number k, const coeffs r); |
---|
| 29 | number npMult (number a, number b, const coeffs r); |
---|
| 30 | number npInit (int i, const coeffs r); |
---|
| 31 | int npInt (number &n, const coeffs r); |
---|
| 32 | number npAdd (number a, number b,const coeffs r); |
---|
| 33 | number npSub (number a, number b,const coeffs r); |
---|
| 34 | void npPower (number a, int i, number * result,const coeffs r); |
---|
| 35 | BOOLEAN npIsZero (number a,const coeffs r); |
---|
| 36 | BOOLEAN npIsOne (number a,const coeffs r); |
---|
| 37 | BOOLEAN npIsMOne (number a,const coeffs r); |
---|
| 38 | number npDiv (number a, number b,const coeffs r); |
---|
| 39 | number npNeg (number c,const coeffs r); |
---|
| 40 | number npInvers (number c,const coeffs r); |
---|
| 41 | BOOLEAN npGreater (number a, number b,const coeffs r); |
---|
| 42 | BOOLEAN npEqual (number a, number b,const coeffs r); |
---|
| 43 | void npWrite (number &a, const coeffs r); |
---|
[c7e3d7] | 44 | void npCoeffWrite (const coeffs r); |
---|
[7d90aa] | 45 | const char * npRead (const char *s, number *a,const coeffs r); |
---|
[35aab3] | 46 | #ifdef LDEBUG |
---|
[2b957a] | 47 | BOOLEAN npDBTest (number a, const char *f, const int l, const coeffs r); |
---|
| 48 | #define npTest(A,r) npDBTest(A,__FILE__,__LINE__, r) |
---|
[f1e33bb] | 49 | #else |
---|
[7d90aa] | 50 | #define npTest(A,r) (0) |
---|
[35aab3] | 51 | #endif |
---|
| 52 | |
---|
| 53 | //int npGetChar(); |
---|
| 54 | |
---|
[7d90aa] | 55 | nMapFunc npSetMap(const coeffs src, const coeffs dst); |
---|
[2336d0] | 56 | number npMapP(number from, const coeffs src, const coeffs r); |
---|
| 57 | number npMap0(number from, const coeffs src, const coeffs r); |
---|
[35aab3] | 58 | /*-------specials for spolys, do NOT use otherwise--------------------------*/ |
---|
| 59 | /* for npMultM, npSubM, npNegM, npEqualM : */ |
---|
| 60 | #ifdef HAVE_DIV_MOD |
---|
[900802] | 61 | extern unsigned short *npInvTable; |
---|
[35aab3] | 62 | #else |
---|
| 63 | #ifndef HAVE_MULT_MOD |
---|
| 64 | extern long npPminus1M; |
---|
[900802] | 65 | extern unsigned short *npExpTable; |
---|
| 66 | extern unsigned short *npLogTable; |
---|
[35aab3] | 67 | #endif |
---|
| 68 | #endif |
---|
| 69 | |
---|
| 70 | #if 0 |
---|
| 71 | inline number npMultM(number a, number b) |
---|
| 72 | // return (a*b)%n |
---|
| 73 | { |
---|
| 74 | double ab; |
---|
| 75 | long q, res; |
---|
| 76 | |
---|
| 77 | ab = ((double) ((int)a)) * ((double) ((int)b)); |
---|
| 78 | q = (long) (ab/((double) npPrimeM)); // q could be off by (+/-) 1 |
---|
| 79 | res = (long) (ab - ((double) q)*((double) npPrimeM)); |
---|
| 80 | res += (res >> 31) & npPrimeM; |
---|
| 81 | res -= npPrimeM; |
---|
| 82 | res += (res >> 31) & npPrimeM; |
---|
| 83 | return (number)res; |
---|
| 84 | } |
---|
| 85 | #endif |
---|
| 86 | #ifdef HAVE_MULT_MOD |
---|
[7d90aa] | 87 | static inline number npMultM(number a, number b, const coeffs r) |
---|
[35aab3] | 88 | { |
---|
| 89 | return (number) |
---|
[7d90aa] | 90 | ((((unsigned long) a)*((unsigned long) b)) % ((unsigned long) r->npPrimeM)); |
---|
[35aab3] | 91 | } |
---|
| 92 | #else |
---|
[7d90aa] | 93 | static inline number npMultM(number a, number b, const coeffs r) |
---|
[35aab3] | 94 | { |
---|
[7d90aa] | 95 | long x = (long)r->npLogTable[(long)a]+ r->npLogTable[(long)b]; |
---|
| 96 | return (number)(long)r->npExpTable[x<r->npPminus1M ? x : x- r->npPminus1M]; |
---|
[35aab3] | 97 | } |
---|
| 98 | #endif |
---|
| 99 | |
---|
| 100 | #if 0 |
---|
| 101 | inline number npAddAsm(number a, number b, int m) |
---|
| 102 | { |
---|
| 103 | number r; |
---|
| 104 | asm ("addl %2, %1; cmpl %3, %1; jb 0f; subl %3, %1; 0:" |
---|
| 105 | : "=&r" (r) |
---|
| 106 | : "%0" (a), "g" (b), "g" (m) |
---|
| 107 | : "cc"); |
---|
| 108 | return r; |
---|
| 109 | } |
---|
| 110 | inline number npSubAsm(number a, number b, int m) |
---|
| 111 | { |
---|
| 112 | number r; |
---|
| 113 | asm ("subl %2, %1; jnc 0f; addl %3, %1; 0:" |
---|
| 114 | : "=&r" (r) |
---|
| 115 | : "%0" (a), "g" (b), "g" (m) |
---|
| 116 | : "cc"); |
---|
| 117 | return r; |
---|
| 118 | } |
---|
| 119 | #endif |
---|
| 120 | #ifdef HAVE_GENERIC_ADD |
---|
[7d90aa] | 121 | static inline number npAddM(number a, number b, const coeffs r) |
---|
[35aab3] | 122 | { |
---|
[7d90aa] | 123 | long R = (long)a + (long)b; |
---|
| 124 | return (number)(R >= r->npPrimeM ? R - r->npPrimeM : R); |
---|
[35aab3] | 125 | } |
---|
[7d90aa] | 126 | static inline number npSubM(number a, number b, const coeffs r) |
---|
[35aab3] | 127 | { |
---|
[6c56a8] | 128 | return (number)((long)a<(long)b ? |
---|
[7d90aa] | 129 | r->npPrimeM-(long)b+(long)a : (long)a-(long)b); |
---|
[35aab3] | 130 | } |
---|
| 131 | #else |
---|
[7d90aa] | 132 | static inline number npAddM(number a, number b, const coeffs r) |
---|
[35aab3] | 133 | { |
---|
[6c56a8] | 134 | long res = ((long)a + (long)b); |
---|
[7d90aa] | 135 | res -= r->npPrimeM; |
---|
[6c56a8] | 136 | #if SIZEOF_LONG == 8 |
---|
[7d90aa] | 137 | res += (res >> 63) & r->npPrimeM; |
---|
[6c56a8] | 138 | #else |
---|
[7d90aa] | 139 | res += (res >> 31) & r->npPrimeM; |
---|
[6c56a8] | 140 | #endif |
---|
[35aab3] | 141 | return (number)res; |
---|
| 142 | } |
---|
[7d90aa] | 143 | static inline number npSubM(number a, number b, const coeffs r) |
---|
[35aab3] | 144 | { |
---|
[6c56a8] | 145 | long res = ((long)a - (long)b); |
---|
| 146 | #if SIZEOF_LONG == 8 |
---|
[7d90aa] | 147 | res += (res >> 63) & r->npPrimeM; |
---|
[6c56a8] | 148 | #else |
---|
[7d90aa] | 149 | res += (res >> 31) & r->npPrimeM; |
---|
[6c56a8] | 150 | #endif |
---|
[35aab3] | 151 | return (number)res; |
---|
| 152 | } |
---|
| 153 | #endif |
---|
| 154 | |
---|
[b58d47] | 155 | static inline BOOLEAN npIsZeroM (number a, const coeffs) |
---|
[35aab3] | 156 | { |
---|
| 157 | return 0 == (long)a; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /* |
---|
| 161 | *inline number npMultM(number a, number b) |
---|
| 162 | *{ |
---|
| 163 | * return (number)(((long)a*(long)b) % npPrimeM); |
---|
| 164 | *} |
---|
| 165 | */ |
---|
| 166 | |
---|
[7d90aa] | 167 | #define npNegM(A,r) (number)(r->npPrimeM-(long)(A)) |
---|
| 168 | #define npEqualM(A,B,r) ((A)==(B)) |
---|
[35aab3] | 169 | |
---|
| 170 | |
---|
| 171 | #ifdef NV_OPS |
---|
[7f7b2a] | 172 | #pragma GCC diagnostic ignored "-Wlong-long" |
---|
[7d90aa] | 173 | static inline number nvMultM(number a, number b, const coeffs r) |
---|
[35aab3] | 174 | { |
---|
| 175 | #if SIZEOF_LONG == 4 |
---|
[cccd36] | 176 | #define ULONG64 (unsigned long long)(unsigned long) |
---|
[35aab3] | 177 | #else |
---|
[cccd36] | 178 | #define ULONG64 (unsigned long) |
---|
[35aab3] | 179 | #endif |
---|
| 180 | return (number) |
---|
[7d90aa] | 181 | (unsigned long)((ULONG64 a)*(ULONG64 b) % (ULONG64 r->npPrimeM)); |
---|
[35aab3] | 182 | } |
---|
[7d90aa] | 183 | number nvMult (number a, number b, const coeffs r); |
---|
| 184 | number nvDiv (number a, number b, const coeffs r); |
---|
| 185 | number nvInvers (number c, const coeffs r); |
---|
| 186 | void nvPower (number a, int i, number * result, const coeffs r); |
---|
[35aab3] | 187 | #endif |
---|
| 188 | |
---|
| 189 | #endif |
---|