source: git/factory/int_int.h @ 59334c

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