source: git/factory/cf_factory.cc @ 538e06

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