bit32 for Lua 5.1?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

bit32 for Lua 5.1?

Hisham Muhammad
Hi,

By any chance, is there a Lua-5.2-compatible implementation of the
bit32 module written for Lua 5.1?

I suppose that for new code written now using Lua 5.1, using bit32 now
would reduce the porting efforts when the codebase transitions to Lua
5.2 later.

However, I just need to split parts of a bitfield in 32-bit unsigned
values; given I won't have to deal with signedness, overflow, etc., so
maybe it just makes no difference which one I pick and porting from,
say, bitop to bit32 will be a simple search-and-replace?

-- Hisham

Reply | Threaded
Open this post in threaded view
|

Re: bit32 for Lua 5.1?

Luiz Henrique de Figueiredo
> By any chance, is there a Lua-5.2-compatible implementation of the
> bit32 module written for Lua 5.1?

The simple hack below seems to work (it does compile :) in a 32-bit Linux:

24c24,26
<
---
> typedef unsigned int lua_Unsigned;
> #define luaL_checkunsigned (lua_Unsigned) luaL_checkinteger
> #define lua_pushunsigned lua_pushinteger
159,160c161,162
< LUAMOD_API int luaopen_bit32 (lua_State *L) {
<   luaL_newlib(L, bitlib);
---
> int luaopen_bit32 (lua_State *L) {
>   luaL_register(L, "bit32", bitlib);

Reply | Threaded
Open this post in threaded view
|

Re: bit32 for Lua 5.1?

Hisham Muhammad
On Mon, Jan 17, 2011 at 2:42 PM, Luiz Henrique de Figueiredo
<[hidden email]> wrote:

>> By any chance, is there a Lua-5.2-compatible implementation of the
>> bit32 module written for Lua 5.1?
>
> The simple hack below seems to work (it does compile :) in a 32-bit Linux:
>
> 24c24,26
> <
> ---
>> typedef unsigned int lua_Unsigned;
>> #define luaL_checkunsigned (lua_Unsigned) luaL_checkinteger
>> #define lua_pushunsigned lua_pushinteger
> 159,160c161,162
> < LUAMOD_API int luaopen_bit32 (lua_State *L) {
> <   luaL_newlib(L, bitlib);
> ---
>> int luaopen_bit32 (lua_State *L) {
>>   luaL_register(L, "bit32", bitlib);

Thanks! Preliminary tests show it works fine. I also wrote a rockspec
for it called "bit32 5.2.0alpha" for Lua 5.1. After a few more testing
I'll post it to the LuaRocks repository.

-- Hisham

Reply | Threaded
Open this post in threaded view
|

Re: bit32 for Lua 5.1?

Luiz Henrique de Figueiredo
> Thanks! Preliminary tests show it works fine. I also wrote a rockspec
> for it called "bit32 5.2.0alpha" for Lua 5.1. After a few more testing
> I'll post it to the LuaRocks repository.

Like I said, it was a simple hack for 32-bit Linux. Before it's ready for
prime time, I think you need to include some #defines in lbitlib.c like
those in the 5.2 luaconf.h for the proper definition of lua_Unsigned.

Reply | Threaded
Open this post in threaded view
|

Re: bit32 for Lua 5.1?

Sean Bolton-2
On Jan 18, 2011, at 7:31 AM, Luiz Henrique de Figueiredo wrote:

>> Thanks! Preliminary tests show it works fine. I also wrote a rockspec
>> for it called "bit32 5.2.0alpha" for Lua 5.1. After a few more  
>> testing
>> I'll post it to the LuaRocks repository.
>
> Like I said, it was a simple hack for 32-bit Linux. Before it's  
> ready for
> prime time, I think you need to include some #defines in lbitlib.c  
> like
> those in the 5.2 luaconf.h for the proper definition of lua_Unsigned.

Yes, be careful.  LHF's "simple hack" patch fails a number of the tests
in the lua 5.2.0 alpha test suite (in bitwise.lua).  In particular, in
some of the tests, the most significant bit is lost.

Here's a version of the bit32 library from 5.2.0-alpha, fully back-
ported
to 5.1.4, with all the needed #defines and such:

   http://smbolton.com/lua/lbitlib-5.2.0-alpha-backport1.c

It passes all the bitwise.lua tests on my linux x86 and Mac OS X PPC
systems.  Hisham, feel free to incorporate anything useful from it into
your version; it's all MIT licensed (actually, the only thing original
I wrote were a few comments.)

Cheers,

-Sean


Reply | Threaded
Open this post in threaded view
|

Re: bit32 for Lua 5.1?

Hisham Muhammad
On Tue, Jan 18, 2011 at 5:59 PM, Sean Bolton <[hidden email]> wrote:

> On Jan 18, 2011, at 7:31 AM, Luiz Henrique de Figueiredo wrote:
>>>
>>> Thanks! Preliminary tests show it works fine. I also wrote a rockspec
>>> for it called "bit32 5.2.0alpha" for Lua 5.1. After a few more testing
>>> I'll post it to the LuaRocks repository.
>>
>> Like I said, it was a simple hack for 32-bit Linux. Before it's ready for
>> prime time, I think you need to include some #defines in lbitlib.c like
>> those in the 5.2 luaconf.h for the proper definition of lua_Unsigned.
>
> Yes, be careful.  LHF's "simple hack" patch fails a number of the tests
> in the lua 5.2.0 alpha test suite (in bitwise.lua).  In particular, in
> some of the tests, the most significant bit is lost.
>
> Here's a version of the bit32 library from 5.2.0-alpha, fully back-ported
> to 5.1.4, with all the needed #defines and such:
>
>  http://smbolton.com/lua/lbitlib-5.2.0-alpha-backport1.c
>
> It passes all the bitwise.lua tests on my linux x86 and Mac OS X PPC
> systems.  Hisham, feel free to incorporate anything useful from it into
> your version; it's all MIT licensed (actually, the only thing original
> I wrote were a few comments.)

Many thanks!

I took the liberty to pack and uploaded this as a rock:

http://luarocks.org/repositories/rocks/bit32-5.2.0alpha.1-1.rockspec

-- Hisham

Reply | Threaded
Open this post in threaded view
|

Re: bit32 for Lua 5.1?

Luiz Henrique de Figueiredo
In reply to this post by Sean Bolton-2
> Here's a version of the bit32 library from 5.2.0-alpha, fully back-
> ported
> to 5.1.4, with all the needed #defines and such:
>
>   http://smbolton.com/lua/lbitlib-5.2.0-alpha-backport1.c

To avoid having to edit much of the original code, I suggest adding the
defines below to your define section at the top:

#define LUAMOD_API
#define LUA_BITLIBNAME "bit32"
#define luaL_newlib(x,y) luaL_register(x,LUA_BITLIBNAME,y)

Also, please preserve the RCS Id line so that we know which version it uses.

lua_pushunsigned can be static.

Perhaps it would be easier to pack all backport code into a lbitlib.h and
just add one line to lbitlib.c, ideally at the very top:

#include "lbitlib.h"

Reply | Threaded
Open this post in threaded view
|

Re: bit32 for Lua 5.1?

David Manura
On Wed, Jan 19, 2011 at 9:09 PM, Luiz Henrique de Figueiredo
<[hidden email]> wrote:
> Perhaps it would be easier to pack all backport code into a lbitlib.h and
> just add one line to lbitlib.c, ideally at the very top:
> #include "lbitlib.h"

I'd prefer that.  Taking it one step further, you could leave
lbitlib.c unchanged but create a "lbitlib51.c" file that does

  #define LUAMOD_API
  #define LUA_BITLIBNAME  "bit32"
  #define luaL_newlib(x,y) luaL_register(x,LUA_BITLIBNAME,y)
  // ... etc...
  #include "lbitlib.c"