競技プログラミング用 C++ ライブラリ
components/fps_direct_eval.hpp · GitHub で見る
共通ページ
fps_detail::evaluate_direct(dst,f,n,points,m) は 多点評価 が使う内部処理です。通常は fps/multipoint_eval.hpp を include して multipoint_eval を利用してください。
| 関数 | 内容・返り値 | 計算量 |
|---|---|---|
fps_detail::evaluate_direct(dst,f,n,points,m) |
f の先頭 n 個を係数とする多項式を points の先頭 m 個で評価し、dst に書き込む |
\(O(nm)\) |
f の先頭 n 個を係数として、points の先頭 m 個での評価値を dst に書き込みます。各配列は法 998244353 の内部 Mint ワード表現を使い、出力領域は入力領域と重ならないものとします。
この内部関数は x86 のみで定義され、AVX2 対応を実行時に確認してから呼び出す必要があります。公開関数は対応 CPU と対象サイズの場合だけこの経路を選び、非対応 CPU と Mac ARM では既存のスカラー処理を使います。
時間 \(O(nm)\)、出力領域を除く追加空間 \(O(1)\) です。
#ifndef CP_COMPONENTS_FPS_DIRECT_EVAL_HPP
#define CP_COMPONENTS_FPS_DIRECT_EVAL_HPP
#include "mint_arithmetic.hpp"
#include <algorithm>
#include <cstddef>
namespace fps_detail {
using mint_detail::Ops;
#if defined(__x86_64__) || defined(__i386__)
__attribute__((target("avx2"))) inline void evaluate_direct(Ops::U *dst, const Ops::U *f,
std::size_t n, const Ops::U *points, std::size_t m) {
using I = Ops::I8;
for (std::size_t i = 0; i < m; i += 32) {
alignas(32) Ops::U tail[32] = {}, out[32];
const std::size_t count = std::min<std::size_t>(32, m - i);
if (count < 8) {
for (std::size_t k = 0; k < count; ++k) {
mint value = 0, x = mint::word(points[i + k]);
for (std::size_t j = n; j-- > 0;) value = value * x + mint::word(f[j]);
dst[i + k] = value.v;
}
break;
}
const Ops::U *xs = points + i;
if (count < 32) {
std::copy_n(xs, count, tail);
xs = tail;
}
I x0 = Ops::load8(xs), x1 = Ops::load8(xs + 8);
I x2 = Ops::load8(xs + 16), x3 = Ops::load8(xs + 24);
I a0 = _mm256_setzero_si256(), a1 = a0, a2 = a0, a3 = a0;
for (std::size_t j = n; j-- > 0;) {
I c = _mm256_set1_epi32(f[j]);
a0 = Ops::add8(Ops::mul8(a0, x0), c);
a1 = Ops::add8(Ops::mul8(a1, x1), c);
a2 = Ops::add8(Ops::mul8(a2, x2), c);
a3 = Ops::add8(Ops::mul8(a3, x3), c);
}
Ops::U *output = count == 32 ? dst + i : out;
Ops::store8(output, a0);
Ops::store8(output + 8, a1);
Ops::store8(output + 16, a2);
Ops::store8(output + 24, a3);
if (count < 32) std::copy_n(out, count, dst + i);
}
}
#endif
}
#endif
#line 1 "components/fps_direct_eval.hpp"
#line 1 "components/mint_arithmetic.hpp"
#line 1 "math/mint.hpp"
#include <cassert>
#include <istream>
#include <ostream>
#include <type_traits>
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC push_options
#pragma GCC optimize("O3")
#endif
namespace mint_detail {
template <class I>
constexpr bool mint_integral = std::is_integral_v<I>
#if defined(__SIZEOF_INT128__)
|| std::is_same_v<I, __int128> || std::is_same_v<I, unsigned __int128>
#endif
;
struct [[gnu::warn_unused]] Mint {
using mint = Mint;
unsigned v = 0;
[[nodiscard]] static constexpr unsigned mod() { return 998244353; }
[[nodiscard, gnu::always_inline]] static unsigned reduce(unsigned long long x) {
return (x + (unsigned long long)(unsigned(x) * 998244351u) * mod()) >> 32;
}
[[gnu::always_inline]] Mint() = default;
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
Mint(I x) {
long long r = x % (long long)mod();
if (r < 0) r += mod();
v = reduce((unsigned long long)r * 932051910);
}
[[nodiscard, gnu::always_inline]] static Mint raw(unsigned x) {
Mint a;
a.v = reduce((unsigned long long)x * 932051910);
return a;
}
[[nodiscard, gnu::always_inline]] static Mint word(unsigned x) {
Mint a;
a.v = x;
return a;
}
[[nodiscard, gnu::always_inline]] unsigned val() const {
unsigned r = reduce(v);
return r < mod() ? r : r - mod();
}
friend std::istream &operator>>(std::istream &is, Mint &x) {
long long value;
if (is >> value) x = Mint(value);
return is;
}
friend std::ostream &operator<<(std::ostream &os, const Mint &x) {
return os << x.val();
}
[[gnu::always_inline]] Mint &operator+=(Mint b) {
v += b.v;
if (v >= 2 * mod()) v -= 2 * mod();
return *this;
}
[[gnu::always_inline]] Mint &operator-=(Mint b) {
v -= b.v;
if (v >> 31) v += 2 * mod();
return *this;
}
[[gnu::always_inline]] Mint &operator*=(Mint b) {
v = reduce((unsigned long long)v * b.v);
return *this;
}
Mint &operator/=(Mint b) { return *this *= b.inv(); }
Mint &operator++() { return *this += Mint(1); }
Mint &operator--() { return *this -= Mint(1); }
Mint operator++(int) {
Mint a = *this;
++*this;
return a;
}
Mint operator--(int) {
Mint a = *this;
--*this;
return a;
}
[[nodiscard, gnu::always_inline]] Mint operator+() const { return *this; }
[[nodiscard, gnu::always_inline]] Mint operator-() const { return word(v ? 2 * mod() - v : 0); }
[[nodiscard, gnu::always_inline]] Mint operator+(Mint b) const { return Mint(*this) += b; }
[[nodiscard, gnu::always_inline]] Mint operator-(Mint b) const { return Mint(*this) -= b; }
[[nodiscard, gnu::always_inline]] Mint operator*(Mint b) const { return Mint(*this) *= b; }
[[nodiscard]] Mint operator/(Mint b) const { return Mint(*this) /= b; }
[[nodiscard, gnu::always_inline]] bool operator==(Mint b) const { return (v < mod() ? v : v - mod()) == (b.v < mod() ? b.v : b.v - mod()); }
[[nodiscard, gnu::always_inline]] bool operator!=(Mint b) const { return !(*this == b); }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend Mint operator+(I a, Mint b) { return Mint(a) + b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend Mint operator-(I a, Mint b) { return Mint(a) - b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend Mint operator*(I a, Mint b) { return Mint(a) * b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend Mint operator/(I a, Mint b) { return Mint(a) / b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend bool operator==(I a, Mint b) { return Mint(a) == b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend bool operator!=(I a, Mint b) { return Mint(a) != b; }
[[nodiscard]] Mint pow(long long n) const {
assert(n >= 0);
if (!n) return Mint(1);
Mint r(1), a = *this;
for (; n > 1; n >>= 1, a *= a)
if (n & 1) r *= a;
return r * a;
}
[[nodiscard, gnu::noinline]] Mint inv() const {
assert(*this != Mint(0));
Mint a2 = *this * *this, a3 = a2 * *this;
Mint a7 = a3 * a3 * *this;
Mint a31 = a7;
for (int i = 0; i < 2; ++i) a31 *= a31;
a31 *= a3;
Mint r = a7;
for (int i = 0; i < 3; ++i) r *= r;
r *= a3;
r *= r;
for (int j = 0; j < 4; ++j) {
for (int i = 0; i < 5; ++i) r *= r;
r *= a31;
}
for (int i = 0; i < 3; ++i) r *= r;
return r * a7;
}
};
} // namespace mint_detail
using mint = mint_detail::Mint;
using fps_mint = mint;
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC pop_options
#endif
#line 5 "components/mint_arithmetic.hpp"
#if defined(__x86_64__) || defined(__i386__)
#include <immintrin.h>
#endif
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC push_options
#pragma GCC optimize("O3")
#endif
namespace mint_detail {
struct Ops {
using T = mint;
typedef unsigned int U __attribute__((may_alias));
static constexpr U mod = 998244353, twice = 2 * mod, one = 301989884;
static U reduce(unsigned long long x) { return T::reduce(x); }
static U mul(U a, U b) { return reduce((unsigned long long)a * b); }
static U add(U a, U b) {
U c = a + b;
return c < twice ? c : c - twice;
}
static U sub(U a, U b) {
U c = a - b;
return c < twice ? c : c + twice;
}
static U in(U a) { return mul(a, 932051910); }
static U out(U a) {
a = reduce(a);
return a < mod ? a : a - mod;
}
static U quotient(U r) { return ((unsigned long long)r << 32) / mod; }
static U fixed_mul(U a, U r, U q) {
return a * r - U(((unsigned long long)a * q) >> 32) * mod;
}
#if defined(__x86_64__) || defined(__i386__)
static bool vectorized() {
#if defined(__AVX2__)
return true;
#else
static const bool available = __builtin_cpu_supports("avx2");
return available;
#endif
}
using I8 = __m256i;
__attribute__((target("avx2"))) static I8 load8(const U *a) {
return _mm256_loadu_si256(reinterpret_cast<const I8 *>(a));
}
__attribute__((target("avx2"))) static void store8(U *a, I8 x) {
_mm256_storeu_si256(reinterpret_cast<I8 *>(a), x);
}
__attribute__((target("avx2"), always_inline)) static inline I8 reduce8(I8 x, I8 y) {
I8 r = _mm256_set1_epi32(998244351), p = _mm256_set1_epi32(mod);
x = _mm256_add_epi64(x, _mm256_mul_epu32(_mm256_mul_epu32(x, r), p));
y = _mm256_add_epi64(y, _mm256_mul_epu32(_mm256_mul_epu32(y, r), p));
return _mm256_blend_epi32(_mm256_srli_epi64(x, 32), y, 0xaa);
}
__attribute__((target("avx2"))) static I8 mul8(I8 a, I8 b) {
return reduce8(_mm256_mul_epu32(a, b), _mm256_mul_epu32(_mm256_srli_epi64(a, 32), _mm256_srli_epi64(b, 32)));
}
__attribute__((target("avx2"))) static I8 fixed_mul8(I8 a, I8 r, I8 q) {
I8 x = _mm256_srli_epi64(_mm256_mul_epu32(a, q), 32);
I8 y = _mm256_mul_epu32(_mm256_srli_epi64(a, 32), q);
I8 t = _mm256_blend_epi32(x, y, 0xaa);
return _mm256_sub_epi32(_mm256_mullo_epi32(a, r),
_mm256_mullo_epi32(t, _mm256_set1_epi32(mod)));
}
__attribute__((target("avx2"))) static I8 fixed_even8(I8 a, I8 r, I8 q) {
I8 t = _mm256_srli_epi64(_mm256_mul_epu32(a, q), 32);
return _mm256_sub_epi64(_mm256_mul_epu32(a, r),
_mm256_mul_epu32(t, _mm256_set1_epi32(mod)));
}
__attribute__((target("avx2"))) static I8 add8(I8 a, I8 b) {
I8 x = _mm256_add_epi32(a, b);
return _mm256_min_epu32(x, _mm256_sub_epi32(x, _mm256_set1_epi32(twice)));
}
__attribute__((target("avx2"))) static I8 sub8(I8 a, I8 b) {
I8 x = _mm256_sub_epi32(a, b);
return _mm256_min_epu32(x, _mm256_add_epi32(x, _mm256_set1_epi32(twice)));
}
#endif
};
}
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC pop_options
#endif
#line 5 "components/fps_direct_eval.hpp"
#include <algorithm>
#include <cstddef>
namespace fps_detail {
using mint_detail::Ops;
#if defined(__x86_64__) || defined(__i386__)
__attribute__((target("avx2"))) inline void evaluate_direct(Ops::U *dst, const Ops::U *f,
std::size_t n, const Ops::U *points, std::size_t m) {
using I = Ops::I8;
for (std::size_t i = 0; i < m; i += 32) {
alignas(32) Ops::U tail[32] = {}, out[32];
const std::size_t count = std::min<std::size_t>(32, m - i);
if (count < 8) {
for (std::size_t k = 0; k < count; ++k) {
mint value = 0, x = mint::word(points[i + k]);
for (std::size_t j = n; j-- > 0;) value = value * x + mint::word(f[j]);
dst[i + k] = value.v;
}
break;
}
const Ops::U *xs = points + i;
if (count < 32) {
std::copy_n(xs, count, tail);
xs = tail;
}
I x0 = Ops::load8(xs), x1 = Ops::load8(xs + 8);
I x2 = Ops::load8(xs + 16), x3 = Ops::load8(xs + 24);
I a0 = _mm256_setzero_si256(), a1 = a0, a2 = a0, a3 = a0;
for (std::size_t j = n; j-- > 0;) {
I c = _mm256_set1_epi32(f[j]);
a0 = Ops::add8(Ops::mul8(a0, x0), c);
a1 = Ops::add8(Ops::mul8(a1, x1), c);
a2 = Ops::add8(Ops::mul8(a2, x2), c);
a3 = Ops::add8(Ops::mul8(a3, x3), c);
}
Ops::U *output = count == 32 ? dst + i : out;
Ops::store8(output, a0);
Ops::store8(output + 8, a1);
Ops::store8(output + 16, a2);
Ops::store8(output + 24, a3);
if (count < 32) std::copy_n(out, count, dst + i);
}
}
#endif
}