extract cycling reshape implementation from shape_c2

currently somewhat-inefficient, but separating out for reference, to allow the moving commit to largely not change anything
This commit is contained in:
dzaima 2025-05-29 22:59:05 +03:00
parent d0ab742313
commit dd7c21ed86

View File

@ -205,6 +205,7 @@ static void shape_c2_prim0(B c) {
} }
#define SHAPE_C2_PRIM1(ID, GET) if (ID!=n_atop & ID!=n_floor & ID!=n_reverse & ID!=n_take) thrF("𝕨⥊𝕩: 𝕨 must consist of natural numbers or ∘ ⌊ ⌽ ↑ (contained %B)", GET) #define SHAPE_C2_PRIM1(ID, GET) if (ID!=n_atop & ID!=n_floor & ID!=n_reverse & ID!=n_take) thrF("𝕨⥊𝕩: 𝕨 must consist of natural numbers or ∘ ⌊ ⌽ ↑ (contained %B)", GET)
B reshape_cycle(usz nia, usz xia, B x, ur nr, ShArr* sh);
B shape_c2(B t, B w, B x); B shape_c2(B t, B w, B x);
B shape_c2_01(usz wia, B w, B x) { B shape_c2_01(usz wia, B w, B x) {
switch (wia) { default: UD; switch (wia) { default: UD;
@ -304,10 +305,23 @@ B shape_c2(B t, B w, B x) {
if (nia <= xia) { if (nia <= xia) {
return truncReshape(x, xia, nia, nr, sh); return truncReshape(x, xia, nia, nr, sh);
} else { } else {
return reshape_cycle(nia, xia, x, nr, sh);
}
} else {
r = reshape_one(nia, x);
}
return taga(arr_shSetUO(r,nr,sh));
}
B reshape_cycle(usz nia, usz xia, B x, ur nr, ShArr* sh) {
assert(nia > xia);
Arr* r;
if (xia <= 1) { if (xia <= 1) {
if (RARE(xia == 0)) thrM("𝕨⥊𝕩: Empty 𝕩 and non-empty result"); if (RARE(xia == 0)) thrM("𝕨⥊𝕩: Empty 𝕩 and non-empty result");
x = TO_GET(x, 0); x = TO_GET(x, 0);
goto unit;
r = reshape_one(nia, x);
return taga(arr_shSetUO(r,nr,sh));
} }
if (xia <= nia/2) x = squeeze_any(x); if (xia <= nia/2) x = squeeze_any(x);
@ -363,6 +377,7 @@ B shape_c2(B t, B w, B x) {
bf = (u64)nia<<xk; bf = (u64)nia<<xk;
memcpy(rp, tyany_ptr(x), bi); memcpy(rp, tyany_ptr(x), bi);
} }
decG(x); decG(x);
if (bi<=8 && !(bi & (bi-1))) { if (bi<=8 && !(bi & (bi-1))) {
// Divisor of 8: write words // Divisor of 8: write words
@ -377,11 +392,6 @@ B shape_c2(B t, B w, B x) {
u64 e=bi; for (; e+bi<=bf; e+=bi) memcpy(rp+e, rp, bi); u64 e=bi; for (; e+bi<=bf; e+=bi) memcpy(rp+e, rp, bi);
if (e<bf) memcpy(rp+e, rp, bf-e); if (e<bf) memcpy(rp+e, rp, bf-e);
} }
}
} else {
unit:;
r = reshape_one(nia, x);
}
return taga(arr_shSetUO(r,nr,sh)); return taga(arr_shSetUO(r,nr,sh));
} }