diff --git a/bigmath.code-workspace b/bigmath.code-workspace new file mode 100644 index 0000000..b0fd153 --- /dev/null +++ b/bigmath.code-workspace @@ -0,0 +1,69 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": { + "C_Cpp.default.includePath": [ + "./src", + "../ckittylib/src" + ], + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "charconv": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "format": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "span": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "text_encoding": "cpp", + "typeinfo": "cpp", + "variant": "cpp", + "iostream": "cpp", + "map": "cpp", + "iomanip": "cpp", + "istream": "cpp", + "sstream": "cpp" + }, + "makefile.configureOnOpen": false + } +} \ No newline at end of file diff --git a/bin/bigmath/BigMath.d b/bin/bigmath/BigMath.d new file mode 100644 index 0000000..223e7df --- /dev/null +++ b/bin/bigmath/BigMath.d @@ -0,0 +1,2 @@ +bin/bigmath/BigMath.o: src/bigmath/BigMath.cpp +src/bigmath/BigMath.cpp: diff --git a/bin/bigmath/BigMath.o b/bin/bigmath/BigMath.o new file mode 100644 index 0000000..c280bf1 Binary files /dev/null and b/bin/bigmath/BigMath.o differ diff --git a/bin/bigmath/chain/chain.d b/bin/bigmath/chain/chain.d new file mode 100644 index 0000000..955abb2 --- /dev/null +++ b/bin/bigmath/chain/chain.d @@ -0,0 +1,2 @@ +bin/bigmath/chain/chain.o: src/bigmath/chain/chain.cpp +src/bigmath/chain/chain.cpp: diff --git a/bin/bigmath/chain/chain.o b/bin/bigmath/chain/chain.o new file mode 100644 index 0000000..f23eb76 Binary files /dev/null and b/bin/bigmath/chain/chain.o differ diff --git a/bin/bigmath/chain/operations.d b/bin/bigmath/chain/operations.d new file mode 100644 index 0000000..07a31cb --- /dev/null +++ b/bin/bigmath/chain/operations.d @@ -0,0 +1,4 @@ +bin/bigmath/chain/operations.o: src/bigmath/chain/operations.cpp \ + src/bigmath/chain/operations.hpp +src/bigmath/chain/operations.cpp: +src/bigmath/chain/operations.hpp: diff --git a/bin/bigmath/chain/operations.o b/bin/bigmath/chain/operations.o new file mode 100644 index 0000000..67ed80f Binary files /dev/null and b/bin/bigmath/chain/operations.o differ diff --git a/bin/bigmath/chain/segment.d b/bin/bigmath/chain/segment.d new file mode 100644 index 0000000..dbb2b05 --- /dev/null +++ b/bin/bigmath/chain/segment.d @@ -0,0 +1,3 @@ +bin/bigmath/chain/segment.o: src/bigmath/chain/segment.cpp src/bigmath/chain/segment.hpp +src/bigmath/chain/segment.cpp: +src/bigmath/chain/segment.hpp: diff --git a/bin/bigmath/chain/segment.o b/bin/bigmath/chain/segment.o new file mode 100644 index 0000000..3ee832e Binary files /dev/null and b/bin/bigmath/chain/segment.o differ diff --git a/bin/bigmath/chain/util.d b/bin/bigmath/chain/util.d new file mode 100644 index 0000000..60a8764 --- /dev/null +++ b/bin/bigmath/chain/util.d @@ -0,0 +1,3 @@ +bin/bigmath/chain/util.o: src/bigmath/chain/util.cpp src/bigmath/chain/util.hpp +src/bigmath/chain/util.cpp: +src/bigmath/chain/util.hpp: diff --git a/bin/bigmath/chain/util.o b/bin/bigmath/chain/util.o new file mode 100644 index 0000000..c5752b7 Binary files /dev/null and b/bin/bigmath/chain/util.o differ diff --git a/makefile b/makefile new file mode 100644 index 0000000..766ebda --- /dev/null +++ b/makefile @@ -0,0 +1,65 @@ +#Compiler and Linker +CC := g++ + +#The Target Binary Program +TARGET := test.exe + +#The Directories, Source, Includes, Objects, Binary and Resources +SRCDIR := src +BUILDDIR := bin +TARGETDIR := out +SRCEXT := cpp +DEPEXT := d +OBJEXT := o + +#Flags, Libraries and Includes +ROOT := ./ +CFLAGS := -Wall -std=c++20 -DBIGMATH_DEBUG +LFLAGS := -Wall -std=c++20 -static +LIB := +INC := -I./src/ -I"$(ROOT)/../ckittylib/src" + +#--------------------------------------------------------------------------------- +#DO NOT EDIT BELOW THIS LINE +#--------------------------------------------------------------------------------- +SOURCES := $(shell /c/msys64/usr/bin/find $(SRCDIR) -type f -name *.$(SRCEXT)) +OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.$(OBJEXT))) + +#Defauilt Make +all: directories $(TARGET) + +#Remake +remake: cleaner all + +#Make the Directories +directories: + @mkdir -p $(TARGETDIR) + @mkdir -p $(BUILDDIR) + +#Clean only Objecst +clean: + @$(RM) -rf $(BUILDDIR) + +#Full Clean, Objects and Binaries +cleaner: clean + @$(RM) -rf $(TARGETDIR) + +#Pull in dependency info for *existing* .o files +-include $(OBJECTS:.$(OBJEXT)=.$(DEPEXT)) + +#Link +$(TARGET): $(OBJECTS) + $(CC) $(LFLAGS) -o $(TARGETDIR)/$(TARGET) $^ $(LIB) + +#Compile +$(BUILDDIR)/%.$(OBJEXT): $(SRCDIR)/%.$(SRCEXT) + @mkdir -p $(dir $@) + $(CC) $(CFLAGS) $(INC) -c -o $@ $< + @$(CC) $(CFLAGS) -MM $(SRCDIR)/$*.$(SRCEXT) > $(BUILDDIR)/$*.$(DEPEXT) + @cp -f $(BUILDDIR)/$*.$(DEPEXT) $(BUILDDIR)/$*.$(DEPEXT).tmp + @sed -e 's|.*:|$(BUILDDIR)/$*.$(OBJEXT):|' < $(BUILDDIR)/$*.$(DEPEXT).tmp > $(BUILDDIR)/$*.$(DEPEXT) + @sed -e 's/.*://' -e 's/\\$$//' < $(BUILDDIR)/$*.$(DEPEXT).tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $(BUILDDIR)/$*.$(DEPEXT) + @rm -f $(BUILDDIR)/$*.$(DEPEXT).tmp + +#Non-File Targets +.PHONY: all remake clean cleaner resources \ No newline at end of file diff --git a/multest.txt b/multest.txt new file mode 100644 index 0000000..bff067f --- /dev/null +++ b/multest.txt @@ -0,0 +1,3 @@ +9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 + + diff --git a/out/test.exe b/out/test.exe new file mode 100644 index 0000000..2721c35 Binary files /dev/null and b/out/test.exe differ diff --git a/src/bigmath/BigMath.cpp b/src/bigmath/BigMath.cpp new file mode 100644 index 0000000..863e242 --- /dev/null +++ b/src/bigmath/BigMath.cpp @@ -0,0 +1,23 @@ +#include + +#include +#include +//#include + +#include + +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; +} \ No newline at end of file diff --git a/src/bigmath/BigMath.hpp b/src/bigmath/BigMath.hpp new file mode 100644 index 0000000..8540aae --- /dev/null +++ b/src/bigmath/BigMath.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include + +#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; + +} diff --git a/src/bigmath/chain/chain.cpp b/src/bigmath/chain/chain.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/bigmath/chain/chain.hpp b/src/bigmath/chain/chain.hpp new file mode 100644 index 0000000..8b36610 --- /dev/null +++ b/src/bigmath/chain/chain.hpp @@ -0,0 +1,10 @@ +#pragma once + +namespace bigmath { + + /** + * A chain connects various segments together. + */ + class chain {}; + +} \ No newline at end of file diff --git a/src/bigmath/chain/segment.cpp b/src/bigmath/chain/segment.cpp new file mode 100644 index 0000000..a489873 --- /dev/null +++ b/src/bigmath/chain/segment.cpp @@ -0,0 +1,79 @@ +#include "segment.hpp" + +#include +#include + +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(); + } + +} \ No newline at end of file diff --git a/src/bigmath/chain/segment.hpp b/src/bigmath/chain/segment.hpp new file mode 100644 index 0000000..48f1b2a --- /dev/null +++ b/src/bigmath/chain/segment.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include + +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; + + }; + +} \ No newline at end of file diff --git a/src/bigmath/chain/util.cpp b/src/bigmath/chain/util.cpp new file mode 100644 index 0000000..c35941e --- /dev/null +++ b/src/bigmath/chain/util.cpp @@ -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; + } + +} \ No newline at end of file diff --git a/src/bigmath/chain/util.hpp b/src/bigmath/chain/util.hpp new file mode 100644 index 0000000..9c7fdb1 --- /dev/null +++ b/src/bigmath/chain/util.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include + +#include + +namespace bigmath { + + template + using pair = std::array; + + template + inline void noop(T t) {} + + u64 ipow(u64 b, u64 p); + +} \ No newline at end of file