add STRICT_ALIGN, use by default in FOR_BUILD

This commit is contained in:
dzaima 2025-06-14 19:28:00 +03:00
parent 1c77e3b06d
commit 597e59c085
3 changed files with 40 additions and 3 deletions

View File

@ -1513,13 +1513,18 @@ B bitcast_impl(B el0, B el1, B x) {
if (rl>=USZ_MAX) thrM("•bit._cast: output too large");
B r = convert(xct, x);
u8 rt = typeOfCast(rct);
if (rt==t_bitarr && (!reusable(r) || ARR_IS_SLICE(TY(r)))) {
if (rt==t_bitarr) {
if (reusable(r) && !ARR_IS_SLICE(TY(r))) {
REUSE(r);
} else {
r = taga(copy(xct, r));
}
} else if (!reusable(r)) {
B r0 = incG(r);
Arr* r2 = TI(r,slice)(r, 0, IA(r));
r = taga(arr_shSetI(r2, xr, shObj(r0)));
decG(r0);
goto possibly_unaligned;
} else {
REUSE(r);
#if VERIFY_TAIL
@ -1527,7 +1532,28 @@ B bitcast_impl(B el0, B el1, B x) {
FINISH_OVERALLOC(a(r), offsetof(TyArr,a)+IA(r)/8, offsetof(TyArr,a) + (BIT_N(IA(r))<<3));
}
#endif
goto possibly_unaligned;
}
if (0) {
possibly_unaligned:;
#if STRICT_ALIGN || FOR_BUILD
if (IS_TYSLICE(TY(r))) {
assert(rct.s != 1 && xct.s != 1); // rt==t_bitarr handles rct.s==1, and xct.s==1 never currently makes a slice (..though it could)
u8 arrt = SLICE_TO_ARR(TY(r));
void* r0p = tyslicev_ptr(a(r));
if (ptr2u64(r0p) & ((rct.s>>3) - 1)) {
Arr* r1;
void* r2p = m_tyarrp(&r1, xct.s>>3, IA(r), arrt);
memcpy(r2p, r0p, IA(r)*(xct.s>>3));
arr_shCopyUnchecked(r1, r);
decG(r);
r = taga(r1);
}
}
#endif
}
return set_bit_result(r, rt, xr, rl, sh);
}

View File

@ -322,7 +322,6 @@ r←•MakeRand 1 ⋄ ! 1¨⊸≡ ∊{𝕊: 500 r.Deal 1000}¨ ↕4
%USE eqvar {t𝕩18163264 {t•bit._cast 𝕩}_eqvar 4=128}¨ 44
! 101 816•bit._cast c02'a'+11 •rand.Range 26 ! 102 c
! 101 816•bit._cast c 2'a'+11 •rand.Range 26 ! 102 c
832•bit._cast 37 %% 100992003
!"•bit._cast: incompatible lengths" % 18•bit._cast 10
!"•bit._cast: incompatible lengths" % 832•bit._cast 20
!"•bit._cast: incompatible lengths" % 832•bit._cast 100

12
test/cases/unaligned.bqn Normal file
View File

@ -0,0 +1,12 @@
832•bit._cast 37 %% 100992003
(
r(•args).GetRand@
{ ·dw·𝕊skswe:
a0 {
dw=64? 𝕩01;
𝕩 r.Range e
} (1+8×1dw÷sw)
{•internal.Keep¨ swdw •bit._cast 1𝕩•internal.Variation a0}¨ "Inc"¨ sk¨ "AS"
}˜ "b"12, "i8"8100, "i16"161e4, "i32"321e9, "f64"640
)