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