source: git/libpolys/coeffs/longrat.h @ d31cb91

spielwiese
Last change on this file since d31cb91 was d31cb91, checked in by Jakob Kroeker <kroeker@…>, 8 years ago
move declarations to header
  • Property mode set to 100644
File size: 3.8 KB
Line 
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
14struct snumber; typedef struct snumber  *number;
15
16number   nlGetDenom(number &n, const coeffs r);
17number   nlGetNumerator(number &n, const coeffs r);
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
49struct 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
74BOOLEAN nlInitChar(coeffs, void*);
75
76/// only used by slimgb (tgb.cc)
77static  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
102static 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; // immidiate int
109
110  return ( q->s == 3 );
111}
112
113number nlModP(number q, const coeffs Q, const coeffs Zp);
114void   nlNormalize(number &x, const coeffs r);
115void   nlInpGcd(number &a, number b, const coeffs r);
116
117
118/// create a rational i/j (implicitly) over Q
119/// NOTE: make sure to use correct Q in debug mode
120number   nlInit2 (int i, int j, const coeffs r);
121
122/// create a rational i/j (implicitly) over Q
123/// NOTE: make sure to use correct Q in debug mode
124number   nlInit2gmp (mpz_t i, mpz_t j, const coeffs r);
125
126// FIXME: TODO:  why only if HAVE_RINGS? bug?
127#  ifdef HAVE_RINGS
128void   nlGMP(number &i, number n, const coeffs r); // to be replaced with n_MPZ(number n, number &i,const coeffs r)???
129number nlMapGMP(number from, const coeffs src, const coeffs dst);
130#  endif
131
132#endif
133
134
Note: See TracBrowser for help on using the repository browser.