46# if !defined(__SIZEOF_INT128__)
47# error "ntt.H requires compiler support for __uint128_t"
57# include <string_view>
58# include <type_traits>
61# if (defined(__GNUC__) or defined(__clang__)) \
62 and (defined(__x86_64__) or defined(__i386__) \
63 or defined(_M_X64) or defined(_M_IX86))
64# include <immintrin.h>
65# define ALEPH_NTT_HAS_X86_AVX2_DISPATCH 1
66# define ALEPH_NTT_AVX2_TARGET __attribute__((target("avx2")))
68# define ALEPH_NTT_HAS_X86_AVX2_DISPATCH 0
71# if (defined(__GNUC__) or defined(__clang__)) \
72 and (defined(__aarch64__) or defined(_M_ARM64))
74# define ALEPH_NTT_HAS_ARM_NEON_DISPATCH 1
76# define ALEPH_NTT_HAS_ARM_NEON_DISPATCH 0
79# if ALEPH_NTT_HAS_ARM_NEON_DISPATCH and defined(__linux__)
81# include <asm/hwcap.h>
113 template <u
int64_t MOD = 998244353ULL, u
int64_t ROOT = 3ULL>
116 static_assert(
MOD > 1,
"NTT requires MOD > 1");
117 static_assert((
MOD & 1ULL) == 1ULL,
"NTT requires an odd modulus");
118 static_assert(
ROOT > 0
and ROOT <
MOD,
"NTT root must lie in (0, MOD)");
144 static constexpr MontgomeryCtx
mctx_ = montgomery_ctx_for_mod<MOD>();
146 [[nodiscard]]
static constexpr bool
149 return n != 0
and (n & (n - 1)) == 0;
152 [[nodiscard]]
static constexpr uint64_t
154 const uint64_t rhs)
noexcept
156 const __uint128_t
sum =
static_cast<__uint128_t
>(lhs) + rhs;
160 [[nodiscard]]
static constexpr uint64_t
162 const uint64_t rhs)
noexcept
164 return lhs >= rhs ? lhs - rhs :
MOD - (rhs - lhs);
167 [[nodiscard]]
static constexpr bool
170 return MOD <= std::numeric_limits<uint64_t>::max() / 2;
173 [[nodiscard]]
static constexpr uint64_t
175 uint64_t
exp)
noexcept
185 result =
static_cast<uint64_t
>(
186 (
static_cast<__uint128_t
>(result) * base) %
MOD);
187 base =
static_cast<uint64_t
>(
188 (
static_cast<__uint128_t
>(base) * base) %
MOD);
195 [[nodiscard]]
static constexpr uint64_t
200 while ((
value & 1ULL) == 0)
208 [[nodiscard]]
static constexpr bool
215 [[nodiscard]]
static constexpr bool
218 return order != 0
and (
MOD - 1) % order == 0;
221 [[nodiscard]]
static constexpr bool
227 if (n >
static_cast<size_t>((
MOD - 1) / 2))
230 const uint64_t order =
static_cast<uint64_t
>(n) * 2ULL;
234 if (n > std::numeric_limits<size_t>::max() / 2)
237 const size_t required = n * 2 - 1;
238 size_t conv_size = 1;
239 while (conv_size < required)
241 if (conv_size > std::numeric_limits<size_t>::max() / 2)
251 const char *
const ctx)
254 << ctx <<
": root order must be positive";
256 << ctx <<
": order " << order
257 <<
" does not divide MOD - 1 (" << (
MOD - 1) <<
")";
260 [[nodiscard]]
static constexpr uint64_t
268 const char *
const ctx)
271 << ctx <<
": size must be positive";
273 << ctx <<
": size " << n
274 <<
" is not supported by MOD " <<
MOD
275 <<
" (power-of-two sizes require n | 2^k <= "
277 <<
"; Bluestein sizes require 2*n | (MOD - 1) and an internal "
281 [[nodiscard]]
static size_t
283 const char *
const ctx)
292 << ctx <<
": next power of two overflows size_t for requested size "
305 for (
size_t i = 0; i < n; ++i)
308 for (
size_t i = 0; i < input.
size(); ++i)
319 <<
"NTT::prefix_copy: length " << length
320 <<
" exceeds input size " << input.
size();
324 for (
size_t i = 0; i < length; ++i)
333 [[nodiscard]]
static constexpr const char *
349 [[nodiscard]]
static constexpr const char *
369 if (
const char *
mode = std::getenv(
"ALEPH_NTT_SIMD");
373 if (
value ==
"scalar")
396 [[nodiscard]]
static bool
399# if ALEPH_NTT_HAS_X86_AVX2_DISPATCH
400 static const bool available = []()
noexcept
402 __builtin_cpu_init();
403 return static_cast<bool>(__builtin_cpu_supports(
"avx2"));
412 [[nodiscard]]
static bool
415# if ALEPH_NTT_HAS_ARM_NEON_DISPATCH
416# if defined(__linux__) and defined(HWCAP_ASIMD)
417 static const bool available = []()
noexcept
419 return (getauxval(AT_HWCAP) & HWCAP_ASIMD) != 0;
454 [[nodiscard]]
static const char *
488 template <
typename F>
493 const size_t chunk_size)
505 for (
size_t i = 0; i <
count; ++i)
516 const size_t end)
const
518 for (
size_t j = begin; j < end; ++j)
520 const uint64_t u = a[base + j];
522 mont_mul(a[base + j + half], twiddles[
offset + j],
mctx_);
524 a(base + j + half) =
sub_mod(u, v);
531# if !ALEPH_NTT_HAS_X86_AVX2_DISPATCH
538 if (pool !=
nullptr and pool->num_threads() > 1)
548# if !ALEPH_NTT_HAS_ARM_NEON_DISPATCH
555 if (pool !=
nullptr and pool->num_threads() > 1)
562# if ALEPH_NTT_HAS_X86_AVX2_DISPATCH
563 [[nodiscard]]
static __m256i
564 avx2_cmp_ge_u64(
const __m256i lhs,
565 const __m256i rhs)
noexcept ALEPH_NTT_AVX2_TARGET
567 alignas(32)
static const uint64_t sign_bits[4] = {
568 0x8000000000000000ULL,
569 0x8000000000000000ULL,
570 0x8000000000000000ULL,
571 0x8000000000000000ULL
574 _mm256_load_si256(
reinterpret_cast<const __m256i *
>(sign_bits));
575 const __m256i lhs_signed = _mm256_xor_si256(lhs, sign);
576 const __m256i rhs_signed = _mm256_xor_si256(rhs, sign);
577 const __m256i gt = _mm256_cmpgt_epi64(lhs_signed, rhs_signed);
578 const __m256i
eq = _mm256_cmpeq_epi64(lhs, rhs);
579 return _mm256_or_si256(gt,
eq);
583 avx2_apply_chunk(uint64_t *
const low,
584 uint64_t *
const high,
585 const uint64_t *
const twiddle_ptr) ALEPH_NTT_AVX2_TARGET
587 alignas(32) uint64_t products[4];
588 for (
size_t lane = 0; lane < 4; ++lane)
589 products[lane] = mont_mul(high[lane], twiddle_ptr[lane],
mctx_);
591 alignas(32)
static const uint64_t mod_lanes[4] = {
MOD,
MOD,
MOD,
MOD};
593 _mm256_load_si256(
reinterpret_cast<const __m256i *
>(mod_lanes));
595 _mm256_loadu_si256(
reinterpret_cast<const __m256i *
>(
low));
597 _mm256_load_si256(
reinterpret_cast<const __m256i *
>(products));
599 const __m256i
sum = _mm256_add_epi64(u, v);
600 const __m256i sum_minus_mod = _mm256_sub_epi64(sum, modv);
601 const __m256i sum_mask = avx2_cmp_ge_u64(sum, modv);
602 const __m256i sum_result = _mm256_blendv_epi8(sum, sum_minus_mod, sum_mask);
604 const __m256i
diff = _mm256_sub_epi64(u, v);
605 const __m256i vu = _mm256_sub_epi64(v, u);
606 const __m256i diff_alt = _mm256_sub_epi64(modv, vu);
607 const __m256i diff_mask = avx2_cmp_ge_u64(u, v);
608 const __m256i diff_result =
609 _mm256_blendv_epi8(diff_alt,
diff, diff_mask);
611 _mm256_storeu_si256(
reinterpret_cast<__m256i *
>(
low), sum_result);
612 _mm256_storeu_si256(
reinterpret_cast<__m256i *
>(high), diff_result);
619 for (
size_t stage = 0; stage <
log_n_; ++stage)
621 const size_t half =
static_cast<size_t>(1) << stage;
622 const size_t len = half << 1;
623 const size_t blocks =
n_ / len;
624 const size_t offset = half - 1;
626 for (
size_t block = 0;
block < blocks; ++
block)
628 const size_t base =
block * len;
630 for (; j + 4 <= half; j += 4)
631 avx2_apply_chunk(&a(base + j),
641# if ALEPH_NTT_HAS_ARM_NEON_DISPATCH
643 neon_apply_chunk(uint64_t *
const low,
644 uint64_t *
const high,
645 const uint64_t *
const twiddle_ptr)
647 alignas(16) uint64_t products[2];
648 for (
size_t lane = 0; lane < 2; ++lane)
649 products[lane] = mont_mul(high[lane], twiddle_ptr[lane],
mctx_);
651 const uint64x2_t modv = vdupq_n_u64(
MOD);
652 const uint64x2_t u = vld1q_u64(
low);
653 const uint64x2_t v = vld1q_u64(products);
654 const uint64x2_t
sum = vaddq_u64(u, v);
655 const uint64x2_t sum_minus_mod = vsubq_u64(sum, modv);
656 const uint64x2_t sum_mask = vcgeq_u64(sum, modv);
657 const uint64x2_t sum_result = vbslq_u64(sum_mask, sum_minus_mod, sum);
659 const uint64x2_t
diff = vsubq_u64(u, v);
660 const uint64x2_t vu = vsubq_u64(v, u);
661 const uint64x2_t diff_alt = vsubq_u64(modv, vu);
662 const uint64x2_t diff_mask = vcgeq_u64(u, v);
663 const uint64x2_t diff_result = vbslq_u64(diff_mask,
diff, diff_alt);
665 vst1q_u64(
low, sum_result);
666 vst1q_u64(high, diff_result);
673 for (
size_t stage = 0; stage <
log_n_; ++stage)
675 const size_t half =
static_cast<size_t>(1) << stage;
676 const size_t len = half << 1;
677 const size_t blocks =
n_ / len;
678 const size_t offset = half - 1;
680 for (
size_t block = 0;
block < blocks; ++
block)
682 const size_t base =
block * len;
684 for (; j + 2 <= half; j += 2)
685 neon_apply_chunk(&a(base + j),
700 for (
size_t i = 1, j = 0; i <
n_; ++i)
702 size_t bit =
n_ >> 1;
703 for (; j & bit; bit >>= 1)
719 const uint64_t mont_one = to_mont(1,
mctx_);
720 for (
size_t stage = 0; stage <
log_n_; ++stage)
722 const size_t half =
static_cast<size_t>(1) << stage;
723 const size_t len = half << 1;
724 const size_t offset = half - 1;
727 const uint64_t root_len_inv =
mod_inv(root_len,
MOD);
729 uint64_t w_fwd = mont_one;
730 uint64_t w_inv = mont_one;
731 const uint64_t w_step_fwd = to_mont(root_len,
mctx_);
732 const uint64_t w_step_inv = to_mont(root_len_inv,
mctx_);
734 for (
size_t j = 0; j < half; ++j)
738 w_fwd = mont_mul(w_fwd, w_step_fwd,
mctx_);
739 w_inv = mont_mul(w_inv, w_step_inv,
mctx_);
763 <<
"NTT::Plan: size " <<
n_
764 <<
" is too large for Bluestein convolution sizing";
766 const size_t required =
n_ * 2 - 1;
770 <<
" exceeds the power-of-two capacity of MOD " <<
MOD;
772 const uint64_t order =
static_cast<uint64_t
>(
n_) * 2ULL;
781 for (
size_t i = 0; i <
n_; ++i)
783 const uint64_t exponent =
784 static_cast<uint64_t
>((
static_cast<__uint128_t
>(i) * i) % order);
799 for (
size_t i = 1; i <
n_; ++i)
817 for (
size_t i = 0; i <
n_; ++i)
825 const size_t chunk_size)
const
827 auto lift_one = [&a](
const size_t i)
837 const size_t chunk_size)
const
839 auto scale_one = [
this, &a](
const size_t i)
849 const size_t chunk_size)
const
851 auto lower_one = [&a](
const size_t i)
853 a(i) = from_mont(a[i],
mctx_);
862 const size_t chunk_size)
const
864 for (
size_t stage = 0; stage <
log_n_; ++stage)
866 const size_t half =
static_cast<size_t>(1) << stage;
867 const size_t len = half << 1;
868 const size_t blocks =
n_ / len;
869 const size_t offset = half - 1;
871 auto butterfly_block = [
this, &a, &twiddles, half, len,
offset]
874 const size_t base = block * len;
887 const size_t chunk_size)
const
892# if ALEPH_NTT_HAS_X86_AVX2_DISPATCH
895 apply_butterflies_avx2(a, twiddles);
900# if ALEPH_NTT_HAS_ARM_NEON_DISPATCH
903 apply_butterflies_neon(a, twiddles);
915 const size_t chunk_size)
const
918 <<
"NTT::Plan::apply_bluestein_transform: missing internal plan";
931 auto initialize = [&a, &work, &input_chirp](
const size_t i)
943 auto pointwise = [&work, &kernel](
const size_t i)
954 auto finalize = [
this, &a, &work, &output_chirp, invert](
const size_t i)
970 const size_t chunk_size)
const
973 <<
"NTT::Plan::transform: input size " << a.
size()
974 <<
" does not match plan size " <<
n_;
997 <<
"NTT::Plan::apply_transform: Bluestein path expects standard "
998 <<
"input representation";
1000 <<
"NTT::Plan::apply_transform: Bluestein path returns standard "
1001 <<
"output representation";
1011 const size_t chunk_size)
const
1017 std::numeric_limits<size_t>::max()
1019 <<
"NTT::Plan::multiply: product size exceeds size_t capacity";
1021 const size_t required = a.
size() + b.
size() - 1;
1023 <<
"NTT::Plan::multiply: product size " << required
1024 <<
" exceeds plan size " <<
n_;
1040 auto pointwise_product = [&fa, &fb](
const size_t i)
1042 fa(i) = mont_mul(fa[i], fb[i],
mctx_);
1062 auto pointwise_product = [&fa, &fb](
const size_t i)
1114 n_(std::exchange(other.n_, 0)),
1115 log_n_(std::exchange(other.log_n_, 0)),
1116 bit_rev_(std::move(other.bit_rev_)),
1119 inv_n_(std::exchange(other.inv_n_, 0)),
1120 inv_n_std_(std::exchange(other.inv_n_std_, 1)),
1154 std::swap(
n_, other.n_);
1155 std::swap(
log_n_, other.log_n_);
1159 std::swap(
inv_n_, other.inv_n_);
1170 [[nodiscard]]
size_t size() const noexcept
1182 const bool invert)
const
1198 const bool invert =
false)
const
1232 const size_t chunk_size = 0)
const
1252 const bool invert =
false,
1253 const size_t chunk_size = 0)
const
1274 const size_t chunk_size = 0)
const
1286 const bool invert)
const
1288 for (
size_t i = 0; i < batch.size(); ++i)
1291 <<
"NTT::Plan::transform_batch: batch item " << i
1292 <<
" has size " << batch[i].size()
1293 <<
" but plan size is " <<
n_;
1316 const size_t chunk_size = 0)
const
1318 for (
size_t i = 0; i < batch.size(); ++i)
1320 <<
"NTT::Plan::ptransform_batch: batch item " << i
1321 <<
" has size " << batch[i].size()
1322 <<
" but plan size is " <<
n_;
1324 if (batch.is_empty())
1329 for (
size_t i = 0; i < batch.size(); ++i)
1337 auto transform_one = [
this, &batch, invert](
const size_t i)
1355 const bool invert =
false)
const
1374 const bool invert =
false,
1375 const size_t chunk_size = 0)
const
1396 for (
size_t i = 0; i < input.
size(); ++i)
1406 for (
size_t i = 0; i < n; ++i)
1416 const size_t limit = std::min(input.
size(), n);
1417 for (
size_t i = 0; i < limit; ++i)
1428 for (
size_t i = 0; i < input.
size()
and i < n; ++i)
1438 for (
size_t i = input.
size(); i > 0; --i)
1449 for (
size_t i = 0; i < n; ++i)
1451 const uint64_t a = i < lhs.
size() ? lhs[i] %
MOD : 0;
1452 const uint64_t b = i < rhs.
size() ? rhs[i] %
MOD : 0;
1464 for (
size_t i = 0; i < n; ++i)
1466 const uint64_t a = i < lhs.
size() ? lhs[i] %
MOD : 0;
1467 const uint64_t b = i < rhs.
size() ? rhs[i] %
MOD : 0;
1477 const size_t n = std::max(lhs.
size(), rhs.
size());
1487 const size_t n = std::max(lhs.
size(), rhs.
size());
1500 for (
size_t i = 0; i < input.
size()
and i < n; ++i)
1524 if (coeffs.
size() <= 1)
1528 for (
size_t i = 1; i < coeffs.
size(); ++i)
1530 static_cast<uint64_t
>(i) %
MOD,
1540 for (
size_t i = 0; i < coeffs.
size(); ++i)
1542 const uint64_t inv =
mod_inv(
static_cast<uint64_t
>(i + 1),
MOD);
1548 [[nodiscard]]
static uint64_t
1550 const char *
const ctx)
1558 << ctx <<
": constant term " << a
1559 <<
" is not a quadratic residue modulo " <<
MOD;
1567 uint64_t q =
MOD - 1;
1569 while ((q & 1ULL) == 0)
1588 while (i <
m and t2i != 1)
1595 << ctx <<
": Tonelli-Shanks failed to converge";
1598 for (
size_t j = 0; j + i + 1 <
m; ++j)
1608 return std::min(
r,
r == 0 ? 0 :
MOD -
r);
1615 const size_t capacity =
count == 0 ? 0 :
count * 4 + 4;
1617 for (
size_t i = 0; i < capacity; ++i)
1629 if (left + 1 == right)
1638 const size_t mid = left + (right - left) / 2;
1641 tree(node) =
multiply(tree[node << 1], tree[(node << 1) | 1]);
1646 const char *
const ctx)
1648 for (
size_t i = 0; i < points.
size(); ++i)
1649 for (
size_t j = i + 1; j < points.
size(); ++j)
1651 << ctx <<
": points[" << i <<
"] and points[" << j
1652 <<
"] collide modulo " <<
MOD;
1665 if (dividend.
size() < divisor.
size())
1679 if (left + 1 == right)
1685 const size_t mid = left + (right - left) / 2;
1687 poly.
size() < tree[node << 1].size() ?
1691 poly.
size() < tree[(node << 1) | 1].
size() ?
1693 poly_mod(poly, tree[(node << 1) | 1]);
1696 node << 1, left, mid);
1698 (node << 1) | 1, mid, right);
1708 if (left + 1 == right)
1711 const size_t mid = left + (right - left) / 2;
1718 multiply(left_poly, tree[(node << 1) | 1]),
1719 multiply(right_poly, tree[node << 1]));
1728 [[nodiscard]]
static constexpr uint64_t
1740 [[nodiscard]]
static constexpr bool
1754 [[nodiscard]]
static constexpr uint64_t
1757 if (not std::is_constant_evaluated())
1786 const bool invert =
false)
1809 std::numeric_limits<size_t>::max()
1811 <<
"NTT::multiply: product size exceeds size_t capacity";
1813 const size_t required = a.
size() + b.
size() - 1;
1854 <<
"NTT::negacyclic_multiply: inputs must have positive size";
1856 <<
"NTT::negacyclic_multiply: lhs size " << a.
size()
1857 <<
" does not match rhs size " << b.
size();
1859 const size_t n = a.
size();
1861 <<
"NTT::negacyclic_multiply: size " << n
1862 <<
" is not a power of two";
1864 <<
"NTT::negacyclic_multiply: size " << n
1865 <<
" requires a primitive root of order " << (n << 1)
1866 <<
", but the largest supported power-of-two order is "
1870 const uint64_t inv_twist =
mod_inv(twist,
MOD);
1876 for (
size_t i = 0; i < n; ++i)
1885 for (
size_t i = 0; i < n; ++i)
1890 for (
size_t i = 0; i < n; ++i)
1909 if (batch.is_empty())
1924 const bool invert =
false)
1926 if (batch.is_empty())
1944 const size_t chunk_size = 0)
1961 const bool invert =
false,
1962 const size_t chunk_size = 0)
1983 const size_t chunk_size = 0)
1989 std::numeric_limits<size_t>::max()
1991 <<
"NTT::pmultiply: product size exceeds size_t capacity";
1993 const size_t required = a.
size() + b.
size() - 1;
2016 const size_t chunk_size = 0)
2018 if (batch.is_empty())
2030 [[nodiscard]]
static uint64_t
2035 const uint64_t x_mod = x %
MOD;
2036 for (
size_t i = coeffs.
size(); i > 0; --i)
2059 <<
"NTT::poly_inverse: input polynomial must be non-empty";
2061 const uint64_t c0 = coeffs[0] %
MOD;
2063 <<
"NTT::poly_inverse: constant term must be invertible modulo " <<
MOD;
2071 const size_t m2 = std::min(n,
m << 1);
2076 for (
size_t i = 1; i < m2; ++i)
2077 correction(i) = fg[i] == 0 ? 0 :
MOD - fg[i];
2100 <<
"NTT::poly_divmod: divisor cannot be the zero polynomial";
2105 const size_t quotient_size = a.
size() - b.
size() + 1;
2135 const uint64_t c0 = coeffs.
is_empty() ? 0 : coeffs[0] %
MOD;
2137 <<
"NTT::poly_log: constant term must be 1 modulo " <<
MOD;
2163 const uint64_t c0 = coeffs.
is_empty() ? 0 : coeffs[0] %
MOD;
2165 <<
"NTT::poly_exp: constant term must be 0 modulo " <<
MOD;
2173 const size_t m2 = std::min(n,
m << 1);
2177 delta(0) =
add_mod(delta[0], 1);
2202 while (lead < input.
size()
and input[lead] %
MOD == 0)
2205 if (lead == input.
size())
2209 <<
"NTT::poly_sqrt: first non-zero term appears at odd degree "
2214 const size_t shift = lead / 2;
2217 for (
size_t i = lead; i < input.
size(); ++i)
2222 for (
size_t i = 0; i < rooted.
size()
and i + shift < n; ++i)
2223 output(i + shift) = rooted[i];
2234 const size_t m2 = std::min(n,
m << 1);
2270 while (lead < input.
size()
and input[lead] %
MOD == 0)
2273 if (lead == input.
size())
2276 const __uint128_t shift128 =
2277 static_cast<__uint128_t
>(lead) *
static_cast<__uint128_t
>(
k);
2281 const size_t shift =
static_cast<size_t>(shift128);
2282 const size_t target = n - shift;
2283 const uint64_t lead_coeff = input[lead] %
MOD;
2284 const uint64_t inv_lead =
mod_inv(lead_coeff,
MOD);
2288 for (
size_t i = lead; i < input.
size(); ++i)
2292 for (
size_t i = 0; i < scaled_log.
size(); ++i)
2293 scaled_log(i) =
mod_mul(scaled_log[i],
2294 static_cast<uint64_t
>(
k %
MOD),
MOD);
2301 for (
size_t i = 0; i < powered.
size()
and i + shift < n; ++i)
2302 output(i + shift) = powered[i];
2320 for (
size_t i = 0; i < points.
size(); ++i)
2321 reduced_points(i) = points[i] %
MOD;
2327 for (
size_t i = 0; i <
output.size(); ++i)
2331 1, 0, reduced_points.
size());
2349 <<
"NTT::interpolate: points size " << points.
size()
2350 <<
" does not match values size " << values.
size();
2357 for (
size_t i = 0; i < points.
size(); ++i)
2359 reduced_points(i) = points[i] %
MOD;
2360 reduced_values(i) = values[i] %
MOD;
2373 for (
size_t i = 0; i < values.
size(); ++i)
2376 <<
"NTT::interpolate: derivative vanished at point index " << i;
2377 scaled_values(i) =
mod_mul(reduced_values[i],
2382 1, 0, reduced_points.
size()));
2406 template <u
int64_t Base = (1ULL << 15)>
2407 [[nodiscard]] static Array<u
int64_t>
2408 big
int_multiply(const Array<u
int64_t> & a,
2409 const Array<u
int64_t> & b);
2427 template <u
int64_t Base = (1ULL << 15)>
2428 [[nodiscard]] static Array<u
int64_t>
2429 pbig
int_multiply(ThreadPool & pool,
2430 const Array<u
int64_t> & a,
2431 const Array<u
int64_t> & b,
2432 const
size_t chunk_size = 0);
2444 u
int64_t max_power_of_two = 0;
2465 using coeff_type = __u
int128_t;
2468 using Prime0NTT = NTT<998244353ULL, 3ULL>;
2469 using Prime1NTT = NTT<469762049ULL, 3ULL>;
2470 using Prime2NTT = NTT<1004535809ULL, 3ULL>;
2472 struct CoefficientStats
2474 u
int64_t max_value = 0;
2475 size_t non_zero = 0;
2479 static constexpr NTTPrime primes_[] = {
2480 {998244353ULL, 3ULL, 23ULL},
2481 {469762049ULL, 3ULL, 26ULL},
2482 {1004535809ULL, 3ULL, 21ULL}
2485 [[nodiscard]] static constexpr coeff_type
2486 exact_modulus_product_impl() noexcept
2488 return static_cast<coeff_type>(primes_[0].mod)
2489 * static_cast<coeff_type>(primes_[1].mod)
2490 * static_cast<coeff_type>(primes_[2].mod);
2493 [[nodiscard]] static constexpr u
int64_t
2494 sub_mod(const u
int64_t lhs,
2496 const u
int64_t mod) noexcept
2498 return lhs >= rhs ? lhs - rhs : mod - (rhs - lhs);
2501 [[nodiscard]] static constexpr coeff_type
2502 add_capped(const coeff_type lhs,
2503 const coeff_type rhs,
2504 const coeff_type cap) noexcept
2506 return lhs >= cap - rhs ? cap : lhs + rhs;
2509 [[nodiscard]]
static constexpr coeff_type
2514 if (lhs == 0 or rhs == 0)
2516 return lhs > cap / rhs ? cap : lhs * rhs;
2519 [[nodiscard]]
static constexpr size_t
2528 if (
value > std::numeric_limits<size_t>::max() / 2)
2536 template <
typename PrimeNTT>
2537 [[nodiscard]]
static constexpr bool
2543 if (PrimeNTT::supports_size(required))
2546 const size_t n = next_power_of_two(required);
2547 return n != 0
and PrimeNTT::supports_size(n);
2550 [[nodiscard]]
static std::string
2559 const auto digit =
static_cast<unsigned>(
value % 10);
2560 digits.push_back(
static_cast<char>(
'0' + digit));
2564 std::reverse(digits.begin(), digits.end());
2568 [[nodiscard]]
static CoefficientStats
2572 const coeff_type cap = exact_modulus_product();
2573 for (
size_t i = 0; i < input.
size(); ++i)
2575 const uint64_t
value = input[i];
2588 [[nodiscard]]
static coeff_type
2595 const coeff_type cap = exact_modulus_product();
2611 if (overlap_bound < bound)
2612 bound = overlap_bound;
2616 if (lhs_sum_bound < bound)
2617 bound = lhs_sum_bound;
2621 if (rhs_sum_bound < bound)
2622 bound = rhs_sum_bound;
2630 const char *
const ctx)
2633 std::numeric_limits<size_t>::max()
2635 << ctx <<
": product size exceeds size_t capacity";
2637 const size_t required = a.
size() + b.
size() - 1;
2639 << ctx <<
": required product size " << required
2640 <<
" is not supported by the three-prime CRT pack";
2642 const coeff_type bound = conservative_bound(a, b);
2644 << ctx <<
": cannot guarantee exact reconstruction inside CRT range "
2645 << coeff_to_string(exact_modulus_product())
2646 <<
" with conservative coefficient bound "
2647 << coeff_to_string(bound);
2650 [[nodiscard]]
static coeff_type
2655 static const uint64_t m0 = primes_[0].mod;
2656 static const uint64_t m1 = primes_[1].mod;
2657 static const uint64_t m2 = primes_[2].mod;
2660 static const uint64_t m0_inv_mod_m1 =
mod_inv(m0 % m1, m1);
2661 static const uint64_t m0m1_inv_mod_m2 =
2662 mod_inv(
static_cast<uint64_t
>(m0m1 % m2), m2);
2665 mod_mul(sub_mod(r1, r0 % m1, m1), m0_inv_mod_m1, m1);
2670 const uint64_t x01_mod_m2 =
static_cast<uint64_t
>(x01 % m2);
2672 mod_mul(sub_mod(r2, x01_mod_m2, m2), m0m1_inv_mod_m2, m2);
2674 return x01 +
static_cast<coeff_type>(t2) * m0m1;
2682 const size_t chunk_size)
2685 <<
"NTTExact::reconstruct_product: inconsistent residue sizes";
2689 auto reconstruct_one = [&
output, &c0, &c1, &c2](
const size_t i)
2691 output(i) = reconstruct_coefficient(c0[i], c1[i], c2[i]);
2697 for (
size_t i = 0; i < c0.
size(); ++i)
2705 [[nodiscard]]
static constexpr size_t
2708 return sizeof(primes_) /
sizeof(primes_[0]);
2716 [[nodiscard]]
static constexpr coeff_type
2719 return exact_modulus_product_impl();
2728 [[nodiscard]]
static constexpr bool
2731 return prime_supports_product_size<Prime0NTT>(required)
2732 and prime_supports_product_size<Prime1NTT>(required)
2733 and prime_supports_product_size<Prime2NTT>(required);
2752 validate_inputs(a, b,
"NTTExact::multiply");
2757 return reconstruct_product(c0, c1, c2,
nullptr, 0);
2780 const size_t chunk_size = 0)
2785 validate_inputs(a, b,
"NTTExact::pmultiply");
2788 return multiply(a, b);
2790 auto f0 = pool.
enqueue([&a, &b]()
2792 return Prime0NTT::multiply(a, b);
2794 auto f1 = pool.
enqueue([&a, &b]()
2796 return Prime1NTT::multiply(a, b);
2798 auto f2 = pool.
enqueue([&a, &b]()
2800 return Prime2NTT::multiply(a, b);
2806 return reconstruct_product(c0, c1, c2, &pool, chunk_size);
2810 template <u
int64_t MOD, u
int64_t ROOT>
2811 template <u
int64_t Base>
2816 static_assert(Base > 1,
"NTT::bigint_multiply requires Base >= 2");
2819 auto zero_digits = []()
2827 const char *
const name,
2828 const char *
const ctx)
2830 for (
size_t i = 0; i < digits.
size(); ++i)
2832 << ctx <<
": " << name <<
"[" << i <<
"] = " << digits[i]
2833 <<
" is not in [0, " << Base <<
")";
2840 for (
size_t i = 0; i < input.size(); ++i)
2844 static_cast<void>(
output.remove_last());
2850 if (coeffs.is_empty())
2851 return zero_digits();
2856 ExactCoeff carry = 0;
2857 for (
size_t i = 0; i < coeffs.size(); ++i)
2859 const ExactCoeff total = coeffs[i] + carry;
2860 output.append(
static_cast<uint64_t
>(total % Base));
2861 carry = total / Base;
2866 output.append(
static_cast<uint64_t
>(carry % Base));
2871 static_cast<void>(
output.remove_last());
2876 validate_digits(a,
"a",
"NTT::bigint_multiply");
2877 validate_digits(b,
"b",
"NTT::bigint_multiply");
2882 return zero_digits();
2885 std::numeric_limits<size_t>::max()
2887 <<
"NTT::bigint_multiply: product size exceeds size_t capacity";
2889 const size_t required = lhs.
size() + rhs.
size() - 1;
2890 const size_t internal_size =
2891 supports_size(required) ?
2893 next_power_of_two(required,
"NTT::bigint_multiply");
2895 const ExactCoeff digit_max =
static_cast<ExactCoeff
>(Base - 1);
2896 const ExactCoeff single_prime_bound =
2897 static_cast<ExactCoeff
>(std::min(lhs.
size(), rhs.
size()))
2898 * digit_max * digit_max;
2900 if (single_prime_bound <
static_cast<ExactCoeff
>(
MOD)
2901 and supports_size(internal_size))
2905 for (
size_t i = 0; i < coeffs.
size(); ++i)
2906 exact(i) =
static_cast<ExactCoeff
>(coeffs[i]);
2907 return propagate_carries(exact);
2913 template <u
int64_t MOD, u
int64_t ROOT>
2914 template <u
int64_t Base>
2919 const size_t chunk_size)
2921 static_assert(Base > 1,
"NTT::pbigint_multiply requires Base >= 2");
2924 auto zero_digits = []()
2932 const char *
const name,
2933 const char *
const ctx)
2935 for (
size_t i = 0; i < digits.
size(); ++i)
2937 << ctx <<
": " << name <<
"[" << i <<
"] = " << digits[i]
2938 <<
" is not in [0, " << Base <<
")";
2945 for (
size_t i = 0; i < input.size(); ++i)
2949 static_cast<void>(
output.remove_last());
2955 if (coeffs.is_empty())
2956 return zero_digits();
2961 ExactCoeff carry = 0;
2962 for (
size_t i = 0; i < coeffs.size(); ++i)
2964 const ExactCoeff total = coeffs[i] + carry;
2965 output.append(
static_cast<uint64_t
>(total % Base));
2966 carry = total / Base;
2971 output.append(
static_cast<uint64_t
>(carry % Base));
2976 static_cast<void>(
output.remove_last());
2981 validate_digits(a,
"a",
"NTT::pbigint_multiply");
2982 validate_digits(b,
"b",
"NTT::pbigint_multiply");
2987 return zero_digits();
2990 std::numeric_limits<size_t>::max()
2992 <<
"NTT::pbigint_multiply: product size exceeds size_t capacity";
2994 const size_t required = lhs.
size() + rhs.
size() - 1;
2995 const size_t internal_size =
2996 supports_size(required) ?
2998 next_power_of_two(required,
"NTT::pbigint_multiply");
3000 const ExactCoeff digit_max =
static_cast<ExactCoeff
>(Base - 1);
3001 const ExactCoeff single_prime_bound =
3002 static_cast<ExactCoeff
>(std::min(lhs.
size(), rhs.
size()))
3003 * digit_max * digit_max;
3005 if (single_prime_bound <
static_cast<ExactCoeff
>(
MOD)
3006 and supports_size(internal_size))
3010 for (
size_t i = 0; i < coeffs.
size(); ++i)
3011 exact(i) =
static_cast<ExactCoeff
>(coeffs[i]);
3012 return propagate_carries(exact);
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
size_t size_t int32_t value
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
void swap(Array &s) noexcept
Swap this with s
T & get_last()
Return a modifiable reference to the last element.
static Array create(const size_t n)
Create an array with n logical elements.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
static coeff_type conservative_bound(const Array< uint64_t > &a, const Array< uint64_t > &b)
static constexpr coeff_type mul_capped(const coeff_type lhs, const coeff_type rhs, const coeff_type cap) noexcept
static Array< coeff_type > reconstruct_product(const Array< uint64_t > &c0, const Array< uint64_t > &c1, const Array< uint64_t > &c2, ThreadPool *const pool, const size_t chunk_size)
static constexpr coeff_type exact_modulus_product() noexcept
Product of the three CRT moduli.
static CoefficientStats analyze_coefficients(const Array< uint64_t > &input)
static coeff_type reconstruct_coefficient(const uint64_t r0, const uint64_t r1, const uint64_t r2)
static Array< coeff_type > pmultiply(ThreadPool &pool, const Array< uint64_t > &a, const Array< uint64_t > &b, const size_t chunk_size=0)
Exact parallel polynomial multiplication.
static constexpr bool prime_supports_product_size(const size_t required) noexcept
static constexpr bool supports_product_size(const size_t required) noexcept
Check whether a target product length is supported.
static constexpr size_t next_power_of_two(const size_t n) noexcept
static std::string coeff_to_string(coeff_type value)
static void validate_inputs(const Array< uint64_t > &a, const Array< uint64_t > &b, const char *const ctx)
static Array< coeff_type > multiply(const Array< uint64_t > &a, const Array< uint64_t > &b)
Exact sequential polynomial multiplication.
static constexpr size_t prime_count() noexcept
Number of CRT primes in the exact multiplier.
Precomputed plans for NTT transforms.
Array< Array< uint64_t > > transformed_batch(const Array< Array< uint64_t > > &input, const bool invert=false) const
Return a transformed copy of an entire batch.
Array< uint64_t > multiply(const Array< uint64_t > &a, const Array< uint64_t > &b) const
Multiply two polynomials using this plan size.
void ptransform(ThreadPool &pool, Array< uint64_t > &a, const bool invert, const size_t chunk_size=0) const
Parallel in-place transform using a ThreadPool.
Array< uint64_t > multiply_impl(const Array< uint64_t > &a, const Array< uint64_t > &b, ThreadPool *const pool, const size_t chunk_size) const
size_t size() const noexcept
Return the transform size bound to the plan.
void initialize_twiddles()
Array< uint64_t > bluestein_kernel_forward_
void apply_bit_reversal(Array< uint64_t > &a) const noexcept
Array< uint64_t > transformed(const Array< uint64_t > &input, const bool invert=false) const
Transform an input array and return the result.
std::shared_ptr< const Plan > bluestein_plan_
Array< uint64_t > pmultiply(ThreadPool &pool, const Array< uint64_t > &a, const Array< uint64_t > &b, const size_t chunk_size=0) const
Parallel polynomial multiplication using this plan size.
Plan(const size_t n)
Construct a reusable plan for a fixed transform size.
Array< uint64_t > bluestein_chirp_inv_
void swap(Plan &other) noexcept
Exchange in O(1) the tables of this plan and another.
void transform(Array< uint64_t > &a, const bool invert) const
In-place forward or inverse transform.
void ptransform_batch(ThreadPool &pool, Array< Array< uint64_t > > &batch, const bool invert, const size_t chunk_size=0) const
Parallel batch transform for equal-sized inputs.
void lift_input(Array< uint64_t > &a, ThreadPool *const pool, const size_t chunk_size) const
void initialize_power_of_two_plan()
Array< uint64_t > twiddles_inv_
Plan & operator=(const Plan &other)=default
Copy the tables of another plan.
static void for_each_index(ThreadPool *const pool, const size_t count, F &&fn, const size_t chunk_size)
Array< Array< uint64_t > > ptransformed_batch(ThreadPool &pool, const Array< Array< uint64_t > > &input, const bool invert=false, const size_t chunk_size=0) const
Return a parallel-transformed copy of an entire batch.
Array< uint64_t > bluestein_chirp_fwd_
void apply_transform(Array< uint64_t > &a, const bool invert, const Representation input_repr, const Representation output_repr, ThreadPool *const pool, const size_t chunk_size) const
bool should_use_avx2(ThreadPool *const pool) const noexcept
void apply_butterflies_scalar(Array< uint64_t > &a, const Array< uint64_t > &twiddles, ThreadPool *const pool, const size_t chunk_size) const
void apply_butterflies(Array< uint64_t > &a, const bool invert, ThreadPool *const pool, const size_t chunk_size) const
void apply_bluestein_transform(Array< uint64_t > &a, const bool invert, ThreadPool *const pool, const size_t chunk_size) const
bool should_use_neon(ThreadPool *const pool) const noexcept
void initialize_bit_reversal()
Plan(Plan &&other) noexcept
Take the tables of another plan, which is left empty: its size is 0, and it only transforms empty arr...
void scale_inverse(Array< uint64_t > &a, ThreadPool *const pool, const size_t chunk_size) const
void apply_scalar_butterfly_range(Array< uint64_t > &a, const Array< uint64_t > &twiddles, const size_t base, const size_t half, const size_t offset, const size_t begin, const size_t end) const
void transform_batch(Array< Array< uint64_t > > &batch, const bool invert) const
Sequential batch transform for equal-sized inputs.
Array< uint64_t > twiddles_fwd_
void lower_output(Array< uint64_t > &a, ThreadPool *const pool, const size_t chunk_size) const
Plan(const Plan &other)=default
Copy the tables of another plan.
void initialize_bluestein_plan()
Array< uint64_t > bluestein_kernel_inverse_
Array< uint64_t > ptransformed(ThreadPool &pool, const Array< uint64_t > &input, const bool invert=false, const size_t chunk_size=0) const
Parallel transform returning a new array.
Number Theoretic Transform over Z / MOD Z.
static Array< uint64_t > interpolate_recursive(const Array< Array< uint64_t > > &tree, const Array< uint64_t > &scaled_values, const size_t node, const size_t left, const size_t right)
static Array< uint64_t > poly_sqrt(const Array< uint64_t > &coeffs, const size_t n)
Formal polynomial square root modulo x^n.
static uint64_t tonelli_shanks(const uint64_t value, const char *const ctx)
static bool avx2_dispatch_available() noexcept
Returns whether AVX2 dispatch is available at runtime.
static constexpr const char * simd_preference_name(const SimdPreference preference) noexcept
static void validate_distinct_points(const Array< uint64_t > &points, const char *const ctx)
static constexpr bool supports_bluestein_size(const size_t n) noexcept
static Array< uint64_t > bigint_multiply(const Array< uint64_t > &a, const Array< uint64_t > &b)
Multiply two non-negative integers represented as base-Base digits.
static Array< uint64_t > poly_exp(const Array< uint64_t > &coeffs, const size_t n)
Formal polynomial exponential modulo x^n.
static Array< Array< uint64_t > > transformed_batch(const Array< Array< uint64_t > > &batch, const bool invert=false)
Return a transformed copy of an entire batch.
static constexpr uint64_t add_mod(const uint64_t lhs, const uint64_t rhs) noexcept
static void transform_batch(Array< Array< uint64_t > > &batch, const bool invert)
Sequential batch transform for equal-sized inputs.
static Array< uint64_t > poly_sub_series(const Array< uint64_t > &lhs, const Array< uint64_t > &rhs, const size_t n)
static Array< uint64_t > negacyclic_multiply(const Array< uint64_t > &a, const Array< uint64_t > &b)
Negacyclic convolution modulo x^N + 1.
static Array< uint64_t > poly_integral(const Array< uint64_t > &coeffs)
static constexpr bool supports_size(const size_t n) noexcept
Check whether a transform size is supported.
static constexpr uint64_t sub_mod(const uint64_t lhs, const uint64_t rhs) noexcept
static void multipoint_eval_recursive(const Array< Array< uint64_t > > &tree, const Array< uint64_t > &poly, Array< uint64_t > &output, const size_t node, const size_t left, const size_t right)
static Array< Array< uint64_t > > make_product_tree_storage(const size_t count)
NTTSimdBackend
SIMD backends available to the NTT butterfly core.
@ avx2
x86-64 AVX2 grouped butterfly path.
@ scalar
Portable scalar implementation.
@ neon
AArch64 NEON grouped butterfly path.
static Array< uint64_t > poly_power(const Array< uint64_t > &coeffs, const uint64_t k, const size_t n)
Formal polynomial power modulo x^n.
static constexpr bool supports_root_order(const uint64_t order) noexcept
static size_t next_power_of_two(size_t n, const char *const ctx)
static Array< uint64_t > multipoint_eval(const Array< uint64_t > &coeffs, const Array< uint64_t > &points)
Evaluate a polynomial on multiple points modulo MOD.
static Array< uint64_t > poly_inverse(const Array< uint64_t > &coeffs, const size_t n)
Formal polynomial inverse modulo x^n.
static Array< uint64_t > poly_sub_normalized(const Array< uint64_t > &lhs, const Array< uint64_t > &rhs)
static Array< uint64_t > multiply(const Array< uint64_t > &a, const Array< uint64_t > &b)
Multiply two polynomials modulo MOD.
static Array< uint64_t > poly_add_series(const Array< uint64_t > &lhs, const Array< uint64_t > &rhs, const size_t n)
static Array< uint64_t > reverse_poly(const Array< uint64_t > &input)
static Array< uint64_t > poly_derivative(const Array< uint64_t > &coeffs)
static void validate_root_order(const uint64_t order, const char *const ctx)
static Array< uint64_t > pmultiply(ThreadPool &pool, const Array< uint64_t > &a, const Array< uint64_t > &b, const size_t chunk_size=0)
Parallel polynomial multiplication modulo MOD.
static Array< uint64_t > truncate_poly(const Array< uint64_t > &input, const size_t n)
static void ptransform(ThreadPool &pool, Array< uint64_t > &a, const bool invert, const size_t chunk_size=0)
Parallel in-place transform using a ThreadPool.
static Array< uint64_t > prefix_copy(const Array< uint64_t > &input, const size_t length)
static constexpr uint64_t max_transform_size() noexcept
Maximum supported power-of-two transform size.
static constexpr const char * simd_backend_name(const NTTSimdBackend backend) noexcept
static Array< uint64_t > interpolate(const Array< uint64_t > &points, const Array< uint64_t > &values)
Interpolate a polynomial from point-value samples modulo MOD.
static void multiply_inplace(Array< uint64_t > &a, const Array< uint64_t > &b)
Replace a by the product a * b.
static constexpr uint64_t mod
static Array< uint64_t > ptransformed(ThreadPool &pool, const Array< uint64_t > &input, const bool invert=false, const size_t chunk_size=0)
Parallel transform returning a new array.
static SimdPreference simd_preference() noexcept
static uint64_t poly_eval(const Array< uint64_t > &coeffs, const uint64_t x)
Evaluate a polynomial at a single point modulo MOD.
static Array< uint64_t > transformed(const Array< uint64_t > &input, const bool invert=false)
Transform an input array and return the result.
static Array< uint64_t > series_prefix(const Array< uint64_t > &input, const size_t n)
static void ptransform_batch(ThreadPool &pool, Array< Array< uint64_t > > &batch, const bool invert, const size_t chunk_size=0)
Parallel batch transform for equal-sized inputs.
static NTTSimdBackend detected_simd_backend() noexcept
static constexpr bool supports_power_of_two_size(const size_t n) noexcept
static constexpr MontgomeryCtx mctx_
static constexpr bool simd_mod_supported() noexcept
static constexpr uint64_t pow_mod_constexpr(uint64_t base, uint64_t exp) noexcept
static Array< uint64_t > poly_mul_trunc(const Array< uint64_t > &lhs, const Array< uint64_t > &rhs, const size_t n)
static constexpr uint64_t root
static bool neon_dispatch_available() noexcept
Returns whether AArch64 NEON dispatch is available at runtime.
static Array< uint64_t > poly_add_normalized(const Array< uint64_t > &lhs, const Array< uint64_t > &rhs)
static std::pair< Array< uint64_t >, Array< uint64_t > > poly_divmod(const Array< uint64_t > ÷nd, const Array< uint64_t > &divisor)
Polynomial division with remainder modulo MOD.
static void transform(Array< uint64_t > &a, const bool invert)
In-place forward or inverse transform.
static Array< uint64_t > poly_mod(const Array< uint64_t > ÷nd, const Array< uint64_t > &divisor)
static constexpr uint64_t primitive_root_of_unity(const size_t n)
Return an n-th primitive root of unity modulo MOD.
static Array< uint64_t > normalize_poly(const Array< uint64_t > &input)
static NTTSimdBackend simd_backend() noexcept
Returns the SIMD backend selected under ALEPH_NTT_SIMD.
static void validate_supported_size(const size_t n, const char *const ctx)
static Array< uint64_t > poly_scalar_mul_series(const Array< uint64_t > &input, const uint64_t scalar, const size_t n)
static Array< uint64_t > pbigint_multiply(ThreadPool &pool, const Array< uint64_t > &a, const Array< uint64_t > &b, const size_t chunk_size=0)
Parallel big-integer multiplication in base Base.
static void trim_trailing_zeros(Array< uint64_t > &poly)
static constexpr bool is_power_of_two(const size_t n) noexcept
static Array< uint64_t > padded_copy(const Array< uint64_t > &input, const size_t n)
static Array< uint64_t > zero_series(const size_t n)
static constexpr uint64_t primitive_root_of_order(const uint64_t order)
static const char * simd_backend_name() noexcept
Returns the active SIMD backend name.
static void build_product_tree(Array< Array< uint64_t > > &tree, const Array< uint64_t > &points, const size_t node, const size_t left, const size_t right)
static Array< uint64_t > poly_log(const Array< uint64_t > &coeffs, const size_t n)
Formal polynomial logarithm modulo x^n.
static constexpr uint64_t max_transform_size_impl() noexcept
A reusable thread pool for efficient parallel task execution.
size_t num_threads() const noexcept
Get the number of worker threads.
auto enqueue(F &&f, Args &&... args) -> std::future< std::invoke_result_t< F, Args... > >
Submit a task for execution and get a future for the result.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_exp_function > > exp(const __gmp_expr< T, U > &expr)
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_remainder_function > > remainder(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
Safe modular arithmetic, extended Euclidean algorithm, and Chinese Remainder Theorem.
Compiler_SSA_Block & block(Compiler_SSA_Function &function, const Compiler_SSA_Block_Id id)
Main namespace for Aleph-w library functions.
uint64_t mod_inv(const uint64_t a, const uint64_t m)
Modular Inverse.
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
static long & low(typename GT::Node *p)
Internal helper: low-link value stored directly in NODE_COOKIE(p).
void parallel_for_index(ThreadPool &pool, size_t start, size_t end, F &&f, size_t chunk_size=0)
Apply a function to each element in parallel (index-based).
and
Check uniqueness with explicit hash + equality functors.
uint64_t mod_exp(uint64_t base, uint64_t exp, const uint64_t m)
Modular exponentiation.
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
uint64_t mod_mul(uint64_t a, uint64_t b, uint64_t m)
Safe 64-bit modular multiplication.
auto mode(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute the mode (most frequent value).
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
A modern, efficient thread pool for parallel task execution.
Dynamic array container with automatic resizing.