source: git/factory/int_int.h @ 2667bc8

spielwiese
Last change on this file since 2667bc8 was d07137, checked in by Hans Schönemann <hannes@…>, 21 years ago
*hannes: faster conversions git-svn-id: file:///usr/local/Singular/svn/trunk@6895 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 5.9 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2/* $Id: int_int.h,v 1.11 2003-10-15 17:19:41 Singular Exp $ */
3
4#ifndef INCL_INT_INT_H
5#define INCL_INT_INT_H
6
7#include <config.h>
8
9#ifndef NOSTREAMIO
10#include <iostream.h>
11#endif /* NOSTREAMIO */
12
13#include "assert.h"
14
15#include "int_cf.h"
16#include "cf_gmp.h"
17#include "gmpext.h"
18
19
20#ifdef HAVE_OMALLOC
21#include <omalloc.h>
22#endif
23
24class InternalInteger : public InternalCF
25{
26private:
27    MP_INT thempi;
28
29    // auxilliary methods
30    inline InternalCF * normalizeMyself ();
31    inline InternalCF * uiNormalizeMyself ();
32
33    static inline InternalCF * normalizeMPI ( MP_INT & );
34    static inline InternalCF * uiNormalizeMPI ( MP_INT & );
35
36    static inline MP_INT & MPI ( const InternalCF * const c );
37#ifdef HAVE_OMALLOC
38  static const omBin InternalInteger_bin;
39#endif
40public:
41#ifdef HAVE_OMALLOC
42  void* operator new(size_t size)
43    {
44      void* addr;
45      omTypeAllocBin(void*, addr, InternalInteger_bin);
46      return addr;
47    }
48  void operator delete(void* addr, size_t size)
49    {
50      omFreeBin(addr, InternalInteger_bin);
51    }
52#endif
53
54    InternalInteger();
55    InternalInteger( const InternalCF& )
56    {
57        ASSERT( 0, "ups there is something wrong in your code" );
58    }
59    InternalInteger( const int i );
60    InternalInteger( const char * str, const int base=10 );
61    InternalInteger( const MP_INT & );
62    ~InternalInteger();
63    InternalCF* deepCopyObject() const;
64    const char * const classname() const { return "InternalInteger"; }
65#ifndef NOSTREAMIO
66    void print( ostream&, char* );
67#endif /* NOSTREAMIO */
68    InternalCF* genZero();
69    InternalCF* genOne();
70
71    bool is_imm() const;
72
73    int levelcoeff() const { return IntegerDomain; }
74    InternalCF* neg();
75
76    int comparesame( InternalCF* );
77
78    InternalCF* addsame( InternalCF* );
79    InternalCF* subsame( InternalCF* );
80    InternalCF* mulsame( InternalCF* );
81    InternalCF* dividesame( InternalCF* );
82    InternalCF* modulosame( InternalCF* );
83    InternalCF* divsame( InternalCF* );
84    InternalCF* modsame( InternalCF* );
85    void divremsame( InternalCF*, InternalCF*&, InternalCF*& );
86    bool divremsamet( InternalCF*, InternalCF*&, InternalCF*& );
87
88    int comparecoeff( InternalCF* );
89
90    InternalCF* addcoeff( InternalCF* );
91    InternalCF* subcoeff( InternalCF*, bool );
92    InternalCF* mulcoeff( InternalCF* );
93    InternalCF* dividecoeff( InternalCF*, bool );
94    InternalCF* modulocoeff( InternalCF*, bool );
95    InternalCF* divcoeff( InternalCF*, bool );
96    InternalCF* modcoeff( InternalCF*, bool );
97    void divremcoeff( InternalCF*, InternalCF*&, InternalCF*&, bool );
98    bool divremcoefft( InternalCF*, InternalCF*&, InternalCF*&, bool );
99
100    InternalCF * bgcdsame ( const InternalCF * const ) const;
101    InternalCF * bgcdcoeff ( const InternalCF * const );
102
103    InternalCF * bextgcdsame ( InternalCF *, CanonicalForm &, CanonicalForm & );
104    InternalCF * bextgcdcoeff ( InternalCF *, CanonicalForm &, CanonicalForm & );
105
106    int intval() const;
107
108    int intmod( int p ) const;
109
110    int sign() const;
111
112    InternalCF* sqrt();
113
114    int ilog2();
115   
116    friend class InternalRational;
117#ifdef SINGULAR
118    friend MP_INT gmp_numerator ( const CanonicalForm & f );
119    friend MP_INT gmp_denominator ( const CanonicalForm & f );
120#endif /* SINGULAR */
121    friend MP_INT getmpi ( InternalCF * value, bool symmetric );
122};
123
124//{{{ inline InternalCF * InternalInteger::normalizeMyself, uiNormalizeMyself ()
125//{{{ docu
126//
127// normalizeMyself(), uiNormalizeMyself() - normalize CO.
128//
129// If CO fits into an immediate integer, delete CO and return the
130// immediate.  Otherwise, return a pointer to CO.
131//
132// `uiNormalizeMyself()' is the same as `normalizeMyself()'
133// except that CO is expected to be non-begative.  In this case,
134// we may use `mpz_get_ui()' to convert the underlying mpi into
135// an immediate which is slightly faster than the signed variant.
136//
137// Note: We do not mind reference counting at this point!  CO is
138// deleted unconditionally!
139//
140//}}}
141inline InternalCF *
142InternalInteger::normalizeMyself ()
143{
144    ASSERT( getRefCount() == 1, "internal error: must not delete CO" );
145
146    if ( mpz_is_imm( &thempi ) ) {
147        InternalCF * result = int2imm( mpz_get_si( &thempi ) );
148        delete this;
149        return result;
150    } else
151        return this;
152}
153
154inline InternalCF *
155InternalInteger::uiNormalizeMyself ()
156{
157    ASSERT( getRefCount() == 1, "internal error: must not delete CO" );
158
159    if ( mpz_is_imm( &thempi ) ) {
160        InternalCF * result = int2imm( mpz_get_ui( &thempi ) );
161        delete this;
162        return result;
163    } else
164        return this;
165}
166//}}}
167
168//{{{ static inline InternalCF * InternalInteger::normalizeMPI, uiNormalizeMPI ( MP_INT & aMpi )
169//{{{ docu
170//
171// normalizeMPI(), uiNormalizeMPI() - normalize a mpi.
172//
173// If `aMpi' fits into an immediate integer, clear `aMpi' and
174// return the immediate.  Otherwise, return a new
175// `InternalInteger' with `aMpi' as underlying mpi.
176//
177// `uiNormalizeMPI()' is the same as `normalizeMPI()' except that
178// `aMpi' is expected to be non-begative.  In this case, we may
179// use `mpz_get_ui()' to convert `aMpi' into an immediate which
180// is slightly faster than the signed variant.
181//
182//}}}
183inline InternalCF *
184InternalInteger::normalizeMPI ( MP_INT & aMpi )
185{
186    if ( mpz_is_imm( &aMpi ) ) {
187        InternalCF * result = int2imm( mpz_get_si( &aMpi ) );
188        mpz_clear( &aMpi );
189        return result;
190    } else
191        return new InternalInteger( aMpi );
192}
193
194inline InternalCF *
195InternalInteger::uiNormalizeMPI ( MP_INT & aMpi )
196{
197    if ( mpz_is_imm( &aMpi ) ) {
198        InternalCF * result = int2imm( mpz_get_ui( &aMpi ) );
199        mpz_clear( &aMpi );
200        return result;
201    } else
202        return new InternalInteger( aMpi );
203}
204//}}}
205
206//{{{ inline MP_INT & InternalInteger::MPI ( const InternalCF * const c )
207//{{{ docu
208//
209// MPI() - return underlying mpi of `c'.
210//
211// `c' is expected to be an `InternalInteger *'.  `c's underlying
212// mpi is returned.
213//
214//}}}
215inline MP_INT &
216InternalInteger::MPI ( const InternalCF * const c )
217{
218    return (((InternalInteger*)c)->thempi);
219}
220//}}}
221
222#endif /* ! INCL_INT_INT_H */
Note: See TracBrowser for help on using the repository browser.