source: git/factory/int_int.h @ 6c44098

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