1 | #ifndef MODULOP_H |
---|
2 | #define MODULOP_H |
---|
3 | /**************************************** |
---|
4 | * Computer Algebra System SINGULAR * |
---|
5 | ****************************************/ |
---|
6 | /* $Id$ */ |
---|
7 | /* |
---|
8 | * ABSTRACT: numbers modulo p (<=32003) |
---|
9 | */ |
---|
10 | #include <coeffs/coeffs.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 int npGen; |
---|
25 | |
---|
26 | BOOLEAN npInitChar(coeffs r, void* p); |
---|
27 | |
---|
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); |
---|
44 | void npCoeffWrite (const coeffs r); |
---|
45 | const char * npRead (const char *s, number *a,const coeffs r); |
---|
46 | #ifdef LDEBUG |
---|
47 | BOOLEAN npDBTest (number a, const char *f, const int l, const coeffs r); |
---|
48 | #define npTest(A,r) npDBTest(A,__FILE__,__LINE__, r) |
---|
49 | #else |
---|
50 | #define npTest(A,r) (0) |
---|
51 | #endif |
---|
52 | |
---|
53 | //int npGetChar(); |
---|
54 | |
---|
55 | nMapFunc npSetMap(const coeffs src, const coeffs dst); |
---|
56 | number npMapP(number from, const coeffs src, const coeffs r); |
---|
57 | number npMap0(number from, const coeffs src, const coeffs r); |
---|
58 | /*-------specials for spolys, do NOT use otherwise--------------------------*/ |
---|
59 | /* for npMultM, npSubM, npNegM, npEqualM : */ |
---|
60 | #ifdef HAVE_DIV_MOD |
---|
61 | extern unsigned short *npInvTable; |
---|
62 | #else |
---|
63 | #ifndef HAVE_MULT_MOD |
---|
64 | extern long npPminus1M; |
---|
65 | extern unsigned short *npExpTable; |
---|
66 | extern unsigned short *npLogTable; |
---|
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 |
---|
87 | static inline number npMultM(number a, number b, const coeffs r) |
---|
88 | { |
---|
89 | return (number) |
---|
90 | ((((unsigned long) a)*((unsigned long) b)) % ((unsigned long) r->npPrimeM)); |
---|
91 | } |
---|
92 | #else |
---|
93 | static inline number npMultM(number a, number b, const coeffs r) |
---|
94 | { |
---|
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]; |
---|
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 |
---|
121 | static inline number npAddM(number a, number b, const coeffs r) |
---|
122 | { |
---|
123 | long R = (long)a + (long)b; |
---|
124 | return (number)(R >= r->npPrimeM ? R - r->npPrimeM : R); |
---|
125 | } |
---|
126 | static inline number npSubM(number a, number b, const coeffs r) |
---|
127 | { |
---|
128 | return (number)((long)a<(long)b ? |
---|
129 | r->npPrimeM-(long)b+(long)a : (long)a-(long)b); |
---|
130 | } |
---|
131 | #else |
---|
132 | static inline number npAddM(number a, number b, const coeffs r) |
---|
133 | { |
---|
134 | long res = ((long)a + (long)b); |
---|
135 | res -= r->npPrimeM; |
---|
136 | #if SIZEOF_LONG == 8 |
---|
137 | res += (res >> 63) & r->npPrimeM; |
---|
138 | #else |
---|
139 | res += (res >> 31) & r->npPrimeM; |
---|
140 | #endif |
---|
141 | return (number)res; |
---|
142 | } |
---|
143 | static inline number npSubM(number a, number b, const coeffs r) |
---|
144 | { |
---|
145 | long res = ((long)a - (long)b); |
---|
146 | #if SIZEOF_LONG == 8 |
---|
147 | res += (res >> 63) & r->npPrimeM; |
---|
148 | #else |
---|
149 | res += (res >> 31) & r->npPrimeM; |
---|
150 | #endif |
---|
151 | return (number)res; |
---|
152 | } |
---|
153 | #endif |
---|
154 | |
---|
155 | static inline BOOLEAN npIsZeroM (number a, const coeffs) |
---|
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 | |
---|
167 | #define npNegM(A,r) (number)(r->npPrimeM-(long)(A)) |
---|
168 | #define npEqualM(A,B,r) ((A)==(B)) |
---|
169 | |
---|
170 | |
---|
171 | #ifdef NV_OPS |
---|
172 | #pragma GCC diagnostic ignored "-Wlong-long" |
---|
173 | static inline number nvMultM(number a, number b, const coeffs r) |
---|
174 | { |
---|
175 | #if SIZEOF_LONG == 4 |
---|
176 | #define ULONG64 (unsigned long long)(unsigned long) |
---|
177 | #else |
---|
178 | #define ULONG64 (unsigned long) |
---|
179 | #endif |
---|
180 | return (number) |
---|
181 | (unsigned long)((ULONG64 a)*(ULONG64 b) % (ULONG64 r->npPrimeM)); |
---|
182 | } |
---|
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); |
---|
187 | #endif |
---|
188 | |
---|
189 | #endif |
---|