/* LICENSE.Nyaan
Creative Commons Legal Code
CC0 1.0 Universal
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
HEREUNDER.
Statement of Purpose
The laws of most jurisdictions throughout the world automatically confer
exclusive Copyright and Related Rights (defined below) upon the creator
and subsequent owner(s) (each and all, an "owner") of an original work of
authorship and/or a database (each, a "Work").
Certain owners wish to permanently relinquish those rights to a Work for
the purpose of contributing to a commons of creative, cultural and
scientific works ("Commons") that the public can reliably and without fear
of later claims of infringement build upon, modify, incorporate in other
works, reuse and redistribute as freely as possible in any form whatsoever
and for any purposes, including without limitation commercial purposes.
These owners may contribute to the Commons to promote the ideal of a free
culture and the further production of creative, cultural and scientific
works, or to gain reputation or greater distribution for their Work in
part through the use and efforts of others.
For these and/or other purposes and motivations, and without any
expectation of additional consideration or compensation, the person
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
is an owner of Copyright and Related Rights in the Work, voluntarily
elects to apply CC0 to the Work and publicly distribute the Work under its
terms, with knowledge of his or her Copyright and Related Rights in the
Work and the meaning and intended legal effect of CC0 on those rights.
1. Copyright and Related Rights. A Work made available under CC0 may be
protected by copyright and related or neighboring rights ("Copyright and
Related Rights"). Copyright and Related Rights include, but are not
limited to, the following:
i. the right to reproduce, adapt, distribute, perform, display,
communicate, and translate a Work;
ii. moral rights retained by the original author(s) and/or performer(s);
iii. publicity and privacy rights pertaining to a person's image or
likeness depicted in a Work;
iv. rights protecting against unfair competition in regards to a Work,
subject to the limitations in paragraph 4(a), below;
v. rights protecting the extraction, dissemination, use and reuse of data
in a Work;
vi. database rights (such as those arising under Directive 96/9/EC of the
European Parliament and of the Council of 11 March 1996 on the legal
protection of databases, and under any national implementation
thereof, including any amended or successor version of such
directive); and
vii. other similar, equivalent or corresponding rights throughout the
world based on applicable law or treaty, and any national
implementations thereof.
2. Waiver. To the greatest extent permitted by, but not in contravention
of, applicable law, Affirmer hereby overtly, fully, permanently,
irrevocably and unconditionally waives, abandons, and surrenders all of
Affirmer's Copyright and Related Rights and associated claims and causes
of action, whether now known or unknown (including existing as well as
future claims and causes of action), in the Work (i) in all territories
worldwide, (ii) for the maximum duration provided by applicable law or
treaty (including future time extensions), (iii) in any current or future
medium and for any number of copies, and (iv) for any purpose whatsoever,
including without limitation commercial, advertising or promotional
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
member of the public at large and to the detriment of Affirmer's heirs and
successors, fully intending that such Waiver shall not be subject to
revocation, rescission, cancellation, termination, or any other legal or
equitable action to disrupt the quiet enjoyment of the Work by the public
as contemplated by Affirmer's express Statement of Purpose.
3. Public License Fallback. Should any part of the Waiver for any reason
be judged legally invalid or ineffective under applicable law, then the
Waiver shall be preserved to the maximum extent permitted taking into
account Affirmer's express Statement of Purpose. In addition, to the
extent the Waiver is so judged Affirmer hereby grants to each affected
person a royalty-free, non transferable, non sublicensable, non exclusive,
irrevocable and unconditional license to exercise Affirmer's Copyright and
Related Rights in the Work (i) in all territories worldwide, (ii) for the
maximum duration provided by applicable law or treaty (including future
time extensions), (iii) in any current or future medium and for any number
of copies, and (iv) for any purpose whatsoever, including without
limitation commercial, advertising or promotional purposes (the
"License"). The License shall be deemed effective as of the date CC0 was
applied by Affirmer to the Work. Should any part of the License for any
reason be judged legally invalid or ineffective under applicable law, such
partial invalidity or ineffectiveness shall not invalidate the remainder
of the License, and in such case Affirmer hereby affirms that he or she
will not (i) exercise any of his or her remaining Copyright and Related
Rights in the Work or (ii) assert any associated claims and causes of
action with respect to the Work, in either case contrary to Affirmer's
express Statement of Purpose.
4. Limitations and Disclaimers.
a. No trademark or patent rights held by Affirmer are waived, abandoned,
surrendered, licensed or otherwise affected by this document.
b. Affirmer offers the Work as-is and makes no representations or
warranties of any kind concerning the Work, express, implied,
statutory or otherwise, including without limitation warranties of
title, merchantability, fitness for a particular purpose, non
infringement, or the absence of latent or other defects, accuracy, or
the present or absence of errors, whether or not discoverable, all to
the greatest extent permissible under applicable law.
c. Affirmer disclaims responsibility for clearing rights of other persons
that may apply to the Work or any use thereof, including without
limitation any person's Copyright and Related Rights in the Work.
Further, Affirmer disclaims responsibility for obtaining any necessary
consents, permissions or other rights required for any use of the
Work.
d. Affirmer understands and acknowledges that Creative Commons is not a
party to this document and has no duty or obligation with respect to
this CC0 or use of the Work.
*/
// Pure exact reference: no production generator, seed, data, answer or digest.
#define MOD_TRIANGLE_FAST_E7
#include <bits/stdc++.h>
#include <x86intrin.h>
// Original large-denominator remainder-index convolution kernel.
// NTT arithmetic backend derived from NyaanNyaan/library and batch's canonical
// mod<2^31 adaptation; inherited notices and LICENSE.Nyaan are retained.
// All three fields accumulate all q, including direct leaves, before one CRT.
// This header enforces a positive-integer CRT bound: n=1e7 is supported;
// n=1e8 fails that bound and is deliberately refused, never silently wrapped.
#include <bits/stdc++.h>
#include <immintrin.h>
// Derived from public Duck submission 141230, attributed notices retained below.
// Pure field extraction: no 0..9 digit wrapper, no production inputs or seeds.
// Generic padded outer stages and exact generic Shoup arithmetic are original.
#include <immintrin.h>
#include <cstdint>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <algorithm>
#pragma GCC push_options
namespace fast_e7_field {
static uint32_t g_mod=2013265921u,g_generator=31u,g_shmagic=572662301u,g_shint=2u;
/* seat14_fresh_20261031 agg4 2026-10-31 0submit: base cand_twdel_agg4.cpp (sha256 8b8c7813, 101919cp, sid141141) + ONE change: dh4's fused S=4 level, four horizontal final_dif4_vec calls -> transposed vertical radix-4 (two 4x4 lane transposes, one vS, same mod-p math); inherited notices below unchanged. Full 80000004B output identical: mode0 FNV fb206c93723a3583 C0=10, all-9 77fffe38ccc593f7 C0=81, unaligned-c fallback identical. */
/* agg4 ADOPT of saffah_codex_6a_agg3 #140076 <https://duck.ac/submission/140076> (283,842.545us, 2026-10-23 00:51:39):
body adopted in full with attribution (their header below intact; their chain = our #139932 <https://duck.ac/submission/139932>
+ their #139747/#140059 lazy inverse boundaries). Nothing of ours removed.
Gates: full 80,000,004-byte output byte-identical to #139932, random and all-9 inputs.
*/
/* References: saffah_codex_6a_agg3 https://duck.ac/submission/140059: pointer-hoisted engine with three lazy middle inverse boundaries, all prior attribution below.
Idea: Extend the range proof across the padded16384-coefficient slab boundary. In the known even,L4 path, only child slabs1/2/3 feed Montgomery in merge4_dit_s3, so their H4096 outputs can remain below2P. Child0 stays canonical and every other geometry retains its original path. No padding element becomes data. Shorten private names solely for source size.
Purpose: Delete canonicalization before the next Montgomery consumer. Verify padded groups, modular ranges and complete20M outputs at three alignments.
*/
/* References: saffah_v41f1017_agg4 https://duck.ac/submission/139932: pointer-hoisted engine below; saffah_codex_6a_agg3 https://duck.ac/submission/139747: our three proven lazy inverse boundaries.
Idea: Combine those independent improvements. Keep canonical unmultiplied children, but omit normalization for fused64 and middle H64/H256/H1024 outputs consumed only by Montgomery. Bounds a<2P,b<P and unchanged canonical consumers still apply. Shorten three private helper names solely for source size.
Purpose: Validate stage residues/bounds and every full output on the new pointer layout.
*/
/* 1002e7: ac4 FUSE-IM (inv->merge; ours #139464<https://duck.ac/submission/139464>) + agg4 H1FUSE (h1->h4 DIT). Sources per RULES sec.3 (no license; acct+URL+content): saffah_codex_6a_agg3 #139373 <https://duck.ac/submission/139373> base; #139367 <https://duck.ac/submission/139367> merge5; #139358 <https://duck.ac/submission/139358>; #139353 <https://duck.ac/submission/139353>; #133546 <https://duck.ac/submission/133546> slabs; #128549 <https://duck.ac/submission/128549> changes; #125868 <https://duck.ac/submission/125868>; #121456 <https://duck.ac/submission/121456>; #121443 <https://duck.ac/submission/121443>; #121420 <https://duck.ac/submission/121420>; #120684 <https://duck.ac/submission/120684>; #120621 <https://duck.ac/submission/120621>; saffah_cc_v41_agg1 #130191 <https://duck.ac/submission/130191> base; #127195 <https://duck.ac/submission/127195>; #121257 <https://duck.ac/submission/121257>; #122356 <https://duck.ac/submission/122356>; #122333 <https://duck.ac/submission/122333>; #122392 <https://duck.ac/submission/122392>; #126906 <https://duck.ac/submission/126906>; #111674 <https://duck.ac/submission/111674>.
agg4 chain: H1FUSE = ours #139571 <https://duck.ac/submission/139571> (inverse h1->h4 DIT ring fusion).
agg4 additions on that base (all value-preserving; full 80,000,004-byte outputs byte-identical):
(1) S64FUSE: inverse S=16 -> S=64 ring levels fused in registers (removes one 64KB-slab store/load round trip);
(2) DISPHOIST: loop-invariant switch(S) dispatch hoisted out of the ring block loops in fwd_sb_ring and ntt_inv_tail;
(3) PADGATE: PADJUMP/PADJUMP_S four boundary compares replaced by one nearest-boundary gate (min counter), sync at crossings;
(4) PMSPLIT: dh4 g_pmdelta branch split into norm/pm loop variants with delta folded into the base pointer;
(5) S64 kernel always_inline into ntt_inv_tail.
(6) PTRHOIST: ds2/s3 and merge4_dit_s2/s3 tails, stage16_dif_top_batch, and the H16 ring level use raw incremented data/twiddle pointers instead of gcc9 per-iteration pointer-chain reloads; byte-exact, Ir -38.90M vs cand_disp.
(7) PTRHOIST2: single-base table/data addressing from build_tabs layout (MP=H+TPAD) for stage4_dif_h, merge4_dit_h, ds2/s3, merge4_dit_s2/s3, fused inverse l-tables and bodyC; H4 pointer loops; H16 noinline single-base loop with its four prefetches removed. Ir -66.45M vs cand_disp (additional -27.56M vs PTRHOIST).
Gates rand sha1 4bc5ad88828cf2aefc06810ad4032ec5c2ef1677, all-9 9276aacf2a6b58674a5d65128fd2908318e308f0.
*/
#define NTT_MAXN (1u<<25)
#pragma GCC optimize("O3,rename-registers")
#include <immintrin.h>
#include <cstdint>
#include <cstring>
#include <cstdlib>
__attribute__((target("avx2")))static inline __m256i ldu256(const void*p){
__m256i r;
__asm__("vmovdqu %1, %0":"=x"(r):"m"(*(const __m256i*)p));
return r;
}
__attribute__((target("avx2")))static inline void stu256(void*p,__m256i v){
__asm__("vmovdqu %1, %0":"=m"(*(__m256i*)p):"x"(v));
}
typedef uint32_t u32;
typedef uint64_t u64;
#define PF_DIST 192
#define NTP g_mod
#define TPAD 704u
#define TOPPAD 128u
static inline u32 padpos(u32 i){
return i+TOPPAD*((i>>20)+(i>>18)+(i>>16)+(i>>14));
}
#define PADJUMP() if (j >= nxt) { if (j >= nxt0) { bp += TOPPAD; nxt0 += (1u << 20); } \
if (j >= nxt1) { bp += TOPPAD; nxt1 += (1u << 18); } \
if (j >= nxt2) { bp += TOPPAD; nxt2 += (1u << 16); } \
if (j >= nxt3) { bp += TOPPAD; nxt3 += (1u << 14); } \
nxt = nxt0; if (nxt1 < nxt) nxt = nxt1; if (nxt2 < nxt) nxt = nxt2; if (nxt3 < nxt) nxt = nxt3; }
#define PADJUMP_S() if (s >= nxt) { if (s >= nxt0) { bp += TOPPAD; nxt0 += (1u << 20); } \
if (s >= nxt1) { bp += TOPPAD; nxt1 += (1u << 18); } \
if (s >= nxt2) { bp += TOPPAD; nxt2 += (1u << 16); } \
if (s >= nxt3) { bp += TOPPAD; nxt3 += (1u << 14); } \
nxt = nxt0; if (nxt1 < nxt) nxt = nxt1; if (nxt2 < nxt) nxt = nxt2; if (nxt3 < nxt) nxt = nxt3; }
#define TGT __attribute__((target("avx2")))
static u32 g_scale_digit[10];
static u32 g_scale_lo[8]__attribute__((aligned(32)));
static u32 g_scale_hi[8]__attribute__((aligned(32)));
#ifndef NTT_MAXN
#define NTT_MAXN (1u << 25)
#endif
static u32 g_n,g_Sb;
static u32 g_pinv;
static int g_log2n,g_L,g_odd,g_nb,g_BT=13;
static u32 g_ciF,g_cisF,g_ciI,g_cisI;
static u32 *g_tabF;
#define Q_T5OFF (5 * NTT_MAXN / 2 + 4096 - 4 * (1u << 22))
#define Q_SCF (g_tabF + 2*g_n + 131072u)
#define Q_SCI (g_tabI + 2*g_n + 131072u)
static u32 *g_tabI;
struct Tabs{
const u32*l1[40],*l2[40],*l3[40],*m1[40],*m2[40],*m3[40];
const u32*b2,*b2s;
const u32*h1[40],*h2[40],*h3[40],*q1[40],*q2[40],*q3[40];
}
;
static Tabs g_TF,g_TI;
static inline u32 mulmod(u32 a,u32 b){
return(u32)((u64)a*b%NTP);
}
static u32 powmod(u32 a,u32 e){
u32 r=1;
while(e){
if(e&1)r=mulmod(r,a);
a=mulmod(a,a);
e>>=1;
}
return r;
}
#define RRW ((u32)((1ull<<32)%NTP))
static u32 g_wf,g_wif;
static inline u32 shoup_c(u32 w){
return(u32)(((u64)w<<32)/NTP);
}
TGT static inline __m256i vmulhi(__m256i a,__m256i b){
__m256i e=_mm256_mul_epu32(a,b);
__m256i o=_mm256_mul_epu32(_mm256_bsrli_epi128(a,4),_mm256_bsrli_epi128(b,4));
e=_mm256_bsrli_epi128(e,4);
return _mm256_blend_epi32(e,o,0xAA);
}
TGT static inline __m256i vmont(__m256i a,__m256i b,__m256i pv){
const __m256i pinv=_mm256_set1_epi32((int)g_pinv);
__m256i tlo=_mm256_mullo_epi32(a,b);
__m256i thi=vmulhi(a,b);
__m256i m=_mm256_mullo_epi32(tlo,pinv);
__m256i mhi=vmulhi(m,pv);
__m256i nz=_mm256_andnot_si256(_mm256_cmpeq_epi32(tlo,_mm256_setzero_si256()),_mm256_set1_epi32(1));
__m256i u=_mm256_add_epi32(_mm256_add_epi32(thi,mhi),nz);
return _mm256_min_epu32(u,_mm256_sub_epi32(u,pv));
}
TGT static inline __m256i mpa(__m256i a,__m256i b,__m256i pv){
const __m256i np=_mm256_set1_epi64x((long long)g_pinv);
const __m256i p64=_mm256_set1_epi64x((long long)NTP);
__m256i ae=_mm256_mul_epu32(a,b);
__m256i ao=_mm256_mul_epu32(_mm256_bsrli_epi128(a,4),_mm256_bsrli_epi128(b,4));
__m256i me=_mm256_mul_epu32(ae,np),mo=_mm256_mul_epu32(ao,np);
ae=_mm256_add_epi64(ae,_mm256_mul_epu32(me,p64));
ao=_mm256_add_epi64(ao,_mm256_mul_epu32(mo,p64));
__m256i value=_mm256_or_si256(_mm256_bsrli_epi128(ae,4),ao);
return _mm256_min_epu32(value,_mm256_sub_epi32(value,pv));
}
TGT static inline __m256i mpb(__m256i a,__m256i b,__m256i bpi,__m256i pv){
const __m256i p64=_mm256_set1_epi64x((long long)NTP);
__m256i ae=_mm256_mul_epu32(a,b);
__m256i ao=_mm256_mul_epu32(_mm256_bsrli_epi128(a,4),_mm256_bsrli_epi128(b,4));
__m256i me=_mm256_mul_epu32(a,bpi),mo=_mm256_mul_epu32(_mm256_bsrli_epi128(a,4),_mm256_bsrli_epi128(bpi,4));
ae=_mm256_add_epi64(ae,_mm256_mul_epu32(me,p64));
ao=_mm256_add_epi64(ao,_mm256_mul_epu32(mo,p64));
__m256i value=_mm256_or_si256(_mm256_bsrli_epi128(ae,4),ao);
return _mm256_min_epu32(value,_mm256_sub_epi32(value,pv));
}
TGT static inline __m128i mpc(__m128i a,__m128i b,__m128i bpi,__m128i pv){
const __m128i p64=_mm_set1_epi64x((long long)NTP);
__m128i ae=_mm_mul_epu32(a,b);
__m128i ao=_mm_mul_epu32(_mm_bsrli_si128(a,4),_mm_bsrli_si128(b,4));
__m128i me=_mm_mul_epu32(a,bpi),mo=_mm_mul_epu32(_mm_bsrli_si128(a,4),_mm_bsrli_si128(bpi,4));
ae=_mm_add_epi64(ae,_mm_mul_epu32(me,p64));
ao=_mm_add_epi64(ao,_mm_mul_epu32(mo,p64));
__m128i value=_mm_or_si128(_mm_bsrli_si128(ae,4),ao);
return _mm_min_epu32(value,_mm_sub_epi32(value,pv));
}
#define MLO_V g_shmagic
TGT static inline __m256i der_ws(__m256i w){
__m256i q=_mm256_add_epi32(_mm256_mullo_epi32(w,_mm256_set1_epi32(g_shint)),vmulhi(w,_mm256_set1_epi32((int)g_shmagic)));
__m256i q1=_mm256_add_epi32(q,_mm256_set1_epi32(1)),p=_mm256_set1_epi32((int)NTP);
__m256i high=vmulhi(q1,p),low=_mm256_mullo_epi32(q1,p),zero=_mm256_setzero_si256();
__m256i correction=_mm256_or_si256(_mm256_xor_si256(_mm256_cmpeq_epi32(high,w),_mm256_set1_epi32(-1)),_mm256_cmpeq_epi32(low,zero));
return _mm256_add_epi32(q,_mm256_and_si256(correction,_mm256_set1_epi32(1)));
}
TGT static inline __m256i vS(__m256i a,__m256i w,__m256i ws,__m256i pv){
__m256i t=_mm256_mullo_epi32(a,w);
__m256i q=vmulhi(a,ws);
__m256i r=_mm256_sub_epi32(t,_mm256_mullo_epi32(q,pv));
return _mm256_min_epu32(r,_mm256_sub_epi32(r,pv));
}
TGT static inline __m256i vaddm(__m256i a,__m256i b,__m256i pv){
__m256i s=_mm256_add_epi32(a,b);
return _mm256_min_epu32(s,_mm256_sub_epi32(s,pv));
}
TGT static inline __m256i vsubm(__m256i a,__m256i b,__m256i pv){
__m256i d=_mm256_sub_epi32(a,b);
return _mm256_min_epu32(d,_mm256_add_epi32(d,pv));
}
TGT static inline __m128i vmulhi4(__m128i a,__m128i b){
__m128i e=_mm_mul_epu32(a,b);
__m128i o=_mm_mul_epu32(_mm_bsrli_si128(a,4),_mm_bsrli_si128(b,4));
e=_mm_bsrli_si128(e,4);
return _mm_blend_epi32(e,o,0xAA);
}
TGT static inline __m128i sh4(__m128i a,__m128i w,__m128i ws,__m128i pv){
__m128i t=_mm_mullo_epi32(a,w);
__m128i q=vmulhi4(a,ws);
__m128i r=_mm_sub_epi32(t,_mm_mullo_epi32(q,pv));
return _mm_min_epu32(r,_mm_sub_epi32(r,pv));
}
TGT static inline __m128i add4(__m128i a,__m128i b,__m128i pv){
__m128i s=_mm_add_epi32(a,b);
return _mm_min_epu32(s,_mm_sub_epi32(s,pv));
}
TGT static inline __m128i sub4(__m128i a,__m128i b,__m128i pv){
__m128i d=_mm_sub_epi32(a,b);
return _mm_min_epu32(d,_mm_add_epi32(d,pv));
}
TGT static inline __m256i ld2(const __m128i*lo,const __m128i*hi){
return _mm256_inserti128_si256(_mm256_castsi128_si256(_mm_loadu_si128(lo)),_mm_loadu_si128(hi),1);
}
TGT static inline void st2(__m128i*lo,__m128i*hi,__m256i v){
_mm_storeu_si128(lo,_mm256_castsi256_si128(v));
_mm_storeu_si128(hi,_mm256_extracti128_si256(v,1));
}
TGT static inline __m256i bc128(__m128i v){
return _mm256_inserti128_si256(_mm256_castsi128_si256(v),v,1);
}
TGT static inline __m256i vL(__m256i a,__m256i b,__m256i pv){
return _mm256_add_epi32(_mm256_sub_epi32(a,b),pv);
}
TGT static inline __m128i sub_lazy4(__m128i a,__m128i b,__m128i pv){
return _mm_add_epi32(_mm_sub_epi32(a,b),pv);
}
TGT static void stage4_dif_t(u32*a,u32 blk,u32 h,const u32*t1,const u32*t2,const u32*t3,const u32*s1,const u32*s2,const u32*s3,u32 ci,u32 cis){
const __m256i pv=_mm256_set1_epi32((int)NTP);
const __m256i cI=_mm256_set1_epi32((int)ci),cIs=_mm256_set1_epi32((int)cis);
for(u32 i=0;
i<blk;
i+=4*h){
u32*p=a+i;
u32 j=0;
for(;
j+16<=h;
j+=16){
__m256i x0=ldu256((p+j));
__m256i x1=ldu256((p+j+h));
__m256i x2=ldu256((p+j+2*h));
__m256i x3=ldu256((p+j+3*h));
__m256i y0=ldu256((p+j+8));
__m256i y1=ldu256((p+j+h+8));
__m256i y2=ldu256((p+j+2*h+8));
__m256i y3=ldu256((p+j+3*h+8));
__m256i A=vaddm(x0,x2,pv),B=vsubm(x0,x2,pv);
__m256i C=vaddm(x1,x3,pv),D=vL(x1,x3,pv);
__m256i A2=vaddm(y0,y2,pv),B2=vsubm(y0,y2,pv);
__m256i C2=vaddm(y1,y3,pv),D2=vL(y1,y3,pv);
__m256i u0=vaddm(A,C,pv),u2=vL(A,C,pv);
__m256i v0=vaddm(A2,C2,pv),v2=vL(A2,C2,pv);
__m256i E=vS(D,cI,cIs,pv);
__m256i F=vS(D2,cI,cIs,pv);
__m256i u1=_mm256_add_epi32(B,E),u3=vL(B,E,pv);
__m256i v1=_mm256_add_epi32(B2,F),v3=vL(B2,F,pv);
{
u1=mpa(u1,ldu256((t1+j)),pv);
}
{
u2=mpa(u2,ldu256((t2+j)),pv);
}
{
u3=mpa(u3,ldu256((t3+j)),pv);
}
{
v1=mpa(v1,ldu256((t1+j+8)),pv);
}
{
v2=mpa(v2,ldu256((t2+j+8)),pv);
}
{
v3=mpa(v3,ldu256((t3+j+8)),pv);
}
stu256((p+j),u0);
stu256((p+j+h),u1);
stu256((p+j+2*h),u2);
stu256((p+j+3*h),u3);
stu256((p+j+8),v0);
stu256((p+j+h+8),v1);
stu256((p+j+2*h+8),v2);
stu256((p+j+3*h+8),v3);
}
for(;
j+8<=h;
j+=8){
__builtin_prefetch(p+j+192);
__builtin_prefetch(p+j+h+192);
__builtin_prefetch(p+j+2*h+192);
__builtin_prefetch(p+j+3*h+192);
__m256i x0=ldu256((p+j));
__m256i x1=ldu256((p+j+h));
__m256i x2=ldu256((p+j+2*h));
__m256i x3=ldu256((p+j+3*h));
__m256i A=vaddm(x0,x2,pv),B=vsubm(x0,x2,pv);
__m256i C=vaddm(x1,x3,pv),D=vL(x1,x3,pv);
__m256i u0=vaddm(A,C,pv),u2=vL(A,C,pv);
__m256i E=vS(D,cI,cIs,pv);
__m256i u1=_mm256_add_epi32(B,E),u3=vL(B,E,pv);
{
u1=mpa(u1,ldu256((t1+j)),pv);
}
{
u2=mpa(u2,ldu256((t2+j)),pv);
}
{
u3=mpa(u3,ldu256((t3+j)),pv);
}
stu256((p+j),u0);
stu256((p+j+h),u1);
stu256((p+j+2*h),u2);
stu256((p+j+3*h),u3);
}
}
}
template<int H>TGT static void stage4_dif_h(u32*a,u32 blk,u32 _h,const u32*tb,u32 ci,u32 cis){
const u32 MP=H+TPAD;
const __m256i pv=_mm256_set1_epi32((int)NTP);
const __m256i cI=_mm256_set1_epi32((int)ci),cIs=_mm256_set1_epi32((int)cis);
for(u32 i=0;
i<blk;
i+=4*H){
u32*q0=a+i;
const u32*w=tb;
u32 j=0;
for(;
j+16<=H;
j+=16,q0+=16,w+=16){
__builtin_prefetch(q0+192);
__builtin_prefetch(q0+H+192);
__builtin_prefetch(q0+2*H+192);
__builtin_prefetch(q0+3*H+192);
__m256i x0=ldu256(q0),x1=ldu256(q0+H),x2=ldu256(q0+2*H),x3=ldu256(q0+3*H);
__m256i y0=ldu256(q0+8),y1=ldu256(q0+H+8),y2=ldu256(q0+2*H+8),y3=ldu256(q0+3*H+8);
__m256i A=vaddm(x0,x2,pv),B=vsubm(x0,x2,pv);
__m256i C=vaddm(x1,x3,pv),D=vL(x1,x3,pv);
__m256i A2=vaddm(y0,y2,pv),B2=vsubm(y0,y2,pv);
__m256i C2=vaddm(y1,y3,pv),D2=vL(y1,y3,pv);
__m256i u0=vaddm(A,C,pv),u2=vL(A,C,pv);
__m256i v0=vaddm(A2,C2,pv),v2=vL(A2,C2,pv);
__m256i E=vS(D,cI,cIs,pv),F=vS(D2,cI,cIs,pv);
__m256i u1=_mm256_add_epi32(B,E),u3=vL(B,E,pv);
__m256i v1=_mm256_add_epi32(B2,F),v3=vL(B2,F,pv);
u1=mpb(u1,ldu256(w),ldu256(w+3*MP),pv);
u2=mpb(u2,ldu256(w+MP),ldu256(w+4*MP),pv);
u3=mpb(u3,ldu256(w+2*MP),ldu256(w+5*MP),pv);
v1=mpb(v1,ldu256(w+8),ldu256(w+3*MP+8),pv);
v2=mpb(v2,ldu256(w+MP+8),ldu256(w+4*MP+8),pv);
v3=mpb(v3,ldu256(w+2*MP+8),ldu256(w+5*MP+8),pv);
stu256(q0,u0);
stu256(q0+H,u1);
stu256(q0+2*H,u2);
stu256(q0+3*H,u3);
stu256(q0+8,v0);
stu256(q0+H+8,v1);
stu256(q0+2*H+8,v2);
stu256(q0+3*H+8,v3);
}
for(;
j+8<=H;
j+=8,q0+=8,w+=8){
__builtin_prefetch(q0+192);
__builtin_prefetch(q0+H+192);
__builtin_prefetch(q0+2*H+192);
__builtin_prefetch(q0+3*H+192);
__m256i x0=ldu256(q0),x1=ldu256(q0+H),x2=ldu256(q0+2*H),x3=ldu256(q0+3*H);
__m256i A=vaddm(x0,x2,pv),B=vsubm(x0,x2,pv);
__m256i C=vaddm(x1,x3,pv),D=vL(x1,x3,pv);
__m256i u0=vaddm(A,C,pv),u2=vL(A,C,pv);
__m256i E=vS(D,cI,cIs,pv);
__m256i u1=_mm256_add_epi32(B,E),u3=vL(B,E,pv);
u1=mpb(u1,ldu256(w),ldu256(w+3*MP),pv);
u2=mpb(u2,ldu256(w+MP),ldu256(w+4*MP),pv);
u3=mpb(u3,ldu256(w+2*MP),ldu256(w+5*MP),pv);
stu256(q0,u0);
stu256(q0+H,u1);
stu256(q0+2*H,u2);
stu256(q0+3*H,u3);
}
}
}
TGT static inline void transpose4(__m256i&a,__m256i&b,__m256i&c,__m256i&d){
__m256i t0=_mm256_unpacklo_epi32(a,b),t1=_mm256_unpackhi_epi32(a,b),t2=_mm256_unpacklo_epi32(c,d),t3=_mm256_unpackhi_epi32(c,d);
a=_mm256_unpacklo_epi64(t0,t2);
b=_mm256_unpackhi_epi64(t0,t2);
c=_mm256_unpacklo_epi64(t1,t3);
d=_mm256_unpackhi_epi64(t1,t3);
}
TGT static void dh1(u32*a,u32 blk,u32 ci,u32 cis);
static long g_pmdelta=0;
#define E7Z3_PM_ON() (g_pmdelta != 0)
TGT static inline __m256i e7z3_minn(__m256i u,__m256i pv){
return _mm256_min_epu32(u,_mm256_sub_epi32(u,pv));
}
TGT static inline __m256i e7z3_pmn(__m256i u,const u32*p,__m256i pv){
return mpa(ld2((const __m128i*)(p),(const __m128i*)(p+16)),u,pv);
}
TGT __attribute__((always_inline))static inline void dh4(u32*a,u32 nsub,const u32*t1,const u32*t2,const u32*t3,const u32*s1,const u32*s2,const u32*s3,u32 ci,u32 cis){
const __m256i pv=_mm256_set1_epi32((int)NTP);
const __m256i cI=_mm256_set1_epi32((int)ci),cIs=_mm256_set1_epi32((int)cis);
if(nsub<32){
const __m128i pv4=_mm256_castsi256_si128(pv),cI4=_mm256_castsi256_si128(cI),cIs4=_mm256_castsi256_si128(cIs);
__m128i W1=_mm_loadu_si128((const __m128i*)t1);
__m128i WS1=_mm_loadu_si128((const __m128i*)s1);
__m128i W2=_mm_loadu_si128((const __m128i*)t2);
__m128i WS2=_mm_loadu_si128((const __m128i*)s2);
__m128i W3=_mm_loadu_si128((const __m128i*)t3);
__m128i WS3=_mm_loadu_si128((const __m128i*)s3);
for(u32 i=0;
i<nsub;
i+=16){
u32*p=a+i;
__m128i x0=_mm_loadu_si128((const __m128i*)(p));
__m128i x1=_mm_loadu_si128((const __m128i*)(p+4));
__m128i x2=_mm_loadu_si128((const __m128i*)(p+8));
__m128i x3=_mm_loadu_si128((const __m128i*)(p+12));
__m128i A=add4(x0,x2,pv4),B=sub4(x0,x2,pv4);
__m128i C=add4(x1,x3,pv4),D=sub_lazy4(x1,x3,pv4);
__m128i u0=add4(A,C,pv4),u2=sub_lazy4(A,C,pv4);
__m128i E=sh4(D,cI4,cIs4,pv4);
__m128i u1=_mm_add_epi32(B,E),u3=sub_lazy4(B,E,pv4);
u1=mpc(u1,W1,WS1,pv4);
u2=mpc(u2,W2,WS2,pv4);
u3=mpc(u3,W3,WS3,pv4);
_mm_storeu_si128((__m128i*)(p),u0);
_mm_storeu_si128((__m128i*)(p+4),u1);
_mm_storeu_si128((__m128i*)(p+8),u2);
_mm_storeu_si128((__m128i*)(p+12),u3);
}
dh1(a,nsub,ci,cis);
return;
}
__m256i W1=bc128(_mm_loadu_si128((const __m128i*)t1));
__m256i WS1=bc128(_mm_loadu_si128((const __m128i*)s1));
__m256i W2=bc128(_mm_loadu_si128((const __m128i*)t2));
__m256i WS2=bc128(_mm_loadu_si128((const __m128i*)s2));
__m256i W3=bc128(_mm_loadu_si128((const __m128i*)t3));
__m256i WS3=bc128(_mm_loadu_si128((const __m128i*)s3));
if(g_pmdelta==0){
#pragma GCC unroll 2
for(u32 i=0,*p=a;
i<nsub;
i+=32,p+=32){
__m256i x0=ld2((const __m128i*)(p),(const __m128i*)(p+16));
__m256i x1=ld2((const __m128i*)(p+4),(const __m128i*)(p+20));
__m256i x2=ld2((const __m128i*)(p+8),(const __m128i*)(p+24));
__m256i x3=ld2((const __m128i*)(p+12),(const __m128i*)(p+28));
__m256i A=vaddm(x0,x2,pv),B=vsubm(x0,x2,pv);
__m256i C=vaddm(x1,x3,pv),D=vL(x1,x3,pv);
__m256i u0=vaddm(A,C,pv),u2=vL(A,C,pv);
__m256i E=vS(D,cI,cIs,pv);
__m256i u1=_mm256_add_epi32(B,E),u3=vL(B,E,pv);
u1=mpb(u1,W1,WS1,pv);
u2=mpb(u2,W2,WS2,pv);
u3=mpb(u3,W3,WS3,pv);
transpose4(u0,u1,u2,u3);
{
__m256i A4=vaddm(u0,u2,pv),B4=vsubm(u0,u2,pv),C4=vaddm(u1,u3,pv),D4=vL(u1,u3,pv),E4=vS(D4,cI,cIs,pv);
u0=vaddm(A4,C4,pv);
u2=vL(A4,C4,pv);
u1=_mm256_add_epi32(B4,E4);
u3=vL(B4,E4,pv);
transpose4(u0,u1,u2,u3);
}
u0=e7z3_minn(u0,pv);
u1=e7z3_minn(u1,pv);
u2=e7z3_minn(u2,pv);
u3=e7z3_minn(u3,pv);
st2((__m128i*)(p),(__m128i*)(p+16),u0);
st2((__m128i*)(p+4),(__m128i*)(p+20),u1);
st2((__m128i*)(p+8),(__m128i*)(p+24),u2);
st2((__m128i*)(p+12),(__m128i*)(p+28),u3);
}
}
else{
#pragma GCC unroll 2
for(u32 i=0,*p=a,*pd=(u32*)((char*)a+g_pmdelta);
i<nsub;
i+=32,p+=32,pd+=32){
__m256i x0=ld2((const __m128i*)(p),(const __m128i*)(p+16));
__m256i x1=ld2((const __m128i*)(p+4),(const __m128i*)(p+20));
__m256i x2=ld2((const __m128i*)(p+8),(const __m128i*)(p+24));
__m256i x3=ld2((const __m128i*)(p+12),(const __m128i*)(p+28));
__m256i A=vaddm(x0,x2,pv),B=vsubm(x0,x2,pv);
__m256i C=vaddm(x1,x3,pv),D=vL(x1,x3,pv);
__m256i u0=vaddm(A,C,pv),u2=vL(A,C,pv);
__m256i E=vS(D,cI,cIs,pv);
__m256i u1=_mm256_add_epi32(B,E),u3=vL(B,E,pv);
u1=mpb(u1,W1,WS1,pv);
u2=mpb(u2,W2,WS2,pv);
u3=mpb(u3,W3,WS3,pv);
transpose4(u0,u1,u2,u3);
{
__m256i A4=vaddm(u0,u2,pv),B4=vsubm(u0,u2,pv),C4=vaddm(u1,u3,pv),D4=vL(u1,u3,pv),E4=vS(D4,cI,cIs,pv);
u0=vaddm(A4,C4,pv);
u2=vL(A4,C4,pv);
u1=_mm256_add_epi32(B4,E4);
u3=vL(B4,E4,pv);
transpose4(u0,u1,u2,u3);
}
u0=e7z3_pmn(u0,pd,pv);
u1=e7z3_pmn(u1,pd+4,pv);
u2=e7z3_pmn(u2,pd+8,pv);
u3=e7z3_pmn(u3,pd+12,pv);
st2((__m128i*)(pd),(__m128i*)(pd+16),u0);
st2((__m128i*)(pd+4),(__m128i*)(pd+20),u1);
st2((__m128i*)(pd+8),(__m128i*)(pd+24),u2);
st2((__m128i*)(pd+12),(__m128i*)(pd+28),u3);
}
}
}
TGT static void dh1(u32*a,u32 blk,u32 ci,u32 cis){
const __m256i pv=_mm256_set1_epi32((int)NTP);
const __m256i cI=_mm256_set1_epi32((int)ci),cIs=_mm256_set1_epi32((int)cis);
for(u32 i=0;
i<blk;
i+=8){
u32*p=a+i;
__m256i x=ldu256(p);
__m256i t=_mm256_shuffle_epi32(x,_MM_SHUFFLE(1,0,3,2));
__m256i s=vaddm(x,t,pv);
__m256i d=vsubm(x,t,pv);
__m256i Z=_mm256_shuffle_epi32(d,0x00);
__m256i P=_mm256_blend_epi32(s,Z,0xAA);
__m256i E=vS(d,cI,cIs,pv);
__m256i E2=_mm256_shuffle_epi32(E,0x55);
__m256i W=_mm256_shuffle_epi32(s,0x55);
__m256i Q=_mm256_blend_epi32(W,E2,0xAA);
__m256i sum=vaddm(P,Q,pv);
__m256i dif=vsubm(P,Q,pv);
stu256(p,_mm256_blend_epi32(sum,dif,0xCC));
}
}
TGT static void stage2_dif(u32*a,u32 blk,const u32*t,const u32*s){
const __m256i pv=_mm256_set1_epi32((int)NTP);
u32 h=blk>>1;
for(u32 j=0;
j<h;
j+=8){
__m256i x0=ldu256((a+j));
__m256i x1=ldu256((a+j+h));
__m256i u0=vaddm(x0,x1,pv);
__m256i u1=mpa(vsubm(x0,x1,pv),ldu256((t+j)),pv);
stu256((a+j),u0);
stu256((a+j+h),u1);
}
}
TGT static void merge4_dit_t(u32*a,u32 blk,u32 h,const u32*t1,const u32*t2,const u32*t3,const u32*s1,const u32*s2,const u32*s3,u32 ci,u32 cis){
const __m256i pv=_mm256_set1_epi32((int)NTP);
const __m256i cI=_mm256_set1_epi32((int)ci),cIs=_mm256_set1_epi32((int)cis);
for(u32 i=0;
i<blk;
i+=4*h){
u32*p=a+i;
for(u32 j=0;
j<h;
j+=8){
__m256i x0=ldu256((p+j));
__m256i x1=ldu256((p+j+h));
__m256i x2=ldu256((p+j+2*h));
__m256i x3=ldu256((p+j+3*h));
{
x1=mpa(x1,ldu256((t1+j)),pv);
}
{
x2=mpa(x2,ldu256((t2+j)),pv);
}
{
x3=mpa(x3,ldu256((t3+j)),pv);
}
__m256i A=vaddm(x0,x2,pv),B=vsubm(x0,x2,pv);
__m256i C=vaddm(x1,x3,pv),D=vL(x1,x3,pv);
__m256i E=vS(D,cI,cIs,pv);
stu256((p+j),vaddm(A,C,pv));
stu256((p+j+h),vaddm(B,E,pv));
stu256((p+j+2*h),vsubm(A,C,pv));
stu256((p+j+3*h),vsubm(B,E,pv));
}
}
}
template<int H,bool LazyOutput=false>TGT static void merge4_dit_h(u32*a,u32 blk,u32 _h,const u32*t1,const u32*t2,const u32*t3,const u32*s1,const u32*s2,const u32*s3,u32 ci,u32 cis){
const u32 MP=H+TPAD;
const __m256i pv=_mm256_set1_epi32((int)NTP);
const __m256i cI=_mm256_set1_epi32((int)ci),cIs=_mm256_set1_epi32((int)cis);
(void)t2;
(void)t3;
(void)s1;
(void)s2;
(void)s3;
for(u32 i=0;
i<blk;
i+=4*H){
u32*q0=a+i;
const u32*w=t1;
u32 j=0;
for(;
j<H;
j+=16,q0+=16,w+=16){
__builtin_prefetch(q0+192);
__builtin_prefetch(q0+H+192);
__builtin_prefetch(q0+2*H+192);
__builtin_prefetch(q0+3*H+192);
__m256i x0=ldu256(q0),y0=ldu256(q0+8);
__m256i x1=ldu256(q0+H),y1=ldu256(q0+H+8);
__m256i x2=ldu256(q0+2*H),y2=ldu256(q0+2*H+8);
__m256i x3=ldu256(q0+3*H),y3=ldu256(q0+3*H+8);
x1=mpb(x1,ldu256(w),ldu256(w+3*MP),pv);
y1=mpb(y1,ldu256(w+8),ldu256(w+3*MP+8),pv);
x2=mpb(x2,ldu256(w+MP),ldu256(w+4*MP),pv);
y2=mpb(y2,ldu256(w+MP+8),ldu256(w+4*MP+8),pv);
x3=mpb(x3,ldu256(w+2*MP),ldu256(w+5*MP),pv);
y3=mpb(y3,ldu256(w+2*MP+8),ldu256(w+5*MP+8),pv);
__m256i A=vaddm(x0,x2,pv),B=vsubm(x0,x2,pv);
__m256i A2=vaddm(y0,y2,pv),B2=vsubm(y0,y2,pv);
__m256i C=vaddm(x1,x3,pv),D=vL(x1,x3,pv);
__m256i C2=vaddm(y1,y3,pv),D2=vL(y1,y3,pv);
__m256i E=vS(D,cI,cIs,pv),F=vS(D2,cI,cIs,pv);
stu256(q0,(LazyOutput?_mm256_add_epi32(A,C):vaddm(A,C,pv)));
stu256(q0+8,(LazyOutput?_mm256_add_epi32(A2,C2):vaddm(A2,C2,pv)));
stu256(q0+H,(LazyOutput?_mm256_add_epi32(B,E):vaddm(B,E,pv)));
stu256(q0+H+8,(LazyOutput?_mm256_add_epi32(B2,F):vaddm(B2,F,pv)));
stu256(q0+2*H,(LazyOutput?vL(A,C,pv):vsubm(A,C,pv)));
stu256(q0+2*H+8,(LazyOutput?vL(A2,C2,pv):vsubm(A2,C2,pv)));
stu256(q0+3*H,(LazyOutput?vL(B,E,pv):vsubm(B,E,pv)));
stu256(q0+3*H+8,(LazyOutput?vL(B2,F,pv):vsubm(B2,F,pv)));
}
for(;
j<H;
j+=8,q0+=8,w+=8){
__m256i x0=ldu256(q0),x1=ldu256(q0+H),x2=ldu256(q0+2*H),x3=ldu256(q0+3*H);
x1=mpb(x1,ldu256(w),ldu256(w+3*MP),pv);
x2=mpb(x2,ldu256(w+MP),ldu256(w+4*MP),pv);
x3=mpb(x3,ldu256(w+2*MP),ldu256(w+5*MP),pv);
__m256i A=vaddm(x0,x2,pv),B=vsubm(x0,x2,pv);
__m256i C=vaddm(x1,x3,pv),D=vL(x1,x3,pv);
__m256i E=vS(D,cI,cIs,pv);
stu256(q0,(LazyOutput?_mm256_add_epi32(A,C):vaddm(A,C,pv)));
stu256(q0+H,(LazyOutput?_mm256_add_epi32(B,E):vaddm(B,E,pv)));
stu256(q0+2*H,(LazyOutput?vL(A,C,pv):vsubm(A,C,pv)));
stu256(q0+3*H,(LazyOutput?vL(B,E,pv):vsubm(B,E,pv)));
}
}
}
TGT static void merge4_dit_h4(u32*a,u32 nsub,const u32*t1,const u32*t2,const u32*t3,const u32*s1,const u32*s2,const u32*s3,u32 ci,u32 cis){
const __m256i pv=_mm256_set1_epi32((int)NTP);
const __m256i cI=_mm256_set1_epi32((int)ci),cIs=_mm256_set1_epi32((int)cis);
if(nsub<32){
const __m128i pv4=_mm256_castsi256_si128(pv),cI4=_mm256_castsi256_si128(cI),cIs4=_mm256_castsi256_si128(cIs);
__m128i W1=_mm_loadu_si128((const __m128i*)t1);
__m128i WS1=_mm_loadu_si128((const __m128i*)s1);
__m128i W2=_mm_loadu_si128((const __m128i*)t2);
__m128i WS2=_mm_loadu_si128((const __m128i*)s2);
__m128i W3=_mm_loadu_si128((const __m128i*)t3);
__m128i WS3=_mm_loadu_si128((const __m128i*)s3);
for(u32 i=0;
i<nsub;
i+=16){
u32*p=a+i;
__m128i x0=_mm_loadu_si128((const __m128i*)(p));
__m128i x1=mpc(_mm_loadu_si128((const __m128i*)(p+4)),W1,WS1,pv4);
__m128i x2=mpc(_mm_loadu_si128((const __m128i*)(p+8)),W2,WS2,pv4);
__m128i x3=mpc(_mm_loadu_si128((const __m128i*)(p+12)),W3,WS3,pv4);
__m128i A=add4(x0,x2,pv4),B=sub4(x0,x2,pv4);
__m128i C=add4(x1,x3,pv4),D=sub4(x1,x3,pv4);
__m128i E=sh4(D,cI4,cIs4,pv4);
_mm_storeu_si128((__m128i*)(p),add4(A,C,pv4));
_mm_storeu_si128((__m128i*)(p+4),add4(B,E,pv4));
_mm_storeu_si128((__m128i*)(p+8),sub4(A,C,pv4));
_mm_storeu_si128((__m128i*)(p+12),sub4(B,E,pv4));
}
return;
}
__m256i W1=bc128(_mm_loadu_si128((const __m128i*)t1));
__m256i WS1=bc128(_mm_loadu_si128((const __m128i*)s1));
__m256i W2=bc128(_mm_loadu_si128((const __m128i*)t2));
__m256i WS2=bc128(_mm_loadu_si128((const __m128i*)s2));
__m256i W3=bc128(_mm_loadu_si128((const __m128i*)t3));
__m256i WS3=bc128(_mm_loadu_si128((const __m128i*)s3));
for(u32 i=0;
i<nsub;
i+=32){
u32*p=a+i;
__m256i x0=ld2((const __m128i*)(p),(const __m128i*)(p+16));
__m256i x1=ld2((const __m128i*)(p+4),(const __m128i*)(p+20));
__m256i x2=ld2((const __m128i*)(p+8),(const __m128i*)(p+24));
__m256i x3=ld2((const __m128i*)(p+12),(const __m128i*)(p+28));
x1=mpb(x1,W1,WS1,pv);
x2=mpb(x2,W2,WS2,pv);
x3=mpb(x3,W3,WS3,pv);
__m256i A=vaddm(x0,x2,pv),B=vsubm(x0,x2,pv);
__m256i C=vaddm(x1,x3,pv),D=vL(x1,x3,pv);
__m256i E=vS(D,cI,cIs,pv);
st2((__m128i*)(p),(__m128i*)(p+16),vaddm(A,C,pv));
st2((__m128i*)(p+4),(__m128i*)(p+20),vaddm(B,E,pv));
st2((__m128i*)(p+8),(__m128i*)(p+24),vsubm(A,C,pv));
st2((__m128i*)(p+12),(__m128i*)(p+28),vsubm(B,E,pv));
}
}
TGT static inline __m256i merge4_h1v(__m256i x,__m256i pv,__m256i cI,__m256i cIs){
__m256i t=_mm256_shuffle_epi32(x,_MM_SHUFFLE(1,0,3,2)),sv=vaddm(x,t,pv),d=vsubm(x,t,pv),Z=_mm256_shuffle_epi32(d,0x00),P=_mm256_blend_epi32(sv,Z,0xAA),E=vS(d,cI,cIs,pv),E2=_mm256_shuffle_epi32(E,0x55),W=_mm256_shuffle_epi32(sv,0x55),Q=_mm256_blend_epi32(W,E2,0xAA),sum=vaddm(P,Q,pv),dif=vsubm(P,Q,pv);
return _mm256_blend_epi32(sum,dif,0xCC);
}
TGT static inline __m256i merge4_h1v_lazy(__m256i x,__m256i pv,__m256i cI,__m256i cIs){
__m256i t=_mm256_shuffle_epi32(x,_MM_SHUFFLE(1,0,3,2)),sv=vaddm(x,t,pv),d=vsubm(x,t,pv),Z=_mm256_shuffle_epi32(d,0x00),P=_mm256_blend_epi32(sv,Z,0xAA),E=vS(d,cI,cIs,pv),E2=_mm256_shuffle_epi32(E,0x55),W=_mm256_shuffle_epi32(sv,0x55),Q=_mm256_blend_epi32(W,E2,0xAA),sum=_mm256_add_epi32(P,Q),dif=vL(P,Q,pv);
return _mm256_blend_epi32(sum,dif,0xCC);
}
TGT static void mhh(u32*a,u32 nsub,const u32*t1,const u32*t2,const u32*t3,const u32*s1,const u32*s2,const u32*s3,u32 ci,u32 cis){
const __m256i pv=_mm256_set1_epi32((int)NTP),cI=_mm256_set1_epi32((int)ci),cIs=_mm256_set1_epi32((int)cis);
__m256i W1=bc128(_mm_loadu_si128((const __m128i*)t1)),WS1=bc128(_mm_loadu_si128((const __m128i*)s1)),W2=bc128(_mm_loadu_si128((const __m128i*)t2)),WS2=bc128(_mm_loadu_si128((const __m128i*)s2)),W3=bc128(_mm_loadu_si128((const __m128i*)t3)),WS3=bc128(_mm_loadu_si128((const __m128i*)s3));
for(u32 i=0;
i+32<=nsub;
i+=32){
u32*p=a+i;
__m256i g0=merge4_h1v(ldu256((const void*)(p)),pv,cI,cIs),g1=merge4_h1v(ldu256((const void*)(p+8)),pv,cI,cIs),g2=merge4_h1v(ldu256((const void*)(p+16)),pv,cI,cIs),g3=merge4_h1v(ldu256((const void*)(p+24)),pv,cI,cIs);
__m256i x0=_mm256_permute2x128_si256(g0,g2,0x20),x1=_mm256_permute2x128_si256(g0,g2,0x31),x2=_mm256_permute2x128_si256(g1,g3,0x20),x3=_mm256_permute2x128_si256(g1,g3,0x31);
x1=mpb(x1,W1,WS1,pv);
x2=mpb(x2,W2,WS2,pv);
x3=mpb(x3,W3,WS3,pv);
__m256i A=vaddm(x0,x2,pv),B=vsubm(x0,x2,pv),C=vaddm(x1,x3,pv),D=vL(x1,x3,pv),E=vS(D,cI,cIs,pv);
st2((__m128i*)(p),(__m128i*)(p+16),vaddm(A,C,pv));
st2((__m128i*)(p+4),(__m128i*)(p+20),vaddm(B,E,pv));
st2((__m128i*)(p+8),(__m128i*)(p+24),vsubm(A,C,pv));
st2((__m128i*)(p+12),(__m128i*)(p+28),vsubm(B,E,pv));
}
for(u32 i=(nsub&~31u);
i<nsub;
i+=8){
u32*p=a+i;
stu256((void*)p,merge4_h1v(ldu256((const void*)p),pv,cI,cIs));
}
}
#define H4B32(Q,R1,R2,R3,R4,LAZY) {const u32*q=(Q);__m256i g0=merge4_h1v(ldu256((const void*)(q)),pv,cI,cIs),g1=merge4_h1v_lazy(ldu256((const void*)(q+8)),pv,cI,cIs),g2=merge4_h1v(ldu256((const void*)(q+16)),pv,cI,cIs),g3=merge4_h1v_lazy(ldu256((const void*)(q+24)),pv,cI,cIs);__m256i e0=_mm256_permute2x128_si256(g0,g2,0x20),e1=_mm256_permute2x128_si256(g0,g2,0x31),e2=_mm256_permute2x128_si256(g1,g3,0x20),e3=_mm256_permute2x128_si256(g1,g3,0x31);e1=mpb(e1,W1,WS1,pv);e2=mpb(e2,W2,WS2,pv);e3=mpb(e3,W3,WS3,pv);__m256i a1=vaddm(e0,e2,pv),b1=vsubm(e0,e2,pv),c1=vaddm(e1,e3,pv),d1=vL(e1,e3,pv),f1=vS(d1,cI,cIs,pv);if constexpr(LAZY){R1=_mm256_add_epi32(a1,c1);R2=_mm256_add_epi32(b1,f1);R3=vL(a1,c1,pv);R4=vL(b1,f1,pv);}else{R1=vaddm(a1,c1,pv);R2=vaddm(b1,f1,pv);R3=vsubm(a1,c1,pv);R4=vsubm(b1,f1,pv);}}
TGT __attribute__((always_inline))static inline void mhh64(u32*a,u32 nsub,const u32*t1,const u32*t2,const u32*t3,const u32*s1,const u32*s2,const u32*s3,u32 ci,u32 cis,const u32*u1,const u32*u2,const u32*u3,const u32*v1,const u32*v2,const u32*v3){
const __m256i pv=_mm256_set1_epi32((int)NTP),cI=_mm256_set1_epi32((int)ci),cIs=_mm256_set1_epi32((int)cis);
const __m256i W1=bc128(_mm_loadu_si128((const __m128i*)t1)),WS1=bc128(_mm_loadu_si128((const __m128i*)s1)),W2=bc128(_mm_loadu_si128((const __m128i*)t2)),WS2=bc128(_mm_loadu_si128((const __m128i*)s2)),W3=bc128(_mm_loadu_si128((const __m128i*)t3)),WS3=bc128(_mm_loadu_si128((const __m128i*)s3));
for(u32 i=0,*p=a;
i+64<=nsub;
i+=64,p+=64){
__m256i R1,R2,R3,R4,R1b,R2b,R3b,R4b;
H4B32(p,R1,R2,R3,R4,false)H4B32(p+32,R1b,R2b,R3b,R4b,true)__m256i x0=_mm256_permute2x128_si256(R1,R2,0x20),y0=_mm256_permute2x128_si256(R3,R4,0x20),x1=_mm256_permute2x128_si256(R1,R2,0x31),y1=_mm256_permute2x128_si256(R3,R4,0x31),x2=_mm256_permute2x128_si256(R1b,R2b,0x20),y2=_mm256_permute2x128_si256(R3b,R4b,0x20),x3=_mm256_permute2x128_si256(R1b,R2b,0x31),y3=_mm256_permute2x128_si256(R3b,R4b,0x31);
x1=mpb(x1,ldu256((const void*)(u1)),ldu256((const void*)(v1)),pv);
y1=mpb(y1,ldu256((const void*)(u1+8)),ldu256((const void*)(v1+8)),pv);
x2=mpb(x2,ldu256((const void*)(u2)),ldu256((const void*)(v2)),pv);
y2=mpb(y2,ldu256((const void*)(u2+8)),ldu256((const void*)(v2+8)),pv);
x3=mpb(x3,ldu256((const void*)(u3)),ldu256((const void*)(v3)),pv);
y3=mpb(y3,ldu256((const void*)(u3+8)),ldu256((const void*)(v3+8)),pv);
__m256i A=vaddm(x0,x2,pv),B=vsubm(x0,x2,pv),A2=vaddm(y0,y2,pv),B2=vsubm(y0,y2,pv),C=vaddm(x1,x3,pv),D=vL(x1,x3,pv),C2=vaddm(y1,y3,pv),D2=vL(y1,y3,pv),E=vS(D,cI,cIs,pv),F=vS(D2,cI,cIs,pv);
if(nsub>=256u && (nsub&255u)==0u && (i&192u)!=0u){
stu256((void*)(p),_mm256_add_epi32(A,C));
stu256((void*)(p+8),_mm256_add_epi32(A2,C2));
stu256((void*)(p+16),_mm256_add_epi32(B,E));
stu256((void*)(p+24),_mm256_add_epi32(B2,F));
stu256((void*)(p+32),vL(A,C,pv));
stu256((void*)(p+40),vL(A2,C2,pv));
stu256((void*)(p+48),vL(B,E,pv));
stu256((void*)(p+56),vL(B2,F,pv));
}
else{
stu256((void*)(p),vaddm(A,C,pv));
stu256((void*)(p+8),vaddm(A2,C2,pv));
stu256((void*)(p+16),vaddm(B,E,pv));
stu256((void*)(p+24),vaddm(B2,F,pv));
stu256((void*)(p+32),vsubm(A,C,pv));
stu256((void*)(p+40),vsubm(A2,C2,pv));
stu256((void*)(p+48),vsubm(B,E,pv));
stu256((void*)(p+56),vsubm(B2,F,pv));
}
}
for(u32 i=(nsub&~63u);
i<nsub;
i+=8){
u32*p=a+i;
stu256((void*)p,merge4_h1v(ldu256((const void*)p),pv,cI,cIs));
}
}
TGT static void mh1(u32*a,u32 blk,u32 ci,u32 cis){
const __m256i pv=_mm256_set1_epi32((int)NTP);
const __m256i cI=_mm256_set1_epi32((int)ci),cIs=_mm256_set1_epi32((int)cis);
for(u32 i=0;
i<blk;
i+=8){
u32*p=a+i;
__m256i x=ldu256(p);
__m256i t=_mm256_shuffle_epi32(x,_MM_SHUFFLE(1,0,3,2));
__m256i s=vaddm(x,t,pv);
__m256i d=vsubm(x,t,pv);
__m256i Z=_mm256_shuffle_epi32(d,0x00);
__m256i P=_mm256_blend_epi32(s,Z,0xAA);
__m256i E=vS(d,cI,cIs,pv);
__m256i E2=_mm256_shuffle_epi32(E,0x55);
__m256i W=_mm256_shuffle_epi32(s,0x55);
__m256i Q=_mm256_blend_epi32(W,E2,0xAA);
__m256i sum=vaddm(P,Q,pv);
__m256i dif=vsubm(P,Q,pv);
stu256(p,_mm256_blend_epi32(sum,dif,0xCC));
}
}
TGT static void merge2_dit(u32*a,u32 blk,const u32*t,const u32*s){
const __m256i pv=_mm256_set1_epi32((int)NTP);
u32 h=blk>>1;
for(u32 j=0;
j<h;
j+=8){
__m256i x0=ldu256((a+j));
__m256i x1=mpa(ldu256((a+j+h)),ldu256((t+j)),pv);
stu256((a+j),vaddm(x0,x1,pv));
stu256((a+j+h),vsubm(x0,x1,pv));
}
}
TGT static inline void ntst256(void*p,__m256i v){
if(((uintptr_t)p&31u)==0)_mm256_stream_si256((__m256i*)p,v);
else stu256(p,v);
}
TGT static void gen_geo_val(u32*dst,u32 cnt,u32 v0,u32 r);
TGT static void gen_geo(u32*dst,u32*sc,u32 cnt,u32 v0,u32 r){
if(!sc){
gen_geo_val(dst,cnt,v0,r);
return;
}
if(!cnt)return;
if(cnt<32){
u32 x=v0%NTP;
for(u32 i=0;
i<cnt;
i++){
dst[i]=x;
sc[i]=shoup_c(x);
x=mulmod(x,r);
}
return;
}
const __m256i pv=_mm256_set1_epi32((int)NTP);
const __m256i mlo=_mm256_set1_epi32((int)g_shmagic);
u32 seq[32],cur=v0%NTP;
for(int i=0;
i<32;
i++){
seq[i]=cur;
cur=mulmod(cur,r);
}
if(v0!=1u)cur=powmod(r,32);
u32 rs=shoup_c(cur);
const __m256i rv=_mm256_set1_epi32((int)cur);
const __m256i rsv=_mm256_set1_epi32((int)rs);
__m256i a=ldu256(seq),b=ldu256(seq+8),c=ldu256(seq+16),d=ldu256(seq+24);
u32 i=0;
for(;
i+32<=cnt;
i+=32){
ntst256(dst+i,a);
ntst256(dst+i+8,b);
ntst256(dst+i+16,c);
ntst256(dst+i+24,d);
ntst256(sc+i,der_ws(a));
ntst256(sc+i+8,der_ws(b));
ntst256(sc+i+16,der_ws(c));
ntst256(sc+i+24,der_ws(d));
a=vS(a,rv,rsv,pv);
b=vS(b,rv,rsv,pv);
c=vS(c,rv,rsv,pv);
d=vS(d,rv,rsv,pv);
}
if(i<cnt){
u32 x=mulmod(v0,powmod(r,i));
for(;
i<cnt;
i++){
dst[i]=x;
sc[i]=shoup_c(x);
x=mulmod(x,r);
}
}
}
TGT static void gen_geo_val(u32*dst,u32 cnt,u32 v0,u32 r){
if(!cnt)return;
if(cnt<32){
u32 x=v0%NTP;
for(u32 i=0;
i<cnt;
i++){
dst[i]=x;
x=mulmod(x,r);
}
return;
}
const __m256i pv=_mm256_set1_epi32((int)NTP);
u32 seq[32],cur=v0%NTP;
for(int i=0;
i<32;
i++){
seq[i]=cur;
cur=mulmod(cur,r);
}
if(v0!=1u)cur=powmod(r,32);
if(v0!=1u)cur=powmod(r,32);
u32 rs=shoup_c(cur);
const __m256i rv=_mm256_set1_epi32((int)cur);
const __m256i rsv=_mm256_set1_epi32((int)rs);
__m256i a=ldu256(seq),b=ldu256(seq+8),c=ldu256(seq+16),d=ldu256(seq+24);
u32 i=0;
for(;
i+32<=cnt;
i+=32){
ntst256(dst+i,a);
ntst256(dst+i+8,b);
ntst256(dst+i+16,c);
ntst256(dst+i+24,d);
a=vS(a,rv,rsv,pv);
b=vS(b,rv,rsv,pv);
c=vS(c,rv,rsv,pv);
d=vS(d,rv,rsv,pv);
}
if(i<cnt){
u32 x=mulmod(v0,powmod(r,i));
for(;
i<cnt;
i++){
dst[i]=x;
x=mulmod(x,r);
}
}
}
static void build_tabs(Tabs&T,u32*tab,u32 n,u32 Sb,int L,int odd,u32 root,u32*scbase,u32 skipsc){
u32*p=tab;
u32*sp=scbase;
u32 S=n;
for(int l=0;
l<L;
l++){
u32 M=S>>2;
const u32 MPr=M+TPAD;
T .l1[l]=p;
T .l2[l]=p+MPr;
T .l3[l]=p+2*MPr;
T .m1[l]=sp;
T .m2[l]=sp+MPr;
T .m3[l]=sp+2*MPr;
u32 base=powmod(root,n/S);
u32 b1=base,b2=mulmod(base,base),b3=mulmod(b2,base);
{
const bool nosc=((skipsc>>l)&1u)!=0;
const u32 unit=(u32)((1ull<<32)%NTP);
gen_geo(p,nosc?0:sp,M,unit,b1);
gen_geo(p+MPr,nosc?0:sp+MPr,M,unit,b2);
gen_geo(p+2*MPr,nosc?0:sp+2*MPr,M,unit,b3);
}
p+=3*MPr;
sp+=3*MPr;
S>>=2;
}
T .b2=0;
T .b2s=0;
if(odd){
u32 M=Sb>>1;
T .b2=p;
T .b2s=p+M;
gen_geo(p,0,M,(u32)((1ull<<32)%NTP),powmod(root,n/Sb));
p+=2*M;
}
int s=0;
u32 S2v=4;
while(s<g_nb){
u32 M=S2v>>2;
if(M>=32)p=(u32*)(((uintptr_t)p+31u)&~(uintptr_t)31u);
const u32 MP=M+TPAD;
T .h1[s]=p;
T .h2[s]=p+MP;
T .h3[s]=p+2*MP;
T .q1[s]=p+3*MP;
T .q2[s]=p+4*MP;
T .q3[s]=p+5*MP;
u32 base=powmod(root,n/S2v);
u32 b1=base,b2=mulmod(base,base),b3=mulmod(b2,base);
gen_geo(p,0,M,(u32)((1ull<<32)%NTP),b1);
gen_geo(p+MP,0,M,(u32)((1ull<<32)%NTP),b2);
gen_geo(p+2*MP,0,M,(u32)((1ull<<32)%NTP),b3);
for(u32 i2=0;
i2<M;
i2++){
p[3*MP+i2]=(u32)((u64)p[i2]*(u64)g_pinv);
p[4*MP+i2]=(u32)((u64)p[MP+i2]*(u64)g_pinv);
p[5*MP+i2]=(u32)((u64)p[2*MP+i2]*(u64)g_pinv);
}
p+=6*MP;
S2v<<=2;
s++;
}
}
static void ntt_init(u32 n){
{
u32 iv=1;
for(int q=0;
q<5;
q++)iv*=2u-NTP*iv;
g_pinv=0u-iv;
}
g_n=n;
int lg=0;
while((1u<<lg)<n)lg++;
g_log2n=lg;
const int BT=g_BT;
int L=0;
while(lg-2*(L+1)>=BT)L++;
g_L=L;
g_Sb=n>>(2*L);
g_odd=((lg-2*L)&1)?1:0;
u32 S2=g_Sb;
if(g_odd)S2>>=1;
g_nb=0;
while(S2>=4){
S2>>=2;
g_nb++;
}
u32 w=powmod(g_generator,(NTP-1)/n);
u32 wi=powmod(w,NTP-2);
u32 iF=powmod(w,n>>2),iI=powmod(wi,n>>2);
g_ciF=iF;
g_cisF=shoup_c(iF);
g_ciI=iI;
g_cisI=shoup_c(iI);
build_tabs(g_TF,g_tabF,n,g_Sb,L,g_odd,w,Q_SCF,~0u);
build_tabs(g_TI,g_tabI,n,g_Sb,L,g_odd,wi,Q_SCI,~0u);
}
int g_fuse16=1;
#define E7_TILE 512u
static u32 g_scrA[16*E7_TILE]__attribute__((aligned(64)));
#ifndef E7Z_SBFUSE
#define E7Z_SBFUSE 1
#endif
#ifndef E7Z_IVFUSE
#define E7Z_IVFUSE 1
#endif
TGT __attribute__((always_inline))static inline void stage4_h16_loop(u32*a,u32 total,const u32*tb,u32 ci,u32 cis){
const u32 MP=16+TPAD;
const __m256i pv=_mm256_set1_epi32((int)NTP);
const __m256i cI=_mm256_set1_epi32((int)ci),cIs=_mm256_set1_epi32((int)cis);
for(u32 i=0;
i<total;
i+=64){
u32*q0=a+i;
const u32*w=tb;
u32 j=0;
for(;
j+16<=16;
j+=16,q0+=16,w+=16){
__m256i x0=ldu256(q0),x1=ldu256(q0+16),x2=ldu256(q0+32),x3=ldu256(q0+48);
__m256i y0=ldu256(q0+8),y1=ldu256(q0+24),y2=ldu256(q0+40),y3=ldu256(q0+56);
__m256i A=vaddm(x0,x2,pv),B=vsubm(x0,x2,pv);
__m256i C=vaddm(x1,x3,pv),D=vL(x1,x3,pv);
__m256i A2=vaddm(y0,y2,pv),B2=vsubm(y0,y2,pv);
__m256i C2=vaddm(y1,y3,pv),D2=vL(y1,y3,pv);
__m256i u0=vaddm(A,C,pv),u2=vL(A,C,pv);
__m256i v0=vaddm(A2,C2,pv),v2=vL(A2,C2,pv);
__m256i E=vS(D,cI,cIs,pv),F=vS(D2,cI,cIs,pv);
__m256i u1=_mm256_add_epi32(B,E),u3=vL(B,E,pv);
__m256i v1=_mm256_add_epi32(B2,F),v3=vL(B2,F,pv);
u1=mpb(u1,ldu256(w),ldu256(w+3*MP),pv);
u2=mpb(u2,ldu256(w+MP),ldu256(w+4*MP),pv);
u3=mpb(u3,ldu256(w+2*MP),ldu256(w+5*MP),pv);
v1=mpb(v1,ldu256(w+8),ldu256(w+3*MP+8),pv);
v2=mpb(v2,ldu256(w+MP+8),ldu256(w+4*MP+8),pv);
v3=mpb(v3,ldu256(w+2*MP+8),ldu256(w+5*MP+8),pv);
stu256(q0,u0);
stu256(q0+16,u1);
stu256(q0+32,u2);
stu256(q0+48,u3);
stu256(q0+8,v0);
stu256(q0+24,v1);
stu256(q0+40,v2);
stu256(q0+56,v3);
}
}
}
TGT static void fwd_sb_ring(u32*a,u32 b){
u32*p=a+padpos(b);
if(g_odd)stage2_dif(p,g_Sb,g_TF .b2,g_TF .b2s);
for(int s=g_nb-1;
s>=0;
s--){
u32 S2=4u<<(2*s);
if(S2>=32){
if(S2==16384){
for(u32 i=0;
i<g_Sb;
i+=S2)stage4_dif_h<4096>(p+i,16384,4096,g_TF .h1[s],g_ciF,g_cisF);
}
else if(S2==4096){
for(u32 i=0;
i<g_Sb;
i+=S2)stage4_dif_h<1024>(p+i,4096,1024,g_TF .h1[s],g_ciF,g_cisF);
}
else if(S2==1024){
for(u32 i=0;
i<g_Sb;
i+=S2)stage4_dif_h<256>(p+i,1024,256,g_TF .h1[s],g_ciF,g_cisF);
}
else if(S2==256){
for(u32 i=0;
i<g_Sb;
i+=S2)stage4_dif_h<64>(p+i,256,64,g_TF .h1[s],g_ciF,g_cisF);
}
else if(S2==64){
stage4_h16_loop(p,g_Sb,g_TF .h1[s],g_ciF,g_cisF);
}
else{
for(u32 i=0;
i<g_Sb;
i+=S2)stage4_dif_t(p+i,S2,S2>>2,g_TF .h1[s],g_TF .h2[s],g_TF .h3[s],g_TF .q1[s],g_TF .q2[s],g_TF .q3[s],g_ciF,g_cisF);
}
}
else if(S2==16){
dh4(p,g_Sb,g_TF .h1[s],g_TF .h2[s],g_TF .h3[s],g_TF .q1[s],g_TF .q2[s],g_TF .q3[s],g_ciF,g_cisF);
}
else{
}
}
}
TGT static void outer_dif(u32*a,u32 h,const u32*t1,const u32*t2,const u32*t3,u32 ci,u32 cis){
const __m256i p=_mm256_set1_epi32((int)NTP),imag=_mm256_set1_epi32(ci),imags=_mm256_set1_epi32(cis);
for(u32 j=0;j<h;j+=8){u32 off=padpos(j),s=padpos(h);__m256i x0=ldu256(a+off),x1=ldu256(a+s+off),x2=ldu256(a+padpos(2*h)+off),x3=ldu256(a+padpos(3*h)+off);
__m256i A=vaddm(x0,x2,p),B=vsubm(x0,x2,p),C=vaddm(x1,x3,p),D=vL(x1,x3,p),E=vS(D,imag,imags,p);
stu256(a+off,vaddm(A,C,p));stu256(a+s+off,mpa(_mm256_add_epi32(B,E),ldu256(t1+j),p));stu256(a+padpos(2*h)+off,mpa(vL(A,C,p),ldu256(t2+j),p));stu256(a+padpos(3*h)+off,mpa(vL(B,E,p),ldu256(t3+j),p));
}
}
TGT static void outer_dit(u32*a,u32 h,const u32*t1,const u32*t2,const u32*t3,u32 ci,u32 cis){
const __m256i p=_mm256_set1_epi32((int)NTP),imag=_mm256_set1_epi32(ci),imags=_mm256_set1_epi32(cis);
for(u32 j=0;j<h;j+=8){u32 off=padpos(j),s=padpos(h);__m256i x0=ldu256(a+off),x1=ldu256(a+s+off),x2=ldu256(a+padpos(2*h)+off),x3=ldu256(a+padpos(3*h)+off);
x0=_mm256_min_epu32(x0,_mm256_sub_epi32(x0,p));x1=_mm256_min_epu32(x1,_mm256_sub_epi32(x1,p));x2=_mm256_min_epu32(x2,_mm256_sub_epi32(x2,p));x3=_mm256_min_epu32(x3,_mm256_sub_epi32(x3,p));
x1=mpa(x1,ldu256(t1+j),p);x2=mpa(x2,ldu256(t2+j),p);x3=mpa(x3,ldu256(t3+j),p);
__m256i A=vaddm(x0,x2,p),B=vsubm(x0,x2,p),C=vaddm(x1,x3,p),D=vL(x1,x3,p),E=vS(D,imag,imags,p);
stu256(a+off,vaddm(A,C,p));stu256(a+s+off,vaddm(B,E,p));stu256(a+padpos(2*h)+off,vsubm(A,C,p));stu256(a+padpos(3*h)+off,vsubm(B,E,p));
}
}
TGT static void ntt_fwd_tail(u32*a,u32 S,int lstart,int l0);
TGT static void ntt_fwd(u32*a,const u32*src=0,u32 len=0){
(void)src;(void)len;for(int l=0;l<g_L;++l){u32 S=g_n>>(2*l),h=S/4;for(u32 b=0;b<g_n;b+=S)outer_dif(a+padpos(b),h,g_TF.l1[l],g_TF.l2[l],g_TF.l3[l],g_ciF,g_cisF);}
for(u32 b=0;b<g_n;b+=g_Sb)fwd_sb_ring(a,b);
}
TGT static void ntt_fwd_pair_batch(u32**aa,u32**bb,int nb,const long*deltas){(void)deltas;g_pmdelta=0;for(int i=0;i<nb;++i){ntt_fwd(aa[i]);ntt_fwd(bb[i]);}}
#define I44 512u
TGT static void ntt_inv_tail(u32*a,const u32*Bmul){
for(u32 b=0;
b<g_n;
b+=g_Sb){
u32*p=a+padpos(b);
if(Bmul&&!E7Z3_PM_ON()){
const __m256i pv=_mm256_set1_epi32((int)NTP);
const u32*q=Bmul+padpos(b);
for(u32 i=0;
i<g_Sb;
i+=8){
__m256i x=ldu256((p+i));
__m256i y=ldu256((q+i));
stu256((p+i),vS(x,y,der_ws(y),pv));
}
}
const int h1fused=(g_nb>=2&&g_Sb>=32u&&(g_Sb%32u)==0u);
const int s64fused=(h1fused&&g_nb>=3&&g_Sb>=64u&&(g_Sb%64u)==0u);
for(int s=0;
s<g_nb;
s++){
u32 S=4u<<(2*s);
if(S>=32){
if(S==16384){
for(u32 i=0;
i<g_Sb;
i+=S){
if(g_Sb==16384&&!g_odd&&g_L==4&&(b&(3*g_Sb)))merge4_dit_h<4096,true>(p+i,16384,4096,g_TI .h1[s],g_TI .h2[s],g_TI .h3[s],g_TI .q1[s],g_TI .q2[s],g_TI .q3[s],g_ciI,g_cisI);
else merge4_dit_h<4096>(p+i,16384,4096,g_TI .h1[s],g_TI .h2[s],g_TI .h3[s],g_TI .q1[s],g_TI .q2[s],g_TI .q3[s],g_ciI,g_cisI);
}
}
else if(S==4096){
for(u32 i=0;
i<g_Sb;
i+=S){
if(g_Sb>=4*S&&(i&(3*S))!=0)merge4_dit_h<1024,true>(p+i,4096,1024,g_TI .h1[s],g_TI .h2[s],g_TI .h3[s],g_TI .q1[s],g_TI .q2[s],g_TI .q3[s],g_ciI,g_cisI);
else merge4_dit_h<1024>(p+i,4096,1024,g_TI .h1[s],g_TI .h2[s],g_TI .h3[s],g_TI .q1[s],g_TI .q2[s],g_TI .q3[s],g_ciI,g_cisI);
}
}
else if(S==1024){
for(u32 i=0;
i<g_Sb;
i+=S){
if(g_Sb>=4*S&&(i&(3*S))!=0)merge4_dit_h<256,true>(p+i,1024,256,g_TI .h1[s],g_TI .h2[s],g_TI .h3[s],g_TI .q1[s],g_TI .q2[s],g_TI .q3[s],g_ciI,g_cisI);
else merge4_dit_h<256>(p+i,1024,256,g_TI .h1[s],g_TI .h2[s],g_TI .h3[s],g_TI .q1[s],g_TI .q2[s],g_TI .q3[s],g_ciI,g_cisI);
}
}
else if(S==256){
for(u32 i=0;
i<g_Sb;
i+=S){
if(g_Sb>=4*S&&(i&(3*S))!=0)merge4_dit_h<64,true>(p+i,256,64,g_TI .h1[s],g_TI .h2[s],g_TI .h3[s],g_TI .q1[s],g_TI .q2[s],g_TI .q3[s],g_ciI,g_cisI);
else merge4_dit_h<64>(p+i,256,64,g_TI .h1[s],g_TI .h2[s],g_TI .h3[s],g_TI .q1[s],g_TI .q2[s],g_TI .q3[s],g_ciI,g_cisI);
}
}
else if(S==64){
if(!s64fused){
for(u32 i=0;
i<g_Sb;
i+=S)merge4_dit_h<16>(p+i,64,16,g_TI .h1[s],g_TI .h2[s],g_TI .h3[s],g_TI .q1[s],g_TI .q2[s],g_TI .q3[s],g_ciI,g_cisI);
}
}
else{
for(u32 i=0;
i<g_Sb;
i+=S)merge4_dit_t(p+i,S,S>>2,g_TI .h1[s],g_TI .h2[s],g_TI .h3[s],g_TI .q1[s],g_TI .q2[s],g_TI .q3[s],g_ciI,g_cisI);
}
}
else if(S==16){
if(s64fused)mhh64(p,g_Sb,g_TI .h1[s],g_TI .h2[s],g_TI .h3[s],g_TI .q1[s],g_TI .q2[s],g_TI .q3[s],g_ciI,g_cisI,g_TI .h1[s+1],g_TI .h2[s+1],g_TI .h3[s+1],g_TI .q1[s+1],g_TI .q2[s+1],g_TI .q3[s+1]);
else if(h1fused)mhh(p,g_Sb,g_TI .h1[s],g_TI .h2[s],g_TI .h3[s],g_TI .q1[s],g_TI .q2[s],g_TI .q3[s],g_ciI,g_cisI);
else merge4_dit_h4(p,g_Sb,g_TI .h1[s],g_TI .h2[s],g_TI .h3[s],g_TI .q1[s],g_TI .q2[s],g_TI .q3[s],g_ciI,g_cisI);
}
else{
if(!h1fused)mh1(p,g_Sb,g_ciI,g_cisI);
}
}
if(g_odd)merge2_dit(p,g_Sb,g_TI .b2,g_TI .b2s);
}
for(int l=g_L-1;l>=0;--l){u32 S=g_n>>(2*l),h=S/4;for(u32 b=0;b<g_n;b+=S)outer_dit(a+padpos(b),h,g_TI.l1[l],g_TI.l2[l],g_TI.l3[l],g_ciI,g_cisI);}
}
TGT static void ntt_inv_batch(u32**blocks,int count){g_pmdelta=0;for(int i=0;i<count;++i)ntt_inv_tail(blocks[i],0);}
struct AlignedBuffer {u32*p=nullptr;size_t capacity=0;~AlignedBuffer(){free(p);}void reserve(size_t words){if(words<=capacity)return;free(p);p=nullptr;if(posix_memalign((void**)&p,64,words*4))std::abort();capacity=words;}};
static AlignedBuffer tableF,tableI,paddedA,paddedB;
static u32 active_n=0,active_p=0;
static u32 primitive_root(u32 p){u32 x=p-1;std::vector<u32>f;for(u32 d=2;u64(d)*d<=x;++d)if(x%d==0){f.push_back(d);while(x%d==0)x/=d;}if(x>1)f.push_back(x);for(u32 g=2;;++g){bool ok=true;for(u32 q:f)if(powmod(g,(p-1)/q)==1){ok=false;break;}if(ok)return g;}}
static void initialize(u32 n,u32 p){assert(n>=64&&(n&(n-1))==0&&p<(1u<<31)&&(p-1)%n==0);g_mod=p;if(active_n==n&&active_p==p)return;g_shmagic=u32(~0ull/u64(p));g_shint=u32((~0ull/u64(p))>>32);if(active_p!=p)g_generator=primitive_root(p);tableF.reserve(3ull*n+262144);tableI.reserve(3ull*n+262144);g_tabF=tableF.p;g_tabI=tableI.p;ntt_init(n);g_pmdelta=0;_mm_sfence();active_n=n;active_p=p;}
static void load_padded(u32*dst,const u32*src,u32 n){for(u32 i=0;i<n;i+=16384u)std::memcpy(dst+padpos(i),src+i,size_t(std::min(16384u,n-i))*4);}
TGT static void store_padded(u32*dst,const u32*src,u32 n,u32 scale){const __m256i p=_mm256_set1_epi32((int)NTP),s=_mm256_set1_epi32(scale),ss=_mm256_set1_epi32(shoup_c(scale));for(u32 base=0;base<n;base+=16384u){const u32*x=src+padpos(base);u32 count=std::min(16384u,n-base);for(u32 i=0;i<count;i+=8)stu256(dst+base+i,vS(ldu256(x+i),s,ss,p));}}
// In-place cyclic product of Montgomery-represented coefficients. Output is
// Montgomery-represented too, compatible with the batch canonical NTT backend.
TGT static bool cyclic_impl(u32*a,u32*b,u32 n,u32 p,bool mont){
if(!n||(n&(n-1))||p>=(1u<<31)||(p-1)%n)return false;
if(n<64){std::vector<u32>c(n);u32 inv=1;for(int i=0;i<5;++i)inv*=2-p*inv;u32 small_pinv=0-inv;for(u32 i=0;i<n;++i)for(u32 j=0;j<n;++j){u64 z=u64(a[i])*b[j],m=u32(z)*u64(small_pinv);u32 v=mont?u32((z+u64(u32(m))*p)>>32):u32(z%p);if(v>=p)v-=p;u32&vout=c[(i+j)&(n-1)];vout+=v;if(vout>=p)vout-=p;}std::copy(c.begin(),c.end(),a);return true;}
initialize(n,p);paddedA.reserve(padpos(n)+64);paddedB.reserve(padpos(n)+64);load_padded(paddedA.p,a,n);load_padded(paddedB.p,b,n);u32*aa[1]={paddedA.p},*bb[1]={paddedB.p};ntt_fwd_pair_batch(aa,bb,1,nullptr);
const __m256i pv=_mm256_set1_epi32(p);for(u32 base=0;base<n;base+=16384u){u32*x=paddedA.p+padpos(base),*y=paddedB.p+padpos(base);u32 count=std::min(16384u,n-base);for(u32 i=0;i<count;i+=8){__m256i ax=ldu256(x+i),by=ldu256(y+i);ax=_mm256_min_epu32(ax,_mm256_sub_epi32(ax,pv));by=_mm256_min_epu32(by,_mm256_sub_epi32(by,pv));stu256(x+i,vmont(ax,by,pv));}}
ntt_inv_batch(aa,1);u32 scale=powmod(n,p-2);if(!mont)scale=mulmod(scale,RRW);store_padded(a,paddedA.p,n,scale);return true;
}
TGT static bool cyclic_mont(u32*a,u32*b,u32 n,u32 p){return cyclic_impl(a,b,n,p,true);}
TGT static bool cyclic_normal(u32*a,u32*b,u32 n,u32 p){return cyclic_impl(a,b,n,p,false);}
static u32 pair_mask(){u32 mask=0;for(int k=0;k<g_nb;++k)mask|=1u<<(2*k);for(int k=0;k<g_L;++k)mask|=1u<<(g_log2n-2-2*k);return mask;}
static u32 reorder_index(u32 i,u32 lo){u32 hi=lo<<1;return ((i&lo)<<1)|((i&hi)>>1)|(i&~(lo|hi));}
TGT static void binary_layout(u32*dst,const u32*src,u32 n,bool to_padded){
const __m256i p=_mm256_set1_epi32(NTP),perm=_mm256_setr_epi32(0,2,1,3,4,6,5,7);u32 mask=pair_mask();
for(u32 i=0;i<n;i+=16){u32 offset=padpos(reorder_index(i,mask));const u32*in=to_padded?src+i:src+offset;__m256i a=ldu256(in),b=ldu256(in+8);a=_mm256_min_epu32(a,_mm256_sub_epi32(a,p));b=_mm256_min_epu32(b,_mm256_sub_epi32(b,p));a=_mm256_permutevar8x32_epi32(a,perm);b=_mm256_permutevar8x32_epi32(b,perm);u32*out=to_padded?dst+offset:dst+i;stu256(out,_mm256_permute2x128_si256(a,b,0x20));stu256(out+8,_mm256_permute2x128_si256(a,b,0x31));}
}
static u32 bitreverse(u32 x,unsigned log){u32 y=0;for(unsigned i=0;i<log;++i){y=y*2+(x&1);x>>=1;}return y;}
static void tiny_transform(u32*a,u32 n,u32 p,bool inverse,bool normal){u32 oldmod=g_mod;g_mod=p;u32 root=powmod(primitive_root(p),(p-1)/n);if(inverse)root=powmod(root,p-2);std::vector<u32>out(n);unsigned log=__builtin_ctz(n);for(u32 k=0;k<n;++k){u32 step=powmod(root,k),w=1,sum=0;for(u32 j=0;j<n;++j){u32 value=a[inverse?bitreverse(j,log):j];sum=(sum+u64(value)*w)%p;w=mulmod(w,step);}if(inverse&&normal)sum=mulmod(sum,powmod(n,p-2));out[inverse?k:bitreverse(k,log)]=sum;}std::copy(out.begin(),out.end(),a);g_mod=oldmod;}
// Public residue-array transforms use BINARY bit-reversed frequency slots,
// exactly matching the canonical Nyaan interface, irrespective of raw radix4
// engine layout and the position of its odd radix2 stage.
TGT static bool ntt_forward(u32*a,u32 n,u32 p){if(!n||(n&(n-1))||p>=1u<<31||(p-1)%n)return false;if(n<64){tiny_transform(a,n,p,false,false);return true;}initialize(n,p);paddedA.reserve(padpos(n)+64);load_padded(paddedA.p,a,n);ntt_fwd(paddedA.p);binary_layout(a,paddedA.p,n,false);return true;}
TGT static bool ntt_inverse(u32*a,u32 n,u32 p,bool normalize=true){if(!n||(n&(n-1))||p>=1u<<31||(p-1)%n)return false;if(n<64){tiny_transform(a,n,p,true,normalize);return true;}initialize(n,p);paddedA.reserve(padpos(n)+64);binary_layout(paddedA.p,a,n,true);u32*blocks[1]={paddedA.p};ntt_inv_batch(blocks,1);store_padded(a,paddedA.p,n,normalize?powmod(n,p-2):1);return true;}
}
#pragma GCC pop_options
#undef NTT_MAXN
#undef PF_DIST
#undef NTP
#undef TPAD
#undef TOPPAD
#undef PADJUMP
#undef PADJUMP_S
#undef TGT
#undef Q_T5OFF
#undef Q_SCF
#undef Q_SCI
#undef RRW
#undef MLO_V
#undef E7Z3_PM_ON
#undef H4B32
#undef E7_TILE
#undef E7Z_SBFUSE
#undef E7Z_IVFUSE
#undef I44
namespace mod_triangle31 { namespace backend {
// Canonical-residue adaptation permits mod < 2^31.
// Derived from NyaanNyaan/library (MIT), see LICENSE.Nyaan.
#include <bits/stdc++.h>
using namespace std;
template <uint32_t mod>
struct LazyMontgomeryModInt {
using mint = LazyMontgomeryModInt;
using i32 = int32_t;
using u32 = uint32_t;
using u64 = uint64_t;
static constexpr u32 get_r() {
u32 ret = mod;
for (i32 i = 0; i < 4; ++i) ret *= 2 - mod * ret;
return ret;
}
static constexpr u32 r = get_r();
static constexpr u32 n2 = -u64(mod) % mod;
static_assert(mod < (1u << 31), "invalid, mod >= 2 ^ 31");
static_assert((mod & 1) == 1, "invalid, mod % 2 == 0");
static_assert(r * mod == 1, "this code has bugs.");
u32 a;
constexpr LazyMontgomeryModInt() : a(0) {}
constexpr LazyMontgomeryModInt(const int64_t &b)
: a(reduce(u64(b % mod + mod) * n2)){};
static constexpr u32 reduce(const u64 &b) {
u32 ret = (b + u64(u32(b) * u32(-r)) * mod) >> 32; return ret >= mod ? ret - mod : ret;
}
constexpr mint &operator+=(const mint &b) {
a += b.a; if(a >= mod) a -= mod;
return *this;
}
constexpr mint &operator-=(const mint &b) {
a = a >= b.a ? a-b.a : a+mod-b.a;
return *this;
}
constexpr mint &operator*=(const mint &b) {
a = reduce(u64(a) * b.a);
return *this;
}
constexpr mint &operator/=(const mint &b) {
*this *= b.inverse();
return *this;
}
constexpr mint operator+(const mint &b) const { return mint(*this) += b; }
constexpr mint operator-(const mint &b) const { return mint(*this) -= b; }
constexpr mint operator*(const mint &b) const { return mint(*this) *= b; }
constexpr mint operator/(const mint &b) const { return mint(*this) /= b; }
constexpr bool operator==(const mint &b) const {
return (a >= mod ? a - mod : a) == (b.a >= mod ? b.a - mod : b.a);
}
constexpr bool operator!=(const mint &b) const {
return (a >= mod ? a - mod : a) != (b.a >= mod ? b.a - mod : b.a);
}
constexpr mint operator-() const { return mint() - mint(*this); }
constexpr mint operator+() const { return mint(*this); }
constexpr mint pow(u64 n) const {
mint ret(1), mul(*this);
while (n > 0) {
if (n & 1) ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
constexpr mint inverse() const { return pow(mod-2); }
friend ostream &operator<<(ostream &os, const mint &b) {
return os << b.get();
}
friend istream &operator>>(istream &is, mint &b) {
int64_t t;
is >> t;
b = LazyMontgomeryModInt<mod>(t);
return (is);
}
constexpr u32 get() const {
u32 ret = reduce(a);
return ret >= mod ? ret - mod : ret;
}
static constexpr u32 get_mod() { return mod; }
};
#include <immintrin.h>
__attribute__((target("sse4.2"))) inline __m128i my128_mullo_epu32(
const __m128i &a, const __m128i &b) {
return _mm_mullo_epi32(a, b);
}
__attribute__((target("sse4.2"))) inline __m128i my128_mulhi_epu32(
const __m128i &a, const __m128i &b) {
__m128i a13 = _mm_shuffle_epi32(a, 0xF5);
__m128i b13 = _mm_shuffle_epi32(b, 0xF5);
__m128i prod02 = _mm_mul_epu32(a, b);
__m128i prod13 = _mm_mul_epu32(a13, b13);
__m128i prod = _mm_unpackhi_epi64(_mm_unpacklo_epi32(prod02, prod13),
_mm_unpackhi_epi32(prod02, prod13));
return prod;
}
__attribute__((target("sse4.2"))) inline __m128i montgomery_mul_128(
const __m128i &a, const __m128i &b, const __m128i &r, const __m128i &m1) {
__m128i v=_mm_sub_epi32(_mm_add_epi32(my128_mulhi_epu32(a, b), m1), my128_mulhi_epu32(my128_mullo_epu32(my128_mullo_epu32(a,b), r),m1)); return _mm_min_epu32(v,_mm_sub_epi32(v,m1));
}
__attribute__((target("sse4.2"))) inline __m128i montgomery_add_128(
const __m128i &a, const __m128i &b, const __m128i &m2, const __m128i &m0) {
__m128i m1=_mm_srli_epi32(m2,1), v=_mm_add_epi32(a,b); return _mm_min_epu32(v,_mm_sub_epi32(v,m1));
}
__attribute__((target("sse4.2"))) inline __m128i montgomery_sub_128(
const __m128i &a, const __m128i &b, const __m128i &m2, const __m128i &m0) {
__m128i m1=_mm_srli_epi32(m2,1), v=_mm_sub_epi32(a,b); return _mm_min_epu32(v,_mm_add_epi32(v,m1));
}
__attribute__((target("avx2"))) inline __m256i my256_mullo_epu32(
const __m256i &a, const __m256i &b) {
return _mm256_mullo_epi32(a, b);
}
__attribute__((target("avx2"))) inline __m256i my256_mulhi_epu32(
const __m256i &a, const __m256i &b) {
__m256i a13 = _mm256_shuffle_epi32(a, 0xF5);
__m256i b13 = _mm256_shuffle_epi32(b, 0xF5);
__m256i prod02 = _mm256_mul_epu32(a, b);
__m256i prod13 = _mm256_mul_epu32(a13, b13);
__m256i prod = _mm256_unpackhi_epi64(_mm256_unpacklo_epi32(prod02, prod13),
_mm256_unpackhi_epi32(prod02, prod13));
return prod;
}
__attribute__((target("avx2"))) inline __m256i montgomery_mul_256(
const __m256i &a, const __m256i &b, const __m256i &r, const __m256i &m1) {
__m256i v=_mm256_sub_epi32(_mm256_add_epi32(my256_mulhi_epu32(a, b), m1), my256_mulhi_epu32(my256_mullo_epu32(my256_mullo_epu32(a,b), r),m1)); return _mm256_min_epu32(v,_mm256_sub_epi32(v,m1));
}
__attribute__((target("avx2"))) inline __m256i montgomery_add_256(
const __m256i &a, const __m256i &b, const __m256i &m2, const __m256i &m0) {
__m256i m1=_mm256_srli_epi32(m2,1), v=_mm256_add_epi32(a,b); return _mm256_min_epu32(v,_mm256_sub_epi32(v,m1));
}
__attribute__((target("avx2"))) inline __m256i montgomery_sub_256(
const __m256i &a, const __m256i &b, const __m256i &m2, const __m256i &m0) {
__m256i m1=_mm256_srli_epi32(m2,1), v=_mm256_sub_epi32(a,b); return _mm256_min_epu32(v,_mm256_add_epi32(v,m1));
}
namespace ntt_inner {
using u64 = uint64_t;
constexpr uint32_t get_pr(uint32_t mod) {
if (mod == 2) return 1;
u64 ds[32] = {};
int idx = 0;
u64 m = mod - 1;
for (u64 i = 2; i * i <= m; ++i) {
if (m % i == 0) {
ds[idx++] = i;
while (m % i == 0) m /= i;
}
}
if (m != 1) ds[idx++] = m;
uint32_t pr = 2;
while (1) {
int flg = 1;
for (int i = 0; i < idx; ++i) {
u64 a = pr, b = (mod - 1) / ds[i], r = 1;
while (b) {
if (b & 1) r = r * a % mod;
a = a * a % mod;
b >>= 1;
}
if (r == 1) {
flg = 0;
break;
}
}
if (flg == 1) break;
++pr;
}
return pr;
}
constexpr int SZ_FFT_BUF = 1 << 25;
uint32_t _buf1[SZ_FFT_BUF] __attribute__((aligned(64)));
uint32_t _buf2[SZ_FFT_BUF] __attribute__((aligned(64)));
} // namespace ntt_inner
// Pure forward/inverse adapter. The public frequency order is binary bit-reversed
// and matches the canonical Nyaan backend; raw radix4 order stays encapsulated.
template<class mint> struct NTT {
void ntt(mint*a,int n){bool ok=fast_e7_field::ntt_forward(reinterpret_cast<uint32_t*>(a),n,mint::get_mod());assert(ok);}
void intt(mint*a,int n,int normalize=true){bool ok=fast_e7_field::ntt_inverse(reinterpret_cast<uint32_t*>(a),n,mint::get_mod(),normalize);assert(ok);}
};
} }
#pragma GCC push_options
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
namespace mod_triangle31 {
using u32=uint32_t;using u64=uint64_t;using u128=__uint128_t;
constexpr u32 P0=2013265921,P1=1811939329,P2=2113929217;
inline u64 powmod(u64 x,u64 e,u64 p){u64 y=1;for(;e;e>>=1,x=x*x%p)if(e&1)y=y*x%p;return y;}
inline u64 max_pair_count(unsigned n){if(n<2)return 0;u64 N=n-1,ans=N;for(u64 l=1;l<=N;){u64 q=N/l,r=N/q;ans+=q*(r-l+1);l=r+1;}return ans;}
inline bool crt_bound(unsigned n){return u128(P0)*P1*P2>u128(max_pair_count(n))*u32(-1)*u32(-1);}
template<u32 P>using Mint=backend::LazyMontgomeryModInt<P>;
inline __m256i addv(__m256i x,__m256i y,u32 p){__m256i z=_mm256_add_epi32(x,y);return _mm256_min_epu32(z,_mm256_sub_epi32(z,_mm256_set1_epi32(p)));}
template<u32 P>void cyclic(unsigned len){bool ok=fast_e7_field::cyclic_mont(backend::ntt_inner::_buf1,backend::ntt_inner::_buf2,len,P);assert(ok);}
constexpr unsigned LEAF=64;
template<u32 P>void triangle(unsigned q,unsigned n,unsigned S,const u32*a,const u32*b,u32*out,unsigned l,unsigned r){
if(r<=S+1||u64(q+1)*l+q>=n)return;
if(r-l<=LEAF){
const __m256i p=_mm256_set1_epi32(P),rr=_mm256_set1_epi32(Mint<P>::r);
for(unsigned j=std::max(l+1,S+1);j<r&&u64(q)*j<n;++j){unsigned end=std::min(j,n-q*j);if(end<=l)continue;__m256i bj=_mm256_set1_epi32(b[j]);unsigned k=l;
for(;k+8<=end;k+=8){__m256i x=_mm256_loadu_si256((const __m256i*)(a+q*j+k)),y=_mm256_loadu_si256((const __m256i*)(out+k));x=backend::montgomery_mul_256(x,bj,rr,p);_mm256_storeu_si256((__m256i*)(out+k),addv(y,x,P));}
for(;k<end;++k){u32 value=Mint<P>::reduce(u64(a[q*j+k])*b[j]),sum=out[k]+value;out[k]=sum>=P?sum-P:sum;}
}return;
}
unsigned w=(r-l)/2,m=l+w;u64 base64=u64(q)*m+l;
if(base64<n&&m<n&&r>S+1){unsigned base=unsigned(base64),needed=(q+1)*w-q,fftlen=8;while(fftlen<needed)fftlen*=2;
assert(fftlen<=unsigned(backend::ntt_inner::SZ_FFT_BUF)&&((P-1)%fftlen)==0);auto*x=backend::ntt_inner::_buf1;auto*y=backend::ntt_inner::_buf2;std::memset(x,0,size_t(fftlen)*4);std::memset(y,0,size_t(fftlen)*4);
unsigned alen=std::min(needed,n-base);std::memcpy(x,a+base,size_t(alen)*4);
for(unsigned j=std::max(m,S+1);j<std::min(r,n);++j)y[q*(r-1-j)]=b[j];
cyclic<P>(fftlen);unsigned off=q*(w-1),end=std::min(w,n-l);
for(unsigned k=0;k<end;++k){u32 sum=out[l+k]+x[off+k];out[l+k]=sum>=P?sum-P:sum;}
}
triangle<P>(q,n,S,a,b,out,l,m);triangle<P>(q,n,S,a,b,out,m,r);
}
template<u32 P>void field(unsigned n,unsigned S,const u32*a,const u32*b,u32*out){
std::vector<u32>am(n),bm(n);for(unsigned i=0;i<n;++i){am[i]=Mint<P>(a[i]).a;if(i>S)bm[i]=Mint<P>(b[i]).a;}std::fill(out,out+n,0);
unsigned Q=(n-1)/(S+1);for(unsigned q=1;q<=Q;++q){unsigned r=1;while(r<=(n-1)/q)r*=2;triangle<P>(q,n,S,am.data(),bm.data(),out,0,r);}
for(unsigned k=0;k<n/2;++k){Mint<P>v;v.a=out[k];out[k]=v.get();}
}
// Adds all contributions with j>S into an already initialized c.
// False means unsupported exactness/transform bound; c is then unchanged.
inline bool add_large(unsigned n,unsigned S,const u32*a,const u32*b,u32*c){
if(n<2||S>=n-1)return true;if(!crt_bound(n))return false;
u64 length=8;while(length<2ull*n)length*=2;if(length>unsigned(backend::ntt_inner::SZ_FFT_BUF)||length>(1u<<25))return false;
std::vector<u32>x(n),y(n),z(n);field<P0>(n,S,a,b,x.data());field<P1>(n,S,a,b,y.data());field<P2>(n,S,a,b,z.data());
constexpr u64 p01=u64(P0)*P1;static const u64 inv0=powmod(P0,P1-2,P1),inv01=powmod(p01%P2,P2-2,P2);
for(unsigned k=0;k<n/2;++k){u64 t=(y[k]+u64(P1)-x[k]%P1)%P1*inv0%P1,v=x[k]+u64(P0)*t,t2=(z[k]+u64(P2)-v%P2)%P2*inv01%P2;c[k]+=u32(v)+u32(p01)*u32(t2);}
u32 suffix=0;for(unsigned k=n;k--;){if(k+1<n&&k+1>S)suffix+=b[k+1];c[k]+=a[k]*suffix;}return true;
}
}
#pragma GCC pop_options
// For a fixed node and transform length, sum every q's correlation spectrum,
// then do a single inverse. B's spectrum is transformed only once. The
// bit-reversed frequency permutation is implemented by octant-interleaved
// packing: each eight-lane sampled B vector uses one contiguous load+permute.
namespace mod_triangle31 {
#pragma GCC push_options
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
template<u32 P>void grouped_node(unsigned n,unsigned S,unsigned Q,const u32*a,const u32*b,u32*out,unsigned l,unsigned r){
if(r<=S+1||2ull*l+1>=n)return;
if(r-l<=LEAF){unsigned first=std::max(l+1,S+1);if(first>=r||first>=n)return;unsigned qmax=std::min(Q,(n-1-l)/first);for(unsigned q=1;q<=qmax;++q)triangle<P>(q,n,S,a,b,out,l,r);return;}
unsigned w=(r-l)/2,m=l+w,j0=std::max(m,S+1),jend=std::min(r,n);
if(j0<jend&&j0+l<n){
unsigned wb=jend-j0,wk=w,qmax=std::min(Q,(n-1-l)/j0);
using mint=Mint<P>;static backend::NTT<mint>fft;const __m256i pp=_mm256_set1_epi32(P),rr=_mm256_set1_epi32(mint::r),seven=_mm256_set1_epi32(7),rev3=_mm256_setr_epi32(0,4,2,6,1,5,3,7);
for(unsigned qlo=1;qlo<=qmax;){unsigned len=8;while(len<u64(qlo)*(wb-1)+wk)len*=2;assert(len<=unsigned(backend::ntt_inner::SZ_FFT_BUF)&&((P-1)%len)==0);
unsigned qhi=wb==1?qmax:std::min(qmax,(len-wk)/(wb-1));assert(qhi>=qlo);unsigned blocks=len/8,shift=__builtin_ctz(blocks);std::vector<u32>reverse(blocks),packed(len);
for(unsigned i=1;i<blocks;++i)reverse[i]=(reverse[i/2]>>1)+((i&1)?blocks/2:0);
auto*x=backend::ntt_inner::_buf1;auto*acc=backend::ntt_inner::_buf2;std::memset(x,0,size_t(len)*4);std::memcpy(x,b+j0,size_t(wb)*4);fft.ntt(reinterpret_cast<mint*>(x),len);
for(unsigned i=0;i<blocks;++i){__m256i v=_mm256_loadu_si256((const __m256i*)(x+8*reverse[i]));v=_mm256_permutevar8x32_epi32(v,rev3);_mm256_storeu_si256((__m256i*)(packed.data()+8*i),v);}std::memset(acc,0,size_t(len)*4);
for(unsigned q=qlo;q<=qhi;++q){unsigned base=q*j0+l,needed=q*(wb-1)+wk,alen=std::min(needed,n-base);std::memset(x,0,size_t(len)*4);std::memcpy(x,a+base,size_t(alen)*4);fft.ntt(reinterpret_cast<mint*>(x),len);
const __m256i qlane=_mm256_and_si256(_mm256_mullo_epi32(rev3,_mm256_set1_epi32(0u-q)),seven);
for(unsigned i=0;i<blocks;++i){unsigned index=(u32(0u-q)*reverse[i])&(len-1),low=index&(blocks-1),high=index>>shift;__m256i v=_mm256_loadu_si256((const __m256i*)(packed.data()+8*low));__m256i perm=_mm256_and_si256(_mm256_add_epi32(qlane,_mm256_set1_epi32(high)),seven);v=_mm256_permutevar8x32_epi32(v,perm);v=backend::montgomery_mul_256(_mm256_loadu_si256((const __m256i*)(x+8*i)),v,rr,pp);v=addv(_mm256_loadu_si256((const __m256i*)(acc+8*i)),v,P);_mm256_storeu_si256((__m256i*)(acc+8*i),v);}
}
fft.intt(reinterpret_cast<mint*>(acc),len,false);const __m256i scale=_mm256_set1_epi32(mint(len).inverse().a);unsigned k=0;
for(;k+8<=wk;k+=8){__m256i v=backend::montgomery_mul_256(_mm256_loadu_si256((const __m256i*)(acc+k)),scale,rr,pp);v=addv(_mm256_loadu_si256((const __m256i*)(out+l+k)),v,P);_mm256_storeu_si256((__m256i*)(out+l+k),v);}
mint scal=mint(len).inverse();for(;k<wk;++k){u32 value=mint::reduce(u64(acc[k])*scal.a),sum=out[l+k]+value;out[l+k]=sum>=P?sum-P:sum;}
qlo=qhi+1;
}
}
grouped_node<P>(n,S,Q,a,b,out,l,m);grouped_node<P>(n,S,Q,a,b,out,m,r);
}
template<u32 P>void grouped_field(unsigned n,unsigned S,const u32*a,const u32*b,u32*out){
std::vector<u32>am(n),bm(n);for(unsigned i=0;i<n;++i){am[i]=Mint<P>(a[i]).a;if(i>S)bm[i]=Mint<P>(b[i]).a;}std::fill(out,out+n,0);unsigned r=1;while(r<n)r*=2;grouped_node<P>(n,S,(n-1)/(S+1),am.data(),bm.data(),out,0,r);for(unsigned k=0;k<n/2;++k){Mint<P>x;x.a=out[k];out[k]=x.get();}
}
inline bool add_large_grouped(unsigned n,unsigned S,const u32*a,const u32*b,u32*c){
if(n<2||S>=n-1)return true;if(!crt_bound(n))return false;u64 length=8;while(length<2ull*n)length*=2;if(length>unsigned(backend::ntt_inner::SZ_FFT_BUF)||length>(1u<<25))return false;
std::vector<u32>x(n),y(n),z(n);grouped_field<P0>(n,S,a,b,x.data());grouped_field<P1>(n,S,a,b,y.data());grouped_field<P2>(n,S,a,b,z.data());
constexpr u64 p01=u64(P0)*P1;static const u64 inv0=powmod(P0,P1-2,P1),inv01=powmod(p01%P2,P2-2,P2);
for(unsigned k=0;k<n/2;++k){u64 t=(y[k]+u64(P1)-x[k]%P1)%P1*inv0%P1,v=x[k]+u64(P0)*t,t2=(z[k]+u64(P2)-v%P2)%P2*inv01%P2;c[k]+=u32(v)+u32(p01)*u32(t2);}
u32 suffix=0;for(unsigned k=n;k--;){if(k+1<n&&k+1>S)suffix+=b[k+1];c[k]+=a[k]*suffix;}return true;
}
#pragma GCC pop_options
}
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
namespace adaptive_mod {
using u32=uint32_t;using u64=uint64_t;
struct Range{unsigned l,r;};struct Cross{unsigned l,r;bool merge;};
inline u64 direct_count(unsigned n,unsigned l,unsigned r){u64 total=0,A=n-1-l,end=std::min<u64>(r-1,A);for(u64 j=l+1;j<=end;){u64 q=A/j,hi=std::min(end,A/q),cut=std::min(hi,u64(n)/(q+1));if(cut>=j){u64 count=cut-j+1;total+=q*((j+cut)*count/2-u64(l)*count);}u64 lo=std::max(j,cut+1);if(lo<=hi){u64 count=hi-lo+1;total+=count*(n-q*l)-(lo+hi)*count/2;}j=hi+1;}return total;}
struct Plan{unsigned n;double fft_weight;std::vector<Range>direct;std::vector<Cross>cross;
double build(unsigned l,unsigned r){if(r-l<2||2ull*l+1>=n)return 0;double native=double(direct_count(n,l,r));if(r-l<=64){direct.push_back({l,r});return native;}
size_t ds=direct.size(),cs=cross.size();unsigned w=(r-l)/2,m=l+w;double child=build(l,m)+build(m,r);unsigned qmax=(n-1-l)/m;if(!qmax)return child;double grouped=0;
for(unsigned qlo=1;qlo<=qmax;){unsigned len=8;while(len<u64(qlo)*(w-1)+w)len*=2;unsigned qhi=w==1?qmax:std::min(qmax,(len-w)/(w-1));grouped+=double(qhi-qlo+3)/3.*len*__builtin_ctz(len);qlo=qhi+1;}
unsigned span=n-l-m,wk=std::min(w,span),len=8;while(len<u64(span)+wk-1)len*=2;double merged=double(len)*__builtin_ctz(len);double cost=child+fft_weight*std::min(grouped,merged);
if(native<cost){direct.resize(ds);cross.resize(cs);direct.push_back({l,r});return native;}
cross.push_back({l,r,merged<grouped});return cost;
}
Plan(unsigned size,double weight):n(size),fft_weight(weight){unsigned r=1;while(r<n)r*=2;build(0,r);}
};
inline void direct_range(unsigned n,const unsigned* __restrict a,const unsigned* __restrict b,unsigned* __restrict c,unsigned l,unsigned r){
if(n<64){for(unsigned j=l+1;j<std::min(r,n-l);++j)for(unsigned start=j;start+l<n;start+=j)for(unsigned k=l;k<std::min(r,std::min(j,n-start));++k)c[k]+=a[start+k]*b[j];return;}
for(unsigned j=l+1;j<std::min(r,65u);++j){u32 sum[64]={};for(unsigned start=j;start+l<n;start+=j){unsigned end=std::min(j,n-start);for(unsigned k=l;k<end;++k)sum[k]+=a[start+k];}for(unsigned k=l;k<j;++k)c[k]+=sum[k]*b[j];}
const unsigned limit=std::min(r,n/2+1),jlo=std::max(l+1,65u);if(jlo>=r)return;
for(unsigned k0=l;k0<limit;k0+=32){__m256i z[4],idx[4],endmask[4];for(unsigned v=0;v<4;++v){z[v]=_mm256_loadu_si256((const __m256i*)(c+k0+8*v));idx[v]=_mm256_setr_epi32(k0+8*v,k0+8*v+1,k0+8*v+2,k0+8*v+3,k0+8*v+4,k0+8*v+5,k0+8*v+6,k0+8*v+7);endmask[v]=_mm256_cmpgt_epi32(_mm256_set1_epi32(r),idx[v]);}
unsigned first=std::max(jlo,k0+1),first_full=std::max(jlo,k0+32),qmax=(n-1-k0)/first;
for(unsigned q=1;q<=qmax;++q){unsigned last=std::min(r-1,(n-1-k0)/q),full_last=std::min(r-1,(n-k0-32)/q),j=first;
for(;j<=last&&j<first_full;++j){__m256i bj=_mm256_set1_epi32(b[j]);unsigned start=q*j+k0;for(unsigned v=0;v<4;++v){__m256i mask=_mm256_and_si256(endmask[v],_mm256_and_si256(_mm256_cmpgt_epi32(_mm256_set1_epi32(j),idx[v]),_mm256_cmpgt_epi32(_mm256_set1_epi32(n-q*j),idx[v])));__m256i x=start+8*v<n?_mm256_maskload_epi32((const int*)(a+start+8*v),mask):_mm256_setzero_si256();z[v]=_mm256_add_epi32(z[v],_mm256_mullo_epi32(x,bj));}}
for(;j<=full_last;++j){__m256i bj=_mm256_set1_epi32(b[j]);const u32*x=a+q*j+k0;for(unsigned v=0;v<4;++v){__m256i value=_mm256_loadu_si256((const __m256i*)(x+8*v));if(k0+32>r)value=_mm256_and_si256(value,endmask[v]);z[v]=_mm256_add_epi32(z[v],_mm256_mullo_epi32(value,bj));}}
for(;j<=last;++j){__m256i bj=_mm256_set1_epi32(b[j]);unsigned start=q*j+k0;for(unsigned v=0;v<4;++v){__m256i mask=_mm256_and_si256(endmask[v],_mm256_and_si256(_mm256_cmpgt_epi32(_mm256_set1_epi32(j),idx[v]),_mm256_cmpgt_epi32(_mm256_set1_epi32(n-q*j),idx[v])));__m256i x=start+8*v<n?_mm256_maskload_epi32((const int*)(a+start+8*v),mask):_mm256_setzero_si256();z[v]=_mm256_add_epi32(z[v],_mm256_mullo_epi32(x,bj));}}
}
for(unsigned v=0;v<4;++v)_mm256_storeu_si256((__m256i*)(c+k0+8*v),z[v]);
}
}
template<u32 P>void merge_cross(unsigned n,const u32*a,const u32*b,u32*out,unsigned l,unsigned r){using namespace mod_triangle31;unsigned w=(r-l)/2,m=l+w,span=n-l-m,wk=std::min(w,span),len=8;while(len<u64(span)+wk-1)len*=2;assert(len<=unsigned(backend::ntt_inner::SZ_FFT_BUF));auto*x=backend::ntt_inner::_buf1;auto*y=backend::ntt_inner::_buf2;std::memset(x,0,size_t(len)*4);std::memset(y,0,size_t(len)*4);std::memcpy(x,a+m+l,size_t(span)*4);
for(unsigned j=m;j<std::min(r,n-l);++j){u32 bj=b[j];for(unsigned d=j;d<n-l;d+=j){unsigned at=span-1-(d-m),sum=y[at]+bj;y[at]=sum>=P?sum-P:sum;}}
cyclic<P>(len);for(unsigned k=0;k<wk;++k){u32 sum=out[l+k]+x[span-1+k];out[l+k]=sum>=P?sum-P:sum;}
}
}
namespace mod_triangle31 {
template<u32 P>void grouped_cross(unsigned n,unsigned S,unsigned Q,const u32*a,const u32*b,u32*out,unsigned l,unsigned r){
if(r<=S+1||2ull*l+1>=n)return;
if(r-l<=LEAF){unsigned first=std::max(l+1,S+1);if(first>=r||first>=n)return;unsigned qmax=std::min(Q,(n-1-l)/first);for(unsigned q=1;q<=qmax;++q)triangle<P>(q,n,S,a,b,out,l,r);return;}
unsigned w=(r-l)/2,m=l+w,j0=std::max(m,S+1),jend=std::min(r,n);
if(j0<jend&&j0+l<n){
unsigned wb=jend-j0,wk=w,qmax=std::min(Q,(n-1-l)/j0);
using mint=Mint<P>;static backend::NTT<mint>fft;const __m256i pp=_mm256_set1_epi32(P),rr=_mm256_set1_epi32(mint::r),seven=_mm256_set1_epi32(7),rev3=_mm256_setr_epi32(0,4,2,6,1,5,3,7);
for(unsigned qlo=1;qlo<=qmax;){unsigned len=8;while(len<u64(qlo)*(wb-1)+wk)len*=2;assert(len<=unsigned(backend::ntt_inner::SZ_FFT_BUF)&&((P-1)%len)==0);
unsigned qhi=wb==1?qmax:std::min(qmax,(len-wk)/(wb-1));assert(qhi>=qlo);unsigned blocks=len/8,shift=__builtin_ctz(blocks);std::vector<u32>reverse(blocks),packed(len);
for(unsigned i=1;i<blocks;++i)reverse[i]=(reverse[i/2]>>1)+((i&1)?blocks/2:0);
auto*x=backend::ntt_inner::_buf1;auto*acc=backend::ntt_inner::_buf2;std::memset(x,0,size_t(len)*4);std::memcpy(x,b+j0,size_t(wb)*4);fft.ntt(reinterpret_cast<mint*>(x),len);
for(unsigned i=0;i<blocks;++i){__m256i v=_mm256_loadu_si256((const __m256i*)(x+8*reverse[i]));v=_mm256_permutevar8x32_epi32(v,rev3);_mm256_storeu_si256((__m256i*)(packed.data()+8*i),v);}std::memset(acc,0,size_t(len)*4);
for(unsigned q=qlo;q<=qhi;++q){unsigned base=q*j0+l,needed=q*(wb-1)+wk,alen=std::min(needed,n-base);std::memset(x,0,size_t(len)*4);std::memcpy(x,a+base,size_t(alen)*4);fft.ntt(reinterpret_cast<mint*>(x),len);
const __m256i qlane=_mm256_and_si256(_mm256_mullo_epi32(rev3,_mm256_set1_epi32(0u-q)),seven);
for(unsigned i=0;i<blocks;++i){unsigned index=(u32(0u-q)*reverse[i])&(len-1),low=index&(blocks-1),high=index>>shift;__m256i v=_mm256_loadu_si256((const __m256i*)(packed.data()+8*low));__m256i perm=_mm256_and_si256(_mm256_add_epi32(qlane,_mm256_set1_epi32(high)),seven);v=_mm256_permutevar8x32_epi32(v,perm);v=backend::montgomery_mul_256(_mm256_loadu_si256((const __m256i*)(x+8*i)),v,rr,pp);v=addv(_mm256_loadu_si256((const __m256i*)(acc+8*i)),v,P);_mm256_storeu_si256((__m256i*)(acc+8*i),v);}
}
fft.intt(reinterpret_cast<mint*>(acc),len,false);const __m256i scale=_mm256_set1_epi32(mint(len).inverse().a);unsigned k=0;
for(;k+8<=wk;k+=8){__m256i v=backend::montgomery_mul_256(_mm256_loadu_si256((const __m256i*)(acc+k)),scale,rr,pp);v=addv(_mm256_loadu_si256((const __m256i*)(out+l+k)),v,P);_mm256_storeu_si256((__m256i*)(out+l+k),v);}
mint scal=mint(len).inverse();for(;k<wk;++k){u32 value=mint::reduce(u64(acc[k])*scal.a),sum=out[l+k]+value;out[l+k]=sum>=P?sum-P:sum;}
qlo=qhi+1;
}
}
}
}
namespace adaptive_mod {
template<u32 P>void field(const Plan&plan,const u32*a,const u32*b,u32*out){using namespace mod_triangle31;unsigned n=plan.n;std::vector<u32>am(n),bm(n);for(unsigned i=0;i<n;++i){am[i]=Mint<P>(a[i]).a;bm[i]=Mint<P>(b[i]).a;}std::fill(out,out+n,0);for(auto node:plan.cross){if(node.merge)merge_cross<P>(n,am.data(),bm.data(),out,node.l,node.r);else grouped_cross<P>(n,0,n-1,am.data(),bm.data(),out,node.l,node.r);}for(unsigned k=0;k<n/2;++k){Mint<P>x;x.a=out[k];out[k]=x.get();}}
inline bool solve(unsigned n,const unsigned*a,const unsigned*b,unsigned*c,double fft_weight=64){using namespace mod_triangle31;if(!crt_bound(n))return false;u32 suffix=0;for(unsigned k=n;k--;){c[k]=a[k]*suffix;suffix+=b[k];}if(n<2)return true;Plan plan(n,fft_weight);for(auto q:plan.direct)direct_range(n,a,b,c,q.l,q.r);if(plan.cross.empty())return true;std::vector<u32>x(n),y(n),z(n);field<P0>(plan,a,b,x.data());field<P1>(plan,a,b,y.data());field<P2>(plan,a,b,z.data());constexpr u64 p01=u64(P0)*P1;static const u64 inv0=powmod(P0,P1-2,P1),inv01=powmod(p01%P2,P2-2,P2);for(unsigned k=0;k<n/2;++k){u64 t=(y[k]+u64(P1)-x[k]%P1)%P1*inv0%P1,v=x[k]+u64(P0)*t,t2=(z[k]+u64(P2)-v%P2)%P2*inv01%P2;c[k]+=u32(v)+u32(p01)*u32(t2);}return true;}
}
void convolution(unsigned n,const unsigned*a,const unsigned*b,unsigned*c){if(!adaptive_mod::solve(n,a,b,c,16))__builtin_trap();}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 2.402 s | 51 MB + 972 KB | Accepted | Score: 100 | 显示更多 |