From 85f54b2b254371787ca89c7a72060706ebefe944 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Sun, 13 Nov 2022 17:59:50 -0500 Subject: [PATCH] =?UTF-8?q?Check=20for=20a=20small=20return=20type=20in=20?= =?UTF-8?q?large-range=20/=E2=81=BC=20with=20a=20sparse=20table?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/builtins/slash.c | 75 ++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/src/builtins/slash.c b/src/builtins/slash.c index 434dd803..f0c01a10 100644 --- a/src/builtins/slash.c +++ b/src/builtins/slash.c @@ -39,16 +39,16 @@ // SHOULD do something for odd cell widths in Replicate // Indices inverse (/⁼), a lot like Group -// COULD always give a squeezed result, sometimes expensive -// SHOULD sort large-range 𝕩 to find minimum result type +// Always gives a squeezed result for integer 𝕩 // Boolean 𝕩: just count 1s // Long i8 and i16 𝕩: count into zeroed buffer before anything else // Only zero positive part; if total is too small there were negatives // Cutoff is set so short 𝕩 gives a result of the same type // Scan for strictly ascending 𝕩 // COULD vectorize with find-compare -// COULD find descending too // Unsigned maximum for integers to avoid a separate negative check +// If (≠÷⌈´)𝕩 is small, find result type with a sparse u8 table +// COULD use a u16 table for i32 𝕩 to detect i16 result #include "../core.h" #include "../utils/mut.h" @@ -913,26 +913,45 @@ B slash_im(B t, B x) { rp[sum>0] = sum; rp[0] = xia - sum; r = num_squeeze(r); break; } +#define IIND_INT(N) \ + if (xp[0]<0) thrM("/⁼: Argument cannot contain negative numbers"); \ + usz a=1; while (axp[a-1]) a++; \ + u##N max=xp[a-1]; \ + if (amax) max=c; } \ + if ((i##N)max<0) thrM("/⁼: Argument cannot contain negative numbers"); \ + usz ria = max + 1; \ + if (xia < ria/8) { \ + u8 maxcount = 0; \ + TALLOC(u8, tab, ria); \ + for (usz i=0; i=16 && maxcount<127) { \ + i8* rp; r = m_i8arrv(&rp, ria); for (usz i=0; ixp[a-1]) a++; \ - u##N max=xp[a-1]; \ - if (a==xia) { /* Sorted unique argument */ \ - usz ria = max + 1; \ - u64* rp; r = m_bitarrv(&rp, ria); \ - for (usz i=0; imax) max=c; } \ - if ((i##N)max<0) thrM("/⁼: Argument cannot contain negative numbers"); \ - usz ria = max+1; \ - i##N* rp; r = m_i##N##arrv(&rp, ria); for (usz i=0; im/2) thrM("/⁼: Argument cannot contain negative numbers"); \ i32* rp; r = m_i32arrv(&rp, ria); for (usz i=0; imax?c:max; if (c<0) thrM("/⁼: Argument cannot contain negative numbers"); } - usz ria = max+1; - if (i==xia) { - u64* rp; r = m_bitarrv(&rp, ria); for (usz i=0; i