initial upload
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <bigmath/BigMath.hpp>
|
||||
#include <bigmath/chain/segment.hpp>
|
||||
//#include <bigmath/chain/operations.hpp>
|
||||
|
||||
#include <ckitty/memory/buffers.hpp>
|
||||
|
||||
using namespace bigmath;
|
||||
|
||||
int main() {
|
||||
segment s;
|
||||
s.set(0, 8);
|
||||
s.set(1, 5);
|
||||
s.set(2, 5);
|
||||
|
||||
s.set(125, 8);
|
||||
s.set(126, 1);
|
||||
s.set(127, 2);
|
||||
buffers::hexdump(data_view(s.bytes(), segment::byte_count), std::cout);
|
||||
std::cout << std::endl;
|
||||
std::cout << s.describe() << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <ckitty/memory/primitives.hpp>
|
||||
#include <ckitty/memory/array.hpp>
|
||||
#include <iostream>
|
||||
|
||||
#ifdef BIGMATH_DEBUG
|
||||
|
||||
#define BIGMATH_ASSERT(EXPR, ERR_MSG) \
|
||||
if(!(EXPR)) throw std::runtime_error(ERR_MSG)
|
||||
|
||||
#define BIGMATH_DLOG(MSG) \
|
||||
std::cerr << MSG << std::endl
|
||||
|
||||
#else
|
||||
|
||||
#define BIGMATH_ASSERT(EXPR, ERR_MSG) do {} while(0)
|
||||
|
||||
#define BIGMATH_DLOG(MSG) do {} while(0)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
namespace bigmath {
|
||||
|
||||
using namespace ckitty;
|
||||
|
||||
class segment;
|
||||
class chain;
|
||||
|
||||
using d10_t = u8;
|
||||
using digits_t = d10_t*;
|
||||
using dlen_t = u8;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
namespace bigmath {
|
||||
|
||||
/**
|
||||
* A chain connects various segments together.
|
||||
*/
|
||||
class chain {};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
#include "segment.hpp"
|
||||
|
||||
#include <sstream>
|
||||
#include <iomanip>
|
||||
|
||||
namespace bigmath {
|
||||
|
||||
const dlen_t segment::digit_count = 128;
|
||||
const dlen_t segment::byte_count = segment::digit_count / 2;
|
||||
|
||||
segment::segment() {
|
||||
digits = new dlen_t[byte_count]{};
|
||||
}
|
||||
|
||||
segment::~segment() {
|
||||
delete[] digits;
|
||||
}
|
||||
|
||||
d10_t segment::operator[](dlen_t i) const {
|
||||
dlen_t j = i >> 1;
|
||||
dlen_t k = (i & 1) * 4;
|
||||
d10_t b = digits[j];
|
||||
return (b >> k) & 0xFu;
|
||||
}
|
||||
|
||||
void segment::set(dlen_t i, d10_t n) {
|
||||
dlen_t j = i >> 1;
|
||||
dlen_t k = (i & 1) * 4;
|
||||
d10_t b = digits[j];
|
||||
b &= 0xF0u >> k;
|
||||
n &= 0xFu;
|
||||
digits[j] = b | (n << k);
|
||||
}
|
||||
|
||||
const digits_t segment::bytes() const {
|
||||
return digits;
|
||||
}
|
||||
|
||||
std::string segment::describe() const {
|
||||
std::ostringstream out;
|
||||
|
||||
// We'll iterate backwards
|
||||
size_t i = digit_count;
|
||||
u32 use_space = 0;
|
||||
while (i > 0) {
|
||||
d10_t val = operator[](i - 1);
|
||||
|
||||
// Count repetitions
|
||||
size_t count = 1;
|
||||
while (i > count && operator[](i - count - 1) == val) ++count;
|
||||
|
||||
// temporal string
|
||||
std::ostringstream one;
|
||||
one << u32(val);
|
||||
auto str = one.str();
|
||||
|
||||
// Decide whether to compress
|
||||
if (count > 5) {
|
||||
if(use_space) out << " ";
|
||||
out << "[" << str << " x " << count << "]";
|
||||
use_space = 4;
|
||||
} else {
|
||||
for (size_t j = 0; j < count; ++j) {
|
||||
if(use_space > 3) {
|
||||
out << " ";
|
||||
use_space = 1;
|
||||
}
|
||||
use_space++;
|
||||
out << str;
|
||||
}
|
||||
}
|
||||
|
||||
i -= count;
|
||||
}
|
||||
|
||||
return out.str();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <bigmath/BigMath.hpp>
|
||||
|
||||
namespace bigmath {
|
||||
|
||||
/**
|
||||
* A segment contains a fixed amount of numbers.
|
||||
*/
|
||||
class segment {
|
||||
public:
|
||||
|
||||
static const dlen_t digit_count;
|
||||
static const dlen_t byte_count;
|
||||
|
||||
private:
|
||||
|
||||
digits_t digits;
|
||||
|
||||
public:
|
||||
|
||||
segment();
|
||||
|
||||
~segment();
|
||||
|
||||
public:
|
||||
|
||||
d10_t operator[](dlen_t i) const;
|
||||
|
||||
void set(dlen_t i, d10_t n);
|
||||
|
||||
public:
|
||||
|
||||
const digits_t bytes() const;
|
||||
|
||||
std::string describe() const;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#include "util.hpp"
|
||||
|
||||
namespace bigmath {
|
||||
|
||||
u64 ipow(u64 b, u64 p) {
|
||||
// FROM stack overflow
|
||||
// "The most efficient way to implement [int pow]"
|
||||
u64 r = 1;
|
||||
for(;;) {
|
||||
if(p & 1) r *= b;
|
||||
p >>= 1;
|
||||
if(!p) break;
|
||||
b *= b;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <bigmath/BigMath.hpp>
|
||||
|
||||
#include <array>
|
||||
|
||||
namespace bigmath {
|
||||
|
||||
template<class T>
|
||||
using pair = std::array<T, 2>;
|
||||
|
||||
template<typename T>
|
||||
inline void noop(T t) {}
|
||||
|
||||
u64 ipow(u64 b, u64 p);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user