9#ifndef __HRCORE_VALUE_HPP__
10#define __HRCORE_VALUE_HPP__
38 auto x = this->q_data;
39 this->q_data =
nullptr;
40 return Integer(x, this->positive,
true,
true);
46 auto x = this->r_data;
47 this->r_data =
nullptr;
48 return Integer(x, this->positive,
true,
true);
64 bool positive =
false;
90 this->_idata_ar = autoRelease;
94 HRCORE_DBG(std::cout <<
"Interface* constructor! (_idata: " << this->_idata <<
")" << std::endl;)
108 template <
typename T>
111 return Integer(
new T,
true,
true,
false);
127 HRCORE_DBG(std::cout <<
"Move constructor! (_idata: " << c._idata <<
" -> " << this->_idata <<
")" << std::endl;)
129 this->_idata = c._idata;
130 this->_isPostive = c._isPostive;
131 this->_idata_ar = c._idata_ar;
144 HRCORE_DBG(std::cout <<
"Move operator! (_idata: " << c._idata <<
" -> " << this->_idata <<
")" << std::endl;)
146 this->_idata = c._idata;
147 this->_idata_ar = c._idata_ar;
148 this->_isPostive = c._isPostive;
164 for (
auto i = c._idata->
begin(), j = this->_idata->begin(); i && j; i = c._idata->
next(i), j = this->_idata->next(j))
166 this->_idata_ar =
true;
167 this->_isPostive = c._isPostive;
168 HRCORE_DBG(std::cout <<
"Copy constructor! (_idata: " << c._idata <<
" -> " << this->_idata <<
")" << std::endl;)
173 if (this->_idata_ar && this->_idata !=
nullptr) {
174 HRCORE_DBG(std::cout <<
"~Integer! (_idata: " << this->_idata <<
")" << std::endl;)
189 *tmp->at(0).data = num;
192 this->_idata_ar =
true;
193 this->_isPostive =
true;
212 bool neg = (this->_isPostive ^ r._isPostive);
215 size_t m = std::max(this->_idata->
length(), r._idata->
length());
217 const Integer& op1 = (bigger ? *this : r);
218 const Integer& op2 = (bigger ? r : *
this);
221 _absSub(op1, op2, tmp);
222 flag = op1._isPostive;
224 _absPlus(op1, op2, tmp);
225 flag = this->_isPostive;
227 return Integer(tmp, flag,
true,
true);
248 bool flag = !(this->_isPostive ^ r._isPostive);
253 const Integer& num1 = (bigger ? *this : r);
254 const Integer& num2 = (bigger ? r : *
this);
258#error HRCORE_HIGHPERF is enabled but currently not support (FFT) !
260 auto sop = tmp->begin();
261 for (
auto i = num1._idata->
begin(); i; i = num1._idata->
next(i), sop = tmp->next(sop)) {
263 for (
auto j = num2._idata->
begin(); j; j = num2._idata->
next(j), op = tmp->next(op)) {
264 idval_t t = (idval_t)*i.data * (idval_t)*j.data;
265 _doInc(tmp, op, t & (ival_t)0xFFFFFFFFFFFFFFFF);
268 _doInc(tmp, tmp->next(op), inc);
272 return Integer(tmp, flag,
true,
true);
283 bool flag = !(this->_isPostive ^ r._isPostive);
287#error HRCORE_HIGHPERF is enabled but currently not support (NTT) !
295 HRCORE_DBGI(std::cout << ls + 1 << std::endl;)
303 size_t roffset = (ls) >> (HRCORE_UNIT_DIV);
304 int rem = ls - (roffset << (HRCORE_UNIT_DIV));
305 tmp->extend(roffset + 1);
306 auto op = tmp->at(roffset);
312 while (roffset != 0 || rem != -1) {
314 HRCORE_DBGI(std::cout <<
"[Integer::/] sum = " << sum <<
" div = " << div <<
" dtmp = " << dtmp << std::endl;)
321 *op.data |= 1 << rem;
327 sum._isPostive = this->_isPostive;
329 rt._idata_ar =
false;
332 ret.r_data = rt._idata;
347 x.r_data->delObject(x.r_data);
348 return Integer(x.q_data, x.positive,
true,
true);
360 x.q_data->delObject(x.q_data);
361 return Integer(x.r_data, x.positive,
true,
true);
370 ret._isPostive = !ret._isPostive;
396 if (this->_isPostive ^ r._isPostive)
397 return !this->_isPostive;
409 if (this->_isPostive ^ r._isPostive)
410 return this->_isPostive;
417 if (this->_isPostive ^ r._isPostive)
440 size_t offset = n >> HRCORE_UNIT_DIV;
441 size_t rem = n - (offset << HRCORE_UNIT_DIV);
444 ret->extend(this->_idata->
length() + offset + 1);
445 for (
auto s = this->_idata->
begin(), d = ret->at(offset); s && d; s = this->_idata->
next(s)) {
446 *d.data |= *s.data << rem;
451 return Integer(ret, this->_isPostive,
true,
true);
465 size_t offset = n >> (HRCORE_UNIT_DIV);
466 size_t rem = n - (offset << (HRCORE_UNIT_DIV));
469 if (offset > this->_idata->
length() + 1) {
470 return this->
make() = 0;
472 ret->extend(this->_idata->
length() - offset + 1);
473 for (
auto s = this->_idata->
end(), d = ret->at(this->_idata->length() - offset - 1); d && s; s = this->_idata->prev(s)) {
474 *d.data |= *s.data >> rem;
479 return Integer(ret, this->_isPostive,
true,
true);
492 size_t m = l._idata->
length();
493 size_t n = r._idata->
length();
495 return (m > n ? 1 : -1);
496 auto op1 = l._idata->
end();
497 auto op2 = r._idata->
end();
498 for (
size_t i = 0; i < m; ++i) {
499 ival_t c1 = (op1 ? *op1.data : 0);
500 ival_t c2 = (op2 ? *op2.data : 0);
502 return (c1 > c2 ? 1 : -1);
503 op1 = l._idata->
prev(op1);
504 op2 = r._idata->
prev(op2);
515 static bool _isZero(
const Integer& l)
517 for (
auto x = l._idata->
begin(); x; x = l._idata->
next(x)) {
528 size_t t = this->_idata->
length();
529 for (
auto i = this->_idata->
end(); i && *i.data == 0; i = this->_idata->prev(i))
531 this->_idata->
shrink((t == 0 ? 1 : t));
533 this->_isPostive =
true;
538 static void _doInc(Storage::Interface* i, Storage::Interface::item_t pos, ival_t inc)
541 idval_t ovf = (idval_t)inc + (idval_t)*pos.data;
542 *pos.data = ovf & (ival_t)0xFFFFFFFFFFFFFFFF;
549 static void _absPlus(
const Integer& left,
const Integer& right, Storage::Interface* ret)
551 size_t m = std::max(left._idata->length(), right._idata->length());
552 auto op1 = left._idata->begin();
553 auto op2 = right._idata->begin();
554 auto r = ret->begin();
555 for (
size_t i = 0; i < m; ++i) {
556 ival_t c1 = (op1 ? *op1.data : 0);
557 ival_t c2 = (op2 ? *op2.data : 0);
558 idval_t tmp = (idval_t)c1 + (idval_t)c2;
559 _doInc(ret, r, tmp & (ival_t)0xFFFFFFFFFFFFFFFF);
563 _doInc(ret, ret->next(r), ovf);
564 op1 = left._idata->next(op1);
565 op2 = right._idata->next(op2);
572 static bool _doBorrow(Storage::Interface* i, Storage::Interface::item_t pos)
575 while (pos && *pos.data < 1) {
581 for (
size_t j = 0; j < ls; ++j) {
583 *pos.data = (ival_t)0xffffffffffffffff;
592 static void _absSub(
const Integer& left,
const Integer& right, Storage::Interface* ret)
594 HRCORE_DBG(Integer ll(left);
Integer rr(right);)
595 HRCORE_DBG(std::cout <<
"[Integer::_absSub] Perform " << ll <<
" - " << rr << std::endl;)
596 size_t m = std::max(left._idata->length(), right._idata->length());
597 auto op1 = left._idata->begin();
598 auto op2 = right._idata->begin();
599 auto r = ret->begin();
600 for (
size_t i = 0; i < m; ++i) {
602 throw(Exception::LLR());
603 ival_t c1 = *op1.data;
604 ival_t c2 = (op2 ? *op2.data : 0);
608 if (_doBorrow(left._idata, left._idata->next(op1))) {
612 throw(Exception::LLR());
615 op1 = left._idata->next(op1);
616 op2 = right._idata->next(op2);
622 Storage::Interface* _idata =
nullptr;
623 bool _idata_ar =
false;
624 bool _isPostive =
true;
629#ifndef __HRCORE_FP_OPERATOR_HELPER__
633#define __HRCORE_FP_OPERATOR_HELPER__(x) \
634 bool x(const Float& rhs) \
636 Float l(*this), r(rhs); \
638 return l.Integer::x(r); \
659 this->_fpos = c._fpos;
660 this->_tpos = c._tpos;
666 HRCORE_DBGI(std::cout <<
"[Float::MoveConstructor] " << fpos << std::endl;)
675 this->_fpos = c._fpos;
676 this->_tpos = c._tpos;
690 HRCORE_DBGI(std::cout <<
"[Float::MoveOperator] " << c._fpos << std::endl;)
692 this->_fpos = c._fpos;
693 this->_tpos = c._tpos;
701 this->_fpos = c._fpos;
702 this->_tpos = c._tpos;
709 bool flag = (c >= 0);
710 ival_t m = std::abs(c);
717 for (
double x = (std::abs(c) - m); std::abs(x) > 1e-9; x -= n) {
721 ret = ret * tim + tmp;
725 *
this =
Float((ret << -dtmp) / div, dtmp, 12);
726 this->_isPostive = flag;
748 Float l(*
this), r(s);
750 return Float(l.Integer::operator+(r), l._fpos, l._tpos);
756 Float l(*
this), r(s);
758 return Float(l.Integer::operator-(r), l._fpos, l._tpos);
767 if (this->_tpos >= this->_precision) {
768 return Float(this->Integer::operator/(s), this->_fpos, this->_tpos);
772 size_t n = this->_precision - this->_tpos;
774 HRCORE_DBG(std::cout <<
"[Test] " << div << std::endl;)
781 if (this->_tpos < s._tpos + this->_precision) {
782 size_t n = s._tpos + this->_precision - this->_tpos;
786 return Float(this->Integer::operator/(s), this->_fpos - s._fpos, this->_precision);
793 HRCORE_DBG(std::cout << this->_fpos << std::endl;)
794 auto x = (this->_fpos >= 0 ? *
this << this->_fpos : *
this >> -(this->_fpos));
795 x.positive(this->_isPostive);
799 __HRCORE_FP_OPERATOR_HELPER__(
operator<)
800 __HRCORE_FP_OPERATOR_HELPER__(operator>)
801 __HRCORE_FP_OPERATOR_HELPER__(operator==)
802 __HRCORE_FP_OPERATOR_HELPER__(operator>=)
803 __HRCORE_FP_OPERATOR_HELPER__(operator<=)
805 friend std::istream& operator>>(std::istream& is,
Float& rhs);
806 friend std::ostream& operator<<(std::ostream& os,
Float& rhs);
812 int m = std::max(fp1._fpos, fp2._fpos);
813 int n = std::min(fp1._fpos, fp2._fpos);
817 Float& adj = (m == fp1._fpos ? fp1 : fp2);
818 adj =
Float(adj << k, n, std::max(fp1._tpos, fp2._tpos));
Exception of divided by 0.
Exception of internal error. Please dig into it and solve.
Exception of Invalid Argument.
A class as a storage interface, pure virtual.
virtual Interface * newObject()=0
Get a new object of same type.
virtual size_t length()=0
Get the length of storage.
virtual void delObject(const Interface *ptr)=0
Delete a object from newObject()
virtual bool shrink(size_t len)=0
Remove data from the end to shrink the size to a given length.
virtual bool extend(size_t len)=0
Extend the storage to a given length.
virtual item_t next(item_t cur)=0
Get next item.
virtual item_t end()=0
Returns the last item of storage.
virtual item_t begin()=0
Return the first item of storage.
virtual item_t prev(item_t cur)=0
Float & operator=(Float &&c)
Move operator.
Float operator*(const Float &s)
Reload of operator*.
Float operator/(const Integer &s)
Reload of operator/ (for Integer)
static Integer make()
Construct a new Integer object using given type of Storage::Interface implement.
Float & operator=(const double c)
transform IEEE-754 Floating point value into Float object
Float & operator=(const Float &c)
Copy operator.
Float(Float &&c)
Reload of move constructor.
Float operator+(const Float &s)
Reload of operator+.
Float operator-(const Float &s)
Reload of operator-.
Float(const Float &c)
Reload of copy constructor.
size_t precision()
Get current precision of division and input.
Float operator/(const Float &s)
Reload of operator/ (for Float)
Integer getInteger()
Get Interger section.
Float(const Integer &c, int fpos=0, size_t tpos=0)
Copy value of Integer object and convert to a Float object.
void precision(size_t n)
Set precision of division and input.
Type defination of division result (For Integer only).
Integer getRemainder()
Get remainder of division result.
Integer getQuotient()
Get quotient of division result.
void clear()
Clear the object and release memory allocated.
bool operator==(const Integer &r)
Test if (r == this)
Integer & operator=(const Integer &c)
Copy operator.
Integer operator+(const Integer &r)
Reload of operator+.
friend std::istream & operator>>(std::istream &is, Integer &rhs)
Reload of operator>> to support std::cin and other std::istream with Integer.
Integer(Integer &&c)
Move a Integer object.
Integer operator/(const Integer &r)
Reload of operator/.
Integer operator+() const
Reload of operator+() const.
static bool _absEqual(const Integer &l, const Integer &r)
Examine if l == r.
static int8_t _absRelation(const Integer &l, const Integer &r)
Get relation of l and r.
Integer operator<<(size_t n)
Reload of << (Left shift)
static Integer make()
Construct a new Integer object using given type of Storage::Interface implement.
void positive(bool p)
Set the sign of object.
bool operator>=(const Integer &r)
Test if (this >= r)
Integer operator-(const Integer &r)
Reload of operator-(r)
bool operator<(const Integer &r)
Compare this and r with operator <.
friend std::ostream & operator<<(std::ostream &os, Integer &rhs)
Reload of operator<< to support std::cout and other std::ostream with Integer.
Integer operator>>(size_t n)
Reload of >> (Logical right shift)
Integer & operator=(Integer &&c)
Move operator.
Integer operator*(const Integer &r)
Reload of operator*(r)
Integer(const Integer &c)
Copy and construct a new Integer object.
Integer make()
Make a new object with same Interface type.
static bool _absGreater(const Integer &l, const Integer &r)
Examine if l > r.
bool operator>(const Integer &r)
Compare this and r with operator >
Integer(Storage::Interface *ptr, bool positive=true, bool autoRelease=false, bool sim=true)
Construct a new Integer object.
DivisionResult_t divideBy(const Integer &r)
A function to calculate (*this / r)
bool operator<=(const Integer &r)
Test if (this <= r)
Integer operator%(const Integer &r)
Reload of operator%.
Integer operator-() const
Reload of operator-() const.
bool positive() const
Return the sign of object.
Integer & operator=(ival_t num)
Reload of operator= to support a ival_t be converted to Integer.
#define HRCORE_UNIT_SIZE
Decide how many bits should be used by single storage unit. Default: 32.
#define HRCORE_FP_DEFAULT_PRECISION
Set default precision of Value::Float. Default: 10.
#define HRCORE_FP_BITS_BASE
Decide how many bits as a base of Float. Default: 24.
#define HRCORE_FP_BITS_PER_DIGIT
Decide how many bits for a digit in Float. Default: 8.
HRCore main namespace, contains all classes.