Making Lua BitOp work with LNUM patch

  • From: Methos <methos.oldest@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 7 Nov 2013 13:25:18 -0500

Hi,

(Initially sent on " lua-l@xxxxxxxxxxxxx" but somebody suggested to send it
over here)

I am writing some Lua code for OpenWRT platform that uses Lua BitOp(
http://bitop.luajit.org/).
I am having problem in getting expected result.

1. Initial problem:

On OpenWRT, Lua is applied with LNUM patch that changes the number
representation internally. Initially the compilation of Lua BitOp was
failing with an error:

"Unknown number type, check LUA_NUMBER_* in luaconf.h"

After some investigation with luaconf.h on native Linux platform and
OpenWRT platform, I came to know about LNUM patch and how it adds a
different kind of support for double/ints for OpenWRT (
http://stackoverflow.com/questions/945731/what-is-the-maximum-value-of-a-number-in-lua#answer-947763
).

So I modified bit.c and added

#if defined(LUA_NUMBER_DOUBLE) || defined(LNUM_DOUBLE)

where ever LUA_NUMBER_DOUBLE was "#ifdef"ed in "bit.c"

With this change, the library started compiling for OpenWRT platform and
bit.so was generated.


2. Sanity test failure problem:

I transferred bit.so and bittest.lua (a sanity test script provided by the
library) to my OpenWRT router(attitude-adjustment, Lua5.1). But after
running it, the code fails with the error:

"lua: bit.tobit test failed (got 409089, expected 277312)"

Some more investigation of that script code shows that internally, at one
point, bit.so is returning
4294967295 instead of -1(The latter value being returned on the native
Linux platform where the tests succeed).

Clearly, it is not converting an unsigned number max to signed number -1
but I am not sure why that is happening (I thought DOUBLE in either case is
8 bytes so that should not be a problem, but I am aware that my
understanding is somehow wrong).

3. Another unsuccessful attempt at fixing:

I also tried compiling Lua with LNUM_INT32 enabled and in the bit.c, I
enabled LUA_NUMBER_INT instead of code for LUA_NUMBER_DOUBLE. Then I
transferred the newly compiled Lua interpreter binary and once again tried
to run bittest.lua with it. Even that failed with the same result.

Hopefully somebody has faced this problem before and can help me out.

Thanks.

Other related posts: