From 79bd6fc689dcb43db9ef38e2e11397f2b2882a30 Mon Sep 17 00:00:00 2001 From: dzaima Date: Mon, 30 May 2022 02:07:25 +0300 Subject: [PATCH] use shProd more --- src/builtins/md1.c | 24 ++++++++++++------------ src/builtins/select.c | 5 ++--- src/builtins/sfns.c | 3 +-- src/builtins/sysfn.c | 2 +- src/core/harr.c | 4 ++-- src/core/stuff.c | 4 +--- 6 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/builtins/md1.c b/src/builtins/md1.c index b1eb8ae7..b36931e5 100644 --- a/src/builtins/md1.c +++ b/src/builtins/md1.c @@ -388,18 +388,18 @@ static B m1c2(B t, B f, B w, B x) { // consumes w,x return r; } -#define S_SLICES(X) \ - BSS2A X##_slc = TI(X,slice); \ - usz X##_csz = 1; \ - usz X##_cr = rnk(X)-1; \ - ShArr* X##_csh; \ - if (X##_cr>1) { \ - X##_csh = m_shArr(X##_cr); \ - for (usz i = 0; i < X##_cr; i++) { \ - usz v = a(X)->sh[i+1]; \ - X##_csz*= v; \ - X##_csh->a[i] = v; \ - } \ +#define S_SLICES(X) \ + BSS2A X##_slc = TI(X,slice); \ + usz X##_csz = 1; \ + usz X##_cr = rnk(X)-1; \ + ShArr* X##_csh; \ + if (X##_cr>1) { \ + X##_csh = m_shArr(X##_cr); \ + NOUNROLL for (usz i = 0; i < X##_cr; i++) { \ + usz v = a(X)->sh[i+1]; \ + X##_csz*= v; \ + X##_csh->a[i] = v; \ + } \ } else if (X##_cr!=0) X##_csz*= a(X)->sh[1]; #define SLICE(X, S) ({ Arr* r_ = X##_slc(inc(X), S, X##_csz); arr_shSetI(r_, X##_cr, X##_csh); taga(r_); }) diff --git a/src/builtins/select.c b/src/builtins/select.c index a325105e..320964a3 100644 --- a/src/builtins/select.c +++ b/src/builtins/select.c @@ -16,9 +16,8 @@ B select_c1(B t, B x) { ur xr = rnk(x); if (xr==0) thrM("⊏: Argument cannot be rank 0"); if (a(x)->sh[0]==0) thrF("⊏: Argument shape cannot start with 0 (%H ≡ ≢𝕩)", x); - usz ia = 1; - for (i32 i = 1; i < xr; i++) ia*= a(x)->sh[i]; - Arr* r = TI(x,slice)(inc(x),0, ia); + usz ia = shProd(a(x)->sh, 1, xr); + Arr* r = TI(x,slice)(inc(x), 0, ia); usz* sh = arr_shAlloc(r, xr-1); if (sh) for (i32 i = 1; i < xr; i++) sh[i-1] = a(x)->sh[i]; decG(x); diff --git a/src/builtins/sfns.c b/src/builtins/sfns.c index 0d5fc9ba..13aed77a 100644 --- a/src/builtins/sfns.c +++ b/src/builtins/sfns.c @@ -1132,8 +1132,7 @@ B transp_c1(B t, B x) { usz ia = a(x)->ia; usz* xsh = a(x)->sh; usz h = xsh[0]; - usz w = xsh[1]; - for (usz i = 2; RARE(i < xr); i++) w*= a(x)->sh[i]; + usz w = xsh[1] * shProd(a(x)->sh, 2, xr); Arr* r; usz xi = 0; diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index 114705bd..3e9fb2d7 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -1063,7 +1063,7 @@ B bitcast_impl(B el0, B el1, B x) { sh = zsh; } sh[xr-1]=zl; - usz ia=zl; for (usz i=0;iia=ia; + a(r)->ia = zl*shProd(sh, 0, xr-1); } return r; } diff --git a/src/core/harr.c b/src/core/harr.c index 662291b7..3a7c6f45 100644 --- a/src/core/harr.c +++ b/src/core/harr.c @@ -35,8 +35,8 @@ B toKCells(B x, ur k) { assert(isArr(x) && k<=rnk(x) && k>=0); ur xr = rnk(x); usz* xsh = a(x)->sh; ur cr = xr-k; - usz cam = 1; for (i32 i = 0; i < k ; i++) cam*= xsh[i]; - usz csz = 1; for (i32 i = k; i < xr; i++) csz*= xsh[i]; + usz cam = shProd(xsh, 0, k); + usz csz = shProd(xsh, k, xr); ShArr* csh; if (cr>1) { diff --git a/src/core/stuff.c b/src/core/stuff.c index 9a5eed97..57789069 100644 --- a/src/core/stuff.c +++ b/src/core/stuff.c @@ -846,9 +846,7 @@ void g_pst(void) { vm_pstLive(); fflush(stdout); fflush(stderr); } Arr* a = (Arr*)x; if (prnk(x)<=1) assert(a->sh == &a->ia); else { - u64 shProduct = 1; - for (usz i=0; i < prnk(x); i++) shProduct*= a->sh[i]; - assert(shProduct == a->ia); + assert(shProd(a->sh, 0, prnk(x)) == a->ia); VALIDATE(tag(shObjP(x),OBJ_TAG)); } }