1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* |
---|
5 | * ABSTRACT: numbers modulo 2^m |
---|
6 | */ |
---|
7 | #include "misc/auxiliary.h" |
---|
8 | |
---|
9 | #include "omalloc/omalloc.h" |
---|
10 | |
---|
11 | #include "misc/mylimits.h" |
---|
12 | #include "reporter/reporter.h" |
---|
13 | |
---|
14 | #include "coeffs/si_gmp.h" |
---|
15 | #include "coeffs/coeffs.h" |
---|
16 | #include "coeffs/numbers.h" |
---|
17 | #include "coeffs/longrat.h" |
---|
18 | #include "coeffs/mpr_complex.h" |
---|
19 | |
---|
20 | #include "coeffs/rmodulo2m.h" |
---|
21 | #include "coeffs/rmodulon.h" |
---|
22 | |
---|
23 | #include <string.h> |
---|
24 | |
---|
25 | #ifdef HAVE_RINGS |
---|
26 | |
---|
27 | static inline number nr2mMultM(number a, number b, const coeffs r) |
---|
28 | { |
---|
29 | return (number) |
---|
30 | ((((unsigned long) a) * ((unsigned long) b)) & ((unsigned long)r->mod2mMask)); |
---|
31 | } |
---|
32 | |
---|
33 | static inline number nr2mAddM(number a, number b, const coeffs r) |
---|
34 | { |
---|
35 | return (number) |
---|
36 | ((((unsigned long) a) + ((unsigned long) b)) & ((unsigned long)r->mod2mMask)); |
---|
37 | } |
---|
38 | |
---|
39 | static inline number nr2mSubM(number a, number b, const coeffs r) |
---|
40 | { |
---|
41 | return (number)((unsigned long)a < (unsigned long)b ? |
---|
42 | r->mod2mMask - (unsigned long)b + (unsigned long)a + 1: |
---|
43 | (unsigned long)a - (unsigned long)b); |
---|
44 | } |
---|
45 | |
---|
46 | #define nr2mNegM(A,r) (number)((r->mod2mMask - (unsigned long)(A) + 1) & r->mod2mMask) |
---|
47 | #define nr2mEqualM(A,B) ((A)==(B)) |
---|
48 | |
---|
49 | extern omBin gmp_nrz_bin; /* init in rintegers*/ |
---|
50 | |
---|
51 | static char* nr2mCoeffName(const coeffs cf) |
---|
52 | { |
---|
53 | static char n2mCoeffName_buf[22]; |
---|
54 | snprintf(n2mCoeffName_buf,21,"ZZ/(2^%lu)",cf->modExponent); |
---|
55 | return n2mCoeffName_buf; |
---|
56 | } |
---|
57 | |
---|
58 | static void nr2mCoeffWrite (const coeffs r, BOOLEAN /*details*/) |
---|
59 | { |
---|
60 | Print("ZZ/(2^%lu)", r->modExponent); |
---|
61 | } |
---|
62 | |
---|
63 | static BOOLEAN nr2mCoeffIsEqual(const coeffs r, n_coeffType n, void * p) |
---|
64 | { |
---|
65 | if (n==n_Z2m) |
---|
66 | { |
---|
67 | int m=(int)(long)(p); |
---|
68 | unsigned long mm=r->mod2mMask; |
---|
69 | if (((mm+1)>>m)==1L) return TRUE; |
---|
70 | } |
---|
71 | return FALSE; |
---|
72 | } |
---|
73 | |
---|
74 | static char* nr2mCoeffString(const coeffs r) |
---|
75 | { |
---|
76 | // r->modExponent <=bitsize(long) |
---|
77 | char* s = (char*) omAlloc(11+11); |
---|
78 | sprintf(s,"ZZ/(2^%lu)",r->modExponent); |
---|
79 | return s; |
---|
80 | } |
---|
81 | |
---|
82 | static coeffs nr2mQuot1(number c, const coeffs r) |
---|
83 | { |
---|
84 | coeffs rr; |
---|
85 | long ch = r->cfInt(c, r); |
---|
86 | mpz_t a,b; |
---|
87 | mpz_init_set(a, r->modNumber); |
---|
88 | mpz_init_set_ui(b, ch); |
---|
89 | mpz_ptr gcd; |
---|
90 | gcd = (mpz_ptr) omAlloc(sizeof(mpz_t)); |
---|
91 | mpz_init(gcd); |
---|
92 | mpz_gcd(gcd, a,b); |
---|
93 | if(mpz_cmp_ui(gcd, 1) == 0) |
---|
94 | { |
---|
95 | WerrorS("constant in q-ideal is coprime to modulus in ground ring"); |
---|
96 | WerrorS("Unable to create qring!"); |
---|
97 | return NULL; |
---|
98 | } |
---|
99 | if(mpz_cmp_ui(gcd, 2) == 0) |
---|
100 | { |
---|
101 | rr = nInitChar(n_Zp, (void*)2); |
---|
102 | } |
---|
103 | else |
---|
104 | { |
---|
105 | int kNew = 1; |
---|
106 | mpz_t baseTokNew; |
---|
107 | mpz_init(baseTokNew); |
---|
108 | mpz_set(baseTokNew, r->modBase); |
---|
109 | while(mpz_cmp(gcd, baseTokNew) > 0) |
---|
110 | { |
---|
111 | kNew++; |
---|
112 | mpz_mul(baseTokNew, baseTokNew, r->modBase); |
---|
113 | } |
---|
114 | mpz_clear(baseTokNew); |
---|
115 | rr = nInitChar(n_Z2m, (void*)(long)kNew); |
---|
116 | } |
---|
117 | return(rr); |
---|
118 | } |
---|
119 | |
---|
120 | /* TRUE iff 0 < k <= 2^m / 2 */ |
---|
121 | static BOOLEAN nr2mGreaterZero(number k, const coeffs r) |
---|
122 | { |
---|
123 | if ((unsigned long)k == 0) return FALSE; |
---|
124 | if ((unsigned long)k > ((r->mod2mMask >> 1) + 1)) return FALSE; |
---|
125 | return TRUE; |
---|
126 | } |
---|
127 | |
---|
128 | /* |
---|
129 | * Multiply two numbers |
---|
130 | */ |
---|
131 | static number nr2mMult(number a, number b, const coeffs r) |
---|
132 | { |
---|
133 | if (((unsigned long)a == 0) || ((unsigned long)b == 0)) |
---|
134 | return (number)0; |
---|
135 | else |
---|
136 | return nr2mMultM(a, b, r); |
---|
137 | } |
---|
138 | |
---|
139 | static number nr2mAnn(number b, const coeffs r); |
---|
140 | /* |
---|
141 | * Give the smallest k, such that a * x = k = b * y has a solution |
---|
142 | */ |
---|
143 | static number nr2mLcm(number a, number b, const coeffs) |
---|
144 | { |
---|
145 | unsigned long res = 0; |
---|
146 | if ((unsigned long)a == 0) a = (number) 1; |
---|
147 | if ((unsigned long)b == 0) b = (number) 1; |
---|
148 | while ((unsigned long)a % 2 == 0) |
---|
149 | { |
---|
150 | a = (number)((unsigned long)a / 2); |
---|
151 | if ((unsigned long)b % 2 == 0) b = (number)((unsigned long)b / 2); |
---|
152 | res++; |
---|
153 | } |
---|
154 | while ((unsigned long)b % 2 == 0) |
---|
155 | { |
---|
156 | b = (number)((unsigned long)b / 2); |
---|
157 | res++; |
---|
158 | } |
---|
159 | return (number)(1L << res); // (2**res) |
---|
160 | } |
---|
161 | |
---|
162 | /* |
---|
163 | * Give the largest k, such that a = x * k, b = y * k has |
---|
164 | * a solution. |
---|
165 | */ |
---|
166 | static number nr2mGcd(number a, number b, const coeffs) |
---|
167 | { |
---|
168 | unsigned long res = 0; |
---|
169 | if ((unsigned long)a == 0 && (unsigned long)b == 0) return (number)1; |
---|
170 | while ((unsigned long)a % 2 == 0 && (unsigned long)b % 2 == 0) |
---|
171 | { |
---|
172 | a = (number)((unsigned long)a / 2); |
---|
173 | b = (number)((unsigned long)b / 2); |
---|
174 | res++; |
---|
175 | } |
---|
176 | // if ((unsigned long)b % 2 == 0) |
---|
177 | // { |
---|
178 | // return (number)((1L << res)); // * (unsigned long) a); // (2**res)*a a is a unit |
---|
179 | // } |
---|
180 | // else |
---|
181 | // { |
---|
182 | return (number)((1L << res)); // * (unsigned long) b); // (2**res)*b b is a unit |
---|
183 | // } |
---|
184 | } |
---|
185 | |
---|
186 | /* assumes that 'a' is odd, i.e., a unit in Z/2^m, and computes |
---|
187 | the extended gcd of 'a' and 2^m, in order to find some 's' |
---|
188 | and 't' such that a * s + 2^m * t = gcd(a, 2^m) = 1; |
---|
189 | this code will always find a positive 's' */ |
---|
190 | static void specialXGCD(unsigned long& s, unsigned long a, const coeffs r) |
---|
191 | { |
---|
192 | mpz_ptr u = (mpz_ptr)omAlloc(sizeof(mpz_t)); |
---|
193 | mpz_init_set_ui(u, a); |
---|
194 | mpz_ptr u0 = (mpz_ptr)omAlloc(sizeof(mpz_t)); |
---|
195 | mpz_init(u0); |
---|
196 | mpz_ptr u1 = (mpz_ptr)omAlloc(sizeof(mpz_t)); |
---|
197 | mpz_init_set_ui(u1, 1); |
---|
198 | mpz_ptr u2 = (mpz_ptr)omAlloc(sizeof(mpz_t)); |
---|
199 | mpz_init(u2); |
---|
200 | mpz_ptr v = (mpz_ptr)omAlloc(sizeof(mpz_t)); |
---|
201 | mpz_init_set_ui(v, r->mod2mMask); |
---|
202 | mpz_add_ui(v, v, 1); /* now: v = 2^m */ |
---|
203 | mpz_ptr v0 = (mpz_ptr)omAlloc(sizeof(mpz_t)); |
---|
204 | mpz_init(v0); |
---|
205 | mpz_ptr v1 = (mpz_ptr)omAlloc(sizeof(mpz_t)); |
---|
206 | mpz_init(v1); |
---|
207 | mpz_ptr v2 = (mpz_ptr)omAlloc(sizeof(mpz_t)); |
---|
208 | mpz_init_set_ui(v2, 1); |
---|
209 | mpz_ptr q = (mpz_ptr)omAlloc(sizeof(mpz_t)); |
---|
210 | mpz_init(q); |
---|
211 | mpz_ptr rr = (mpz_ptr)omAlloc(sizeof(mpz_t)); |
---|
212 | mpz_init(rr); |
---|
213 | |
---|
214 | while (mpz_sgn1(v) != 0) /* i.e., while v != 0 */ |
---|
215 | { |
---|
216 | mpz_div(q, u, v); |
---|
217 | mpz_mod(rr, u, v); |
---|
218 | mpz_set(u, v); |
---|
219 | mpz_set(v, rr); |
---|
220 | mpz_set(u0, u2); |
---|
221 | mpz_set(v0, v2); |
---|
222 | mpz_mul(u2, u2, q); mpz_sub(u2, u1, u2); /* u2 = u1 - q * u2 */ |
---|
223 | mpz_mul(v2, v2, q); mpz_sub(v2, v1, v2); /* v2 = v1 - q * v2 */ |
---|
224 | mpz_set(u1, u0); |
---|
225 | mpz_set(v1, v0); |
---|
226 | } |
---|
227 | |
---|
228 | while (mpz_sgn1(u1) < 0) /* i.e., while u1 < 0 */ |
---|
229 | { |
---|
230 | /* we add 2^m = (2^m - 1) + 1 to u1: */ |
---|
231 | mpz_add_ui(u1, u1, r->mod2mMask); |
---|
232 | mpz_add_ui(u1, u1, 1); |
---|
233 | } |
---|
234 | s = mpz_get_ui(u1); /* now: 0 <= s <= 2^m - 1 */ |
---|
235 | |
---|
236 | mpz_clear(u); omFree((ADDRESS)u); |
---|
237 | mpz_clear(u0); omFree((ADDRESS)u0); |
---|
238 | mpz_clear(u1); omFree((ADDRESS)u1); |
---|
239 | mpz_clear(u2); omFree((ADDRESS)u2); |
---|
240 | mpz_clear(v); omFree((ADDRESS)v); |
---|
241 | mpz_clear(v0); omFree((ADDRESS)v0); |
---|
242 | mpz_clear(v1); omFree((ADDRESS)v1); |
---|
243 | mpz_clear(v2); omFree((ADDRESS)v2); |
---|
244 | mpz_clear(q); omFree((ADDRESS)q); |
---|
245 | mpz_clear(rr); omFree((ADDRESS)rr); |
---|
246 | } |
---|
247 | |
---|
248 | static unsigned long InvMod(unsigned long a, const coeffs r) |
---|
249 | { |
---|
250 | assume((unsigned long)a % 2 != 0); |
---|
251 | unsigned long s; |
---|
252 | specialXGCD(s, a, r); |
---|
253 | return s; |
---|
254 | } |
---|
255 | |
---|
256 | static inline number nr2mInversM(number c, const coeffs r) |
---|
257 | { |
---|
258 | assume((unsigned long)c % 2 != 0); |
---|
259 | // Table !!! |
---|
260 | unsigned long inv; |
---|
261 | inv = InvMod((unsigned long)c,r); |
---|
262 | return (number)inv; |
---|
263 | } |
---|
264 | |
---|
265 | static number nr2mInvers(number c, const coeffs r) |
---|
266 | { |
---|
267 | if ((unsigned long)c % 2 == 0) |
---|
268 | { |
---|
269 | WerrorS("division by zero divisor"); |
---|
270 | return (number)0; |
---|
271 | } |
---|
272 | return nr2mInversM(c, r); |
---|
273 | } |
---|
274 | |
---|
275 | /* |
---|
276 | * Give the largest k, such that a = x * k, b = y * k has |
---|
277 | * a solution. |
---|
278 | */ |
---|
279 | static number nr2mExtGcd(number a, number b, number *s, number *t, const coeffs r) |
---|
280 | { |
---|
281 | unsigned long res = 0; |
---|
282 | if ((unsigned long)a == 0 && (unsigned long)b == 0) return (number)1; |
---|
283 | while ((unsigned long)a % 2 == 0 && (unsigned long)b % 2 == 0) |
---|
284 | { |
---|
285 | a = (number)((unsigned long)a / 2); |
---|
286 | b = (number)((unsigned long)b / 2); |
---|
287 | res++; |
---|
288 | } |
---|
289 | if ((unsigned long)b % 2 == 0) |
---|
290 | { |
---|
291 | *t = NULL; |
---|
292 | *s = nr2mInvers(a,r); |
---|
293 | return (number)((1L << res)); // * (unsigned long) a); // (2**res)*a a is a unit |
---|
294 | } |
---|
295 | else |
---|
296 | { |
---|
297 | *s = NULL; |
---|
298 | *t = nr2mInvers(b,r); |
---|
299 | return (number)((1L << res)); // * (unsigned long) b); // (2**res)*b b is a unit |
---|
300 | } |
---|
301 | } |
---|
302 | |
---|
303 | static void nr2mPower(number a, int i, number * result, const coeffs r) |
---|
304 | { |
---|
305 | if (i == 0) |
---|
306 | { |
---|
307 | *(unsigned long *)result = 1; |
---|
308 | } |
---|
309 | else if (i == 1) |
---|
310 | { |
---|
311 | *result = a; |
---|
312 | } |
---|
313 | else |
---|
314 | { |
---|
315 | nr2mPower(a, i-1, result, r); |
---|
316 | *result = nr2mMultM(a, *result, r); |
---|
317 | } |
---|
318 | } |
---|
319 | |
---|
320 | /* |
---|
321 | * create a number from int |
---|
322 | */ |
---|
323 | static number nr2mInit(long i, const coeffs r) |
---|
324 | { |
---|
325 | if (i == 0) return (number)(unsigned long)i; |
---|
326 | |
---|
327 | long ii = i; |
---|
328 | unsigned long j = (unsigned long)1; |
---|
329 | if (ii < 0) { j = r->mod2mMask; ii = -ii; } |
---|
330 | unsigned long k = (unsigned long)ii; |
---|
331 | k = k & r->mod2mMask; |
---|
332 | /* now we have: i = j * k mod 2^m */ |
---|
333 | return (number)nr2mMult((number)j, (number)k, r); |
---|
334 | } |
---|
335 | |
---|
336 | /* |
---|
337 | * convert a number to an int in ]-k/2 .. k/2], |
---|
338 | * where k = 2^m; i.e., an int in ]-2^(m-1) .. 2^(m-1)]; |
---|
339 | */ |
---|
340 | static long nr2mInt(number &n, const coeffs r) |
---|
341 | { |
---|
342 | unsigned long nn = (unsigned long)(unsigned long)n & r->mod2mMask; |
---|
343 | unsigned long l = r->mod2mMask >> 1; l++; /* now: l = 2^(m-1) */ |
---|
344 | if ((unsigned long)nn > l) |
---|
345 | return (long)((unsigned long)nn - r->mod2mMask - 1); |
---|
346 | else |
---|
347 | return (long)((unsigned long)nn); |
---|
348 | } |
---|
349 | |
---|
350 | static number nr2mAdd(number a, number b, const coeffs r) |
---|
351 | { |
---|
352 | return nr2mAddM(a, b, r); |
---|
353 | } |
---|
354 | |
---|
355 | static number nr2mSub(number a, number b, const coeffs r) |
---|
356 | { |
---|
357 | return nr2mSubM(a, b, r); |
---|
358 | } |
---|
359 | |
---|
360 | static BOOLEAN nr2mIsUnit(number a, const coeffs) |
---|
361 | { |
---|
362 | return ((unsigned long)a % 2 == 1); |
---|
363 | } |
---|
364 | |
---|
365 | static number nr2mGetUnit(number k, const coeffs) |
---|
366 | { |
---|
367 | if (k == NULL) return (number)1; |
---|
368 | unsigned long erg = (unsigned long)k; |
---|
369 | while (erg % 2 == 0) erg = erg / 2; |
---|
370 | return (number)erg; |
---|
371 | } |
---|
372 | |
---|
373 | static BOOLEAN nr2mIsZero(number a, const coeffs) |
---|
374 | { |
---|
375 | return 0 == (unsigned long)a; |
---|
376 | } |
---|
377 | |
---|
378 | static BOOLEAN nr2mIsOne(number a, const coeffs) |
---|
379 | { |
---|
380 | return 1 == (unsigned long)a; |
---|
381 | } |
---|
382 | |
---|
383 | static BOOLEAN nr2mIsMOne(number a, const coeffs r) |
---|
384 | { |
---|
385 | return ((r->mod2mMask == (unsigned long)a) &&(1L!=(long)a))/*for char 2^1*/; |
---|
386 | } |
---|
387 | |
---|
388 | static BOOLEAN nr2mEqual(number a, number b, const coeffs) |
---|
389 | { |
---|
390 | return (a == b); |
---|
391 | } |
---|
392 | |
---|
393 | static number nr2mDiv(number a, number b, const coeffs r) |
---|
394 | { |
---|
395 | if ((unsigned long)a == 0) return (number)0; |
---|
396 | else if ((unsigned long)b % 2 == 0) |
---|
397 | { |
---|
398 | if ((unsigned long)b != 0) |
---|
399 | { |
---|
400 | while (((unsigned long)b % 2 == 0) && ((unsigned long)a % 2 == 0)) |
---|
401 | { |
---|
402 | a = (number)((unsigned long)a / 2); |
---|
403 | b = (number)((unsigned long)b / 2); |
---|
404 | } |
---|
405 | } |
---|
406 | if ((unsigned long)b % 2 == 0) |
---|
407 | { |
---|
408 | WerrorS("Division not possible, even by cancelling zero divisors."); |
---|
409 | WerrorS("Result is integer division without remainder."); |
---|
410 | return (number) ((unsigned long) a / (unsigned long) b); |
---|
411 | } |
---|
412 | } |
---|
413 | return (number)nr2mMult(a, nr2mInversM(b,r),r); |
---|
414 | } |
---|
415 | |
---|
416 | /* Is 'a' divisible by 'b'? There are two cases: |
---|
417 | 1) a = 0 mod 2^m; then TRUE iff b = 0 or b is a power of 2 |
---|
418 | 2) a, b <> 0; then TRUE iff b/gcd(a, b) is a unit mod 2^m */ |
---|
419 | static BOOLEAN nr2mDivBy (number a, number b, const coeffs r) |
---|
420 | { |
---|
421 | if (a == NULL) |
---|
422 | { |
---|
423 | unsigned long c = r->mod2mMask + 1; |
---|
424 | if (c != 0) /* i.e., if no overflow */ |
---|
425 | return (c % (unsigned long)b) == 0; |
---|
426 | else |
---|
427 | { |
---|
428 | /* overflow: we need to check whether b |
---|
429 | is zero or a power of 2: */ |
---|
430 | c = (unsigned long)b; |
---|
431 | while (c != 0) |
---|
432 | { |
---|
433 | if ((c % 2) != 0) return FALSE; |
---|
434 | c = c >> 1; |
---|
435 | } |
---|
436 | return TRUE; |
---|
437 | } |
---|
438 | } |
---|
439 | else |
---|
440 | { |
---|
441 | number n = nr2mGcd(a, b, r); |
---|
442 | n = nr2mDiv(b, n, r); |
---|
443 | return nr2mIsUnit(n, r); |
---|
444 | } |
---|
445 | } |
---|
446 | |
---|
447 | static BOOLEAN nr2mGreater(number a, number b, const coeffs r) |
---|
448 | { |
---|
449 | return nr2mDivBy(a, b,r); |
---|
450 | } |
---|
451 | |
---|
452 | static int nr2mDivComp(number as, number bs, const coeffs) |
---|
453 | { |
---|
454 | unsigned long a = (unsigned long)as; |
---|
455 | unsigned long b = (unsigned long)bs; |
---|
456 | assume(a != 0 && b != 0); |
---|
457 | while (a % 2 == 0 && b % 2 == 0) |
---|
458 | { |
---|
459 | a = a / 2; |
---|
460 | b = b / 2; |
---|
461 | } |
---|
462 | if (a % 2 == 0) |
---|
463 | { |
---|
464 | return -1; |
---|
465 | } |
---|
466 | else |
---|
467 | { |
---|
468 | if (b % 2 == 1) |
---|
469 | { |
---|
470 | return 2; |
---|
471 | } |
---|
472 | else |
---|
473 | { |
---|
474 | return 1; |
---|
475 | } |
---|
476 | } |
---|
477 | } |
---|
478 | |
---|
479 | static number nr2mMod(number a, number b, const coeffs r) |
---|
480 | { |
---|
481 | /* |
---|
482 | We need to return the number rr which is uniquely determined by the |
---|
483 | following two properties: |
---|
484 | (1) 0 <= rr < |b| (with respect to '<' and '<=' performed in Z x Z) |
---|
485 | (2) There exists some k in the integers Z such that a = k * b + rr. |
---|
486 | Consider g := gcd(2^m, |b|). Note that then |b|/g is a unit in Z/2^m. |
---|
487 | Now, there are three cases: |
---|
488 | (a) g = 1 |
---|
489 | Then |b| is a unit in Z/2^m, i.e. |b| (and also b) divides a. |
---|
490 | Thus rr = 0. |
---|
491 | (b) g <> 1 and g divides a |
---|
492 | Then a = (a/g) * (|b|/g)^(-1) * b (up to sign), i.e. again rr = 0. |
---|
493 | (c) g <> 1 and g does not divide a |
---|
494 | Let's denote the division with remainder of a by g as follows: |
---|
495 | a = s * g + t. Then t = a - s * g = a - s * (|b|/g)^(-1) * |b| |
---|
496 | fulfills (1) and (2), i.e. rr := t is the correct result. Hence |
---|
497 | in this third case, rr is the remainder of division of a by g in Z. |
---|
498 | This algorithm is the same as for the case Z/n, except that we may |
---|
499 | compute the gcd of |b| and 2^m "by hand": We just extract the highest |
---|
500 | power of 2 (<= 2^m) that is contained in b. |
---|
501 | */ |
---|
502 | assume((unsigned long) b != 0); |
---|
503 | unsigned long g = 1; |
---|
504 | unsigned long b_div = (unsigned long) b; |
---|
505 | |
---|
506 | /* |
---|
507 | * b_div is unsigned, so that (b_div < 0) evaluates false at compile-time |
---|
508 | * |
---|
509 | if (b_div < 0) b_div = -b_div; // b_div now represents |b|, BUT b_div is unsigned! |
---|
510 | */ |
---|
511 | |
---|
512 | unsigned long rr = 0; |
---|
513 | while ((g < r->mod2mMask ) && (b_div > 0) && (b_div % 2 == 0)) |
---|
514 | { |
---|
515 | b_div = b_div >> 1; |
---|
516 | g = g << 1; |
---|
517 | } // g is now the gcd of 2^m and |b| |
---|
518 | |
---|
519 | if (g != 1) rr = (unsigned long)a % g; |
---|
520 | return (number)rr; |
---|
521 | } |
---|
522 | |
---|
523 | #if 0 |
---|
524 | // unused |
---|
525 | static number nr2mIntDiv(number a, number b, const coeffs r) |
---|
526 | { |
---|
527 | if ((unsigned long)a == 0) |
---|
528 | { |
---|
529 | if ((unsigned long)b == 0) |
---|
530 | return (number)1; |
---|
531 | if ((unsigned long)b == 1) |
---|
532 | return (number)0; |
---|
533 | unsigned long c = r->mod2mMask + 1; |
---|
534 | if (c != 0) /* i.e., if no overflow */ |
---|
535 | return (number)(c / (unsigned long)b); |
---|
536 | else |
---|
537 | { |
---|
538 | /* overflow: c = 2^32 resp. 2^64, depending on platform */ |
---|
539 | mpz_ptr cc = (mpz_ptr)omAlloc(sizeof(mpz_t)); |
---|
540 | mpz_init_set_ui(cc, r->mod2mMask); mpz_add_ui(cc, cc, 1); |
---|
541 | mpz_div_ui(cc, cc, (unsigned long)(unsigned long)b); |
---|
542 | unsigned long s = mpz_get_ui(cc); |
---|
543 | mpz_clear(cc); omFree((ADDRESS)cc); |
---|
544 | return (number)(unsigned long)s; |
---|
545 | } |
---|
546 | } |
---|
547 | else |
---|
548 | { |
---|
549 | if ((unsigned long)b == 0) |
---|
550 | return (number)0; |
---|
551 | return (number)((unsigned long) a / (unsigned long) b); |
---|
552 | } |
---|
553 | } |
---|
554 | #endif |
---|
555 | |
---|
556 | static number nr2mAnn(number b, const coeffs r) |
---|
557 | { |
---|
558 | if ((unsigned long)b == 0) |
---|
559 | return NULL; |
---|
560 | if ((unsigned long)b == 1) |
---|
561 | return NULL; |
---|
562 | unsigned long c = r->mod2mMask + 1; |
---|
563 | if (c != 0) /* i.e., if no overflow */ |
---|
564 | return (number)(c / (unsigned long)b); |
---|
565 | else |
---|
566 | { |
---|
567 | /* overflow: c = 2^32 resp. 2^64, depending on platform */ |
---|
568 | mpz_ptr cc = (mpz_ptr)omAlloc(sizeof(mpz_t)); |
---|
569 | mpz_init_set_ui(cc, r->mod2mMask); mpz_add_ui(cc, cc, 1); |
---|
570 | mpz_div_ui(cc, cc, (unsigned long)(unsigned long)b); |
---|
571 | unsigned long s = mpz_get_ui(cc); |
---|
572 | mpz_clear(cc); omFree((ADDRESS)cc); |
---|
573 | return (number)(unsigned long)s; |
---|
574 | } |
---|
575 | } |
---|
576 | |
---|
577 | static number nr2mNeg(number c, const coeffs r) |
---|
578 | { |
---|
579 | if ((unsigned long)c == 0) return c; |
---|
580 | return nr2mNegM(c, r); |
---|
581 | } |
---|
582 | |
---|
583 | static number nr2mMapMachineInt(number from, const coeffs /*src*/, const coeffs dst) |
---|
584 | { |
---|
585 | unsigned long i = ((unsigned long)from) % dst->mod2mMask ; |
---|
586 | return (number)i; |
---|
587 | } |
---|
588 | |
---|
589 | static number nr2mMapProject(number from, const coeffs /*src*/, const coeffs dst) |
---|
590 | { |
---|
591 | unsigned long i = ((unsigned long)from) % (dst->mod2mMask + 1); |
---|
592 | return (number)i; |
---|
593 | } |
---|
594 | |
---|
595 | number nr2mMapZp(number from, const coeffs /*src*/, const coeffs dst) |
---|
596 | { |
---|
597 | unsigned long j = (unsigned long)1; |
---|
598 | long ii = (long)from; |
---|
599 | if (ii < 0) { j = dst->mod2mMask; ii = -ii; } |
---|
600 | unsigned long i = (unsigned long)ii; |
---|
601 | i = i & dst->mod2mMask; |
---|
602 | /* now we have: from = j * i mod 2^m */ |
---|
603 | return (number)nr2mMult((number)i, (number)j, dst); |
---|
604 | } |
---|
605 | |
---|
606 | static number nr2mMapGMP(number from, const coeffs /*src*/, const coeffs dst) |
---|
607 | { |
---|
608 | mpz_ptr erg = (mpz_ptr)omAllocBin(gmp_nrz_bin); |
---|
609 | mpz_init(erg); |
---|
610 | mpz_ptr k = (mpz_ptr)omAlloc(sizeof(mpz_t)); |
---|
611 | mpz_init_set_ui(k, dst->mod2mMask); |
---|
612 | |
---|
613 | mpz_and(erg, (mpz_ptr)from, k); |
---|
614 | number res = (number) mpz_get_ui(erg); |
---|
615 | |
---|
616 | mpz_clear(erg); omFree((ADDRESS)erg); |
---|
617 | mpz_clear(k); omFree((ADDRESS)k); |
---|
618 | |
---|
619 | return (number)res; |
---|
620 | } |
---|
621 | |
---|
622 | static number nr2mMapQ(number from, const coeffs src, const coeffs dst) |
---|
623 | { |
---|
624 | mpz_ptr gmp = (mpz_ptr)omAllocBin(gmp_nrz_bin); |
---|
625 | mpz_init(gmp); |
---|
626 | nlGMP(from, gmp, src); // FIXME? TODO? // extern void nlGMP(number &i, number n, const coeffs r); // to be replaced with n_MPZ(erg, from, src); // ? |
---|
627 | number res=nr2mMapGMP((number)gmp,src,dst); |
---|
628 | mpz_clear(gmp); omFree((ADDRESS)gmp); |
---|
629 | return res; |
---|
630 | } |
---|
631 | |
---|
632 | static number nr2mMapZ(number from, const coeffs src, const coeffs dst) |
---|
633 | { |
---|
634 | if (SR_HDL(from) & SR_INT) |
---|
635 | { |
---|
636 | long f_i=SR_TO_INT(from); |
---|
637 | return nr2mInit(f_i,dst); |
---|
638 | } |
---|
639 | return nr2mMapGMP(from,src,dst); |
---|
640 | } |
---|
641 | |
---|
642 | static nMapFunc nr2mSetMap(const coeffs src, const coeffs dst) |
---|
643 | { |
---|
644 | if ((src->rep==n_rep_int) && nCoeff_is_Ring_2toM(src) |
---|
645 | && (src->mod2mMask == dst->mod2mMask)) |
---|
646 | { |
---|
647 | return ndCopyMap; |
---|
648 | } |
---|
649 | if ((src->rep==n_rep_int) && nCoeff_is_Ring_2toM(src) |
---|
650 | && (src->mod2mMask < dst->mod2mMask)) |
---|
651 | { /* i.e. map an integer mod 2^s into Z mod 2^t, where t < s */ |
---|
652 | return nr2mMapMachineInt; |
---|
653 | } |
---|
654 | if ((src->rep==n_rep_int) && nCoeff_is_Ring_2toM(src) |
---|
655 | && (src->mod2mMask > dst->mod2mMask)) |
---|
656 | { /* i.e. map an integer mod 2^s into Z mod 2^t, where t > s */ |
---|
657 | // to be done |
---|
658 | return nr2mMapProject; |
---|
659 | } |
---|
660 | if ((src->rep==n_rep_gmp) && nCoeff_is_Ring_Z(src)) |
---|
661 | { |
---|
662 | return nr2mMapGMP; |
---|
663 | } |
---|
664 | if ((src->rep==n_rep_gap_gmp) /*&& nCoeff_is_Ring_Z(src)*/) |
---|
665 | { |
---|
666 | return nr2mMapZ; |
---|
667 | } |
---|
668 | if ((src->rep==n_rep_gap_rat) && (nCoeff_is_Q(src)||nCoeff_is_Ring_Z(src))) |
---|
669 | { |
---|
670 | return nr2mMapQ; |
---|
671 | } |
---|
672 | if ((src->rep==n_rep_int) && nCoeff_is_Zp(src) && (src->ch == 2)) |
---|
673 | { |
---|
674 | return nr2mMapZp; |
---|
675 | } |
---|
676 | if ((src->rep==n_rep_gmp) && |
---|
677 | (nCoeff_is_Ring_PtoM(src) || nCoeff_is_Ring_ModN(src))) |
---|
678 | { |
---|
679 | if (mpz_divisible_2exp_p(src->modNumber,dst->modExponent)) |
---|
680 | return nr2mMapGMP; |
---|
681 | } |
---|
682 | return NULL; // default |
---|
683 | } |
---|
684 | |
---|
685 | /* |
---|
686 | * set the exponent |
---|
687 | */ |
---|
688 | |
---|
689 | static void nr2mSetExp(int m, coeffs r) |
---|
690 | { |
---|
691 | if (m > 1) |
---|
692 | { |
---|
693 | /* we want mod2mMask to be the bit pattern |
---|
694 | '111..1' consisting of m one's: */ |
---|
695 | r->modExponent= m; |
---|
696 | r->mod2mMask = 1; |
---|
697 | for (int i = 1; i < m; i++) r->mod2mMask = (r->mod2mMask << 1) + 1; |
---|
698 | } |
---|
699 | else |
---|
700 | { |
---|
701 | r->modExponent= 2; |
---|
702 | /* code unexpectedly called with m = 1; we continue with m = 2: */ |
---|
703 | r->mod2mMask = 3; /* i.e., '11' in binary representation */ |
---|
704 | } |
---|
705 | } |
---|
706 | |
---|
707 | static void nr2mInitExp(int m, coeffs r) |
---|
708 | { |
---|
709 | nr2mSetExp(m, r); |
---|
710 | if (m < 2) |
---|
711 | WarnS("nr2mInitExp unexpectedly called with m = 1 (we continue with Z/2^2"); |
---|
712 | } |
---|
713 | |
---|
714 | #ifdef LDEBUG |
---|
715 | static BOOLEAN nr2mDBTest (number a, const char *, const int, const coeffs r) |
---|
716 | { |
---|
717 | //if ((unsigned long)a < 0) return FALSE; // is unsigned! |
---|
718 | if (((unsigned long)a & r->mod2mMask) != (unsigned long)a) return FALSE; |
---|
719 | return TRUE; |
---|
720 | } |
---|
721 | #endif |
---|
722 | |
---|
723 | static void nr2mWrite (number a, const coeffs r) |
---|
724 | { |
---|
725 | long i = nr2mInt(a, r); |
---|
726 | StringAppend("%ld", i); |
---|
727 | } |
---|
728 | |
---|
729 | static const char* nr2mEati(const char *s, int *i, const coeffs r) |
---|
730 | { |
---|
731 | |
---|
732 | if (((*s) >= '0') && ((*s) <= '9')) |
---|
733 | { |
---|
734 | (*i) = 0; |
---|
735 | do |
---|
736 | { |
---|
737 | (*i) *= 10; |
---|
738 | (*i) += *s++ - '0'; |
---|
739 | if ((*i) >= (MAX_INT_VAL / 10)) (*i) = (*i) & r->mod2mMask; |
---|
740 | } |
---|
741 | while (((*s) >= '0') && ((*s) <= '9')); |
---|
742 | (*i) = (*i) & r->mod2mMask; |
---|
743 | } |
---|
744 | else (*i) = 1; |
---|
745 | return s; |
---|
746 | } |
---|
747 | |
---|
748 | static const char * nr2mRead (const char *s, number *a, const coeffs r) |
---|
749 | { |
---|
750 | int z; |
---|
751 | int n=1; |
---|
752 | |
---|
753 | s = nr2mEati(s, &z,r); |
---|
754 | if ((*s) == '/') |
---|
755 | { |
---|
756 | s++; |
---|
757 | s = nr2mEati(s, &n,r); |
---|
758 | } |
---|
759 | if (n == 1) |
---|
760 | *a = (number)(long)z; |
---|
761 | else |
---|
762 | *a = nr2mDiv((number)(long)z,(number)(long)n,r); |
---|
763 | return s; |
---|
764 | } |
---|
765 | |
---|
766 | /* for initializing function pointers */ |
---|
767 | BOOLEAN nr2mInitChar (coeffs r, void* p) |
---|
768 | { |
---|
769 | assume( getCoeffType(r) == n_Z2m ); |
---|
770 | nr2mInitExp((int)(long)(p), r); |
---|
771 | |
---|
772 | r->is_field=FALSE; |
---|
773 | r->is_domain=FALSE; |
---|
774 | r->rep=n_rep_int; |
---|
775 | |
---|
776 | //r->cfKillChar = ndKillChar; /* dummy*/ |
---|
777 | r->nCoeffIsEqual = nr2mCoeffIsEqual; |
---|
778 | r->cfCoeffString = nr2mCoeffString; |
---|
779 | |
---|
780 | r->modBase = (mpz_ptr) omAllocBin (gmp_nrz_bin); |
---|
781 | mpz_init_set_si (r->modBase, 2L); |
---|
782 | r->modNumber= (mpz_ptr) omAllocBin (gmp_nrz_bin); |
---|
783 | mpz_init (r->modNumber); |
---|
784 | mpz_pow_ui (r->modNumber, r->modBase, r->modExponent); |
---|
785 | |
---|
786 | /* next cast may yield an overflow as mod2mMask is an unsigned long */ |
---|
787 | r->ch = (int)r->mod2mMask + 1; |
---|
788 | |
---|
789 | r->cfInit = nr2mInit; |
---|
790 | //r->cfCopy = ndCopy; |
---|
791 | r->cfInt = nr2mInt; |
---|
792 | r->cfAdd = nr2mAdd; |
---|
793 | r->cfSub = nr2mSub; |
---|
794 | r->cfMult = nr2mMult; |
---|
795 | r->cfDiv = nr2mDiv; |
---|
796 | r->cfAnn = nr2mAnn; |
---|
797 | r->cfIntMod = nr2mMod; |
---|
798 | r->cfExactDiv = nr2mDiv; |
---|
799 | r->cfInpNeg = nr2mNeg; |
---|
800 | r->cfInvers = nr2mInvers; |
---|
801 | r->cfDivBy = nr2mDivBy; |
---|
802 | r->cfDivComp = nr2mDivComp; |
---|
803 | r->cfGreater = nr2mGreater; |
---|
804 | r->cfEqual = nr2mEqual; |
---|
805 | r->cfIsZero = nr2mIsZero; |
---|
806 | r->cfIsOne = nr2mIsOne; |
---|
807 | r->cfIsMOne = nr2mIsMOne; |
---|
808 | r->cfGreaterZero = nr2mGreaterZero; |
---|
809 | r->cfWriteLong = nr2mWrite; |
---|
810 | r->cfRead = nr2mRead; |
---|
811 | r->cfPower = nr2mPower; |
---|
812 | r->cfSetMap = nr2mSetMap; |
---|
813 | // r->cfNormalize = ndNormalize; // default |
---|
814 | r->cfLcm = nr2mLcm; |
---|
815 | r->cfGcd = nr2mGcd; |
---|
816 | r->cfIsUnit = nr2mIsUnit; |
---|
817 | r->cfGetUnit = nr2mGetUnit; |
---|
818 | r->cfExtGcd = nr2mExtGcd; |
---|
819 | r->cfCoeffWrite = nr2mCoeffWrite; |
---|
820 | r->cfCoeffName = nr2mCoeffName; |
---|
821 | r->cfQuot1 = nr2mQuot1; |
---|
822 | #ifdef LDEBUG |
---|
823 | r->cfDBTest = nr2mDBTest; |
---|
824 | #endif |
---|
825 | r->has_simple_Alloc=TRUE; |
---|
826 | return FALSE; |
---|
827 | } |
---|
828 | |
---|
829 | #endif |
---|
830 | /* #ifdef HAVE_RINGS */ |
---|