[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 | |
---|
[95eb6d] | 24 | // extern int npGen; // obsolete |
---|
[35aab3] | 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); |
---|
[2f3764] | 30 | number npInit (long i, const coeffs r); |
---|
[7d90aa] | 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); |
---|
[03f7b5] | 44 | void npCoeffWrite (const coeffs r, BOOLEAN details); |
---|
[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); |
---|
[35aab3] | 57 | /*-------specials for spolys, do NOT use otherwise--------------------------*/ |
---|
| 58 | /* for npMultM, npSubM, npNegM, npEqualM : */ |
---|
| 59 | #ifdef HAVE_DIV_MOD |
---|
[900802] | 60 | extern unsigned short *npInvTable; |
---|
[35aab3] | 61 | #else |
---|
| 62 | #ifndef HAVE_MULT_MOD |
---|
| 63 | extern long npPminus1M; |
---|
[900802] | 64 | extern unsigned short *npExpTable; |
---|
| 65 | extern unsigned short *npLogTable; |
---|
[35aab3] | 66 | #endif |
---|
| 67 | #endif |
---|
| 68 | |
---|
[e77676] | 69 | // inline number npMultM(number a, number b, int npPrimeM) |
---|
| 70 | // // return (a*b)%n |
---|
| 71 | // { |
---|
| 72 | // double ab; |
---|
| 73 | // long q, res; |
---|
| 74 | // |
---|
| 75 | // ab = ((double) ((int)a)) * ((double) ((int)b)); |
---|
| 76 | // q = (long) (ab/((double) npPrimeM)); // q could be off by (+/-) 1 |
---|
| 77 | // res = (long) (ab - ((double) q)*((double) npPrimeM)); |
---|
| 78 | // res += (res >> 31) & npPrimeM; |
---|
| 79 | // res -= npPrimeM; |
---|
| 80 | // res += (res >> 31) & npPrimeM; |
---|
| 81 | // return (number)res; |
---|
| 82 | // } |
---|
[35aab3] | 83 | #ifdef HAVE_MULT_MOD |
---|
[7d90aa] | 84 | static inline number npMultM(number a, number b, const coeffs r) |
---|
[35aab3] | 85 | { |
---|
| 86 | return (number) |
---|
[e77676] | 87 | ((((unsigned long) a)*((unsigned long) b)) % ((unsigned long) r->ch)); |
---|
[35aab3] | 88 | } |
---|
| 89 | #else |
---|
[7d90aa] | 90 | static inline number npMultM(number a, number b, const coeffs r) |
---|
[35aab3] | 91 | { |
---|
[7d90aa] | 92 | long x = (long)r->npLogTable[(long)a]+ r->npLogTable[(long)b]; |
---|
| 93 | return (number)(long)r->npExpTable[x<r->npPminus1M ? x : x- r->npPminus1M]; |
---|
[35aab3] | 94 | } |
---|
| 95 | #endif |
---|
| 96 | |
---|
| 97 | #if 0 |
---|
| 98 | inline number npAddAsm(number a, number b, int m) |
---|
| 99 | { |
---|
| 100 | number r; |
---|
| 101 | asm ("addl %2, %1; cmpl %3, %1; jb 0f; subl %3, %1; 0:" |
---|
| 102 | : "=&r" (r) |
---|
| 103 | : "%0" (a), "g" (b), "g" (m) |
---|
| 104 | : "cc"); |
---|
| 105 | return r; |
---|
| 106 | } |
---|
| 107 | inline number npSubAsm(number a, number b, int m) |
---|
| 108 | { |
---|
| 109 | number r; |
---|
| 110 | asm ("subl %2, %1; jnc 0f; addl %3, %1; 0:" |
---|
| 111 | : "=&r" (r) |
---|
| 112 | : "%0" (a), "g" (b), "g" (m) |
---|
| 113 | : "cc"); |
---|
| 114 | return r; |
---|
| 115 | } |
---|
| 116 | #endif |
---|
| 117 | #ifdef HAVE_GENERIC_ADD |
---|
[7d90aa] | 118 | static inline number npAddM(number a, number b, const coeffs r) |
---|
[35aab3] | 119 | { |
---|
[7d90aa] | 120 | long R = (long)a + (long)b; |
---|
[e77676] | 121 | return (number)(R >= r->ch ? R - r->ch : R); |
---|
[35aab3] | 122 | } |
---|
[7d90aa] | 123 | static inline number npSubM(number a, number b, const coeffs r) |
---|
[35aab3] | 124 | { |
---|
[6c56a8] | 125 | return (number)((long)a<(long)b ? |
---|
[e77676] | 126 | r->ch-(long)b+(long)a : (long)a-(long)b); |
---|
[35aab3] | 127 | } |
---|
| 128 | #else |
---|
[7d90aa] | 129 | static inline number npAddM(number a, number b, const coeffs r) |
---|
[35aab3] | 130 | { |
---|
[6c56a8] | 131 | long res = ((long)a + (long)b); |
---|
[e77676] | 132 | res -= r->ch; |
---|
[6c56a8] | 133 | #if SIZEOF_LONG == 8 |
---|
[e77676] | 134 | res += (res >> 63) & r->ch; |
---|
[6c56a8] | 135 | #else |
---|
[e77676] | 136 | res += (res >> 31) & r->ch; |
---|
[6c56a8] | 137 | #endif |
---|
[35aab3] | 138 | return (number)res; |
---|
| 139 | } |
---|
[7d90aa] | 140 | static inline number npSubM(number a, number b, const coeffs r) |
---|
[35aab3] | 141 | { |
---|
[6c56a8] | 142 | long res = ((long)a - (long)b); |
---|
| 143 | #if SIZEOF_LONG == 8 |
---|
[e77676] | 144 | res += (res >> 63) & r->ch; |
---|
[6c56a8] | 145 | #else |
---|
[e77676] | 146 | res += (res >> 31) & r->ch; |
---|
[6c56a8] | 147 | #endif |
---|
[35aab3] | 148 | return (number)res; |
---|
| 149 | } |
---|
| 150 | #endif |
---|
| 151 | |
---|
[b58d47] | 152 | static inline BOOLEAN npIsZeroM (number a, const coeffs) |
---|
[35aab3] | 153 | { |
---|
| 154 | return 0 == (long)a; |
---|
| 155 | } |
---|
| 156 | |
---|
[e77676] | 157 | // inline number npMultM(number a, number b, int npPrimeM) |
---|
| 158 | // { |
---|
| 159 | // return (number)(((long)a*(long)b) % npPrimeM); |
---|
| 160 | // } |
---|
[35aab3] | 161 | |
---|
| 162 | |
---|
[e77676] | 163 | #define npNegM(A,r) (number)(r->ch-(long)(A)) |
---|
| 164 | #define npEqualM(A,B,r) ((A)==(B)) |
---|
[35aab3] | 165 | |
---|
| 166 | |
---|
| 167 | #endif |
---|