source: git/factory/imm.h @ ac7e53

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