source: git/factory/int_int.h @ 194f5e5

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