From 77de9119efbee9e65f0b50fc8b9315b6fb0910fb Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Fri, 20 Mar 2026 17:59:56 +0100 Subject: Added support for _BitInt (available in clang) --- bitint_wrapper.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 bitint_wrapper.h (limited to 'bitint_wrapper.h') diff --git a/bitint_wrapper.h b/bitint_wrapper.h new file mode 100644 index 0000000..785d0fa --- /dev/null +++ b/bitint_wrapper.h @@ -0,0 +1,50 @@ +#ifndef BITINT_WRAPPER_H +#define BITINT_WRAPPER_H + +#include +#include + +// Wrapper class for _BitInt(N) (port of the C23 feature, compiler +// extension for C++ available in Clang and maybe GCC). This is needed +// because operator<< is not defined on _BitInt(N), apparently. + +template +class BitInt { +public: + _BitInt(N) x; + + constexpr BitInt() : x{_BitInt(N)(0)} {} + constexpr BitInt(int n) : x{_BitInt(N)(n)} {} + constexpr BitInt(long long n) : x{_BitInt(N)(n)} {} + constexpr BitInt(_BitInt(N) n) : x{n} {} + constexpr auto operator<=>(const BitInt& b) const { return x <=> b.x; } + constexpr bool operator==(const BitInt& b) const = default; + constexpr BitInt operator+(const BitInt& b) const { return x + b.x; } + constexpr BitInt operator-(const BitInt& b) const { return x - b.x; } + constexpr BitInt operator*(const BitInt& b) const { return x * b.x; } + constexpr BitInt operator/(const BitInt& b) const { return x / b.x; } + constexpr BitInt operator%(const BitInt& b) const { return x % b.x; } + constexpr BitInt operator-() const { return -x; } + constexpr BitInt operator+=(const BitInt& b) { return *this = *this + b; } + constexpr BitInt operator-=(const BitInt& b) { return *this = *this - b; } + constexpr BitInt operator*=(const BitInt& b) { return *this = *this * b; } + constexpr BitInt operator/=(const BitInt& b) { return *this = *this / b; } + constexpr BitInt operator%=(const BitInt& b) { return *this = *this % b; } + + friend std::ostream& operator<<(std::ostream& os, const BitInt& b) { + if (b > 0) { + std::string s; + auto bb = b; + while (bb != 0) { + char c = (bb.x % 10) + '0'; + s = c + s; + bb /= 10; + } + return os << s; + } else if (b < 0) { + return os << "-" << -b; + } else return os << "0"; + } +}; + +#endif -- cgit v1.3