1 | // emacs edit mode for this file is -*- C++ -*- |
---|
2 | // $Id: imm.h,v 1.1 1997-03-27 10:00:31 schmidt Exp $ |
---|
3 | |
---|
4 | #ifndef INCL_IMMEDIATE_H |
---|
5 | #define INCL_IMMEDIATE_H |
---|
6 | |
---|
7 | /* |
---|
8 | $Log: not supported by cvs2svn $ |
---|
9 | Revision 1.0 1996/05/17 10:59:41 stobbe |
---|
10 | Initial revision |
---|
11 | |
---|
12 | */ |
---|
13 | |
---|
14 | #ifndef NOSTREAMIO |
---|
15 | #include <iostream.h> |
---|
16 | #endif /* NOSTREAMIO */ |
---|
17 | |
---|
18 | #include "assert.h" |
---|
19 | |
---|
20 | #include "cf_defs.h" |
---|
21 | |
---|
22 | #include "cf_globals.h" |
---|
23 | #include "ffops.h" |
---|
24 | #include "gfops.h" |
---|
25 | #include "cf_factory.h" |
---|
26 | #include "canonicalform.h" |
---|
27 | #include "int_cf.h" |
---|
28 | |
---|
29 | const int INTMARK = 1; |
---|
30 | const int FFMARK = 2; |
---|
31 | const int GFMARK = 3; |
---|
32 | |
---|
33 | const int MINIMMEDIATE = -268435454; // -2^28-2 |
---|
34 | const int MAXIMMEDIATE = 268435454; // 2^28-2 |
---|
35 | const long long int MINIMMEDIATELL = -268435454LL; |
---|
36 | const long long int MAXIMMEDIATELL = 268435454LL; |
---|
37 | |
---|
38 | class InternalCF; |
---|
39 | |
---|
40 | #ifdef HAS_ARITHMETIC_SHIFT |
---|
41 | |
---|
42 | inline int imm2int ( const InternalCF * const imm ) |
---|
43 | { |
---|
44 | return (int)imm >> 2; |
---|
45 | } |
---|
46 | |
---|
47 | inline InternalCF * int2imm ( int i ) |
---|
48 | { |
---|
49 | return (InternalCF*)((i << 2) | INTMARK ); |
---|
50 | } |
---|
51 | |
---|
52 | #else |
---|
53 | |
---|
54 | inline int imm2int ( const InternalCF * const imm ) |
---|
55 | { |
---|
56 | // this could be better done by masking the sign bit |
---|
57 | if ( (int)imm < 0 ) |
---|
58 | return -((-(int)imm) >> 2); |
---|
59 | else |
---|
60 | return (int)imm >> 2; |
---|
61 | } |
---|
62 | |
---|
63 | inline InternalCF * int2imm ( int i ) |
---|
64 | { |
---|
65 | if ( i < 0 ) |
---|
66 | return (InternalCF*)(-(((-i) << 2) | INTMARK)); |
---|
67 | else |
---|
68 | return (InternalCF*)((i << 2) | INTMARK ); |
---|
69 | } |
---|
70 | |
---|
71 | #endif |
---|
72 | |
---|
73 | |
---|
74 | inline InternalCF * int2imm_p ( int i ) |
---|
75 | { |
---|
76 | return (InternalCF*)((i << 2) | FFMARK ); |
---|
77 | } |
---|
78 | |
---|
79 | inline InternalCF * int2imm_gf ( int i ) |
---|
80 | { |
---|
81 | return (InternalCF*)((i << 2) | GFMARK ); |
---|
82 | } |
---|
83 | |
---|
84 | inline int is_imm ( const InternalCF * const ptr ) |
---|
85 | { |
---|
86 | // returns 0 if ptr is not immediate |
---|
87 | return ( (int)ptr & 3 ); |
---|
88 | } |
---|
89 | |
---|
90 | inline int imm_iszero ( const InternalCF * const ptr ) |
---|
91 | { |
---|
92 | return imm2int( ptr ) == 0; |
---|
93 | } |
---|
94 | |
---|
95 | inline int imm_isone ( const InternalCF * const ptr ) |
---|
96 | { |
---|
97 | return imm2int( ptr ) == 1; |
---|
98 | } |
---|
99 | |
---|
100 | inline int imm_iszero_p ( const InternalCF * const ptr ) |
---|
101 | { |
---|
102 | return imm2int( ptr ) == 0; |
---|
103 | } |
---|
104 | |
---|
105 | inline int imm_isone_p ( const InternalCF * const ptr ) |
---|
106 | { |
---|
107 | return imm2int( ptr ) == 1; |
---|
108 | } |
---|
109 | |
---|
110 | inline int imm_iszero_gf ( const InternalCF * const ptr ) |
---|
111 | { |
---|
112 | return gf_iszero( imm2int( ptr ) ); |
---|
113 | } |
---|
114 | |
---|
115 | inline int imm_isone_gf ( const InternalCF * const ptr ) |
---|
116 | { |
---|
117 | return gf_isone( imm2int( ptr ) ); |
---|
118 | } |
---|
119 | |
---|
120 | inline int imm_cmp ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
121 | { |
---|
122 | if ( imm2int( lhs ) == imm2int( rhs ) ) |
---|
123 | return 0; |
---|
124 | else if ( imm2int( lhs ) > imm2int( rhs ) ) |
---|
125 | return 1; |
---|
126 | else |
---|
127 | return -1; |
---|
128 | } |
---|
129 | |
---|
130 | // the following two functions are needed only for comparisons of |
---|
131 | // polynomials (gmpf) |
---|
132 | |
---|
133 | inline int imm_cmp_p ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
134 | { |
---|
135 | if ( imm2int( lhs ) == imm2int( rhs ) ) |
---|
136 | return 0; |
---|
137 | else |
---|
138 | return 1; |
---|
139 | } |
---|
140 | |
---|
141 | inline int imm_cmp_gf ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
142 | { |
---|
143 | if ( imm2int( lhs ) == imm2int( rhs ) ) |
---|
144 | return 0; |
---|
145 | else |
---|
146 | return 1; |
---|
147 | } |
---|
148 | |
---|
149 | inline InternalCF * imm_add ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
150 | { |
---|
151 | int result = imm2int( lhs ) + imm2int( rhs ); |
---|
152 | if ( ( result > MAXIMMEDIATE ) || ( result < MINIMMEDIATE ) ) |
---|
153 | return CFFactory::basic( result ); |
---|
154 | else |
---|
155 | return int2imm( result ); |
---|
156 | } |
---|
157 | |
---|
158 | inline InternalCF * imm_add_p ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
159 | { |
---|
160 | return int2imm_p( ff_add( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
161 | } |
---|
162 | |
---|
163 | inline InternalCF * imm_add_gf ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
164 | { |
---|
165 | return int2imm_gf( gf_add( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
166 | } |
---|
167 | |
---|
168 | inline InternalCF * imm_sub ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
169 | { |
---|
170 | int result = imm2int( lhs ) - imm2int( rhs ); |
---|
171 | if ( ( result > MAXIMMEDIATE ) || ( result < MINIMMEDIATE ) ) |
---|
172 | return CFFactory::basic( result ); |
---|
173 | else |
---|
174 | return int2imm( result ); |
---|
175 | } |
---|
176 | |
---|
177 | inline InternalCF * imm_sub_p ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
178 | { |
---|
179 | return int2imm_p( ff_sub( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
180 | } |
---|
181 | |
---|
182 | inline InternalCF * imm_sub_gf ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
183 | { |
---|
184 | return int2imm_gf( gf_sub( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
185 | } |
---|
186 | |
---|
187 | inline InternalCF * imm_mul ( InternalCF * lhs, InternalCF * rhs ) |
---|
188 | { |
---|
189 | long long int result = (long long int)imm2int( lhs ) * imm2int( rhs ); |
---|
190 | if ( ( result > MAXIMMEDIATELL ) || ( result < MINIMMEDIATELL ) ) { |
---|
191 | InternalCF * res = CFFactory::basic( IntegerDomain, imm2int( lhs ), true ); |
---|
192 | return res->mulcoeff( rhs ); |
---|
193 | } |
---|
194 | else |
---|
195 | return int2imm( (int)result ); |
---|
196 | } |
---|
197 | |
---|
198 | inline InternalCF * imm_mul_p ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
199 | { |
---|
200 | return int2imm_p( ff_mul( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
201 | } |
---|
202 | |
---|
203 | inline InternalCF * imm_mul_gf ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
204 | { |
---|
205 | return int2imm_gf( gf_mul( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
206 | } |
---|
207 | |
---|
208 | inline InternalCF * imm_div ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
209 | { |
---|
210 | int a = imm2int( lhs ); |
---|
211 | int b = imm2int( rhs ); |
---|
212 | if ( a > 0 ) |
---|
213 | return int2imm( a / b ); |
---|
214 | else if ( b > 0 ) |
---|
215 | return int2imm( -((b-a-1)/b) ); |
---|
216 | else |
---|
217 | return int2imm( (-a-b-1)/(-b) ); |
---|
218 | } |
---|
219 | |
---|
220 | inline InternalCF * imm_divrat ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
221 | { |
---|
222 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) |
---|
223 | return CFFactory::rational( imm2int( lhs ), imm2int( rhs ) ); |
---|
224 | else { |
---|
225 | int a = imm2int( lhs ); |
---|
226 | int b = imm2int( rhs ); |
---|
227 | if ( a > 0 ) |
---|
228 | return int2imm( a / b ); |
---|
229 | else if ( b > 0 ) |
---|
230 | return int2imm( -((b-a-1)/b) ); |
---|
231 | else |
---|
232 | return int2imm( (-a-b-1)/(-b) ); |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | inline InternalCF * imm_div_p ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
237 | { |
---|
238 | return int2imm_p( ff_div( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
239 | } |
---|
240 | |
---|
241 | inline InternalCF * imm_div_gf ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
242 | { |
---|
243 | return int2imm_gf( gf_div( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
244 | } |
---|
245 | |
---|
246 | inline InternalCF * imm_mod ( const InternalCF * const lhs, const InternalCF * const rhs ) |
---|
247 | { |
---|
248 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) |
---|
249 | return int2imm( 0 ); |
---|
250 | else { |
---|
251 | int a = imm2int( lhs ); |
---|
252 | int b = imm2int( rhs ); |
---|
253 | if ( a > 0 ) |
---|
254 | if ( b > 0 ) |
---|
255 | return int2imm( a % b ); |
---|
256 | else |
---|
257 | return int2imm( a % (-b) ); |
---|
258 | else |
---|
259 | if ( b > 0 ) { |
---|
260 | int r = (-a) % b; |
---|
261 | return int2imm( (r==0) ? r : b-r ); |
---|
262 | } |
---|
263 | else { |
---|
264 | int r = (-a) % (-b); |
---|
265 | return int2imm( (r==0) ? r : -b-r ); |
---|
266 | } |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | inline InternalCF * imm_mod_p ( const InternalCF * const, const InternalCF * const ) |
---|
271 | { |
---|
272 | return int2imm_p( 0 ); |
---|
273 | } |
---|
274 | |
---|
275 | inline InternalCF * imm_mod_gf ( const InternalCF * const, const InternalCF * const ) |
---|
276 | { |
---|
277 | return int2imm_gf( gf_q ); |
---|
278 | } |
---|
279 | |
---|
280 | inline void imm_divrem ( const InternalCF * const lhs, const InternalCF * const rhs, InternalCF * & q, InternalCF * & r ) |
---|
281 | { |
---|
282 | if ( cf_glob_switches.isOn( SW_RATIONAL ) ) { |
---|
283 | q = imm_divrat( lhs, rhs ); |
---|
284 | r = CFFactory::basic( 0 ); |
---|
285 | } |
---|
286 | else { |
---|
287 | q = imm_div( lhs, rhs ); |
---|
288 | r = imm_mod( lhs, rhs ); |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | inline void imm_divrem_p ( const InternalCF * const lhs, const InternalCF * const rhs, InternalCF * & q, InternalCF * & r ) |
---|
293 | { |
---|
294 | q = int2imm_p( ff_div( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
295 | r = int2imm_p( 0 ); |
---|
296 | } |
---|
297 | |
---|
298 | inline void imm_divrem_gf ( const InternalCF * const lhs, const InternalCF * const rhs, InternalCF * & q, InternalCF * & r ) |
---|
299 | { |
---|
300 | q = int2imm_gf( gf_div( imm2int( lhs ), imm2int( rhs ) ) ); |
---|
301 | r = int2imm_gf( gf_q ); |
---|
302 | } |
---|
303 | |
---|
304 | inline InternalCF * imm_neg ( const InternalCF * const op ) |
---|
305 | { |
---|
306 | return int2imm( -imm2int( op ) ); |
---|
307 | } |
---|
308 | |
---|
309 | inline InternalCF * imm_neg_p ( const InternalCF * const op ) |
---|
310 | { |
---|
311 | return int2imm_p( ff_neg( imm2int( op ) ) ); |
---|
312 | } |
---|
313 | |
---|
314 | inline InternalCF * imm_neg_gf ( const InternalCF * const op ) |
---|
315 | { |
---|
316 | return int2imm_gf( gf_neg( imm2int( op ) ) ); |
---|
317 | } |
---|
318 | |
---|
319 | #ifndef NOSTREAMIO |
---|
320 | inline void imm_print ( ostream & os, const InternalCF * const op, const char * const str ) |
---|
321 | { |
---|
322 | if ( is_imm( op ) == FFMARK ) |
---|
323 | if ( cf_glob_switches.isOn( SW_SYMMETRIC_FF ) ) |
---|
324 | os << ff_symmetric( imm2int( op ) ) << str; |
---|
325 | else |
---|
326 | os << imm2int( op ) << str; |
---|
327 | else if ( is_imm( op ) == GFMARK ) { |
---|
328 | gf_print( os, imm2int( op ) ); |
---|
329 | os << str; |
---|
330 | } |
---|
331 | else |
---|
332 | os << imm2int( op ) << str; |
---|
333 | } |
---|
334 | #endif /* NOSTREAMIO */ |
---|
335 | |
---|
336 | inline int imm_intval ( const InternalCF* const op ) |
---|
337 | { |
---|
338 | if ( is_imm( op ) == FFMARK ) |
---|
339 | if ( cf_glob_switches.isOn( SW_SYMMETRIC_FF ) ) |
---|
340 | return ff_symmetric( imm2int( op ) ); |
---|
341 | else |
---|
342 | return imm2int( op ); |
---|
343 | else if ( is_imm( op ) == GFMARK ) { |
---|
344 | ASSERT( 0, "not yet implemented" ); |
---|
345 | return 0; |
---|
346 | } |
---|
347 | else |
---|
348 | return imm2int( op ); |
---|
349 | } |
---|
350 | |
---|
351 | inline int imm_sign ( const InternalCF * const op ) |
---|
352 | { |
---|
353 | if ( imm2int( op ) == 0 ) |
---|
354 | return 0; |
---|
355 | else if ( is_imm( op ) == FFMARK ) |
---|
356 | if ( cf_glob_switches.isOn( SW_SYMMETRIC_FF ) ) |
---|
357 | if ( ff_symmetric( imm2int( op ) ) > 0 ) |
---|
358 | return 1; |
---|
359 | else |
---|
360 | return -1; |
---|
361 | else |
---|
362 | return 1; |
---|
363 | else if ( is_imm( op ) == GFMARK ) |
---|
364 | return gf_sign( imm2int( op ) ); |
---|
365 | else if ( imm2int( op ) > 0 ) |
---|
366 | return 1; |
---|
367 | else |
---|
368 | return -1; |
---|
369 | } |
---|
370 | |
---|
371 | |
---|
372 | #endif |
---|