Developer documentation

scalar_bigunsigned.hpp
Go to the documentation of this file.
1 #ifndef realroot_BIG_UNSIGNED_H
2 #define realroot_BIG_UNSIGNED_H
3 
4 #include <assert.h>
5 #include <ostream>
7 
8 namespace mmx {
9 
10 template <unsigned N>
12 {
13  typedef unsigned hi;
14  typedef unsigned hdwi_t;
15  static bool s_overflow;
16 
17  hi data[N];
19 
20  bigunsigned( unsigned n )
21  {
22  data[0] = n;
23  for (unsigned i = 1; i < N; data[i] = 0, i ++ ) ;
24  };
25 
26  bigunsigned& operator=( unsigned n )
27  {
28  new (this) bigunsigned(n); return *this;
29  };
30 
32  {
33  new (this) bigunsigned(b); return *this;
34  };
35 
36  inline bool operator<( const bigunsigned<N>& bi )
37  {
38  for ( int i = N-1; i >= 0; i ++ )
39  if ( bi[i] > data[i] ) return true;
40  return false;
41  };
42 
43  inline bool operator>( const bigunsigned<N>& bi )
44  {
45  for ( int i = N-1; i >= 0; i ++ )
46  if ( bi[i] < data[i] ) return true;
47  return false;
48  };
49 
50  inline bool operator==( const bigunsigned<N>& bi )
51  {
52  for (unsigned i = 0; i < N; i ++ )
53  if ( data[i] != bi[i] ) return false;
54  return true;
55  };
56 
57  inline bigunsigned& operator<<=( unsigned n )
58  {
59  if ( n >= numerics::hdwi<hi>::nbit )
60  {
61  do
62  {
63  for ( unsigned i = N-1; i > 0; i -- )
64  data[i] = data[i-1];
65  data[0] = 0;
67  }
68  while ( n >= numerics::hdwi<hi>::nbit );
69  };
70  if ( n )
71  {
72  data[N-1] <<= n;
73  for ( int i = N-1; i > 0; i -- )
74  {
75  data[i] |= (data[i-1]>>(numerics::hdwi<hi>::nbit-n));
76  data[i-1] <<= n;
77  };
78  };
79  return *this;
80  };
81 
82  inline bigunsigned& operator>>=( unsigned n )
83  {
84  if ( n >= numerics::hdwi<hi>::nbit )
85  {
86  do
87  {
88  for ( unsigned i = 0; i < N-1; data[i] = data[i+1], i ++ ) ;
89  data[N-1] = 0;
91  }
92  while ( n >= numerics::hdwi<hi>::nbit );
93  };
94 
95  if ( n )
96  {
97  data[0] >>= n;
98  for ( int i = 0; i < int(N-1); i ++ )
99  {
100  data[i] |= (data[i+1]<<(numerics::hdwi<hi>::nbit-n));
101  data[i+1] >>= n;
102  };
103  };
104  return *this;
105  };
106 
107  inline bigunsigned& operator|=( unsigned n )
108  {
109  data[0] |= n;
110  return *this;
111  };
112 
113  inline bigunsigned& operator|=( const bigunsigned& bu )
114  {
115  for ( unsigned i = 0; i < N; data[i] |= bu[i], i ++ ) ;
116  return *this;
117  };
118 
119  inline bigunsigned<N-1>& next()
120  {
121  return *((bigunsigned<N-1>*)(this->data+1));
122  };
123 
124  inline const bigunsigned<N-1>& next() const
125  {
126  return *((const bigunsigned<N-1>*)(this->data+1));
127  };
128 
129  inline bigunsigned& operator+=( unsigned n )
130  {
131  add(*this,n);
132  return *this;
133  };
134 
135  inline bigunsigned& operator+=( const bigunsigned& a )
136  {
137  add(*this,a);
138  return *this;
139  };
140 
141  unsigned& operator[]( int i )
142  {
143  return data[i];
144  };
145 
146  unsigned operator[]( int i ) const
147  {
148  return data[i];
149  };
150 
151 };
152 
153 template<unsigned N>
154 bigunsigned<N> operator<<( const bigunsigned<N>& b, unsigned n )
155 {
156  bigunsigned<N> tmp = b;
157  tmp <<= n;
158  return tmp;
159 };
160 
161 template<unsigned N>
162 bigunsigned<N> operator|( const bigunsigned<N>& b, unsigned n )
163 {
164  bigunsigned<N> tmp = b;
165  tmp <<= n;
166  return tmp;
167 };
168 
169 template<unsigned N> inline
170 bigunsigned<N> operator+( const bigunsigned<N>& b, unsigned n )
171 {
172  bigunsigned<N> tmp;
173  tmp = b;
174  add(tmp,n);
175  return tmp;
176 };
177 template<unsigned N>
178 unsigned operator&( const bigunsigned<N>& b, unsigned n )
179 {
180  return b[0] & n;
181 };
182 
183 template< unsigned N> bool bigunsigned<N>::s_overflow = false;
184 
185 template<unsigned N> inline
187 {
188  typedef typename bigunsigned<N>::hdwi_t hdwi_t;
189  unsigned i = 0;
190  while ( n )
191  {
192  hdwi_t delta = numerics::hdwi<hdwi_t>::nmax - r[i];
193  if ( delta < n )
194  {
195  r[i] += n;
196  n -= r[i];
197  }
198  else
199  {
200  r[i] += n;
201  n = 0;
202  break;
203  };
204  i = i + 1;
205  if ( i == N ) break;
206  };
207 
208  bigunsigned<N>::s_overflow = (n != 0);
209 
210 };
211 
212 inline void add( bigunsigned<1>& r, const bigunsigned<1>& a ) { add(r,a[0]); };
213 
214 template <unsigned N> inline
215 void add( bigunsigned<N>& r, const bigunsigned<N>& a )
216 {
217  add(r,a[0]);
218  if ( ! bigunsigned<N>::s_overflow )
219  {
220  add(r.next(),a.next());
222  {
223  bigunsigned<N>::s_overflow = true;
224  bigunsigned<N-1>::s_overflow = false;
225  };
226  };
227 };
228 
229 template <unsigned N>
230 void reverse( bigunsigned<N>& r, const bigunsigned<N>& lu )
231 {
232  typedef typename bigunsigned<N>::hdwi_t hdwi_t;
233  for ( unsigned i = 0; i < N/2; i ++ )
234  {
235  r[i] = numerics::hdwi<hdwi_t>::reverse(lu[N-1-i]);
236  r[N-1-i] = numerics::hdwi<hdwi_t>::reverse(lu[i]);
237  };
238  if ( N & 1 ) r[N/2] = numerics::hdwi<hdwi_t>::reverse(lu[N/2]);
239 };
240 
241 inline void rprint( std::ostream& o, unsigned n )
242 {
243  for ( unsigned b = 0; b < numerics::hdwi<unsigned>::nbit; b ++ )
244  {
245  o << ( n& 1);
246  n >>= 1;
247  };
248 };
249 
250 template< unsigned N >
251 void rprint( std::ostream& o, bigunsigned<N>& bu )
252 {
253  for ( unsigned i = 0; i < N; i ++ )
254  rprint(o,bu[i]);
255 };
256 
257 template< unsigned N >
258 void print ( std::ostream& o, const bigunsigned<N>& bi )
259 {
260  bigunsigned<N> tmp;
261  reverse(tmp,bi);
262  rprint(o,tmp);
263 
264 };
265 
266 template<unsigned N>
267 std::ostream& operator<<( std::ostream& o, const bigunsigned<N>& bi )
268 {
269  print(o,bi);
270  return o;
271 };
272 
273 } //namespace mmx
274 
275 #endif
bigunsigned & operator=(const bigunsigned &b)
Definition: scalar_bigunsigned.hpp:31
const C & b
Definition: Interval_glue.hpp:25
unsigned operator[](int i) const
Definition: scalar_bigunsigned.hpp:146
bool operator==(const bigunsigned< N > &bi)
Definition: scalar_bigunsigned.hpp:50
static hdw_int reverse(hdw_int a)
Definition: numerics_hdwi.hpp:54
bigunsigned()
Definition: scalar_bigunsigned.hpp:18
bigunsigned & operator<<=(unsigned n)
Definition: scalar_bigunsigned.hpp:57
unsigned hi
Definition: scalar_bigunsigned.hpp:13
bigunsigned & operator|=(const bigunsigned &bu)
Definition: scalar_bigunsigned.hpp:113
unsigned hdwi_t
Definition: scalar_bigunsigned.hpp:14
unsigned & operator[](int i)
Definition: scalar_bigunsigned.hpp:141
void reverse(Poly &R, const Poly &P)
Definition: contfrac_intervaldata.hpp:139
void rprint(std::ostream &o, unsigned n)
Definition: scalar_bigunsigned.hpp:241
TMPL int N(const MONOMIAL &v)
Definition: monomial_glue.hpp:60
bool operator>(const bigunsigned< N > &bi)
Definition: scalar_bigunsigned.hpp:43
bigunsigned & operator>>=(unsigned n)
Definition: scalar_bigunsigned.hpp:82
extended< NT > operator+(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:122
const bigunsigned< N-1 > & next() const
Definition: scalar_bigunsigned.hpp:124
bigunsigned & operator+=(const bigunsigned &a)
Definition: scalar_bigunsigned.hpp:135
bigunsigned & operator+=(unsigned n)
Definition: scalar_bigunsigned.hpp:129
static bool s_overflow
Definition: scalar_bigunsigned.hpp:15
hi data[N]
Definition: scalar_bigunsigned.hpp:17
bigunsigned(unsigned n)
Definition: scalar_bigunsigned.hpp:20
bigunsigned & operator=(unsigned n)
Definition: scalar_bigunsigned.hpp:26
void print(OSTREAM &os, const Interval< T, r > &a)
Definition: Interval.hpp:135
Definition: numerics_hdwi.hpp:47
bigunsigned< N > operator|(const bigunsigned< N > &b, unsigned n)
Definition: scalar_bigunsigned.hpp:162
unsigned operator&(const bigunsigned< N > &b, unsigned n)
Definition: scalar_bigunsigned.hpp:178
bigunsigned< N-1 > & next()
Definition: scalar_bigunsigned.hpp:119
Definition: scalar_bigunsigned.hpp:11
bigunsigned & operator|=(unsigned n)
Definition: scalar_bigunsigned.hpp:107
Definition: array.hpp:12
void add(dynamic_exp< E > &r, const dynamic_exp< E > &A, const dynamic_exp< E > &B)
Definition: dynamicexp.hpp:295
Home