source: git/factory/int_int.h @ 7c6f06

spielwiese
Last change on this file since 7c6f06 was 7c6f06, checked in by Hans Schoenemann <hannes@…>, 12 years ago
fix: avoid redefine of OM_NDEBUG
  • Property mode set to 100644
File size: 6.1 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2/* $Id$ */
3
4#ifndef INCL_INT_INT_H
5#define INCL_INT_INT_H
6
7// #include "config.h"
8
9#ifndef NOSTREAMIO
10#ifdef HAVE_IOSTREAM
11#include <iostream>
12#define OSTREAM std::ostream
13#elif defined(HAVE_IOSTREAM_H)
14#include <iostream.h>
15#define OSTREAM ostream
16#endif
17#endif /* NOSTREAMIO */
18
19#include "cf_assert.h"
20
21#include "int_cf.h"
22// #include <factory/cf_gmp.h>
23#include "gmpext.h"
24
25#ifdef HAVE_OMALLOC
26#ifndef OM_NDEBUG
27#define OM_NDEBUG
28#endif
29#  include <omalloc/omalloc.h>
30#endif
31
32class InternalInteger : public InternalCF
33{
34private:
35    mpz_t thempi;
36
37    // auxilliary methods
38    inline InternalCF * normalizeMyself ();
39    inline InternalCF * uiNormalizeMyself ();
40
41    static inline InternalCF * normalizeMPI ( mpz_ptr );
42    static inline InternalCF * uiNormalizeMPI ( mpz_ptr );
43
44    static inline mpz_ptr MPI ( const InternalCF * const c );
45#ifdef HAVE_OMALLOC
46  static const omBin InternalInteger_bin;
47#endif
48public:
49#ifdef HAVE_OMALLOC
50  void* operator new(size_t)
51    {
52      void* addr;
53      omTypeAllocBin(void*, addr, InternalInteger_bin);
54      return addr;
55    }
56  void operator delete(void* addr, size_t)
57    {
58      omFreeBin(addr, InternalInteger_bin);
59    }
60#endif
61
62    InternalInteger();
63    InternalInteger( const InternalCF& )
64    {
65        ASSERT( 0, "ups there is something wrong in your code" );
66    }
67    InternalInteger( const int 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    int 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.