diff options
author | Richard Quirk <richard.quirk@gmail.com> | 2014-03-19 19:31:31 +0100 |
---|---|---|
committer | Marcin Bukat <marcin.bukat@gmail.com> | 2014-04-02 20:31:54 +0200 |
commit | 36378988ad4059982742f05f5eb50580b456840a (patch) | |
tree | ee70be810d894566c5e351f21a1ebb79be742a85 /apps/plugins/lua/lbitlib.c | |
parent | 020f16a1c7d5bc9a302671cef03f56ac735e20c5 (diff) | |
download | rockbox-36378988ad4059982742f05f5eb50580b456840a.tar.gz rockbox-36378988ad4059982742f05f5eb50580b456840a.zip |
Update lua plugin to 5.2.3
Prior to this patch the Lua plugin used version 5.1.4. This change
reduces the number of modifications in the Lua source using some new
defines and because the upstream source is now more flexible.
Unless otherwise stated, l*.[ch] files are taken unmodified from the
upstream lua-5.2.3.
fscanf.c:
file descriptors in rockbox are just ints, they are hidden behind a
void* now so liolib requires less modifications. fscanf is updated to
use void* too.
getc.c: this is a new file required for getc implementation in lauxlib.c
lauxlib.c: LoadF replaced FILE* with int, the rockbox file
descriptor int are cast to FILE* (actually void* due to typedef).
getc uses the PREFIX version. stdin is not used, as per 5.1.4.
lbaselib.c: now uses strspn in the number parsing. print uses DEBUGF now
rather than being commented out.
lbitlib.c: use the built-in version from 5.2.3 rather than Reuben
Thomas's external library. Backwards compatible and adds some new bit
operations.
ldo.c: the LUAI_THROW/TRY defines are now in the core lua code, so have
been removed from rockconf.h
liolib.c: here the implementation has changed to use the LStream from
the original source, and cast the FILE* pointers to int. This has
reduced the number of modifications from the upstream version.
llex.c: the only change from upstream is to remove the locale include.
lmathlib.c: updated from the 5.2.3 version and re-applied the changes
that were made vs 5.1.4 for random numbers and to remove unsupported
float functions.
loadlib.c: upstream version, with the 5.1.4 changes for missing
functions.
lobject.c: upstream version, with ctype.h added and sprintf changed to
snprintf.
loslib.c: upstream version with locale.h removed and 5.1.4 changes for
unsupportable functions.
lstrlib.c: sprintf changed to snprintf.
ltable.c: upstream with the hashnum function from 5.1.4 to avoid frexp
in luai_hashnum.
luaconf.h: updated to 5.2.3 version, restored relevant parts from the
original 5.1.4 configuration. The COMPAT defines that are no longer
available are not included.
lundump.c: VERSION macro conflicts with the core Rockbox equivalent.
rocklib.c: luaL_reg is no longer available, replaced by luaL_Reg
equivalent. Moved checkboolean/optboolean functions to this file and out
of core lua files. luaL_getn is no longer available, replaced by
luaL_rawlen. luaL_register is deprecated, use the newlib/setfuncs
replacements. rli_init has to be called before setting up the newlib to
avoid overwriting the rb table.
rocklib_aux.pl: use rli_checkboolean from rocklib.c.
rocklua.c: new default bits library used, update the library loading
code with idiomatic 5.2 code.
strcspn.c: no longer needed, but strspn.c is required for strspn in
lbaselib.c
Change-Id: I0c7945c755f79083afe98ec117e1e8cf13de2651
Reviewed-on: http://gerrit.rockbox.org/774
Tested: Richard Quirk <richard.quirk@gmail.com>
Reviewed-by: Marcin Bukat <marcin.bukat@gmail.com>
Diffstat (limited to 'apps/plugins/lua/lbitlib.c')
-rw-r--r-- | apps/plugins/lua/lbitlib.c | 268 |
1 files changed, 177 insertions, 91 deletions
diff --git a/apps/plugins/lua/lbitlib.c b/apps/plugins/lua/lbitlib.c index 76c8d1d39b..31c7b66f12 100644 --- a/apps/plugins/lua/lbitlib.c +++ b/apps/plugins/lua/lbitlib.c | |||
@@ -1,126 +1,212 @@ | |||
1 | /* Bitwise operations library */ | 1 | /* |
2 | /* (c) Reuben Thomas 2000-2008 */ | 2 | ** $Id: lbitlib.c,v 1.18.1.2 2013/07/09 18:01:41 roberto Exp $ |
3 | /* bitlib is copyright Reuben Thomas 2000-2008, and is released under the MIT | 3 | ** Standard library for bitwise operations |
4 | license, like Lua (see http://www.lua.org/copyright.html; it's | 4 | ** See Copyright Notice in lua.h |
5 | basically the same as the BSD license). There is no warranty. */ | 5 | */ |
6 | 6 | ||
7 | #include "config.h" | 7 | #define lbitlib_c |
8 | #define LUA_LIB | ||
8 | 9 | ||
9 | #include "lua.h" | 10 | #include "lua.h" |
11 | |||
10 | #include "lauxlib.h" | 12 | #include "lauxlib.h" |
11 | #include "lualib.h" | 13 | #include "lualib.h" |
12 | #include <limits.h> | ||
13 | 14 | ||
14 | 15 | ||
15 | /* FIXME: Assume lua_Integer is ptrdiff_t */ | 16 | /* number of bits to consider in a number */ |
16 | #define LUA_INTEGER_MAX INTPTR_MAX | 17 | #if !defined(LUA_NBITS) |
17 | #define LUA_INTEGER_MIN INTPTR_MIN | 18 | #define LUA_NBITS 32 |
19 | #endif | ||
18 | 20 | ||
19 | /* FIXME: Assume size_t is an unsigned lua_Integer */ | ||
20 | typedef size_t lua_UInteger; | ||
21 | #define LUA_UINTEGER_MAX UINT_MAX | ||
22 | 21 | ||
22 | #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) | ||
23 | 23 | ||
24 | /* Bit type size and limits */ | 24 | /* macro to trim extra bits */ |
25 | #define trim(x) ((x) & ALLONES) | ||
25 | 26 | ||
26 | #define BIT_BITS (CHAR_BIT * sizeof(lua_Integer)) | ||
27 | 27 | ||
28 | /* This code may give warnings if BITLIB_FLOAT_* are too big to fit in | 28 | /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ |
29 | long, but that doesn't matter since in that case they won't be | 29 | #define mask(n) (~((ALLONES << 1) << ((n) - 1))) |
30 | used. */ | ||
31 | #define BIT_MAX (LUA_INTEGER_MAX) | ||
32 | 30 | ||
33 | #define BIT_MIN (LUA_INTEGER_MIN) | ||
34 | 31 | ||
35 | #define BIT_UMAX (LUA_UINTEGER_MAX) | 32 | typedef lua_Unsigned b_uint; |
36 | 33 | ||
37 | 34 | ||
38 | /* Define TOBIT to get a bit value */ | ||
39 | #ifdef BUILTIN_CAST | ||
40 | #define | ||
41 | #define TOBIT(L, n, res) \ | ||
42 | ((void)(res), luaL_checkinteger((L), (n))) | ||
43 | #else | ||
44 | 35 | ||
45 | #define TOBIT(L, n, res) \ | 36 | static b_uint andaux (lua_State *L) { |
46 | ((lua_Integer)(((res) = luaL_checknumber(L, (n)) % BIT_UMAX), \ | 37 | int i, n = lua_gettop(L); |
47 | (res) > BIT_MAX ? ((res) -= BIT_UMAX, (res) -= 1) : \ | 38 | b_uint r = ~(b_uint)0; |
48 | ((res) < BIT_MIN ? ((res) += BIT_UMAX, (res) += 1) : (res)))) | 39 | for (i = 1; i <= n; i++) |
49 | #endif | 40 | r &= luaL_checkunsigned(L, i); |
41 | return trim(r); | ||
42 | } | ||
50 | 43 | ||
51 | 44 | ||
52 | #define BIT_TRUNCATE(i) \ | 45 | static int b_and (lua_State *L) { |
53 | ((i) & BIT_UMAX) | 46 | b_uint r = andaux(L); |
47 | lua_pushunsigned(L, r); | ||
48 | return 1; | ||
49 | } | ||
54 | 50 | ||
55 | 51 | ||
56 | /* Operations | 52 | static int b_test (lua_State *L) { |
53 | b_uint r = andaux(L); | ||
54 | lua_pushboolean(L, r != 0); | ||
55 | return 1; | ||
56 | } | ||
57 | 57 | ||
58 | The macros MONADIC and VARIADIC only deal with bitwise operations. | ||
59 | 58 | ||
60 | LOGICAL_SHIFT truncates its left-hand operand before shifting so | 59 | static int b_or (lua_State *L) { |
61 | that any extra bits at the most-significant end are not shifted | 60 | int i, n = lua_gettop(L); |
62 | into the result. | 61 | b_uint r = 0; |
62 | for (i = 1; i <= n; i++) | ||
63 | r |= luaL_checkunsigned(L, i); | ||
64 | lua_pushunsigned(L, trim(r)); | ||
65 | return 1; | ||
66 | } | ||
63 | 67 | ||
64 | ARITHMETIC_SHIFT does not truncate its left-hand operand, so that | ||
65 | the sign bits are not removed and right shift work properly. | ||
66 | */ | ||
67 | |||
68 | #define MONADIC(name, op) \ | ||
69 | static int bit_ ## name(lua_State *L) { \ | ||
70 | lua_Number f; \ | ||
71 | lua_pushinteger(L, BIT_TRUNCATE(op TOBIT(L, 1, f))); \ | ||
72 | return 1; \ | ||
73 | } | ||
74 | 68 | ||
75 | #define VARIADIC(name, op) \ | 69 | static int b_xor (lua_State *L) { |
76 | static int bit_ ## name(lua_State *L) { \ | 70 | int i, n = lua_gettop(L); |
77 | lua_Number f; \ | 71 | b_uint r = 0; |
78 | int n = lua_gettop(L), i; \ | 72 | for (i = 1; i <= n; i++) |
79 | lua_Integer w = TOBIT(L, 1, f); \ | 73 | r ^= luaL_checkunsigned(L, i); |
80 | for (i = 2; i <= n; i++) \ | 74 | lua_pushunsigned(L, trim(r)); |
81 | w op TOBIT(L, i, f); \ | 75 | return 1; |
82 | lua_pushinteger(L, BIT_TRUNCATE(w)); \ | 76 | } |
83 | return 1; \ | ||
84 | } | ||
85 | 77 | ||
86 | #define LOGICAL_SHIFT(name, op) \ | 78 | |
87 | static int bit_ ## name(lua_State *L) { \ | 79 | static int b_not (lua_State *L) { |
88 | lua_Number f; \ | 80 | b_uint r = ~luaL_checkunsigned(L, 1); |
89 | lua_pushinteger(L, BIT_TRUNCATE(BIT_TRUNCATE((lua_UInteger)TOBIT(L, 1, f)) op \ | 81 | lua_pushunsigned(L, trim(r)); |
90 | (unsigned)luaL_checknumber(L, 2))); \ | 82 | return 1; |
91 | return 1; \ | 83 | } |
84 | |||
85 | |||
86 | static int b_shift (lua_State *L, b_uint r, int i) { | ||
87 | if (i < 0) { /* shift right? */ | ||
88 | i = -i; | ||
89 | r = trim(r); | ||
90 | if (i >= LUA_NBITS) r = 0; | ||
91 | else r >>= i; | ||
92 | } | ||
93 | else { /* shift left */ | ||
94 | if (i >= LUA_NBITS) r = 0; | ||
95 | else r <<= i; | ||
96 | r = trim(r); | ||
92 | } | 97 | } |
98 | lua_pushunsigned(L, r); | ||
99 | return 1; | ||
100 | } | ||
101 | |||
102 | |||
103 | static int b_lshift (lua_State *L) { | ||
104 | return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2)); | ||
105 | } | ||
106 | |||
107 | |||
108 | static int b_rshift (lua_State *L) { | ||
109 | return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2)); | ||
110 | } | ||
93 | 111 | ||
94 | #define ARITHMETIC_SHIFT(name, op) \ | 112 | |
95 | static int bit_ ## name(lua_State *L) { \ | 113 | static int b_arshift (lua_State *L) { |
96 | lua_Number f; \ | 114 | b_uint r = luaL_checkunsigned(L, 1); |
97 | lua_pushinteger(L, BIT_TRUNCATE((lua_Integer)TOBIT(L, 1, f) op \ | 115 | int i = luaL_checkint(L, 2); |
98 | (unsigned)luaL_checknumber(L, 2))); \ | 116 | if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1)))) |
99 | return 1; \ | 117 | return b_shift(L, r, -i); |
118 | else { /* arithmetic shift for 'negative' number */ | ||
119 | if (i >= LUA_NBITS) r = ALLONES; | ||
120 | else | ||
121 | r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */ | ||
122 | lua_pushunsigned(L, r); | ||
123 | return 1; | ||
100 | } | 124 | } |
125 | } | ||
126 | |||
127 | |||
128 | static int b_rot (lua_State *L, int i) { | ||
129 | b_uint r = luaL_checkunsigned(L, 1); | ||
130 | i &= (LUA_NBITS - 1); /* i = i % NBITS */ | ||
131 | r = trim(r); | ||
132 | if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */ | ||
133 | r = (r << i) | (r >> (LUA_NBITS - i)); | ||
134 | lua_pushunsigned(L, trim(r)); | ||
135 | return 1; | ||
136 | } | ||
137 | |||
101 | 138 | ||
102 | MONADIC(bnot, ~) | 139 | static int b_lrot (lua_State *L) { |
103 | VARIADIC(band, &=) | 140 | return b_rot(L, luaL_checkint(L, 2)); |
104 | VARIADIC(bor, |=) | 141 | } |
105 | VARIADIC(bxor, ^=) | 142 | |
106 | ARITHMETIC_SHIFT(lshift, <<) | 143 | |
107 | LOGICAL_SHIFT(rshift, >>) | 144 | static int b_rrot (lua_State *L) { |
108 | ARITHMETIC_SHIFT(arshift, >>) | 145 | return b_rot(L, -luaL_checkint(L, 2)); |
109 | 146 | } | |
110 | static const struct luaL_reg bitlib[] = { | 147 | |
111 | {"bnot", bit_bnot}, | 148 | |
112 | {"band", bit_band}, | 149 | /* |
113 | {"bor", bit_bor}, | 150 | ** get field and width arguments for field-manipulation functions, |
114 | {"bxor", bit_bxor}, | 151 | ** checking whether they are valid. |
115 | {"lshift", bit_lshift}, | 152 | ** ('luaL_error' called without 'return' to avoid later warnings about |
116 | {"rshift", bit_rshift}, | 153 | ** 'width' being used uninitialized.) |
117 | {"arshift", bit_arshift}, | 154 | */ |
155 | static int fieldargs (lua_State *L, int farg, int *width) { | ||
156 | int f = luaL_checkint(L, farg); | ||
157 | int w = luaL_optint(L, farg + 1, 1); | ||
158 | luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); | ||
159 | luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); | ||
160 | if (f + w > LUA_NBITS) | ||
161 | luaL_error(L, "trying to access non-existent bits"); | ||
162 | *width = w; | ||
163 | return f; | ||
164 | } | ||
165 | |||
166 | |||
167 | static int b_extract (lua_State *L) { | ||
168 | int w; | ||
169 | b_uint r = luaL_checkunsigned(L, 1); | ||
170 | int f = fieldargs(L, 2, &w); | ||
171 | r = (r >> f) & mask(w); | ||
172 | lua_pushunsigned(L, r); | ||
173 | return 1; | ||
174 | } | ||
175 | |||
176 | |||
177 | static int b_replace (lua_State *L) { | ||
178 | int w; | ||
179 | b_uint r = luaL_checkunsigned(L, 1); | ||
180 | b_uint v = luaL_checkunsigned(L, 2); | ||
181 | int f = fieldargs(L, 3, &w); | ||
182 | int m = mask(w); | ||
183 | v &= m; /* erase bits outside given width */ | ||
184 | r = (r & ~(m << f)) | (v << f); | ||
185 | lua_pushunsigned(L, r); | ||
186 | return 1; | ||
187 | } | ||
188 | |||
189 | |||
190 | static const luaL_Reg bitlib[] = { | ||
191 | {"arshift", b_arshift}, | ||
192 | {"band", b_and}, | ||
193 | {"bnot", b_not}, | ||
194 | {"bor", b_or}, | ||
195 | {"bxor", b_xor}, | ||
196 | {"btest", b_test}, | ||
197 | {"extract", b_extract}, | ||
198 | {"lrotate", b_lrot}, | ||
199 | {"lshift", b_lshift}, | ||
200 | {"replace", b_replace}, | ||
201 | {"rrotate", b_rrot}, | ||
202 | {"rshift", b_rshift}, | ||
118 | {NULL, NULL} | 203 | {NULL, NULL} |
119 | }; | 204 | }; |
120 | 205 | ||
121 | LUALIB_API int luaopen_bit (lua_State *L) { | 206 | |
122 | luaL_register(L, "bit", bitlib); | 207 | |
123 | lua_pushnumber(L, BIT_BITS); | 208 | LUAMOD_API int luaopen_bit32 (lua_State *L) { |
124 | lua_setfield(L, -2, "bits"); | 209 | luaL_newlib(L, bitlib); |
125 | return 1; | 210 | return 1; |
126 | } | 211 | } |
212 | |||