32 #ifndef REAL_INFININT_HPP
33 #define REAL_INFININT_HPP
35 #include "../my_config.h"
40 #include <sys/types.h>
49 #define ZEROED_SIZE 50
54 class user_interaction;
64 #if SIZEOF_OFF_T > SIZEOF_TIME_T
65 #if SIZEOF_OFF_T > SIZEOF_SIZE_T
67 { infinint_from(a); };
70 { infinint_from(a); };
73 #if SIZEOF_TIME_T > SIZEOF_SIZE_T
75 { infinint_from(a); };
78 { infinint_from(a); };
93 {
detruit(); copy_from(ref);
return *
this; };
101 infinint & operator *= (
unsigned char arg);
103 template <
class T>
infinint power(
const T & exponent)
const;
114 {
infinint ret = *
this; ++(*this);
return ret; };
116 {
infinint ret = *
this; --(*this);
return ret; };
118 {
return *
this += 1; };
120 {
return *
this -= 1; };
122 U_32 operator % (U_32 arg)
const
123 {
return modulo(arg); };
128 template <
class T>
void unstack(T &v)
129 { infinint_unstack_to(v); }
131 infinint get_storage_size()
const {
return field->size(); };
134 unsigned char operator [] (
const infinint & position)
const;
145 static bool is_system_big_endian();
147 #ifdef LIBDAR_SPECIAL_ALLOC
151 static const int TG = 4;
153 enum endian { big_endian, little_endian, not_initialized };
154 typedef unsigned char group[TG];
158 bool is_valid()
const;
161 void copy_from(
const infinint & ref);
163 void make_at_least_as_wider_as(
const infinint & ref);
164 template <
class T>
void infinint_from(T a);
165 template <
class T> T max_val_of(T x);
166 template <
class T>
void infinint_unstack_to(T &a);
167 template <
class T> T modulo(T arg)
const;
168 signed int difference(
const infinint & b)
const;
173 static endian used_endian;
174 static U_8 zeroed_field[ZEROED_SIZE];
175 static void setup_endian();
179 #define OPERATOR(OP) inline bool operator OP (const infinint &a, const infinint &b) \
181 return a.difference(b) OP 0; \
192 infinint operator - (const infinint &, const infinint &);
193 infinint operator * (const infinint &, const infinint &);
194 infinint operator * (const infinint &, const
unsigned char);
195 infinint operator * (const
unsigned char, const infinint &);
196 infinint operator / (const infinint &, const infinint &);
197 infinint operator % (const infinint &, const infinint &);
198 infinint operator & (const infinint & a, const infinint & bit);
199 infinint operator | (const infinint & a, const infinint & bit);
200 infinint operator ^ (const infinint & a, const infinint & bit);
201 infinint operator >> (const infinint & a, U_32 bit);
202 infinint operator >> (const infinint & a, const infinint & bit);
203 infinint operator << (const infinint & a, U_32 bit);
204 infinint operator << (const infinint & a, const infinint & bit);
205 void euclide(infinint a, const infinint &b, infinint &q, infinint &r);
206 template <class T> inline
void euclide(T a, T b, T & q, T &r)
211 inline infinint & infinint::operator /= (
const infinint & ref)
217 inline infinint & infinint::operator %= (
const infinint & ref)
228 template <
class T> infinint infinint::power(
const T & exponent)
const
231 for(T count = 0; count < exponent; ++count)
237 template <
class T> T infinint::modulo(T arg)
const
239 infinint tmp = *
this % infinint(arg);
241 unsigned char *debut = (
unsigned char *)(&ret);
242 unsigned char *ptr = debut +
sizeof(T) - 1;
243 storage::iterator it = tmp.field->rbegin();
245 while(it != tmp.field->rend() && ptr >= debut)
254 while(it != tmp.field->rend())
261 if(used_endian == little_endian)
262 int_tools_swap_bytes(debut,
sizeof(T));
268 template <
class T>
void infinint::infinint_from(T a)
270 U_I size =
sizeof(a);
272 unsigned char *ptr, *fin;
274 if(used_endian == not_initialized)
277 if(used_endian == little_endian)
280 ptr = (
unsigned char *)(&a) + (size - 1);
281 fin = (
unsigned char *)(&a) - 1;
286 ptr = (
unsigned char *)(&a);
287 fin = (
unsigned char *)(&a) + size;
290 while(ptr != fin && *ptr == 0)
302 field =
new storage(size);
305 storage::iterator it = field->begin();
313 if(it != field->end())
317 throw Ememory(
"template infinint::infinint_from");
320 template <
class T> T infinint::max_val_of(T x)
328 x = int_tools_rotate_right_one_bit(x);
335 template <
class T>
void infinint::infinint_unstack_to(T &a)
340 static const T max_T = max_val_of(a);
341 infinint step = max_T - a;
346 unsigned char *debut = (
unsigned char *)&transfert;
347 unsigned char *ptr = debut +
sizeof(transfert) - 1;
348 storage::iterator it = field->rbegin();
350 while(ptr >= debut && it != field->rend())
357 if(used_endian == little_endian)
358 int_tools_swap_bytes(debut,
sizeof(transfert));