1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | |
---|
3 | |
---|
4 | #include "config.h" |
---|
5 | |
---|
6 | |
---|
7 | #include "cf_assert.h" |
---|
8 | |
---|
9 | #include "cf_defs.h" |
---|
10 | #include "cf_factory.h" |
---|
11 | #include "canonicalform.h" |
---|
12 | #include "int_cf.h" |
---|
13 | #include "int_int.h" |
---|
14 | #include "int_rat.h" |
---|
15 | #include "int_poly.h" |
---|
16 | #include "int_pp.h" |
---|
17 | #include "imm.h" |
---|
18 | |
---|
19 | /// For optimizing if-branches |
---|
20 | #ifdef __GNUC__ |
---|
21 | #define LIKELY(expression) (__builtin_expect(!!(expression), 1)) |
---|
22 | #define UNLIKELY(expression) (__builtin_expect(!!(expression), 0)) |
---|
23 | #else |
---|
24 | #define LIKELY(expression) (expression) |
---|
25 | #define UNLIKELY(expression) (expression) |
---|
26 | #endif |
---|
27 | |
---|
28 | int CFFactory::currenttype = IntegerDomain; |
---|
29 | |
---|
30 | InternalCF * |
---|
31 | CFFactory::basic ( long value ) |
---|
32 | { |
---|
33 | switch(currenttype) |
---|
34 | { |
---|
35 | case IntegerDomain: |
---|
36 | if (LIKELY( value >= MINIMMEDIATE && value <= MAXIMMEDIATE )) |
---|
37 | return int2imm( value ); |
---|
38 | else |
---|
39 | return new InternalInteger( value ); |
---|
40 | // else if ( currenttype == RationalDomain ) |
---|
41 | // if ( value >= MINIMMEDIATE && value <= MAXIMMEDIATE ) |
---|
42 | // return int2imm( value ); |
---|
43 | // else |
---|
44 | // return new InternalRational( value ); |
---|
45 | case FiniteFieldDomain: |
---|
46 | return int2imm_p( ff_norm( value ) ); |
---|
47 | case GaloisFieldDomain: |
---|
48 | return int2imm_gf( gf_int2gf( value ) ); |
---|
49 | case PrimePowerDomain: |
---|
50 | return new InternalPrimePower( value ); |
---|
51 | default: { |
---|
52 | ASSERT( 0, "illegal basic domain!" ); |
---|
53 | return 0; |
---|
54 | } |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | InternalCF * |
---|
59 | CFFactory::basic ( int type, long value ) |
---|
60 | { |
---|
61 | if ( type == IntegerDomain ) |
---|
62 | if (LIKELY( value >= MINIMMEDIATE && value <= MAXIMMEDIATE )) |
---|
63 | return int2imm( value ); |
---|
64 | else |
---|
65 | return new InternalInteger( value ); |
---|
66 | // else if ( type == RationalDomain ) |
---|
67 | // if ( value >= MINIMMEDIATE && value <= MAXIMMEDIATE ) |
---|
68 | // return int2imm( value ); |
---|
69 | // else |
---|
70 | // return new InternalRational( value ); |
---|
71 | else if ( type == FiniteFieldDomain ) |
---|
72 | return int2imm_p( ff_norm( value ) ); |
---|
73 | else if ( type == GaloisFieldDomain ) |
---|
74 | return int2imm_gf( gf_int2gf( value ) ); |
---|
75 | else { |
---|
76 | ASSERT1( 0, "illegal basic domain (type = %d)!", type ); |
---|
77 | return 0; |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | InternalCF * |
---|
82 | CFFactory::basic ( const char * str ) |
---|
83 | { |
---|
84 | if ( currenttype == IntegerDomain ) { |
---|
85 | InternalInteger * dummy = new InternalInteger( str ); |
---|
86 | if ( dummy->is_imm() ) { |
---|
87 | InternalCF * res = int2imm( dummy->intval() ); |
---|
88 | delete dummy; |
---|
89 | return res; |
---|
90 | } |
---|
91 | else |
---|
92 | return dummy; |
---|
93 | } |
---|
94 | // else if ( currenttype == RationalDomain ) { |
---|
95 | // InternalRational * dummy = new InternalRational( str ); |
---|
96 | // if ( dummy->is_imm() ) { |
---|
97 | // InternalCF * res = int2imm( dummy->intval() ); |
---|
98 | // delete dummy; |
---|
99 | // return res; |
---|
100 | // } |
---|
101 | // else |
---|
102 | // return dummy; |
---|
103 | // } |
---|
104 | else if ( currenttype == FiniteFieldDomain ) { |
---|
105 | InternalInteger * dummy = new InternalInteger( str ); |
---|
106 | InternalCF * res = int2imm_p( dummy->intmod( ff_prime ) ); |
---|
107 | delete dummy; |
---|
108 | return res; |
---|
109 | } |
---|
110 | else if ( currenttype == GaloisFieldDomain ) { |
---|
111 | InternalInteger * dummy = new InternalInteger( str ); |
---|
112 | InternalCF * res = int2imm_gf( gf_int2gf( dummy->intmod( ff_prime ) ) ); |
---|
113 | delete dummy; |
---|
114 | return res; |
---|
115 | } |
---|
116 | else { |
---|
117 | ASSERT( 0, "illegal basic domain!" ); |
---|
118 | return 0; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | InternalCF * |
---|
123 | CFFactory::basic ( const char * str, int base ) |
---|
124 | { |
---|
125 | if ( currenttype == IntegerDomain ) { |
---|
126 | InternalInteger * dummy = new InternalInteger( str, base ); |
---|
127 | if ( dummy->is_imm() ) { |
---|
128 | InternalCF * res = int2imm( dummy->intval() ); |
---|
129 | delete dummy; |
---|
130 | return res; |
---|
131 | } |
---|
132 | else |
---|
133 | return dummy; |
---|
134 | } |
---|
135 | // else if ( currenttype == RationalDomain ) { |
---|
136 | // InternalRational * dummy = new InternalRational( str ); |
---|
137 | // if ( dummy->is_imm() ) { |
---|
138 | // InternalCF * res = int2imm( dummy->intval() ); |
---|
139 | // delete dummy; |
---|
140 | // return res; |
---|
141 | // } |
---|
142 | // else |
---|
143 | // return dummy; |
---|
144 | // } |
---|
145 | else if ( currenttype == FiniteFieldDomain ) { |
---|
146 | InternalInteger * dummy = new InternalInteger( str, base ); |
---|
147 | InternalCF * res = int2imm_p( dummy->intmod( ff_prime ) ); |
---|
148 | delete dummy; |
---|
149 | return res; |
---|
150 | } |
---|
151 | else if ( currenttype == GaloisFieldDomain ) { |
---|
152 | InternalInteger * dummy = new InternalInteger( str, base ); |
---|
153 | InternalCF * res = int2imm_gf( gf_int2gf( dummy->intmod( ff_prime ) ) ); |
---|
154 | delete dummy; |
---|
155 | return res; |
---|
156 | } |
---|
157 | else { |
---|
158 | ASSERT( 0, "illegal basic domain!" ); |
---|
159 | return 0; |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | InternalCF * |
---|
164 | CFFactory::basic ( int type, const char * const str ) |
---|
165 | { |
---|
166 | if ( type == IntegerDomain ) { |
---|
167 | InternalInteger * dummy = new InternalInteger( str ); |
---|
168 | if ( dummy->is_imm() ) { |
---|
169 | InternalCF * res = int2imm( dummy->intval() ); |
---|
170 | delete dummy; |
---|
171 | return res; |
---|
172 | } |
---|
173 | else |
---|
174 | return dummy; |
---|
175 | } |
---|
176 | // else if ( type == RationalDomain ) { |
---|
177 | // InternalRational * dummy = new InternalRational( str ); |
---|
178 | // if ( dummy->is_imm() ) { |
---|
179 | // InternalCF * res = int2imm( dummy->intval() ); |
---|
180 | // delete dummy; |
---|
181 | // return res; |
---|
182 | // } |
---|
183 | // else |
---|
184 | // return dummy; |
---|
185 | // } |
---|
186 | else if ( type == FiniteFieldDomain ) { |
---|
187 | InternalInteger * dummy = new InternalInteger( str ); |
---|
188 | InternalCF * res = int2imm( dummy->intmod( ff_prime ) ); |
---|
189 | delete dummy; |
---|
190 | return res; |
---|
191 | } |
---|
192 | else if ( type == GaloisFieldDomain ) { |
---|
193 | InternalInteger * dummy = new InternalInteger( str ); |
---|
194 | InternalCF * res = int2imm_gf( gf_int2gf( dummy->intmod( ff_prime ) ) ); |
---|
195 | delete dummy; |
---|
196 | return res; |
---|
197 | } |
---|
198 | else { |
---|
199 | ASSERT( 0, "illegal basic domain!" ); |
---|
200 | return 0; |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | InternalCF * |
---|
205 | CFFactory::basic ( int type, long value, bool nonimm ) |
---|
206 | { |
---|
207 | if ( nonimm ) |
---|
208 | if ( type == IntegerDomain ) |
---|
209 | return new InternalInteger( value ); |
---|
210 | else if ( type == RationalDomain ) |
---|
211 | return new InternalRational( value ); |
---|
212 | else { |
---|
213 | ASSERT( 0, "illegal basic domain!" ); |
---|
214 | return 0; |
---|
215 | } |
---|
216 | else |
---|
217 | return CFFactory::basic( type, value ); |
---|
218 | } |
---|
219 | |
---|
220 | InternalCF * |
---|
221 | CFFactory::basic ( const mpz_ptr num ) |
---|
222 | { |
---|
223 | ASSERT (currenttype == IntegerDomain, "Integer domain expected"); |
---|
224 | return new InternalInteger( num ); |
---|
225 | } |
---|
226 | |
---|
227 | InternalCF * |
---|
228 | CFFactory::rational ( long num, long den ) |
---|
229 | { |
---|
230 | InternalRational * res = new InternalRational( num, den ); |
---|
231 | return res->normalize_myself(); |
---|
232 | } |
---|
233 | |
---|
234 | InternalCF * |
---|
235 | CFFactory::rational ( const mpz_ptr num, const mpz_ptr den, bool normalize ) |
---|
236 | { |
---|
237 | if ( normalize ) { |
---|
238 | InternalRational * result = new InternalRational( num, den ); |
---|
239 | return result->normalize_myself(); |
---|
240 | } |
---|
241 | else |
---|
242 | return new InternalRational( num, den ); |
---|
243 | } |
---|
244 | |
---|
245 | InternalCF * |
---|
246 | CFFactory::poly ( const Variable & v, int exp, const CanonicalForm & c ) |
---|
247 | { |
---|
248 | if ( v.level() == LEVELBASE ) |
---|
249 | return c.getval(); |
---|
250 | else |
---|
251 | return new InternalPoly( v, exp, c ); |
---|
252 | } |
---|
253 | |
---|
254 | InternalCF * |
---|
255 | CFFactory::poly ( const Variable & v, int exp ) |
---|
256 | { |
---|
257 | if ( v.level() == LEVELBASE ) |
---|
258 | return CFFactory::basic( 1L ); |
---|
259 | else |
---|
260 | return new InternalPoly( v, exp, 1 ); |
---|
261 | } |
---|
262 | |
---|
263 | void getmpi ( InternalCF * value, mpz_t mpi) |
---|
264 | { |
---|
265 | ASSERT( ! is_imm( value ) && (value->levelcoeff() == IntegerDomain || value->levelcoeff() == PrimePowerDomain), "illegal operation" ); |
---|
266 | mpz_init_set (mpi, ((InternalInteger*)value)->thempi); |
---|
267 | } |
---|