/* References / 引用: saffah_codex61s_agg5 https://duck.ac/submission/169523: complete best protected-gap partition engine/table/layout; inherited account/URL/use/notices retained. https://duck.ac/faq: judge i3-8100, used only for tuning selection. No new outside code.
Approach / 思路: Keep all saved gaps,lookup tables,branch-based read choice,32byte load/store helpers and inplace invariants; request GCCSkylake scheduling for this exact loop. This changes instruction/register/branch placement rather than the algorithm's output. No new data observations.
Experiment purpose / 试验目的: GCC9all65536sixteen-sign layouts,4096sizes,adversarial distributions with128byte guards on both sides, full shape partition plus3multiset fingerprints, final source/ELFcheck then official time. No local timing filter.
*/
/* References / 引用:
saffah_codex61s_agg5 https://duck.ac/submission/168458: complete current per-scale protected-gap partition and exact byte/u32permutation table; all inherited accounts/source/use notices retained below. No new outside code.
References / 引用:
saffah_v41f1017_agg4 https://duck.ac/submission/164299: copied current four-chunk reserved-gap kernel and split-free stores. saffah_codex_6a_agg3 https://duck.ac/submission/163723: exact gap-conservation range comparison and safe32-input saved-tail vector cleanup ideas, independently integrated below. Full inherited authors/URLs/uses remain; no separate attached license.
Approach / 思路: Add independently implemented2048byte permutation storage with exact VPMOVZXBD index reconstruction to the following combined schedule, reducing the random lookup footprint fourfold. Combine four-chunk side scheduling with the invariant that the two free gaps sum to2*GAP. One unsigned range check64<=leftgap<=2*GAP-64 replaces both quad gap comparisons; the analogous32bound handles pairs. Process the final32-input window separately and vector-restore the saved prefix/suffix while at least64output slots remain, then finish scalarly. Maintain the original worst-case admission bounds and once-only input coverage, even for uniform signs. Use512-element reserves for100K as in the current fast small-size family.
Experiment purpose / 试验目的: Exact multiset, order and guard checks across small boundaries/eight sign distributions plus each full shape using GCC9, before prompt official measurement. No local performance filter.
References / 引用: reserved-gap in-place dual-store engine, split-free single-instruction vector loads/stores and the dual-side read schedule from saffah_codex_6a_agg3 https://duck.ac/submission/163626 (accepted for this problem; it credits saffah_v41f1017_agg4 https://duck.ac/submission/163470 for the split-free engine), which itself derives from saffah_codex61s_agg5 https://duck.ac/submission/163182. No separate licence is declared by any of these sources; reuse is limited to those published algorithms with this attribution, as the task requires.
Idea / 思路: keep every inherited invariant, guard bound and the two-hundred-and-fifty-six-plus reserve, and add one schedule step: when both output gaps have sixty-four free entries and two full sixty-four-input windows remain, consume two thirty-two-input chunks from the left and two from the right in one turn (four compactions), otherwise fall back to the inherited dual-side pair or single-side chunk. The same gap bound that protects the pair schedule protects the quad: the left store span ends at lo+128-prefix15 <= l+64 and the right span starts at hi-8-prefix15 >= r-64, both implied by gaps >= 64.
Purpose / 试验目的: halve the side-selection frequency once more without widening the register footprint, then re-validate the exact multiset, the partition property and every guard case at this fixed n.
Approach / 思路: Align the immutable table object to4096bytes and insert1024zero prefix bytes, placing the actual index table at a known page phase. Preserve every index, row width, lookup/permutation instruction, scan/cardinality, protected-window reservation and input/output address. This changes actual table-to-input cache-set relationships, rather than relying on any imagined machine/window.
Experiment purpose / 试验目的: GCC9exhaustive65536signlayouts,4096sizes,adversarial and full100000partition/multiset proof before prompt official timing. Local speed never filters.
*/
#include <immintrin.h>
#include <cstddef>
#pragma GCC target("avx2,popcnt,tune=skylake")
#pragma GCC optimize("O3")
struct GapPerm{unsigned char iv867pad[1024]{};unsigned char p[256][8];constexpr GapPerm():p{}{for(unsigned m=0;m<256;m++){unsigned k=0;for(unsigned i=0;i<8;i++)if(!(m>>i&1))p[m][k++]=i;for(unsigned i=0;i<8;i++)if(m>>i&1)p[m][k++]=i;}}};
alignas(4096)static constexpr GapPerm gp{};
static inline __m256i ld32(const unsigned*p){return _mm256_lddqu_si256((const __m256i*)p);}
static inline void st32(unsigned*p,__m256i v){__asm__("vmovups %0,%1"::"x"(v),"m"(*(__m256i*)p));}
template<unsigned GAP>static void gap_partition(unsigned n,unsigned*a){
alignas(32)unsigned keep[2*GAP+32];
for(unsigned q=0;q<GAP;q+=8){_mm256_store_si256((__m256i*)(keep+q),_mm256_loadu_si256((const __m256i*)(a+q)));_mm256_store_si256((__m256i*)(keep+GAP+q),_mm256_loadu_si256((const __m256i*)(a+n-GAP+q)));}
size_t l=GAP,r=n-GAP,lo=0,hi=n;
auto process=[&](const unsigned*src){
__m256i w0=ld32(src),w1=ld32(src+8),w2=ld32(src+16),w3=ld32(src+24);
unsigned k0=(unsigned)_mm256_movemask_ps(_mm256_castsi256_ps(w0));
unsigned k1=(unsigned)_mm256_movemask_ps(_mm256_castsi256_ps(w1));
unsigned k2=(unsigned)_mm256_movemask_ps(_mm256_castsi256_ps(w2));
unsigned k3=(unsigned)_mm256_movemask_ps(_mm256_castsi256_ps(w3));
unsigned g0=(unsigned)__builtin_popcount(k0),g1=(unsigned)__builtin_popcount(k1);
unsigned g2=(unsigned)__builtin_popcount(k2),g3=(unsigned)__builtin_popcount(k3);
unsigned b1=g0,b2=g0+g1,b3=b2+g2,tot=b3+g3;
w0=_mm256_permutevar8x32_epi32(w0,_mm256_cvtepu8_epi32(_mm_loadl_epi64((const __m128i*)gp.p[k0])));
w1=_mm256_permutevar8x32_epi32(w1,_mm256_cvtepu8_epi32(_mm_loadl_epi64((const __m128i*)gp.p[k1])));
w2=_mm256_permutevar8x32_epi32(w2,_mm256_cvtepu8_epi32(_mm_loadl_epi64((const __m128i*)gp.p[k2])));
w3=_mm256_permutevar8x32_epi32(w3,_mm256_cvtepu8_epi32(_mm_loadl_epi64((const __m128i*)gp.p[k3])));
st32(a+lo,w0);
st32(a+lo+8-b1,w1);
st32(a+lo+16-b2,w2);
st32(a+lo+24-b3,w3);
st32(a+hi-8,w0);
st32(a+hi-8-b1,w1);
st32(a+hi-8-b2,w2);
st32(a+hi-8-b3,w3);
lo+=32-tot;hi-=tot;
};
while(r-l>=64){
if(r-l>=128&&(l-lo-64)<=2*GAP-128){size_t l0=l,r0=r;l+=64;r-=64;process(a+l0);process(a+l0+32);process(a+r0-32);process(a+r0-64);}
else if((l-lo-32)<=2*GAP-64){size_t left=l,right=r-32;l+=32;r-=32;process(a+left);process(a+right);}
else if(l-lo<32){size_t src=l;l+=32;process(a+src);}else{r-=32;process(a+r);}
}
if(r-l>=32){if(l-lo<32){size_t src=l;l+=32;process(a+src);}else{r-=32;process(a+r);}}
unsigned c=2*GAP;while(l<r)keep[c++]=a[l++];
unsigned i=0;for(;i+64<=c;i+=32)process(keep+i);for(;i<c;i++){unsigned x=keep[i];if(x<0x80000000u)a[lo++]=x;else a[--hi]=x;}
}
void partition(unsigned n,unsigned*a){if(n<1024){unsigned l=0,r=n;while(l<r){if(a[l]<0x80000000u)++l;else{unsigned v=a[l];a[l]=a[--r];a[r]=v;}}return;}gap_partition<512>(n,a);}