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.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 | void 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 | 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 | number npMap0(number from, const coeffs src, const coeffs r); |
---|
57 | /*-------specials for spolys, do NOT use otherwise--------------------------*/ |
---|
58 | /* for npMultM, npSubM, npNegM, npEqualM : */ |
---|
59 | #ifdef HAVE_DIV_MOD |
---|
60 | extern unsigned short *npInvTable; |
---|
61 | #else |
---|
62 | #ifndef HAVE_MULT_MOD |
---|
63 | extern long npPminus1M; |
---|
64 | extern unsigned short *npExpTable; |
---|
65 | extern unsigned short *npLogTable; |
---|
66 | #endif |
---|
67 | #endif |
---|
68 | |
---|
69 | #if 0 |
---|
70 | inline number npMultM(number a, number b) |
---|
71 | // return (a*b)%n |
---|
72 | { |
---|
73 | double ab; |
---|
74 | long q, res; |
---|
75 | |
---|
76 | ab = ((double) ((int)a)) * ((double) ((int)b)); |
---|
77 | q = (long) (ab/((double) npPrimeM)); // q could be off by (+/-) 1 |
---|
78 | res = (long) (ab - ((double) q)*((double) npPrimeM)); |
---|
79 | res += (res >> 31) & npPrimeM; |
---|
80 | res -= npPrimeM; |
---|
81 | res += (res >> 31) & npPrimeM; |
---|
82 | return (number)res; |
---|
83 | } |
---|
84 | #endif |
---|
85 | #ifdef HAVE_MULT_MOD |
---|
86 | static inline number npMultM(number a, number b, const coeffs r) |
---|
87 | { |
---|
88 | return (number) |
---|
89 | ((((unsigned long) a)*((unsigned long) b)) % ((unsigned long) r->npPrimeM)); |
---|
90 | } |
---|
91 | #else |
---|
92 | static inline number npMultM(number a, number b, const coeffs r) |
---|
93 | { |
---|
94 | long x = (long)r->npLogTable[(long)a]+ r->npLogTable[(long)b]; |
---|
95 | return (number)(long)r->npExpTable[x<r->npPminus1M ? x : x- r->npPminus1M]; |
---|
96 | } |
---|
97 | #endif |
---|
98 | |
---|
99 | #if 0 |
---|
100 | inline number npAddAsm(number a, number b, int m) |
---|
101 | { |
---|
102 | number r; |
---|
103 | asm ("addl %2, %1; cmpl %3, %1; jb 0f; subl %3, %1; 0:" |
---|
104 | : "=&r" (r) |
---|
105 | : "%0" (a), "g" (b), "g" (m) |
---|
106 | : "cc"); |
---|
107 | return r; |
---|
108 | } |
---|
109 | inline number npSubAsm(number a, number b, int m) |
---|
110 | { |
---|
111 | number r; |
---|
112 | asm ("subl %2, %1; jnc 0f; addl %3, %1; 0:" |
---|
113 | : "=&r" (r) |
---|
114 | : "%0" (a), "g" (b), "g" (m) |
---|
115 | : "cc"); |
---|
116 | return r; |
---|
117 | } |
---|
118 | #endif |
---|
119 | #ifdef HAVE_GENERIC_ADD |
---|
120 | static inline number npAddM(number a, number b, const coeffs r) |
---|
121 | { |
---|
122 | long R = (long)a + (long)b; |
---|
123 | return (number)(R >= r->npPrimeM ? R - r->npPrimeM : R); |
---|
124 | } |
---|
125 | static inline number npSubM(number a, number b, const coeffs r) |
---|
126 | { |
---|
127 | return (number)((long)a<(long)b ? |
---|
128 | r->npPrimeM-(long)b+(long)a : (long)a-(long)b); |
---|
129 | } |
---|
130 | #else |
---|
131 | static inline number npAddM(number a, number b, const coeffs r) |
---|
132 | { |
---|
133 | long res = ((long)a + (long)b); |
---|
134 | res -= r->npPrimeM; |
---|
135 | #if SIZEOF_LONG == 8 |
---|
136 | res += (res >> 63) & r->npPrimeM; |
---|
137 | #else |
---|
138 | res += (res >> 31) & r->npPrimeM; |
---|
139 | #endif |
---|
140 | return (number)res; |
---|
141 | } |
---|
142 | static inline number npSubM(number a, number b, const coeffs r) |
---|
143 | { |
---|
144 | long res = ((long)a - (long)b); |
---|
145 | #if SIZEOF_LONG == 8 |
---|
146 | res += (res >> 63) & r->npPrimeM; |
---|
147 | #else |
---|
148 | res += (res >> 31) & r->npPrimeM; |
---|
149 | #endif |
---|
150 | return (number)res; |
---|
151 | } |
---|
152 | #endif |
---|
153 | |
---|
154 | static inline BOOLEAN npIsZeroM (number a, const coeffs r) |
---|
155 | { |
---|
156 | return 0 == (long)a; |
---|
157 | } |
---|
158 | |
---|
159 | /* |
---|
160 | *inline number npMultM(number a, number b) |
---|
161 | *{ |
---|
162 | * return (number)(((long)a*(long)b) % npPrimeM); |
---|
163 | *} |
---|
164 | */ |
---|
165 | |
---|
166 | #define npNegM(A,r) (number)(r->npPrimeM-(long)(A)) |
---|
167 | #define npEqualM(A,B,r) ((A)==(B)) |
---|
168 | |
---|
169 | |
---|
170 | #ifdef NV_OPS |
---|
171 | #pragma GCC diagnostic ignored "-Wlong-long" |
---|
172 | static inline number nvMultM(number a, number b, const coeffs r) |
---|
173 | { |
---|
174 | #if SIZEOF_LONG == 4 |
---|
175 | #define ULONG64 (unsigned long long)(unsigned long) |
---|
176 | #else |
---|
177 | #define ULONG64 (unsigned long) |
---|
178 | #endif |
---|
179 | return (number) |
---|
180 | (unsigned long)((ULONG64 a)*(ULONG64 b) % (ULONG64 r->npPrimeM)); |
---|
181 | } |
---|
182 | number nvMult (number a, number b, const coeffs r); |
---|
183 | number nvDiv (number a, number b, const coeffs r); |
---|
184 | number nvInvers (number c, const coeffs r); |
---|
185 | void nvPower (number a, int i, number * result, const coeffs r); |
---|
186 | #endif |
---|
187 | |
---|
188 | #endif |
---|