1 | #ifndef MODULOP_H |
---|
2 | #define MODULOP_H |
---|
3 | /**************************************** |
---|
4 | * Computer Algebra System SINGULAR * |
---|
5 | ****************************************/ |
---|
6 | /* |
---|
7 | * ABSTRACT: numbers modulo p (<=32003) |
---|
8 | */ |
---|
9 | #include <coeffs/coeffs.h> |
---|
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 | |
---|
23 | // extern int npGen; // obsolete |
---|
24 | |
---|
25 | BOOLEAN npInitChar(coeffs r, void* p); |
---|
26 | |
---|
27 | BOOLEAN npGreaterZero (number k, const coeffs r); |
---|
28 | number npMult (number a, number b, const coeffs r); |
---|
29 | number npInit (long i, const coeffs r); |
---|
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); |
---|
43 | void npCoeffWrite (const coeffs r, BOOLEAN details); |
---|
44 | const char * npRead (const char *s, number *a,const coeffs r); |
---|
45 | #ifdef LDEBUG |
---|
46 | BOOLEAN npDBTest (number a, const char *f, const int l, const coeffs r); |
---|
47 | #define npTest(A,r) npDBTest(A,__FILE__,__LINE__, r) |
---|
48 | #else |
---|
49 | #define npTest(A,r) (0) |
---|
50 | #endif |
---|
51 | |
---|
52 | //int npGetChar(); |
---|
53 | |
---|
54 | nMapFunc npSetMap(const coeffs src, const coeffs dst); |
---|
55 | number npMapP(number from, const coeffs src, const coeffs r); |
---|
56 | /*-------specials for spolys, do NOT use otherwise--------------------------*/ |
---|
57 | /* for npMultM, npSubM, npNegM, npEqualM : */ |
---|
58 | #ifdef HAVE_DIV_MOD |
---|
59 | extern unsigned short *npInvTable; |
---|
60 | #else |
---|
61 | #ifndef HAVE_MULT_MOD |
---|
62 | extern long npPminus1M; |
---|
63 | extern unsigned short *npExpTable; |
---|
64 | extern unsigned short *npLogTable; |
---|
65 | #endif |
---|
66 | #endif |
---|
67 | |
---|
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 | // } |
---|
82 | #ifdef HAVE_MULT_MOD |
---|
83 | static inline number npMultM(number a, number b, const coeffs r) |
---|
84 | { |
---|
85 | return (number) |
---|
86 | ((((unsigned long) a)*((unsigned long) b)) % ((unsigned long) r->ch)); |
---|
87 | } |
---|
88 | #else |
---|
89 | static inline number npMultM(number a, number b, const coeffs r) |
---|
90 | { |
---|
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]; |
---|
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 |
---|
117 | static inline number npAddM(number a, number b, const coeffs r) |
---|
118 | { |
---|
119 | long R = (long)a + (long)b; |
---|
120 | return (number)(R >= r->ch ? R - r->ch : R); |
---|
121 | } |
---|
122 | static inline number npSubM(number a, number b, const coeffs r) |
---|
123 | { |
---|
124 | return (number)((long)a<(long)b ? |
---|
125 | r->ch-(long)b+(long)a : (long)a-(long)b); |
---|
126 | } |
---|
127 | #else |
---|
128 | static inline number npAddM(number a, number b, const coeffs r) |
---|
129 | { |
---|
130 | long res = ((long)a + (long)b); |
---|
131 | res -= r->ch; |
---|
132 | #if SIZEOF_LONG == 8 |
---|
133 | res += (res >> 63) & r->ch; |
---|
134 | #else |
---|
135 | res += (res >> 31) & r->ch; |
---|
136 | #endif |
---|
137 | return (number)res; |
---|
138 | } |
---|
139 | static inline number npSubM(number a, number b, const coeffs r) |
---|
140 | { |
---|
141 | long res = ((long)a - (long)b); |
---|
142 | #if SIZEOF_LONG == 8 |
---|
143 | res += (res >> 63) & r->ch; |
---|
144 | #else |
---|
145 | res += (res >> 31) & r->ch; |
---|
146 | #endif |
---|
147 | return (number)res; |
---|
148 | } |
---|
149 | #endif |
---|
150 | |
---|
151 | static inline BOOLEAN npIsZeroM (number a, const coeffs) |
---|
152 | { |
---|
153 | return 0 == (long)a; |
---|
154 | } |
---|
155 | |
---|
156 | // inline number npMultM(number a, number b, int npPrimeM) |
---|
157 | // { |
---|
158 | // return (number)(((long)a*(long)b) % npPrimeM); |
---|
159 | // } |
---|
160 | |
---|
161 | |
---|
162 | #define npNegM(A,r) (number)(r->ch-(long)(A)) |
---|
163 | #define npEqualM(A,B,r) ((A)==(B)) |
---|
164 | |
---|
165 | |
---|
166 | #endif |
---|