aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2026-03-20 17:59:56 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2026-03-20 17:59:56 +0100
commit77de9119efbee9e65f0b50fc8b9315b6fb0910fb (patch)
tree17a335925106f44e6b3178c8c61beba583d3b42d
parentd16656919753438a57a32f9ad6ba972c25d66b11 (diff)
downloadzmodn-master.tar.gz
zmodn-master.zip
Added support for _BitInt (available in clang)HEADmaster
-rw-r--r--bitint_wrapper.h50
-rwxr-xr-xtest18
2 files changed, 68 insertions, 0 deletions
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 @@
1#ifndef BITINT_WRAPPER_H
2#define BITINT_WRAPPER_H
3
4#include <cstdio>
5#include <iostream>
6
7// Wrapper class for _BitInt(N) (port of the C23 feature, compiler
8// extension for C++ available in Clang and maybe GCC). This is needed
9// because operator<< is not defined on _BitInt(N), apparently.
10
11template<uint64_t N = 128>
12class BitInt {
13public:
14 _BitInt(N) x;
15
16 constexpr BitInt() : x{_BitInt(N)(0)} {}
17 constexpr BitInt(int n) : x{_BitInt(N)(n)} {}
18 constexpr BitInt(long long n) : x{_BitInt(N)(n)} {}
19 constexpr BitInt(_BitInt(N) n) : x{n} {}
20 constexpr auto operator<=>(const BitInt& b) const { return x <=> b.x; }
21 constexpr bool operator==(const BitInt& b) const = default;
22 constexpr BitInt operator+(const BitInt& b) const { return x + b.x; }
23 constexpr BitInt operator-(const BitInt& b) const { return x - b.x; }
24 constexpr BitInt operator*(const BitInt& b) const { return x * b.x; }
25 constexpr BitInt operator/(const BitInt& b) const { return x / b.x; }
26 constexpr BitInt operator%(const BitInt& b) const { return x % b.x; }
27 constexpr BitInt operator-() const { return -x; }
28 constexpr BitInt operator+=(const BitInt& b) { return *this = *this + b; }
29 constexpr BitInt operator-=(const BitInt& b) { return *this = *this - b; }
30 constexpr BitInt operator*=(const BitInt& b) { return *this = *this * b; }
31 constexpr BitInt operator/=(const BitInt& b) { return *this = *this / b; }
32 constexpr BitInt operator%=(const BitInt& b) { return *this = *this % b; }
33
34 friend std::ostream& operator<<(std::ostream& os, const BitInt<N>& b) {
35 if (b > 0) {
36 std::string s;
37 auto bb = b;
38 while (bb != 0) {
39 char c = (bb.x % 10) + '0';
40 s = c + s;
41 bb /= 10;
42 }
43 return os << s;
44 } else if (b < 0) {
45 return os << "-" << -b;
46 } else return os << "0";
47 }
48};
49
50#endif
diff --git a/test b/test
index 46ed7c3..2657439 100755
--- a/test
+++ b/test
@@ -12,6 +12,10 @@ exit 0
12#include "zmodn.h" 12#include "zmodn.h"
13#include "bigint.h" 13#include "bigint.h"
14 14
15#if defined(__clang__) && __clang_major__ >= 16
16#include "bitint_wrapper.h"
17#endif
18
15#include <concepts> 19#include <concepts>
16#include <functional> 20#include <functional>
17#include <iostream> 21#include <iostream>
@@ -479,6 +483,20 @@ public:
479 assert_equal(inv.value().toint(), expected); 483 assert_equal(inv.value().toint(), expected);
480 } 484 }
481}, 485},
486#if defined(__clang__) && __clang_major__ >= 16
487{
488 .name = "Zmod with BitInt",
489 .f = []() {
490 constexpr BitInt N{1000000000000LL}; // 1e12
491 BitInt a{600000000000LL}; // 6e11
492 BitInt b{700000000000LL}; // 7e11
493 BitInt c{300000000000LL}; // 3e11
494
495 Zmod<N> a_modN(a), b_modN(b), c_modN(c);
496 assert_equal(a_modN + b_modN, c_modN);
497 }
498},
499#endif
482/* 500/*
483{ 501{
484 .name = "This does not compile", 502 .name = "This does not compile",

Generated with cgit - Back to sebastiano.tronto.net