longrat.h
Go to the documentation of this file.
1 #ifndef LONGRAT_H
2 #define LONGRAT_H
3 /****************************************
4 * Computer Algebra System SINGULAR *
5 ****************************************/
6 /*
7 * ABSTRACT: computation with long rational numbers
8 */
9 #include <misc/auxiliary.h>
10 
11 #include <coeffs/si_gmp.h>
12 #include <coeffs/coeffs.h>
13 
14 struct snumber; typedef struct snumber *number;
15 
16 /*-----------------------------------------------------------------*/
17 /**
18 ** 'SR_INT' is the type of those integers small enough to fit into 29 bits.
19 ** Therefor the value range of this small integers is: $-2^{28}...2^{28}-1$.
20 **
21 ** Small integers are represented by an immediate integer handle, containing
22 ** the value instead of pointing to it, which has the following form:
23 **
24 ** +-------+-------+-------+-------+- - - -+-------+-------+-------+
25 ** | guard | sign | bit | bit | | bit | tag | tag |
26 ** | bit | bit | 27 | 26 | | 0 | 0 | 1 |
27 ** +-------+-------+-------+-------+- - - -+-------+-------+-------+
28 **
29 ** Immediate integers handles carry the tag 'SR_INT', i.e. the last bit is 1.
30 ** This distuingishes immediate integers from other handles which point to
31 ** structures aligned on 4 byte boundaries and therefor have last bit zero.
32 ** (The second bit is reserved as tag to allow extensions of this scheme.)
33 ** Using immediates as pointers and dereferencing them gives address errors.
34 **
35 ** To aid overflow check the most significant two bits must always be equal,
36 ** that is to say that the sign bit of immediate integers has a guard bit.
37 **
38 ** The macros 'INT_TO_SR' and 'SR_TO_INT' should be used to convert between
39 ** a small integer value and its representation as immediate integer handle.
40 **
41 ** Large integers and rationals are represented by z and n
42 ** where n may be undefined (if s==3)
43 ** NULL represents only deleted values
44 */
45 
46 struct snumber
47 {
48  mpz_t z; //< Zaehler
49  mpz_t n; //< Nenner
50 #if defined(LDEBUG)
51  int debug;
52 #endif
53 
54  /**
55  * parameter s in number:
56  * 0 (or FALSE): not normalised rational
57  * 1 (or TRUE): normalised rational
58  * 3 : integer with n==NULL
59  **/
60  BOOLEAN s; //< integer parameter
61 };
62 
63 #define SR_HDL(A) ((long)(A))
64 
65 #define SR_INT 1L
66 #define INT_TO_SR(INT) ((number) (((long)INT << 2) + SR_INT))
67 #define SR_TO_INT(SR) (((long)SR) >> 2)
68 
69 #define MP_SMALL 1
70 
71 BOOLEAN nlInitChar(coeffs, void*);
72 
73 /// only used by slimgb (tgb.cc)
74 static FORCE_INLINE int nlQlogSize (number n, const coeffs r)
75 {
76  assume( nCoeff_is_Q (r) );
77 
78  long nl=n_Size(n,r);
79  if (nl==0L) return 0;
80  if (nl==1L)
81  {
82  long i = SR_TO_INT (n);
83  unsigned long v;
84  v = (i >= 0) ? i : -i;
85  int r = 0;
86 
87  while(v >>= 1)
88  {
89  r++;
90  }
91  return r + 1;
92  }
93  //assume denominator is 0
94  number nn=(number) n;
95  return mpz_sizeinbase (nn->z, 2);
96 }
97 
98 
99 static FORCE_INLINE BOOLEAN nlIsInteger(number q, const coeffs r)
100 {
101  assume( nCoeff_is_Q (r) );
102  n_Test(q, r);
103 
104  if (SR_HDL(q) & SR_INT)
105  return TRUE; // immediate int
106 
107  return ( q->s == 3 );
108 }
109 
110 number nlModP(number q, const coeffs Q, const coeffs Zp);
111 void nlNormalize(number &x, const coeffs r);
112 void nlInpGcd(number &a, number b, const coeffs r);
113 
114 
115 /// create a rational i/j (implicitly) over Q
116 /// NOTE: make sure to use correct Q in debug mode
117 number nlInit2 (int i, int j, const coeffs r);
118 
119 /// create a rational i/j (implicitly) over Q
120 /// NOTE: make sure to use correct Q in debug mode
121 number nlInit2gmp (mpz_t i, mpz_t j, const coeffs r);
122 
123 // FIXME: TODO: why only if HAVE_RINGS? bug?
124 # ifdef HAVE_RINGS
125 void nlGMP(number &i, number n, const coeffs r); // to be replaced with n_MPZ(number n, number &i,const coeffs r)???
126 number nlMapGMP(number from, const coeffs src, const coeffs dst);
127 # endif
128 
129 #endif
130 
131 
mpz_t z
Definition: longrat.h:48
const poly a
Definition: syzextra.cc:212
&#39;SR_INT&#39; is the type of those integers small enough to fit into 29 bits.
Definition: longrat.h:46
static FORCE_INLINE BOOLEAN nlIsInteger(number q, const coeffs r)
Definition: longrat.h:99
#define SR_HDL(A)
Definition: longrat.h:63
number nlInit2(int i, int j, const coeffs r)
create a rational i/j (implicitly) over Q NOTE: make sure to use correct Q in debug mode ...
Definition: longrat.cc:2358
#define TRUE
Definition: auxiliary.h:144
#define FORCE_INLINE
Definition: auxiliary.h:386
static FORCE_INLINE int nlQlogSize(number n, const coeffs r)
only used by slimgb (tgb.cc)
Definition: longrat.h:74
#define Q
Definition: sirandom.c:25
static FORCE_INLINE BOOLEAN nCoeff_is_Q(const coeffs r)
Definition: coeffs.h:829
void nlGMP(number &i, number n, const coeffs r)
Definition: longrat.cc:1467
number nlMapGMP(number from, const coeffs src, const coeffs dst)
Definition: longrat.cc:208
mpz_t n
Definition: longrat.h:49
const ring r
Definition: syzextra.cc:208
Coefficient rings, fields and other domains suitable for Singular polynomials.
int j
Definition: myNF.cc:70
BOOLEAN s
parameter s in number: 0 (or FALSE): not normalised rational 1 (or TRUE): normalised rational 3 : int...
Definition: longrat.h:60
#define assume(x)
Definition: mod2.h:405
The main handler for Singular numbers which are suitable for Singular polynomials.
BOOLEAN nlInitChar(coeffs, void *)
Definition: longrat.cc:3301
number nlModP(number q, const coeffs Q, const coeffs Zp)
Definition: longrat.cc:1425
#define n_Test(a, r)
BOOLEAN n_Test(number a, const coeffs r)
Definition: coeffs.h:739
All the auxiliary stuff.
number nlInit2gmp(mpz_t i, mpz_t j, const coeffs r)
create a rational i/j (implicitly) over Q NOTE: make sure to use correct Q in debug mode ...
Definition: longrat.cc:2371
int i
Definition: cfEzgcd.cc:123
#define SR_TO_INT(SR)
Definition: longrat.h:67
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:37
#define SR_INT
Definition: longrat.h:65
Variable x
Definition: cfModGcd.cc:4023
void nlInpGcd(number &a, number b, const coeffs r)
Definition: longrat.cc:2759
void nlNormalize(number &x, const coeffs r)
Definition: longrat.cc:1334
int BOOLEAN
Definition: auxiliary.h:131
const poly b
Definition: syzextra.cc:213
static FORCE_INLINE int n_Size(number n, const coeffs r)
return a non-negative measure for the complexity of n; return 0 only when n represents zero; (used fo...
Definition: coeffs.h:571
int debug
Definition: longrat.h:51