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 | |
---|
31 | class InternalInteger : public InternalCF |
---|
32 | { |
---|
33 | private: |
---|
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 |
---|
47 | public: |
---|
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 char * str, const int base=10 ); |
---|
68 | InternalInteger( const mpz_ptr ); |
---|
69 | ~InternalInteger(); |
---|
70 | InternalCF* deepCopyObject() const; |
---|
71 | const char * classname() const { return "InternalInteger"; } |
---|
72 | #ifndef NOSTREAMIO |
---|
73 | void print( OSTREAM&, char* ); |
---|
74 | #endif /* NOSTREAMIO */ |
---|
75 | InternalCF* genZero(); |
---|
76 | InternalCF* genOne(); |
---|
77 | |
---|
78 | bool is_imm() const; |
---|
79 | |
---|
80 | int levelcoeff() const { return IntegerDomain; } |
---|
81 | InternalCF* neg(); |
---|
82 | |
---|
83 | int comparesame( InternalCF* ); |
---|
84 | |
---|
85 | InternalCF* addsame( InternalCF* ); |
---|
86 | InternalCF* subsame( InternalCF* ); |
---|
87 | InternalCF* mulsame( InternalCF* ); |
---|
88 | InternalCF* dividesame( InternalCF* ); |
---|
89 | InternalCF* modulosame( InternalCF* ); |
---|
90 | InternalCF* divsame( InternalCF* ); |
---|
91 | InternalCF* modsame( InternalCF* ); |
---|
92 | void divremsame( InternalCF*, InternalCF*&, InternalCF*& ); |
---|
93 | bool divremsamet( InternalCF*, InternalCF*&, InternalCF*& ); |
---|
94 | |
---|
95 | int comparecoeff( InternalCF* ); |
---|
96 | |
---|
97 | InternalCF* addcoeff( InternalCF* ); |
---|
98 | InternalCF* subcoeff( InternalCF*, bool ); |
---|
99 | InternalCF* mulcoeff( InternalCF* ); |
---|
100 | InternalCF* dividecoeff( InternalCF*, bool ); |
---|
101 | InternalCF* modulocoeff( InternalCF*, bool ); |
---|
102 | InternalCF* divcoeff( InternalCF*, bool ); |
---|
103 | InternalCF* modcoeff( InternalCF*, bool ); |
---|
104 | void divremcoeff( InternalCF*, InternalCF*&, InternalCF*&, bool ); |
---|
105 | bool divremcoefft( InternalCF*, InternalCF*&, InternalCF*&, bool ); |
---|
106 | |
---|
107 | InternalCF * bgcdsame ( const InternalCF * const ) const; |
---|
108 | InternalCF * bgcdcoeff ( const InternalCF * const ); |
---|
109 | |
---|
110 | InternalCF * bextgcdsame ( InternalCF *, CanonicalForm &, CanonicalForm & ); |
---|
111 | InternalCF * bextgcdcoeff ( InternalCF *, CanonicalForm &, CanonicalForm & ); |
---|
112 | |
---|
113 | int intval() const; |
---|
114 | |
---|
115 | int intmod( int p ) const; |
---|
116 | |
---|
117 | int sign() const; |
---|
118 | |
---|
119 | InternalCF* sqrt(); |
---|
120 | |
---|
121 | int ilog2(); |
---|
122 | |
---|
123 | friend class InternalRational; |
---|
124 | friend void gmp_numerator ( const CanonicalForm & f, mpz_ptr result); |
---|
125 | friend void gmp_denominator ( const CanonicalForm & f, mpz_ptr result ); |
---|
126 | friend mpz_ptr getmpi ( InternalCF * value, bool symmetric ); |
---|
127 | }; |
---|
128 | |
---|
129 | //{{{ inline InternalCF * InternalInteger::normalizeMyself, uiNormalizeMyself () |
---|
130 | //{{{ docu |
---|
131 | // |
---|
132 | // normalizeMyself(), uiNormalizeMyself() - normalize CO. |
---|
133 | // |
---|
134 | // If CO fits into an immediate integer, delete CO and return the |
---|
135 | // immediate. Otherwise, return a pointer to CO. |
---|
136 | // |
---|
137 | // `uiNormalizeMyself()' is the same as `normalizeMyself()' |
---|
138 | // except that CO is expected to be non-begative. In this case, |
---|
139 | // we may use `mpz_get_ui()' to convert the underlying mpi into |
---|
140 | // an immediate which is slightly faster than the signed variant. |
---|
141 | // |
---|
142 | // Note: We do not mind reference counting at this point! CO is |
---|
143 | // deleted unconditionally! |
---|
144 | // |
---|
145 | //}}} |
---|
146 | inline InternalCF * |
---|
147 | InternalInteger::normalizeMyself () |
---|
148 | { |
---|
149 | ASSERT( getRefCount() == 1, "internal error: must not delete CO" ); |
---|
150 | |
---|
151 | if ( mpz_is_imm( thempi ) ) { |
---|
152 | InternalCF * result = int2imm( mpz_get_si( thempi ) ); |
---|
153 | delete this; |
---|
154 | return result; |
---|
155 | } else |
---|
156 | return this; |
---|
157 | } |
---|
158 | |
---|
159 | inline InternalCF * |
---|
160 | InternalInteger::uiNormalizeMyself () |
---|
161 | { |
---|
162 | ASSERT( getRefCount() == 1, "internal error: must not delete CO" ); |
---|
163 | |
---|
164 | if ( mpz_is_imm( thempi ) ) { |
---|
165 | InternalCF * result = int2imm( mpz_get_ui( thempi ) ); |
---|
166 | delete this; |
---|
167 | return result; |
---|
168 | } else |
---|
169 | return this; |
---|
170 | } |
---|
171 | //}}} |
---|
172 | |
---|
173 | //{{{ static inline InternalCF * InternalInteger::normalizeMPI, uiNormalizeMPI ( mpz_ptr aMpi ) |
---|
174 | //{{{ docu |
---|
175 | // |
---|
176 | // normalizeMPI(), uiNormalizeMPI() - normalize a mpi. |
---|
177 | // |
---|
178 | // If `aMpi' fits into an immediate integer, clear `aMpi' and |
---|
179 | // return the immediate. Otherwise, return a new |
---|
180 | // `InternalInteger' with `aMpi' as underlying mpi. |
---|
181 | // |
---|
182 | // `uiNormalizeMPI()' is the same as `normalizeMPI()' except that |
---|
183 | // `aMpi' is expected to be non-begative. In this case, we may |
---|
184 | // use `mpz_get_ui()' to convert `aMpi' into an immediate which |
---|
185 | // is slightly faster than the signed variant. |
---|
186 | // |
---|
187 | //}}} |
---|
188 | inline InternalCF * |
---|
189 | InternalInteger::normalizeMPI ( mpz_ptr aMpi ) |
---|
190 | { |
---|
191 | if ( mpz_is_imm( aMpi ) ) { |
---|
192 | InternalCF * result = int2imm( mpz_get_si( aMpi ) ); |
---|
193 | mpz_clear( aMpi ); |
---|
194 | return result; |
---|
195 | } else |
---|
196 | return new InternalInteger( aMpi ); |
---|
197 | } |
---|
198 | |
---|
199 | inline InternalCF * |
---|
200 | InternalInteger::uiNormalizeMPI ( mpz_ptr aMpi ) |
---|
201 | { |
---|
202 | if ( mpz_is_imm( aMpi ) ) { |
---|
203 | InternalCF * result = int2imm( mpz_get_ui( aMpi ) ); |
---|
204 | mpz_clear( aMpi ); |
---|
205 | return result; |
---|
206 | } else |
---|
207 | return new InternalInteger( aMpi ); |
---|
208 | } |
---|
209 | //}}} |
---|
210 | |
---|
211 | //{{{ inline mpz_ptr InternalInteger::MPI ( const InternalCF * const c ) |
---|
212 | //{{{ docu |
---|
213 | // |
---|
214 | // MPI() - return underlying mpi of `c'. |
---|
215 | // |
---|
216 | // `c' is expected to be an `InternalInteger *'. `c's underlying |
---|
217 | // mpi is returned. |
---|
218 | // |
---|
219 | //}}} |
---|
220 | inline mpz_ptr |
---|
221 | InternalInteger::MPI ( const InternalCF * const c ) |
---|
222 | { |
---|
223 | return (((InternalInteger*)c)->thempi); |
---|
224 | } |
---|
225 | //}}} |
---|
226 | |
---|
227 | #endif /* ! INCL_INT_INT_H */ |
---|