[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Question about the condition in `luaH_getint` function
- From: Roberto Ierusalimschy <roberto@...>
- Date: Tue, 18 Apr 2023 16:51:34 -0300
> Wouldn't it be better to use a more efficient test such as the following
> instead?
>
> !isrealasize(t) && ((l_castS2U(key)-1u) & ~(((lua_Unsigned)t->alimit)-1u)) <
> t->alimit
>
> This test does some bit hacks to test that key-1 is smaller than the next
> power of two of the limit.
> This is done by masking of bit p from (key-1) through ~(t->alimit-1) as p is
> chosen such that it is the integer for which 2^(p+1) >= t->alimit > 2^p
> holds.
> This results in (key-1) & ~(t->alimit-1) staying larger or equal to 2^(p+1)
> when key-1 >= 2^(p+1) and getting smaller than 2^p when key-1 < 2^(p+1) as
> bit p is masked off through ~(t->alimit-1) allowing t->alimit being used as
> the compare value.
Really smart :-) Is there a reference for that or you created it yourself,
on demand?
-- Roberto