From 00a9dc03be3fd15b6412d96f4cf8f7a153032138 Mon Sep 17 00:00:00 2001 From: dzaima Date: Thu, 26 Sep 2024 02:44:50 +0300 Subject: [PATCH] fast path for FFI array range checking --- src/ffi.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/ffi.c b/src/ffi.c index 6e362678..c24cdf20 100644 --- a/src/ffi.c +++ b/src/ffi.c @@ -291,12 +291,28 @@ static u32 styG(B x) { if (nonNum.u!=m_f64(0).u) thrF("FFI: Array provided for %S%S contained %S", ref, desc, genericDesc(nonNum)); if (min==max) return; - incG(x); x = elNum(TI(x,elType))? x : taga(cpyF64Arr(x)); - + u8 xe = TI(x,elType); + incG(x); + if (!elNum(xe)) { + x = taga(cpyF64Arr(x)); + xe = TI(x,elType); + } + i64 emax; + switch (xe) { + case el_i8: emax=I8_MAX; goto echk; + case el_i16: emax=I16_MAX; goto echk; + case el_i32: emax=I32_MAX; goto echk; + echk:; + i64 emin = -emax-1; + if (emin>=min && emax<=max) goto ok; + default:; // fallthrough + } i64 buf[2]; - if (!getRange_fns[TI(x,elType)](tyany_ptr(x), buf, IA(x))) thrF("FFI: Array provided for %S%S contained non-integer", ref, desc); + if (!getRange_fns[xe](tyany_ptr(x), buf, IA(x))) thrF("FFI: Array provided for %S%S contained non-integer", ref, desc); if (buf[0]max) thrF("FFI: Array provided for %S%S contained %l", ref, desc, buf[1]); + + ok:; decG(x); } static bool elChrOk(B x, u64 max) {