Only iterate over set bits in random merge

This commit is contained in:
Marshall Lochbaum 2023-05-01 21:49:40 -04:00
parent bc7275a91b
commit 8191aa6da2

View File

@ -434,7 +434,7 @@ B rand_deal_c1(B t, B x) {
// MergeShuffle // MergeShuffle
usz sh = 0; usz sh = 0;
usz thr = 1<<24; usz thr = 1<<18;
while ((xi >> sh) > thr) sh++; while ((xi >> sh) > thr) sh++;
usz q = 1 << sh; usz q = 1 << sh;
u64 n = xi; u64 n = xi;
@ -454,13 +454,16 @@ B rand_deal_c1(B t, B x) {
usz t = i; usz t = i;
usz j = (n*(p + w)) >> sh; usz j = (n*(p + w)) >> sh;
usz e = (n*(p + 2*w)) >> sh; usz e = (n*(p + 2*w)) >> sh;
u64 r = wyrand(&seed); // Random bits for (usz i0 = i; ; i0 += 64) {
while (1) { for (u64 r = wyrand(&seed); r; r&=r-1) {
u64 bit = r&1; r>>=1; i = i0 + CTZ(r);
if (bit) { if (j == e) break; usz c=rp[i]; rp[i]=rp[j]; rp[j]=c; j++; } if (i > j) { i=j; goto tail; }
else { if (i == j) break; } if (j == e) goto tail;
if (++i%64==0) r = wyrand(&seed); usz c=rp[j]; rp[j]=rp[i]; rp[i]=c;
j++;
}
} }
tail:
for (; i < e; i++) { for (; i < e; i++) {
usz j = wy2u0k(wyrand(&seed), 1+i-t) + t; usz j = wy2u0k(wyrand(&seed), 1+i-t) + t;
usz c=rp[j]; rp[j]=rp[i]; rp[i]=c; usz c=rp[j]; rp[j]=rp[i]; rp[i]=c;