[ramips] select fon20n mach
[openwrt/svn-archive/archive.git] / package / lua / patches / 410-bitlib_25-embedded.patch
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
5 @@ -29,7 +29,7 @@
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
11
12 LUA_T= lua
13 LUA_O= lua.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
18 @@ -0,0 +1,4 @@
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
27 @@ -0,0 +1,133 @@
28 +/* Bitwise operations library */
29 +/* (c) Reuben Thomas 2000-2008 */
30 +/* See README for license */
31 +
32 +#include "lua.h"
33 +#include "lauxlib.h"
34 +#include "limits.h"
35 +
36 +#include "bit_limits.h"
37 +
38 +
39 +/* FIXME: Assume lua_Integer is int */
40 +#define LUA_INTEGER_MAX INT_MAX
41 +#define LUA_INTEGER_MIN INT_MIN
42 +
43 +/* FIXME: Assume uint is an unsigned lua_Integer */
44 +typedef unsigned int lua_UInteger;
45 +#define LUA_UINTEGER_MAX UINT_MAX
46 +
47 +
48 +/* Bit type size and limits */
49 +
50 +#define BIT_BITS \
51 + (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? \
52 + BITLIB_FLOAT_BITS : (CHAR_BIT * sizeof(lua_Integer)))
53 +
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
56 + used. */
57 +#define BIT_MAX \
58 + (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_MAX : LUA_INTEGER_MAX)
59 +
60 +#define BIT_MIN \
61 + (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_MIN : LUA_INTEGER_MIN)
62 +
63 +#define BIT_UMAX \
64 + (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_UMAX : LUA_UINTEGER_MAX)
65 +
66 +
67 +/* Define TOBIT to get a bit value */
68 +#ifdef BUILTIN_CAST
69 +#define
70 +#define TOBIT(L, n, res) \
71 + ((void)(res), luaL_checkinteger((L), (n)))
72 +#else
73 +#include <stdint.h>
74 +#include <math.h>
75 +
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))))
81 +#endif
82 +
83 +
84 +#define BIT_TRUNCATE(i) \
85 + ((i) & BIT_UMAX)
86 +
87 +
88 +/* Operations
89 +
90 + The macros MONADIC and VARIADIC only deal with bitwise operations.
91 +
92 + LOGICAL_SHIFT truncates its left-hand operand before shifting so
93 + that any extra bits at the most-significant end are not shifted
94 + into the result.
95 +
96 + ARITHMETIC_SHIFT does not truncate its left-hand operand, so that
97 + the sign bits are not removed and right shift work properly.
98 + */
99 +
100 +#define MONADIC(name, op) \
101 + static int bit_ ## name(lua_State *L) { \
102 + lua_Number f; \
103 + lua_pushinteger(L, BIT_TRUNCATE(op TOBIT(L, 1, f))); \
104 + return 1; \
105 + }
106 +
107 +#define VARIADIC(name, op) \
108 + static int bit_ ## name(lua_State *L) { \
109 + lua_Number f; \
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)); \
115 + return 1; \
116 + }
117 +
118 +#define LOGICAL_SHIFT(name, op) \
119 + static int bit_ ## name(lua_State *L) { \
120 + lua_Number f; \
121 + lua_pushinteger(L, BIT_TRUNCATE(BIT_TRUNCATE((lua_UInteger)TOBIT(L, 1, f)) op \
122 + (unsigned)luaL_checknumber(L, 2))); \
123 + return 1; \
124 + }
125 +
126 +#define ARITHMETIC_SHIFT(name, op) \
127 + static int bit_ ## name(lua_State *L) { \
128 + lua_Number f; \
129 + lua_pushinteger(L, BIT_TRUNCATE((lua_Integer)TOBIT(L, 1, f) op \
130 + (unsigned)luaL_checknumber(L, 2))); \
131 + return 1; \
132 + }
133 +
134 +MONADIC(cast, +)
135 +MONADIC(bnot, ~)
136 +VARIADIC(band, &=)
137 +VARIADIC(bor, |=)
138 +VARIADIC(bxor, ^=)
139 +ARITHMETIC_SHIFT(lshift, <<)
140 +LOGICAL_SHIFT(rshift, >>)
141 +ARITHMETIC_SHIFT(arshift, >>)
142 +
143 +static const struct luaL_reg bitlib[] = {
144 + {"cast", bit_cast},
145 + {"bnot", bit_bnot},
146 + {"band", bit_band},
147 + {"bor", bit_bor},
148 + {"bxor", bit_bxor},
149 + {"lshift", bit_lshift},
150 + {"rshift", bit_rshift},
151 + {"arshift", bit_arshift},
152 + {NULL, NULL}
153 +};
154 +
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");
159 + return 1;
160 +}
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
165 @@ -24,6 +24,7 @@
166 {LUA_MATHLIBNAME, luaopen_math},
167 {LUA_DBLIBNAME, luaopen_debug},
168 {LUA_POSIXLIBNAME, luaopen_posix},
169 + {LUA_BITLIBNAME, luaopen_bit},
170 {NULL, NULL}
171 };
172
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
177 @@ -42,6 +42,9 @@
178 #define LUA_POSIXLIBNAME "posix"
179 LUALIB_API int (luaopen_posix) (lua_State *L);
180
181 +#define LUA_BITLIBNAME "bit"
182 +LUALIB_API int (luaopen_bit) (lua_State *L);
183 +
184
185 /* open all previous libraries */
186 LUALIB_API void (luaL_openlibs) (lua_State *L);