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; // obsolete |
---|
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, BOOLEAN details); |
---|
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 | /*-------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 | // inline number npMultM(number a, number b, int npPrimeM) |
---|
70 | // // return (a*b)%n |
---|
71 | // { |
---|
72 | // double ab; |
---|
73 | // long q, res; |
---|
74 | // |
---|
75 | // ab = ((double) ((int)a)) * ((double) ((int)b)); |
---|
76 | // q = (long) (ab/((double) npPrimeM)); // q could be off by (+/-) 1 |
---|
77 | // res = (long) (ab - ((double) q)*((double) npPrimeM)); |
---|
78 | // res += (res >> 31) & npPrimeM; |
---|
79 | // res -= npPrimeM; |
---|
80 | // res += (res >> 31) & npPrimeM; |
---|
81 | // return (number)res; |
---|
82 | // } |
---|
83 | #ifdef HAVE_MULT_MOD |
---|
84 | static inline number npMultM(number a, number b, const coeffs r) |
---|
85 | { |
---|
86 | return (number) |
---|
87 | ((((unsigned long) a)*((unsigned long) b)) % ((unsigned long) r->ch)); |
---|
88 | } |
---|
89 | #else |
---|
90 | static inline number npMultM(number a, number b, const coeffs r) |
---|
91 | { |
---|
92 | long x = (long)r->npLogTable[(long)a]+ r->npLogTable[(long)b]; |
---|
93 | return (number)(long)r->npExpTable[x<r->npPminus1M ? x : x- r->npPminus1M]; |
---|
94 | } |
---|
95 | #endif |
---|
96 | |
---|
97 | #if 0 |
---|
98 | inline number npAddAsm(number a, number b, int m) |
---|
99 | { |
---|
100 | number r; |
---|
101 | asm ("addl %2, %1; cmpl %3, %1; jb 0f; subl %3, %1; 0:" |
---|
102 | : "=&r" (r) |
---|
103 | : "%0" (a), "g" (b), "g" (m) |
---|
104 | : "cc"); |
---|
105 | return r; |
---|
106 | } |
---|
107 | inline number npSubAsm(number a, number b, int m) |
---|
108 | { |
---|
109 | number r; |
---|
110 | asm ("subl %2, %1; jnc 0f; addl %3, %1; 0:" |
---|
111 | : "=&r" (r) |
---|
112 | : "%0" (a), "g" (b), "g" (m) |
---|
113 | : "cc"); |
---|
114 | return r; |
---|
115 | } |
---|
116 | #endif |
---|
117 | #ifdef HAVE_GENERIC_ADD |
---|
118 | static inline number npAddM(number a, number b, const coeffs r) |
---|
119 | { |
---|
120 | long R = (long)a + (long)b; |
---|
121 | return (number)(R >= r->ch ? R - r->ch : R); |
---|
122 | } |
---|
123 | static inline number npSubM(number a, number b, const coeffs r) |
---|
124 | { |
---|
125 | return (number)((long)a<(long)b ? |
---|
126 | r->ch-(long)b+(long)a : (long)a-(long)b); |
---|
127 | } |
---|
128 | #else |
---|
129 | static inline number npAddM(number a, number b, const coeffs r) |
---|
130 | { |
---|
131 | long res = ((long)a + (long)b); |
---|
132 | res -= r->ch; |
---|
133 | #if SIZEOF_LONG == 8 |
---|
134 | res += (res >> 63) & r->ch; |
---|
135 | #else |
---|
136 | res += (res >> 31) & r->ch; |
---|
137 | #endif |
---|
138 | return (number)res; |
---|
139 | } |
---|
140 | static inline number npSubM(number a, number b, const coeffs r) |
---|
141 | { |
---|
142 | long res = ((long)a - (long)b); |
---|
143 | #if SIZEOF_LONG == 8 |
---|
144 | res += (res >> 63) & r->ch; |
---|
145 | #else |
---|
146 | res += (res >> 31) & r->ch; |
---|
147 | #endif |
---|
148 | return (number)res; |
---|
149 | } |
---|
150 | #endif |
---|
151 | |
---|
152 | static inline BOOLEAN npIsZeroM (number a, const coeffs) |
---|
153 | { |
---|
154 | return 0 == (long)a; |
---|
155 | } |
---|
156 | |
---|
157 | // inline number npMultM(number a, number b, int npPrimeM) |
---|
158 | // { |
---|
159 | // return (number)(((long)a*(long)b) % npPrimeM); |
---|
160 | // } |
---|
161 | |
---|
162 | |
---|
163 | #define npNegM(A,r) (number)(r->ch-(long)(A)) |
---|
164 | #define npEqualM(A,B,r) ((A)==(B)) |
---|
165 | |
---|
166 | |
---|
167 | #endif |
---|