source: git/factory/cf_factory.cc @ 2fb01f

spielwiese
Last change on this file since 2fb01f was ef4ae2, checked in by Hans Schoenemann <hannes@…>, 4 years ago
opt: factory (mpz_init, is_imm, moved tests out of loops(addTermList) etc
  • Property mode set to 100644
File size: 8.2 KB
Line 
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
28int CFFactory::currenttype = IntegerDomain;
29
30InternalCF *
31CFFactory::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    #ifndef HAVE_NTL
50    case PrimePowerDomain:
51        return new InternalPrimePower( value );
52    #endif
53    default: {
54        ASSERT( 0, "illegal basic domain!" );
55        return 0;
56    }
57  }
58}
59
60#if SIZEOF_LONG == 8
61InternalCF *
62CFFactory::basic ( int value )
63{
64  switch(currenttype)
65  {
66    case IntegerDomain:
67            return int2imm( value );
68//     else  if ( currenttype == RationalDomain )
69//         if ( value >= MINIMMEDIATE && value <= MAXIMMEDIATE )
70//             return int2imm( value );
71//         else
72//             return new InternalRational( value );
73    case FiniteFieldDomain:
74        return int2imm_p( ff_norm( value ) );
75    case GaloisFieldDomain:
76        return int2imm_gf( gf_int2gf( value ) );
77    #ifndef HAVE_NTL
78    case PrimePowerDomain:
79        return new InternalPrimePower( value );
80    #endif
81    default: {
82        ASSERT( 0, "illegal basic domain!" );
83        return 0;
84    }
85  }
86}
87#endif
88
89InternalCF *
90CFFactory::basic ( int type, long value )
91{
92    if ( type == IntegerDomain )
93        if (LIKELY( value >= MINIMMEDIATE && value <= MAXIMMEDIATE ))
94            return int2imm( value );
95        else
96            return new InternalInteger( value );
97//     else  if ( type == RationalDomain )
98//         if ( value >= MINIMMEDIATE && value <= MAXIMMEDIATE )
99//             return int2imm( value );
100//         else
101//             return new InternalRational( value );
102    else  if ( type == FiniteFieldDomain )
103        return int2imm_p( ff_norm( value ) );
104    else  if ( type == GaloisFieldDomain )
105        return int2imm_gf( gf_int2gf( value ) );
106    else {
107        ASSERT1( 0, "illegal basic domain (type = %d)!", type );
108        return 0;
109    }
110}
111
112InternalCF *
113CFFactory::basic ( const char * str )
114{
115    if ( currenttype == IntegerDomain ) {
116        InternalInteger * dummy = new InternalInteger( str );
117        if ( dummy->is_imm() ) {
118            InternalCF * res = int2imm( dummy->intval() );
119            delete dummy;
120            return res;
121        }
122        else
123            return dummy;
124    }
125//     else  if ( currenttype == RationalDomain ) {
126//         InternalRational * dummy = new InternalRational( str );
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 == FiniteFieldDomain ) {
136        InternalInteger * dummy = new InternalInteger( str );
137        InternalCF * res = int2imm_p( dummy->intmod( ff_prime ) );
138        delete dummy;
139        return res;
140    }
141    else  if ( currenttype == GaloisFieldDomain ) {
142        InternalInteger * dummy = new InternalInteger( str );
143        InternalCF * res = int2imm_gf( gf_int2gf( dummy->intmod( ff_prime ) ) );
144        delete dummy;
145        return res;
146    }
147    else {
148        ASSERT( 0, "illegal basic domain!" );
149        return 0;
150    }
151}
152
153InternalCF *
154CFFactory::basic ( const char * str, int base )
155{
156    if ( currenttype == IntegerDomain ) {
157        InternalInteger * dummy = new InternalInteger( str, base );
158        if ( dummy->is_imm() ) {
159            InternalCF * res = int2imm( dummy->intval() );
160            delete dummy;
161            return res;
162        }
163        else
164            return dummy;
165    }
166//     else  if ( currenttype == RationalDomain ) {
167//         InternalRational * dummy = new InternalRational( 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 ( currenttype == FiniteFieldDomain ) {
177        InternalInteger * dummy = new InternalInteger( str, base );
178        InternalCF * res = int2imm_p( dummy->intmod( ff_prime ) );
179        delete dummy;
180        return res;
181    }
182    else  if ( currenttype == GaloisFieldDomain ) {
183        InternalInteger * dummy = new InternalInteger( str, base );
184        InternalCF * res = int2imm_gf( gf_int2gf( dummy->intmod( ff_prime ) ) );
185        delete dummy;
186        return res;
187    }
188    else {
189        ASSERT( 0, "illegal basic domain!" );
190        return 0;
191    }
192}
193
194InternalCF *
195CFFactory::basic ( int type, const char * const str )
196{
197    if ( type == IntegerDomain ) {
198        InternalInteger * dummy = new InternalInteger( str );
199        if ( dummy->is_imm() ) {
200            InternalCF * res = int2imm( dummy->intval() );
201            delete dummy;
202            return res;
203        }
204        else
205            return dummy;
206    }
207//     else  if ( type == RationalDomain ) {
208//         InternalRational * dummy = new InternalRational( str );
209//         if ( dummy->is_imm() ) {
210//             InternalCF * res = int2imm( dummy->intval() );
211//             delete dummy;
212//             return res;
213//         }
214//         else
215//             return dummy;
216//     }
217    else  if ( type == FiniteFieldDomain ) {
218        InternalInteger * dummy = new InternalInteger( str );
219        InternalCF * res = int2imm( dummy->intmod( ff_prime ) );
220        delete dummy;
221        return res;
222    }
223    else  if ( type == GaloisFieldDomain ) {
224        InternalInteger * dummy = new InternalInteger( str );
225        InternalCF * res = int2imm_gf( gf_int2gf( dummy->intmod( ff_prime ) ) );
226        delete dummy;
227        return res;
228    }
229    else {
230        ASSERT( 0, "illegal basic domain!" );
231        return 0;
232    }
233}
234
235InternalCF *
236CFFactory::basic ( int type, long value, bool nonimm )
237{
238    if ( nonimm )
239        if ( type == IntegerDomain )
240            return new InternalInteger( value );
241         else  if ( type == RationalDomain )
242             return new InternalRational( value );
243        else {
244            ASSERT( 0, "illegal basic domain!" );
245            return 0;
246        }
247    else
248        return CFFactory::basic( type, value );
249}
250
251InternalCF *
252CFFactory::basic ( const mpz_ptr num )
253{
254  ASSERT (currenttype == IntegerDomain, "Integer domain expected");
255  return new InternalInteger( num );
256}
257
258InternalCF *
259CFFactory::rational ( long num, long den )
260{
261    InternalRational * res = new InternalRational( num, den );
262    return res->normalize_myself();
263}
264
265InternalCF *
266CFFactory::rational ( const mpz_ptr num, const mpz_ptr den, bool normalize )
267{
268    if ( normalize ) {
269        InternalRational * result = new InternalRational( num, den );
270        return result->normalize_myself();
271    }
272    else
273        return new InternalRational( num, den );
274}
275
276InternalCF *
277CFFactory::poly (  const Variable & v, int exp, const CanonicalForm & c )
278{
279    if ( v.level() == LEVELBASE )
280        return c.getval();
281    else
282        return new InternalPoly( v, exp, c );
283}
284
285InternalCF *
286CFFactory::poly ( const Variable & v, int exp )
287{
288    if ( v.level() == LEVELBASE )
289        return CFFactory::basic( 1L );
290    else
291        return new InternalPoly( v, exp, 1 );
292}
293
294void getmpi ( InternalCF * value, mpz_t mpi)
295{
296    ASSERT( ! is_imm( value ) && (value->levelcoeff() == IntegerDomain || value->levelcoeff() == PrimePowerDomain), "illegal operation" );
297    mpz_init_set (mpi, ((InternalInteger*)value)->thempi);
298}
Note: See TracBrowser for help on using the repository browser.