Fast cases for short •rand.Deal𝕩

This commit is contained in:
Marshall Lochbaum 2023-05-03 21:16:32 -04:00
parent 8191aa6da2
commit 6cc9d40490

View File

@ -425,12 +425,28 @@ B rand_range_c2(B t, B w, B x) {
return taga(r);
}
extern Arr* bitUD[3]; // from fns.c
extern B bit2x[2];
B rand_deal_c1(B t, B x) {
i32 xi = o2i(x);
if (RARE(xi<0)) thrM("(rand).Deal: Argument cannot be negative");
if (xi==0) return emptyIVec();
if (RARE(xi<=1)) {
if (xi<0) thrM("(rand).Deal: Argument cannot be negative");
return xi==0? emptyIVec() : taga(ptr_inc(bitUD[xi]));
}
RAND_START;
i32* rp; B r = m_i32arrv(&rp, xi);
B r;
if (xi == 2) {
r = incG(bit2x[wyrand(&seed)&1]);
} else if (LIKELY(xi <= 128)) {
i8* rp; r = m_i8arrv(&rp, xi);
for (usz i = 0; i < xi; i++) rp[i] = i;
for (usz i = 0; i < xi-1; i++) {
usz j = wy2u0k(wyrand(&seed), xi-i) + i;
usz c=rp[j]; rp[j]=rp[i]; rp[i]=c;
}
} else {
i32* rp; r = m_i32arrv(&rp, xi);
// MergeShuffle
usz sh = 0;
@ -470,7 +486,7 @@ B rand_deal_c1(B t, B x) {
}
}
}
}
RAND_END;
return r;
}
@ -478,10 +494,11 @@ B rand_deal_c1(B t, B x) {
B rand_deal_c2(B t, B w, B x) {
i32 wi = o2i(w);
i32 xi = o2i(x);
if (RARE(wi<0)) thrM("(rand).Deal: 𝕨 cannot be negative");
if (RARE(xi<0)) thrM("(rand).Deal: 𝕩 cannot be negative");
if (RARE(wi>xi)) thrM("(rand).Deal: 𝕨 cannot exceed 𝕩");
if (RARE(wi<0)) thrM("(rand).Deal: 𝕨 cannot be negative");
if (wi==0) return emptyIVec();
if (RARE(wi>xi)) thrM("(rand).Deal: 𝕨 cannot exceed 𝕩");
if (wi==xi) return rand_deal_c1(t, x);
B r;
RAND_START;
if (wi > xi/64) {