lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, Jan 7, 2011 at 2:41 PM, Leo Razoumov <slonik.az@gmail.com> wrote:
> Hi List,
> In Lua-5.1.4 macro
>
> #define ceillog2(x)     (luaO_log2((x)-1) + 1)
>
> gives wrong answer when x is 0.
> ceillog2(0) => 32 (should be 1).
>
> I attached below a standalone code snippet to demonstrate the problem.
> I use Lua-5.1.4 on Ubuntu-9.04 x86 (32 bits).
> There are several  ways to fix it and I like _none_ of them.
>
> (1) Ignore the bug. Just make sure that x>0 always.
>
> (2) Convert luaO_log2 (unsigned int x) to signed int and use "if"
> statement for negative arguments inside it.
>
> (3) Put if statement into the ceillog2 macro using temp. variable and
> GCC extension that allows statements inside expressions (GCC specific
> and not C89)
>
> I am leaning towards (2).
>
> --Leo--
>

Whilst looking at whether this is also in 5.2-alpha, I noticed that
the definition and declaration of luaO_ceillog2 have different types,
which also looks like a bug:

LUAI_FUNC int luaO_ceillog2 (lu_int32 x);
int luaO_ceillog2 (unsigned int x) {
  /* ... */
}