1 Index: lua-5.1.4/src/Makefile
2 ===================================================================
3 --- lua-5.1.4.orig/src/Makefile 2008-09-25 13:08:31.000000000 +0200
4 +++ lua-5.1.4/src/Makefile 2008-09-25 13:08:43.000000000 +0200
6 lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \
7 lundump.o lvm.o lzio.o lnum.o
8 LIB_O= lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o \
9 - lstrlib.o loadlib.o linit.o lposix.o
10 + lstrlib.o loadlib.o linit.o lposix.o lbitlib.o
14 Index: lua-5.1.4/src/bit_limits.h
15 ===================================================================
16 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
17 +++ lua-5.1.4/src/bit_limits.h 2008-09-25 13:09:16.000000000 +0200
19 +#define BITLIB_FLOAT_BITS 53
20 +#define BITLIB_FLOAT_MAX 0xfffffffffffffL
21 +#define BITLIB_FLOAT_MIN (-0x10000000000000L)
22 +#define BITLIB_FLOAT_UMAX 0x1fffffffffffffUL
23 Index: lua-5.1.4/src/lbitlib.c
24 ===================================================================
25 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
26 +++ lua-5.1.4/src/lbitlib.c 2008-09-25 13:05:15.000000000 +0200
28 +/* Bitwise operations library */
29 +/* (c) Reuben Thomas 2000-2008 */
30 +/* See README for license */
36 +#include "bit_limits.h"
39 +/* FIXME: Assume lua_Integer is int */
40 +#define LUA_INTEGER_MAX INT_MAX
41 +#define LUA_INTEGER_MIN INT_MIN
43 +/* FIXME: Assume uint is an unsigned lua_Integer */
44 +typedef unsigned int lua_UInteger;
45 +#define LUA_UINTEGER_MAX UINT_MAX
48 +/* Bit type size and limits */
51 + (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? \
52 + BITLIB_FLOAT_BITS : (CHAR_BIT * sizeof(lua_Integer)))
54 +/* This code may give warnings if BITLIB_FLOAT_* are too big to fit in
55 + long, but that doesn't matter since in that case they won't be
58 + (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_MAX : LUA_INTEGER_MAX)
61 + (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_MIN : LUA_INTEGER_MIN)
64 + (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_UMAX : LUA_UINTEGER_MAX)
67 +/* Define TOBIT to get a bit value */
70 +#define TOBIT(L, n, res) \
71 + ((void)(res), luaL_checkinteger((L), (n)))
76 +/* FIXME: Assume lua_Number fits in a double (use of fmod). */
77 +#define TOBIT(L, n, res) \
78 + ((lua_Integer)(((res) = fmod(luaL_checknumber(L, (n)), (double)BIT_UMAX + 1.0)), \
79 + (res) > BIT_MAX ? ((res) -= (double)BIT_UMAX, (res) -= 1) : \
80 + ((res) < BIT_MIN ? ((res) += (double)BIT_UMAX, (res) += 1) : (res))))
84 +#define BIT_TRUNCATE(i) \
90 + The macros MONADIC and VARIADIC only deal with bitwise operations.
92 + LOGICAL_SHIFT truncates its left-hand operand before shifting so
93 + that any extra bits at the most-significant end are not shifted
96 + ARITHMETIC_SHIFT does not truncate its left-hand operand, so that
97 + the sign bits are not removed and right shift work properly.
100 +#define MONADIC(name, op) \
101 + static int bit_ ## name(lua_State *L) { \
103 + lua_pushinteger(L, BIT_TRUNCATE(op TOBIT(L, 1, f))); \
107 +#define VARIADIC(name, op) \
108 + static int bit_ ## name(lua_State *L) { \
110 + int n = lua_gettop(L), i; \
111 + lua_Integer w = TOBIT(L, 1, f); \
112 + for (i = 2; i <= n; i++) \
113 + w op TOBIT(L, i, f); \
114 + lua_pushinteger(L, BIT_TRUNCATE(w)); \
118 +#define LOGICAL_SHIFT(name, op) \
119 + static int bit_ ## name(lua_State *L) { \
121 + lua_pushinteger(L, BIT_TRUNCATE(BIT_TRUNCATE((lua_UInteger)TOBIT(L, 1, f)) op \
122 + (unsigned)luaL_checknumber(L, 2))); \
126 +#define ARITHMETIC_SHIFT(name, op) \
127 + static int bit_ ## name(lua_State *L) { \
129 + lua_pushinteger(L, BIT_TRUNCATE((lua_Integer)TOBIT(L, 1, f) op \
130 + (unsigned)luaL_checknumber(L, 2))); \
139 +ARITHMETIC_SHIFT(lshift, <<)
140 +LOGICAL_SHIFT(rshift, >>)
141 +ARITHMETIC_SHIFT(arshift, >>)
143 +static const struct luaL_reg bitlib[] = {
144 + {"cast", bit_cast},
145 + {"bnot", bit_bnot},
146 + {"band", bit_band},
148 + {"bxor", bit_bxor},
149 + {"lshift", bit_lshift},
150 + {"rshift", bit_rshift},
151 + {"arshift", bit_arshift},
155 +LUALIB_API int luaopen_bit (lua_State *L) {
156 + luaL_register(L, "bit", bitlib);
157 + lua_pushnumber(L, BIT_BITS);
158 + lua_setfield(L, -2, "bits");
161 Index: lua-5.1.4/src/linit.c
162 ===================================================================
163 --- lua-5.1.4.orig/src/linit.c 2008-09-25 13:08:11.000000000 +0200
164 +++ lua-5.1.4/src/linit.c 2008-09-25 13:08:27.000000000 +0200
166 {LUA_MATHLIBNAME, luaopen_math},
167 {LUA_DBLIBNAME, luaopen_debug},
168 {LUA_POSIXLIBNAME, luaopen_posix},
169 + {LUA_BITLIBNAME, luaopen_bit},
173 Index: lua-5.1.4/src/lualib.h
174 ===================================================================
175 --- lua-5.1.4.orig/src/lualib.h 2008-09-25 13:07:29.000000000 +0200
176 +++ lua-5.1.4/src/lualib.h 2008-09-25 13:08:06.000000000 +0200
178 #define LUA_POSIXLIBNAME "posix"
179 LUALIB_API int (luaopen_posix) (lua_State *L);
181 +#define LUA_BITLIBNAME "bit"
182 +LUALIB_API int (luaopen_bit) (lua_State *L);
185 /* open all previous libraries */
186 LUALIB_API void (luaL_openlibs) (lua_State *L);