source: git/libpolys/coeffs/rmodulo2m.cc @ 560a3d

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