/* References / 引用:
saffah_v41f1017_agg4 https://duck.ac/submission/163626: complete paired left/right reserved-gap compaction, split-free vector loads/stores and original permutation-table/own-algorithm attributions below. No separate license attached; all inherited accounts, URLs and uses retained.
References: saffah_v41f1017_agg4 https://duck.ac/submission/163470 : complete reserved-gap in-place SIMD engine with split-free loads/stores, retained below; its cited sources and uses remain intact.
Idea: reserve a larger initial gap and process one left chunk followed by one right chunk whenever both gaps have32free entries. Both read addresses then depend only on monotone input cursors. Only a rare gap-boundary step chooses one side from data-dependent output counts. The initial saved items are restored once at the end. Experiment purpose: remove frequent unpredictable side branches while retaining exactly-once placement and the exact partition multiset; validate all shapes and full100M before official timing.
References / 引用: reserved-gap in-place dual-store mechanism from saffah_codex61s_agg5 https://duck.ac/submission/163162 (this problem's accepted in-place dual-cursor partition); eight-lane sign-mask permutation table from saffah_codex_6a_agg3 https://duck.ac/submission/163086. Both declare no separate licence; reuse is limited to those published algorithms with this attribution, as the task requires.
Idea / 思路: keep the peak-memory-free in-place scheme with thirty-two inputs per turn, and change three scheduling details: the read side is chosen by a branch (not a conditional move) so the next load address is speculated instead of serialising on the previous turn's low cursor; the four 32-byte vector reads use a single-instruction unaligned load (lddqu) instead of the compiler's split xmm+insert pair; and each 32-byte store is a single vmovups issued from an inline-asm memory operand, again avoiding a compiler-split pair. The stores must stay unaligned because both output cursors advance by data-dependent counts, but the exactly-once placement invariant is unchanged.
Purpose / 试验目的: remove twelve split vector memory operations per turn and the cursor->address dependency while preserving the exact multiset; re-validate the partition property and exactly-once placement at this fixed n with structured, adversarial and random sign layouts.
Approach / 思路: Reserve a larger gap and run up to eight left/right32-input pairs without checking either cursor gap between pairs. The admission bound32*BURST per side protects unread inputs even if every selected value goes to one end. Monotone input cursors are independent of selection counts inside a burst. Restore saved and unread tail with eight-lane permutation plus dual vector stores, avoiding a scalar branch for each reserved item; fewer than16 final items use scalar placement. The exact selected destinations remain disjoint.
Experiment purpose / 试验目的: Full-shape property and exactly-once multiset verification plus exhaustive/adversarial small layouts under GCC9, then prompt official measurement of each still unqualified size. No local timing filter.
*/
#include <immintrin.h>
#include <cstddef>
#pragma GCC target("avx2,popcnt")
#pragma GCC optimize("O3")
struct GapPerm{int 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(32)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,unsigned BURST>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=[&](size_t src){
__m256i w0=ld32(a+src),w1=ld32(a+src+8),w2=ld32(a+src+16),w3=ld32(a+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_load_si256((const __m256i*)gp.p[k0]));
w1=_mm256_permutevar8x32_epi32(w1,_mm256_load_si256((const __m256i*)gp.p[k1]));
w2=_mm256_permutevar8x32_epi32(w2,_mm256_load_si256((const __m256i*)gp.p[k2]));
w3=_mm256_permutevar8x32_epi32(w3,_mm256_load_si256((const __m256i*)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>=32){
if(r-l>=64*BURST&&l-lo>=32*BURST&&hi-r>=32*BURST){
for(unsigned burst=0;burst<BURST;burst++){size_t left=l,right=r-32;l+=32;r-=32;process(left);process(right);}continue;
}
if(r-l>=64&&l-lo>=32&&hi-r>=32){size_t left=l,right=r-32;l+=32;r-=32;process(left);process(right);}
else if(l-lo<32){size_t src=l;l+=32;process(src);}else{r-=32;process(r);}
}
unsigned c=2*GAP;while(l<r)keep[c++]=a[l++];
unsigned i=0;for(;i+16<=c;i+=8){__m256i w=ld32(keep+i);unsigned mask=(unsigned)_mm256_movemask_ps(_mm256_castsi256_ps(w));unsigned negative=__builtin_popcount(mask);w=_mm256_permutevar8x32_epi32(w,_mm256_load_si256((const __m256i*)gp.p[mask]));st32(a+lo,w);st32(a+hi-8,w);lo+=8-negative;hi-=negative;}
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;}if(n>=1000000)gap_partition<1024,8>(n,a);else if(n>=8192)gap_partition<256,2>(n,a);else gap_partition<128,1>(n,a);}