From 77dfaf75a49455ca8c2f8812ab34db4088e6f970 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Fri, 4 Feb 2022 17:21:24 -0500 Subject: [PATCH] =?UTF-8?q?Fix=20=E2=80=A2bit.=5Fcast=20handling=20of=20bo?= =?UTF-8?q?olean=20output=20with=20shared=20or=20slice=20input?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/builtins/sysfn.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index 9aad8b02..84d94016 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -882,6 +882,16 @@ B convert(CastType t, B x) { default: thrM("•bit._cast: unsupported input width"); } } +TyArr* copy(CastType t, B x) { + switch (t.s) { + case 1: return cpyBitArr(x); + case 8: return t.t=='c' ? cpyC8Arr (x) : cpyI8Arr (x); + case 16: return t.t=='c' ? cpyC16Arr(x) : cpyI16Arr(x); + case 32: return t.t=='c' ? cpyC32Arr(x) : cpyI32Arr(x); + case 64: return cpyF64Arr(x); + default: thrM("•bit._cast: unsupported input width"); + } +} u8 typeOfCast(CastType t) { switch (t.s) { case 1: return t_bitarr; @@ -904,13 +914,15 @@ B bitcast_c1(Md1D* d, B x) { B f = d->f; if (zl*zt.s != s) thrM("•bit._cast: incompatible lengths"); // Convert to input type B r = convert(xt, x); - if (v(r)->refc!=1) { + u8 rt = typeOfCast(zt); + if (rt==t_bitarr && (v(r)->refc!=1 || IS_SLICE(v(r)->type))) { + r = taga(copy(xt, r)); + } else if (v(r)->refc!=1) { r = taga(TI(r,slice)(r, 0, a(r)->ia)); sprnk(v(r),xr); } // Cast to output type - u8 rt = typeOfCast(zt); if (IS_SLICE(v(r)->type)) rt = TO_SLICE(rt); - v(r)->type = rt; + v(r)->type = IS_SLICE(v(r)->type) ? TO_SLICE(rt) : rt; // Adjust shape if (xr<=1) { Arr* a = a(r);