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