From 75a10aca7b10a0ea5ce632f49581ac1c80b84338 Mon Sep 17 00:00:00 2001 From: subcrip Date: Mon, 28 Oct 2024 14:53:33 +0800 Subject: [PATCH] Add number/oint.cc Signed-off-by: subcrip --- number/oint.cc | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 number/oint.cc diff --git a/number/oint.cc b/number/oint.cc new file mode 100644 index 0000000..63050f4 --- /dev/null +++ b/number/oint.cc @@ -0,0 +1,28 @@ +template +struct oint { + static_assert(MDL > MAX, "Number will never overflow because modulus is too small"); + bool carry; + T val; + oint() : carry(0), val(0) {} + oint(const T& v) : carry(v > MAX), val(v % MDL) {} + friend oint operator*(const oint& a, const oint& b) { + oint res; + res.carry = a.carry | b.carry; + if (not res.carry and a.val > MAX / b.val) { + res.carry = 1; + } + res.val = (a.val * b.val) % MDL; + return res; + } + friend oint operator+(const oint& a, const oint& b) { + oint res; + res.carry = a.carry | b.carry; + if (not res.carry and a.val > MAX - b.val) { + res.carry = 1; + } + res.val = (a.val + b.val) % MDL; + return res; + } + oint& operator+=(const oint& rhs) { return *this = *this + rhs; } + oint& operator*=(const oint& rhs) { return *this = *this * rhs; } +};