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