|
|
|
|
|
#ifndef DLIB_BIGINT_KERNEl_2_ |
|
#define DLIB_BIGINT_KERNEl_2_ |
|
|
|
#include "bigint_kernel_abstract.h" |
|
#include "../algs.h" |
|
#include "../serialize.h" |
|
#include "../uintn.h" |
|
#include <iosfwd> |
|
#include <cmath> |
|
#include <complex> |
|
#include <vector> |
|
|
|
namespace dlib |
|
{ |
|
|
|
class bigint_kernel_2 |
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct data_record |
|
{ |
|
|
|
|
|
explicit data_record( |
|
uint32 size_ |
|
) : |
|
size(size_), |
|
number(new uint16[size_]), |
|
references(1), |
|
digits_used(1) |
|
{*number = 0;} |
|
|
|
|
|
|
|
|
|
|
|
data_record( |
|
const data_record& item, |
|
uint32 additional_size |
|
) : |
|
size(item.digits_used + additional_size), |
|
number(new uint16[size]), |
|
references(1), |
|
digits_used(item.digits_used) |
|
{ |
|
uint16* source = item.number; |
|
uint16* dest = number; |
|
uint16* end = source + digits_used; |
|
while (source != end) |
|
{ |
|
*dest = *source; |
|
++dest; |
|
++source; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
~data_record( |
|
) |
|
{ |
|
delete [] number; |
|
} |
|
|
|
|
|
const uint32 size; |
|
uint16* number; |
|
uint32 references; |
|
uint32 digits_used; |
|
|
|
private: |
|
|
|
data_record ( data_record&); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
explicit bigint_kernel_2 ( |
|
data_record* data_, int |
|
): slack(25),data(data_) {} |
|
|
|
|
|
|
|
|
|
|
|
public: |
|
|
|
bigint_kernel_2 ( |
|
); |
|
|
|
bigint_kernel_2 ( |
|
uint32 value |
|
); |
|
|
|
bigint_kernel_2 ( |
|
const bigint_kernel_2& item |
|
); |
|
|
|
virtual ~bigint_kernel_2 ( |
|
); |
|
|
|
const bigint_kernel_2 operator+ ( |
|
const bigint_kernel_2& rhs |
|
) const; |
|
|
|
bigint_kernel_2& operator+= ( |
|
const bigint_kernel_2& rhs |
|
); |
|
|
|
const bigint_kernel_2 operator- ( |
|
const bigint_kernel_2& rhs |
|
) const; |
|
|
|
bigint_kernel_2& operator-= ( |
|
const bigint_kernel_2& rhs |
|
); |
|
|
|
const bigint_kernel_2 operator* ( |
|
const bigint_kernel_2& rhs |
|
) const; |
|
|
|
bigint_kernel_2& operator*= ( |
|
const bigint_kernel_2& rhs |
|
); |
|
|
|
const bigint_kernel_2 operator/ ( |
|
const bigint_kernel_2& rhs |
|
) const; |
|
|
|
bigint_kernel_2& operator/= ( |
|
const bigint_kernel_2& rhs |
|
); |
|
|
|
const bigint_kernel_2 operator% ( |
|
const bigint_kernel_2& rhs |
|
) const; |
|
|
|
bigint_kernel_2& operator%= ( |
|
const bigint_kernel_2& rhs |
|
); |
|
|
|
bool operator < ( |
|
const bigint_kernel_2& rhs |
|
) const; |
|
|
|
bool operator == ( |
|
const bigint_kernel_2& rhs |
|
) const; |
|
|
|
bigint_kernel_2& operator= ( |
|
const bigint_kernel_2& rhs |
|
); |
|
|
|
friend std::ostream& operator<< ( |
|
std::ostream& out, |
|
const bigint_kernel_2& rhs |
|
); |
|
|
|
friend std::istream& operator>> ( |
|
std::istream& in, |
|
bigint_kernel_2& rhs |
|
); |
|
|
|
bigint_kernel_2& operator++ ( |
|
); |
|
|
|
const bigint_kernel_2 operator++ ( |
|
int |
|
); |
|
|
|
bigint_kernel_2& operator-- ( |
|
); |
|
|
|
const bigint_kernel_2 operator-- ( |
|
int |
|
); |
|
|
|
friend const bigint_kernel_2 operator+ ( |
|
uint16 lhs, |
|
const bigint_kernel_2& rhs |
|
); |
|
|
|
friend const bigint_kernel_2 operator+ ( |
|
const bigint_kernel_2& lhs, |
|
uint16 rhs |
|
); |
|
|
|
bigint_kernel_2& operator+= ( |
|
uint16 rhs |
|
); |
|
|
|
friend const bigint_kernel_2 operator- ( |
|
uint16 lhs, |
|
const bigint_kernel_2& rhs |
|
); |
|
|
|
friend const bigint_kernel_2 operator- ( |
|
const bigint_kernel_2& lhs, |
|
uint16 rhs |
|
); |
|
|
|
bigint_kernel_2& operator-= ( |
|
uint16 rhs |
|
); |
|
|
|
friend const bigint_kernel_2 operator* ( |
|
uint16 lhs, |
|
const bigint_kernel_2& rhs |
|
); |
|
|
|
friend const bigint_kernel_2 operator* ( |
|
const bigint_kernel_2& lhs, |
|
uint16 rhs |
|
); |
|
|
|
bigint_kernel_2& operator*= ( |
|
uint16 rhs |
|
); |
|
|
|
friend const bigint_kernel_2 operator/ ( |
|
uint16 lhs, |
|
const bigint_kernel_2& rhs |
|
); |
|
|
|
friend const bigint_kernel_2 operator/ ( |
|
const bigint_kernel_2& lhs, |
|
uint16 rhs |
|
); |
|
|
|
bigint_kernel_2& operator/= ( |
|
uint16 rhs |
|
); |
|
|
|
friend const bigint_kernel_2 operator% ( |
|
uint16 lhs, |
|
const bigint_kernel_2& rhs |
|
); |
|
|
|
friend const bigint_kernel_2 operator% ( |
|
const bigint_kernel_2& lhs, |
|
uint16 rhs |
|
); |
|
|
|
bigint_kernel_2& operator%= ( |
|
uint16 rhs |
|
); |
|
|
|
friend bool operator < ( |
|
uint16 lhs, |
|
const bigint_kernel_2& rhs |
|
); |
|
|
|
friend bool operator < ( |
|
const bigint_kernel_2& lhs, |
|
uint16 rhs |
|
); |
|
|
|
friend bool operator == ( |
|
const bigint_kernel_2& lhs, |
|
uint16 rhs |
|
); |
|
|
|
friend bool operator == ( |
|
uint16 lhs, |
|
const bigint_kernel_2& rhs |
|
); |
|
|
|
bigint_kernel_2& operator= ( |
|
uint16 rhs |
|
); |
|
|
|
|
|
void swap ( |
|
bigint_kernel_2& item |
|
) { data_record* temp = data; data = item.data; item.data = temp; } |
|
|
|
|
|
private: |
|
|
|
typedef double t; |
|
typedef std::complex<t> ct; |
|
|
|
void fft( |
|
ct* data, |
|
unsigned long len |
|
) const; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ifft( |
|
ct* data, |
|
unsigned long len |
|
) const; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void long_add ( |
|
const data_record* lhs, |
|
const data_record* rhs, |
|
data_record* result |
|
) const; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void long_sub ( |
|
const data_record* lhs, |
|
const data_record* rhs, |
|
data_record* result |
|
) const; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void long_div ( |
|
const data_record* lhs, |
|
const data_record* rhs, |
|
data_record* result, |
|
data_record* remainder |
|
) const; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void long_mul ( |
|
const data_record* lhs, |
|
const data_record* rhs, |
|
data_record* result |
|
) const; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void short_add ( |
|
const data_record* data, |
|
uint16 value, |
|
data_record* result |
|
) const; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void short_sub ( |
|
const data_record* data, |
|
uint16 value, |
|
data_record* result |
|
) const; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void short_mul ( |
|
const data_record* data, |
|
uint16 value, |
|
data_record* result |
|
) const; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void short_div ( |
|
const data_record* data, |
|
uint16 value, |
|
data_record* result, |
|
uint16& remainder |
|
) const; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void shift_left ( |
|
const data_record* data, |
|
data_record* result, |
|
uint32 shift_amount |
|
) const; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void shift_right ( |
|
const data_record* data, |
|
data_record* result |
|
) const; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool is_less_than ( |
|
const data_record* lhs, |
|
const data_record* rhs |
|
) const; |
|
|
|
|
|
|
|
|
|
|
|
|
|
bool is_equal_to ( |
|
const data_record* lhs, |
|
const data_record* rhs |
|
) const; |
|
|
|
|
|
|
|
|
|
|
|
|
|
void increment ( |
|
const data_record* source, |
|
data_record* dest |
|
) const; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void decrement ( |
|
const data_record* source, |
|
data_record* dest |
|
) const; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const uint32 slack; |
|
data_record* data; |
|
|
|
|
|
|
|
}; |
|
|
|
inline void swap ( |
|
bigint_kernel_2& a, |
|
bigint_kernel_2& b |
|
) { a.swap(b); } |
|
|
|
inline void serialize ( |
|
const bigint_kernel_2& item, |
|
std::ostream& out |
|
) |
|
{ |
|
std::ios::fmtflags oldflags = out.flags(); |
|
out << item << ' '; |
|
out.flags(oldflags); |
|
if (!out) throw serialization_error("Error serializing object of type bigint_kernel_c"); |
|
} |
|
|
|
inline void deserialize ( |
|
bigint_kernel_2& item, |
|
std::istream& in |
|
) |
|
{ |
|
std::ios::fmtflags oldflags = in.flags(); |
|
in >> item; |
|
in.flags(oldflags); |
|
if (in.get() != ' ') |
|
{ |
|
item = 0; |
|
throw serialization_error("Error deserializing object of type bigint_kernel_c"); |
|
} |
|
} |
|
|
|
inline bool operator> (const bigint_kernel_2& a, const bigint_kernel_2& b) { return b < a; } |
|
inline bool operator!= (const bigint_kernel_2& a, const bigint_kernel_2& b) { return !(a == b); } |
|
inline bool operator<= (const bigint_kernel_2& a, const bigint_kernel_2& b) { return !(b < a); } |
|
inline bool operator>= (const bigint_kernel_2& a, const bigint_kernel_2& b) { return !(a < b); } |
|
|
|
} |
|
|
|
#ifdef NO_MAKEFILE |
|
#include "bigint_kernel_2.cpp" |
|
#endif |
|
|
|
#endif |
|
|
|
|