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