source: git/factory/int_int.h @ 70c40f

spielwiese
Last change on this file since 70c40f was 8710ff0, checked in by Martin Lee <martinlee84@…>, 12 years ago
chg: 64bit integers in factory by Adi Popescu
  • Property mode set to 100644
File size: 6.1 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2
3#ifndef INCL_INT_INT_H
4#define INCL_INT_INT_H
5
6// #include "config.h"
7
8#ifndef NOSTREAMIO
9#ifdef HAVE_IOSTREAM
10#include <iostream>
11#define OSTREAM std::ostream
12#elif defined(HAVE_IOSTREAM_H)
13#include <iostream.h>
14#define OSTREAM ostream
15#endif
16#endif /* NOSTREAMIO */
17
18#include "cf_assert.h"
19
20#include "int_cf.h"
21// #include <factory/cf_gmp.h>
22#include "gmpext.h"
23
24#ifdef HAVE_OMALLOC
25#ifndef OM_NDEBUG
26#define OM_NDEBUG
27#endif
28#  include <omalloc/omalloc.h>
29#endif
30
31class InternalInteger : public InternalCF
32{
33private:
34    mpz_t thempi;
35
36    // auxilliary methods
37    inline InternalCF * normalizeMyself ();
38    inline InternalCF * uiNormalizeMyself ();
39
40    static inline InternalCF * normalizeMPI ( mpz_ptr );
41    static inline InternalCF * uiNormalizeMPI ( mpz_ptr );
42
43    static inline mpz_ptr MPI ( const InternalCF * const c );
44#ifdef HAVE_OMALLOC
45  static const omBin InternalInteger_bin;
46#endif
47public:
48#ifdef HAVE_OMALLOC
49  void* operator new(size_t)
50    {
51      void* addr;
52      omTypeAllocBin(void*, addr, InternalInteger_bin);
53      return addr;
54    }
55  void operator delete(void* addr, size_t)
56    {
57      omFreeBin(addr, InternalInteger_bin);
58    }
59#endif
60
61    InternalInteger();
62    InternalInteger( const InternalCF& )
63    {
64        ASSERT( 0, "ups there is something wrong in your code" );
65    }
66    InternalInteger( const int i );
67    InternalInteger( const long i );
68    InternalInteger( const char * str, const int base=10 );
69    InternalInteger( const mpz_ptr );
70    ~InternalInteger();
71    InternalCF* deepCopyObject() const;
72    const char * classname() const { return "InternalInteger"; }
73#ifndef NOSTREAMIO
74    void print( OSTREAM&, char* );
75#endif /* NOSTREAMIO */
76    InternalCF* genZero();
77    InternalCF* genOne();
78
79    bool is_imm() const;
80
81    int levelcoeff() const { return IntegerDomain; }
82    InternalCF* neg();
83
84    int comparesame( InternalCF* );
85
86    InternalCF* addsame( InternalCF* );
87    InternalCF* subsame( InternalCF* );
88    InternalCF* mulsame( InternalCF* );
89    InternalCF* dividesame( InternalCF* );
90    InternalCF* modulosame( InternalCF* );
91    InternalCF* divsame( InternalCF* );
92    InternalCF* modsame( InternalCF* );
93    void divremsame( InternalCF*, InternalCF*&, InternalCF*& );
94    bool divremsamet( InternalCF*, InternalCF*&, InternalCF*& );
95
96    int comparecoeff( InternalCF* );
97
98    InternalCF* addcoeff( InternalCF* );
99    InternalCF* subcoeff( InternalCF*, bool );
100    InternalCF* mulcoeff( InternalCF* );
101    InternalCF* dividecoeff( InternalCF*, bool );
102    InternalCF* modulocoeff( InternalCF*, bool );
103    InternalCF* divcoeff( InternalCF*, bool );
104    InternalCF* modcoeff( InternalCF*, bool );
105    void divremcoeff( InternalCF*, InternalCF*&, InternalCF*&, bool );
106    bool divremcoefft( InternalCF*, InternalCF*&, InternalCF*&, bool );
107
108    InternalCF * bgcdsame ( const InternalCF * const ) const;
109    InternalCF * bgcdcoeff ( const InternalCF * const );
110
111    InternalCF * bextgcdsame ( InternalCF *, CanonicalForm &, CanonicalForm & );
112    InternalCF * bextgcdcoeff ( InternalCF *, CanonicalForm &, CanonicalForm & );
113
114    long intval() const;
115
116    int intmod( int p ) const;
117
118    int sign() const;
119
120    InternalCF* sqrt();
121
122    int ilog2();
123
124    friend class InternalRational;
125    friend void gmp_numerator ( const CanonicalForm & f, mpz_ptr result);
126    friend void gmp_denominator ( const CanonicalForm & f, mpz_ptr result );
127    friend mpz_ptr getmpi ( InternalCF * value, bool symmetric );
128};
129
130//{{{ inline InternalCF * InternalInteger::normalizeMyself, uiNormalizeMyself ()
131//{{{ docu
132//
133// normalizeMyself(), uiNormalizeMyself() - normalize CO.
134//
135// If CO fits into an immediate integer, delete CO and return the
136// immediate.  Otherwise, return a pointer to CO.
137//
138// `uiNormalizeMyself()' is the same as `normalizeMyself()'
139// except that CO is expected to be non-begative.  In this case,
140// we may use `mpz_get_ui()' to convert the underlying mpi into
141// an immediate which is slightly faster than the signed variant.
142//
143// Note: We do not mind reference counting at this point!  CO is
144// deleted unconditionally!
145//
146//}}}
147inline InternalCF *
148InternalInteger::normalizeMyself ()
149{
150    ASSERT( getRefCount() == 1, "internal error: must not delete CO" );
151
152    if ( mpz_is_imm( thempi ) ) {
153        InternalCF * result = int2imm( mpz_get_si( thempi ) );
154        delete this;
155        return result;
156    } else
157        return this;
158}
159
160inline InternalCF *
161InternalInteger::uiNormalizeMyself ()
162{
163    ASSERT( getRefCount() == 1, "internal error: must not delete CO" );
164
165    if ( mpz_is_imm( thempi ) ) {
166        InternalCF * result = int2imm( mpz_get_ui( thempi ) );
167        delete this;
168        return result;
169    } else
170        return this;
171}
172//}}}
173
174//{{{ static inline InternalCF * InternalInteger::normalizeMPI, uiNormalizeMPI ( mpz_ptr aMpi )
175//{{{ docu
176//
177// normalizeMPI(), uiNormalizeMPI() - normalize a mpi.
178//
179// If `aMpi' fits into an immediate integer, clear `aMpi' and
180// return the immediate.  Otherwise, return a new
181// `InternalInteger' with `aMpi' as underlying mpi.
182//
183// `uiNormalizeMPI()' is the same as `normalizeMPI()' except that
184// `aMpi' is expected to be non-begative.  In this case, we may
185// use `mpz_get_ui()' to convert `aMpi' into an immediate which
186// is slightly faster than the signed variant.
187//
188//}}}
189inline InternalCF *
190InternalInteger::normalizeMPI ( mpz_ptr aMpi )
191{
192    if ( mpz_is_imm( aMpi ) ) {
193        InternalCF * result = int2imm( mpz_get_si( aMpi ) );
194        mpz_clear( aMpi );
195        return result;
196    } else
197        return new InternalInteger( aMpi );
198}
199
200inline InternalCF *
201InternalInteger::uiNormalizeMPI ( mpz_ptr aMpi )
202{
203    if ( mpz_is_imm( aMpi ) ) {
204        InternalCF * result = int2imm( mpz_get_ui( aMpi ) );
205        mpz_clear( aMpi );
206        return result;
207    } else
208        return new InternalInteger( aMpi );
209}
210//}}}
211
212//{{{ inline mpz_ptr InternalInteger::MPI ( const InternalCF * const c )
213//{{{ docu
214//
215// MPI() - return underlying mpi of `c'.
216//
217// `c' is expected to be an `InternalInteger *'.  `c's underlying
218// mpi is returned.
219//
220//}}}
221inline mpz_ptr
222InternalInteger::MPI ( const InternalCF * const c )
223{
224    return (((InternalInteger*)c)->thempi);
225}
226//}}}
227
228#endif /* ! INCL_INT_INT_H */
Note: See TracBrowser for help on using the repository browser.