Compare commits

...

2 Commits

Author SHA1 Message Date
Kittycannon 588eb8092b Merge branch 'main' of git.sintekanalytics.com:Kittycannon/libbigmath 2026-08-17 21:15:59 -06:00
Kittycannon 0cb555254c more changes 2026-08-17 21:15:56 -06:00
5 changed files with 78 additions and 34 deletions
+43 -16
View File
@@ -54,9 +54,7 @@ namespace bigmath {
chain::chain(chain&& c) noexcept
: start(c.start), middle(c.middle), end(c.end) {
// Reset source object
c.start = nullptr;
c.middle = nullptr;
c.end = nullptr;
c.start = c.middle = c.end = nullptr;
}
chain& chain::operator=(const chain& c) noexcept {
@@ -71,18 +69,9 @@ namespace bigmath {
chain& chain::operator=(chain&& c) noexcept {
if (this != &c) {
// Free existing nodes first
this->~chain();
// Transfer ownership
start = c.start;
middle = c.middle;
end = c.end;
// Reset source object
c.start = nullptr;
c.middle = nullptr;
c.end = nullptr;
chain temp(std::move(*this));
start = c.start; middle = c.middle; end = c.end;
c.start = c.middle = c.end = nullptr;
}
return *this;
}
@@ -100,6 +89,40 @@ namespace bigmath {
start = middle = end = nullptr;
}
chain::operator bool() const noexcept {
return start && middle && end;
}
std::array<chain::link*, 2> chain::split() const noexcept {
// find link* pointers by walking to the middle
if(!start) return { nullptr, nullptr };
if(start == middle) return { start, end };
// <- L [* * *] [{*} * * *] R <- //
chain::link* a_left = end;
chain::link* a_mid = end;
chain::link* a_right = middle->next;
// this must be odd
while(a_left != a_right) {
a_mid = a_left->prev;
a_left = a_left->prev;
a_right = a_right->next;
}
chain::link* b_left = middle;
chain::link* b_right = start;
// this must be even
while(b_left->next != b_right) {
b_left = b_left->prev;
b_right = b_right->next;
}
return { b_left, a_mid };
}
void chain::grow() noexcept {
// (*) -> (*) [{*} *]
// * (*) * -> * (*) {*} [* *]
@@ -227,7 +250,11 @@ namespace bigmath {
}
// normalize
while(shrink()) ;
while (shrink());
}
// ============================================================================ //
// MULTIPLICATION //
// ============================================================================ //
}
+20 -7
View File
@@ -4,6 +4,11 @@
#include <bigmath/chain/segment.hpp>
#include <bigmath/util/util.hpp>
#include <array>
namespace bigmath {
/**
@@ -51,24 +56,32 @@ namespace bigmath {
~chain() noexcept;
public:
std::array<chain, 2> split() noexcept;
public:
void add(const chain& c) noexcept;
void sub(const chain& c) noexcept;
void mul_long(const chain& c) noexcept;
chain mul_long(const chain& c) noexcept;
void mul_(const chain& c) noexcept;
chain mul_karatsuba(const chain& c) noexcept;
void mul_karatsuba(const chain& c) noexcept;
chain mul_toomcook3(const chain& c) noexcept;
div_result<chain> div_long(const chain& c) noexcept;
chain div_newtonraphson(const chain& c) noexcept;
chain div_goldschmidt(const chain& c) noexcept;
chain div_burnikelziegler(const chain& c) noexcept;
public:
operator bool() const noexcept;
std::array<link*, 2> split() const noexcept;
/**
* Grows the chain by two links.
* This growth strategy keeps the middle
-11
View File
@@ -1,11 +0,0 @@
#pragma once
#include <bigmath/BigMath.hpp>
#include <array>
namespace bigmath {
u64 ipow(u64 b, u64 p);
}
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include <bigmath/BigMath.hpp>
namespace bigmath {
template<typename T>
struct div_result {
T quot;
T remain;
};
u64 ipow(u64 b, u64 p);
}