1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | |
---|
3 | #ifndef INCL_IMM_H |
---|
4 | #define INCL_IMM_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 "cf_defs.h" |
---|
21 | #include "cf_globals.h" |
---|
22 | #include "ffops.h" |
---|
23 | #include "gfops.h" |
---|
24 | #include "cf_factory.h" |
---|
25 | #include "canonicalform.h" |
---|
26 | #include "int_cf.h" |
---|
27 | |
---|
28 | const long INTMARK = 1; |
---|
29 | const long FFMARK = 2; |
---|
30 | const long GFMARK = 3; |
---|
31 | |
---|
32 | /* define type of your compilers 64 bit integer type */ |
---|
33 | #ifndef INT64 |
---|
34 | #define INT64 long long int |
---|
35 | #endif |
---|
36 | |
---|
37 | #if SIZEOF_LONG == 4 |
---|
38 | const long MINIMMEDIATE = -268435454; // -2^28-2 |
---|
39 | const long MAXIMMEDIATE = 268435454; // 2^28-2 |
---|
40 | #else |
---|
41 | const long MINIMMEDIATE = -(1L<<60)+2L; // -2^60-2 |
---|
42 | const long MAXIMMEDIATE = (1L<<60)-2L; // 2^60-2 |
---|
43 | #endif |
---|
44 | |
---|
45 | #if defined(WINNT) && ! defined(__GNUC__) |
---|
46 | const INT64 MINIMMEDIATELL = -268435454i64; |
---|
47 | const INT64 MAXIMMEDIATELL = 268435454i64; |
---|
48 | #else |
---|
49 | const INT64 MINIMMEDIATELL = -268435454LL; |
---|
50 | const INT64 MAXIMMEDIATELL = 268435454LL; |
---|
51 | #endif |
---|
52 | |
---|
53 | //{{{ conversion functions |
---|
54 | //#ifdef HAS_ARITHMETIC_SHIFT |
---|
55 | #if 1 |
---|
56 | |
---|
57 | inline long imm2int ( const InternalCF * const imm ) |
---|
58 | { |
---|
59 | return ((long)imm) >> 2; |
---|
60 | } |
---|
61 | |
---|
62 | inline InternalCF * int2imm ( long i ) |
---|
63 | { |
---|
64 | return (InternalCF*)(long)((i << 2) | INTMARK ); |
---|
65 | } |
---|
66 | |
---|
67 | #else |
---|
68 | |
---|
69 | inline int imm2int ( const InternalCF * const imm ) |
---|
70 | { |
---|
71 | // this could be better done by masking the sign bit |
---|
72 | if ( ((int)((long)imm)) < 0 ) |
---|
73 | return -((-(long)imm) >> 2); |
---|
74 | else |
---|
75 | return (long)imm >> 2; |
---|
76 | } |
---|
77 | |
---|
78 | inline InternalCF * int2imm ( long i ) |
---|
79 | { |
---|
80 | if ( i < 0 ) |
---|
81 | return (InternalCF*)(long)(-(((-i) << 2) | INTMARK)); |
---|
82 | else |
---|
83 | return (InternalCF*)(long)((i << 2) | INTMARK ); |
---|
84 | } |
---|
85 | |
---|
86 | #endif |
---|
87 | |
---|
88 | inline InternalCF * int2imm_p ( long i ) |
---|
89 | { |
---|
90 | return (InternalCF*)(long)((i << 2) | FFMARK ); |
---|
91 | } |
---|
92 | |
---|
93 | inline InternalCF * int2imm_gf ( long i ) |
---|
94 | { |
---|
95 | return (InternalCF*)(long)((i << 2) | GFMARK ); |
---|
96 | } |
---|
97 | //}}} |
---|
98 | |
---|
99 | // predicates |
---|
100 | #if 0 |
---|
101 | inline int is_imm ( const InternalCF * const ptr ) |
---|
102 | { |
---|
103 | // returns 0 if ptr is not immediate |
---|
104 | return ( (long)ptr & 3 ); |
---|
105 | } |
---|
106 | #endif |
---|
107 | |
---|
108 | //{{{ inline int imm_isone, imm_isone_p, imm_isone_gf ( const InternalCF * const ptr ) |
---|
109 | // docu: see CanonicalForm::isOne() |
---|
110 | inline int |
---|
111 | imm_isone ( const InternalCF * const ptr ) |
---|
112 | { |
---|
113 | return imm2int( ptr ) == 1; |
---|
114 | } |
---|
115 | |
---|
116 | inline int |
---|
117 | imm_isone_p ( const InternalCF * const ptr ) |
---|
118 | { |
---|
119 | return imm2int( ptr ) == 1; |
---|
120 | } |
---|
121 | |
---|
122 | inline int |
---|
123 | imm_isone_gf ( const InternalCF * const ptr ) |
---|
124 | { |
---|
125 | return gf_isone( imm2int( ptr ) ); |
---|
126 | } |
---|
127 | //}}} |
---|
128 | |
---|
129 | //{{{ inline int imm_iszero, imm_iszero_p, imm_iszero_gf ( const InternalCF * const ptr ) |
---|
130 | // docu: see CanonicalForm::isZero() |
---|
131 | inline int |
---|
132 | imm_iszero ( const InternalCF * const ptr ) |
---|
133 | { |
---|
134 | return imm2int( ptr ) == 0; |
---|
135 | } |
---|
136 | |
---|
137 | inline int |
---|
138 | imm_iszero_p ( const InternalCF * const ptr ) |
---|
139 | { |
---|
140 | return imm2int( ptr ) == 0; |
---|
141 | } |
---|
142 | |
---|
143 | inline int |
---|
144 | imm_iszero_gf ( const InternalCF * const ptr ) |
---|
145 | { |
---|
146 | return gf_iszero( imm2int( ptr ) ); |
---|
147 | } |
---|
148 | //}}} |
---|
149 | |
---|
150 | //{{{ conversion functions |
---|
151 | inline long imm_intval ( const InternalCF* const op ) |
---|
152 | { |
---|
153 | if ( is_imm( op ) == FFMARK ) |
---|
154 | if ( cf_glob_switches.isOn( SW_SYMMETRIC_FF ) ) |
---|
155 | return ff_symmetric( imm2int( op ) ); |
---|
156 | else |
---|
157 | return imm2int( op ); |
---|
158 | else if ( is_imm( op ) == GFMARK ) { |
---|
159 | ASSERT( gf_isff( imm2int( op ) ), "invalid conversion" ); |
---|
160 | if ( cf_glob_switches.isOn( SW_SYMMETRIC_FF ) ) |
---|
161 | return ff_symmetric( gf_gf2ff( imm2int( op ) ) ); |
---|
162 | else |
---|
163 | return gf_gf2ff( imm2int( op ) ); |
---|
164 | } |
---|
165 | else |
---|
166 | return imm2int( op ); |
---|
167 | } |
---|
168 | //}}} |
---|
169 | |
---|
170 | //{{{ inline int imm_sign ( const InternalCF * const op ) |
---|
171 | //{{{ docu |
---|
172 | // |
---|
173 | // imm_sign() - return sign of immediate object. |
---|
174 | // |
---|
175 | // If CO is an immediate integer, the sign is defined as usual. |
---|
176 | // If CO is an element of FF(p) and SW_SYMMETRIC_FF is on the |
---|
177 | // sign of CO is the sign of the symmetric representation of CO. |
---|
178 | // If CO is in GF(q) or in FF(p) and SW_SYMMETRIC_FF is off, the |
---|
179 | // sign of CO is zero iff CO is zero, otherwise the sign is one. |
---|
180 | // |
---|
181 | // See also: CanonicalForm::sign(), gf_sign() |
---|
182 | // |
---|
183 | //}}} |
---|
184 | inline int |
---|
185 | imm_sign ( const InternalCF * const op ) |
---|
186 | { |
---|
187 | if ( is_imm( op ) == FFMARK ) |
---|
188 | if ( imm2int( op ) == 0 ) |
---|
189 | return 0; |
---|
190 | else if ( cf_glob_switches.isOn( SW_SYMMETRIC_FF ) ) |
---|
191 | if ( ff_symmetric( imm2int( op ) ) > 0 ) |
---|
192 | return 1; |
---|
193 | else |
---|
194 | return -1; |
---|
195 | else |
---|
196 | return 1; |
---|
197 | else if ( is_imm( op ) == GFMARK ) |
---|
198 | return gf_sign( imm2int( op ) ); |
---|
199 | else if ( imm2int( op ) == 0 ) |
---|
200 | return 0; |
---|
201 | else if ( imm2int( op ) > 0 ) |
---|
202 | return 1; |
---|
203 | else |
---|
204 | return -1; |
---|
205 | } |
---|
206 | //}}} |
---|
207 | |
---|
208 | //{{{ inline int imm_cmp, imm_cmp_p, imm_cmp_gf ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
209 | //{{{ docu |
---|
210 | // |
---|
211 | // imm_cmp(), imm_cmp_p(), imm_cmp_gf() - compare immediate objects. |
---|
212 | // |
---|
213 | // For immediate integers, it is clear how this should be done. |
---|
214 | // For objects from finite fields, it is not clear since they |
---|
215 | // are not ordered fields. However, since we want to have a |
---|
216 | // total well order on polynomials we have to define a total well |
---|
217 | // order on all coefficients, too. I decided to use simply the |
---|
218 | // order on the representation as `int's of such objects. |
---|
219 | // |
---|
220 | // See also: CanonicalForm::operator <(), CanonicalForm::operator ==() |
---|
221 | // |
---|
222 | //}}} |
---|
223 | inline int |
---|
224 | imm_cmp ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
225 | { |
---|
226 | if ( imm2int( lhs ) == imm2int( rhs ) ) |
---|
227 | return 0; |
---|
228 | else if ( imm2int( lhs ) > imm2int( rhs ) ) |
---|
229 | return 1; |
---|
230 | else |
---|
231 | return -1; |
---|
232 | } |
---|
233 | |
---|
234 | inline int |
---|
235 | imm_cmp_p ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
236 | { |
---|
237 | if ( imm2int( lhs ) == imm2int( rhs ) ) |
---|
238 | return 0; |
---|
239 | else if ( imm2int( lhs ) > imm2int( rhs ) ) |
---|
240 | return 1; |
---|
241 | else |
---|
242 | return -1; |
---|
243 | } |
---|
244 | |
---|
245 | inline int |
---|
246 | imm_cmp_gf ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
247 | { |
---|
248 | if ( imm2int( lhs ) == imm2int( rhs ) ) |
---|
249 | return 0; |
---|
250 | // check is done in this way because zero should be minimal |
---|
251 | else if ( imm2int( lhs ) > imm2int( rhs ) ) |
---|
252 | return -1; |
---|
253 | else |
---|
254 | return 1; |
---|
255 | } |
---|
256 | //}}} |
---|
257 | |
---|
258 | //{{{ arithmetic operators |
---|
259 | inline InternalCF * imm_add ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
260 | { |
---|
261 | long result = imm2int( lhs ) + imm2int( rhs ); |
---|
262 | if ( ( result > MAXIMMEDIATE ) || ( result < MINIMMEDIATE ) ) |
---|
263 | return CFFactory::basic( result ); |
---|
264 | else |
---|
265 | return int2imm( result ); |
---|
266 | } |
---|
267 | |
---|
268 | inline InternalCF * imm_add_p ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
269 | { |
---|
270 | return int2imm_p( ff_add( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
271 | } |
---|
272 | |
---|
273 | inline InternalCF * imm_add_gf ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
274 | { |
---|
275 | return int2imm_gf( gf_add( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
276 | } |
---|
277 | |
---|
278 | inline InternalCF * imm_sub ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
279 | { |
---|
280 | long result = imm2int( lhs ) - imm2int( rhs ); |
---|
281 | if ( ( result > MAXIMMEDIATE ) || ( result < MINIMMEDIATE ) ) |
---|
282 | return CFFactory::basic( result ); |
---|
283 | else |
---|
284 | return int2imm( result ); |
---|
285 | } |
---|
286 | |
---|
287 | inline InternalCF * imm_sub_p ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
288 | { |
---|
289 | return int2imm_p( ff_sub( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
290 | } |
---|
291 | |
---|
292 | inline InternalCF * imm_sub_gf ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
293 | { |
---|
294 | return int2imm_gf( gf_sub( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
295 | } |
---|
296 | |
---|
297 | inline InternalCF * |
---|
298 | imm_mul ( InternalCF * lhs, InternalCF * rhs ) |
---|
299 | { |
---|
300 | long a = imm2int( lhs ); |
---|
301 | long b = imm2int( rhs ); |
---|
302 | INT64 result = (INT64)a * (INT64)b; |
---|
303 | if ( ( a!=0L ) && ((result/a!=b) |
---|
304 | ||(result>MAXIMMEDIATE)||(result<MINIMMEDIATE) ) ) |
---|
305 | { |
---|
306 | InternalCF * res = CFFactory::basic( IntegerDomain, a, true ); |
---|
307 | return res->mulcoeff( rhs ); |
---|
308 | } |
---|
309 | else |
---|
310 | return int2imm( result ); |
---|
311 | } |
---|
312 | |
---|
313 | inline InternalCF * imm_mul_p ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
314 | { |
---|
315 | return int2imm_p( ff_mul( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
316 | } |
---|
317 | |
---|
318 | inline InternalCF * imm_mul_gf ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
319 | { |
---|
320 | return int2imm_gf( gf_mul( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
321 | } |
---|
322 | |
---|
323 | inline InternalCF * imm_div ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
324 | { |
---|
325 | long a = imm2int( lhs ); |
---|
326 | long b = imm2int( rhs ); |
---|
327 | if ( a > 0 ) |
---|
328 | return int2imm( a / b ); |
---|
329 | else if ( b > 0 ) |
---|
330 | return int2imm( -((b-a-1)/b) ); |
---|
331 | else |
---|
332 | return int2imm( (-a-b-1)/(-b) ); |
---|
333 | } |
---|
334 | |
---|
335 | inline InternalCF * imm_divrat ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
336 | { |
---|
337 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) |
---|
338 | return CFFactory::rational( imm2int( lhs ), imm2int( rhs ) ); |
---|
339 | else { |
---|
340 | long a = imm2int( lhs ); |
---|
341 | long b = imm2int( rhs ); |
---|
342 | if ( a > 0 ) |
---|
343 | return int2imm( a / b ); |
---|
344 | else if ( b > 0 ) |
---|
345 | return int2imm( -((b-a-1)/b) ); |
---|
346 | else |
---|
347 | return int2imm( (-a-b-1)/(-b) ); |
---|
348 | } |
---|
349 | } |
---|
350 | |
---|
351 | inline InternalCF * imm_div_p ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
352 | { |
---|
353 | return int2imm_p( ff_div( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
354 | } |
---|
355 | |
---|
356 | inline InternalCF * imm_div_gf ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
357 | { |
---|
358 | return int2imm_gf( gf_div( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
359 | } |
---|
360 | |
---|
361 | inline InternalCF * imm_mod ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
362 | { |
---|
363 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) |
---|
364 | return int2imm( 0 ); |
---|
365 | else { |
---|
366 | long a = imm2int( lhs ); |
---|
367 | long b = imm2int( rhs ); |
---|
368 | if ( a > 0 ) |
---|
369 | if ( b > 0 ) |
---|
370 | return int2imm( a % b ); |
---|
371 | else |
---|
372 | return int2imm( a % (-b) ); |
---|
373 | else |
---|
374 | if ( b > 0 ) { |
---|
375 | long r = (-a) % b; |
---|
376 | return int2imm( (r==0) ? r : b-r ); |
---|
377 | } |
---|
378 | else { |
---|
379 | long r = (-a) % (-b); |
---|
380 | return int2imm( (r==0) ? r : -b-r ); |
---|
381 | } |
---|
382 | } |
---|
383 | } |
---|
384 | |
---|
385 | inline InternalCF * imm_mod_p ( const InternalCF * const, const InternalCF * const ) |
---|
386 | { |
---|
387 | return int2imm_p( 0 ); |
---|
388 | } |
---|
389 | |
---|
390 | inline InternalCF * imm_mod_gf ( const InternalCF * const, const InternalCF * const ) |
---|
391 | { |
---|
392 | return int2imm_gf( gf_q ); |
---|
393 | } |
---|
394 | |
---|
395 | inline void imm_divrem ( const InternalCF * const lhs, const InternalCF * const rhs, InternalCF * & q, InternalCF * & r ) |
---|
396 | { |
---|
397 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) { |
---|
398 | q = imm_divrat( lhs, rhs ); |
---|
399 | r = CFFactory::basic( 0L ); |
---|
400 | } |
---|
401 | else { |
---|
402 | q = imm_div( lhs, rhs ); |
---|
403 | r = imm_mod( lhs, rhs ); |
---|
404 | } |
---|
405 | } |
---|
406 | |
---|
407 | inline void imm_divrem_p ( const InternalCF * const lhs, const InternalCF * const rhs, InternalCF * & q, InternalCF * & r ) |
---|
408 | { |
---|
409 | q = int2imm_p( ff_div( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
410 | r = int2imm_p( 0 ); |
---|
411 | } |
---|
412 | |
---|
413 | inline void imm_divrem_gf ( const InternalCF * const lhs, const InternalCF * const rhs, InternalCF * & q, InternalCF * & r ) |
---|
414 | { |
---|
415 | q = int2imm_gf( gf_div( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
416 | r = int2imm_gf( gf_q ); |
---|
417 | } |
---|
418 | |
---|
419 | //{{{ inline InternalCF * imm_neg, imm_neg_p, imm_neg_gf ( const InternalCF * const op ) |
---|
420 | // docu: see CanonicalForm::operator -() |
---|
421 | inline InternalCF * |
---|
422 | imm_neg ( const InternalCF * const op ) |
---|
423 | { |
---|
424 | return int2imm( -imm2int( op ) ); |
---|
425 | } |
---|
426 | |
---|
427 | inline InternalCF * |
---|
428 | imm_neg_p ( const InternalCF * const op ) |
---|
429 | { |
---|
430 | return int2imm_p( ff_neg( imm2int( op ) ) ); |
---|
431 | } |
---|
432 | |
---|
433 | inline InternalCF * |
---|
434 | imm_neg_gf ( const InternalCF * const op ) |
---|
435 | { |
---|
436 | return int2imm_gf( gf_neg( imm2int( op ) ) ); |
---|
437 | } |
---|
438 | //}}} |
---|
439 | //}}} |
---|
440 | |
---|
441 | //{{{ input/output |
---|
442 | #ifndef NOSTREAMIO |
---|
443 | inline void imm_print ( OSTREAM & os, const InternalCF * const op, const char * const str ) |
---|
444 | { |
---|
445 | if ( is_imm( op ) == FFMARK ) |
---|
446 | if ( cf_glob_switches.isOn( SW_SYMMETRIC_FF ) ) |
---|
447 | os << ff_symmetric( imm2int( op ) ) << str; |
---|
448 | else |
---|
449 | os << imm2int( op ) << str; |
---|
450 | else if ( is_imm( op ) == GFMARK ) { |
---|
451 | gf_print( os, imm2int( op ) ); |
---|
452 | os << str; |
---|
453 | } |
---|
454 | else |
---|
455 | os << imm2int( op ) << str; |
---|
456 | } |
---|
457 | #endif /* NOSTREAMIO */ |
---|
458 | //}}} |
---|
459 | |
---|
460 | #endif /* ! INCL_IMM_H */ |
---|