diff options
author | Marcin Bukat <marcin.bukat@gmail.com> | 2014-04-02 20:46:06 +0200 |
---|---|---|
committer | Marcin Bukat <marcin.bukat@gmail.com> | 2014-04-02 20:46:06 +0200 |
commit | bfd0179042b0b02fb88748d54e56e7e208bb117f (patch) | |
tree | 42d5fd51574054caaf673420fca1ec962d62d2f2 /apps/plugins/lua/lbitlib.c | |
parent | 36378988ad4059982742f05f5eb50580b456840a (diff) | |
download | rockbox-bfd0179042b0b02fb88748d54e56e7e208bb117f.tar.gz rockbox-bfd0179042b0b02fb88748d54e56e7e208bb117f.zip |
Revert "Update lua plugin to 5.2.3"
FILE typedef to *void needs more work to not break sim and
application builds. I checked only a few random native builds
unfortunately. Sorry for inconvenience.
Diffstat (limited to 'apps/plugins/lua/lbitlib.c')
-rw-r--r-- | apps/plugins/lua/lbitlib.c | 268 |
1 files changed, 91 insertions, 177 deletions
diff --git a/apps/plugins/lua/lbitlib.c b/apps/plugins/lua/lbitlib.c index 31c7b66f12..76c8d1d39b 100644 --- a/apps/plugins/lua/lbitlib.c +++ b/apps/plugins/lua/lbitlib.c | |||
@@ -1,212 +1,126 @@ | |||
1 | /* | 1 | /* Bitwise operations library */ |
2 | ** $Id: lbitlib.c,v 1.18.1.2 2013/07/09 18:01:41 roberto Exp $ | 2 | /* (c) Reuben Thomas 2000-2008 */ |
3 | ** Standard library for bitwise operations | 3 | /* bitlib is copyright Reuben Thomas 2000-2008, and is released under the MIT |
4 | ** See Copyright Notice in lua.h | 4 | license, like Lua (see http://www.lua.org/copyright.html; it's |
5 | */ | 5 | basically the same as the BSD license). There is no warranty. */ |
6 | 6 | ||
7 | #define lbitlib_c | 7 | #include "config.h" |
8 | #define LUA_LIB | ||
9 | 8 | ||
10 | #include "lua.h" | 9 | #include "lua.h" |
11 | |||
12 | #include "lauxlib.h" | 10 | #include "lauxlib.h" |
13 | #include "lualib.h" | 11 | #include "lualib.h" |
12 | #include <limits.h> | ||
14 | 13 | ||
15 | 14 | ||
16 | /* number of bits to consider in a number */ | 15 | /* FIXME: Assume lua_Integer is ptrdiff_t */ |
17 | #if !defined(LUA_NBITS) | 16 | #define LUA_INTEGER_MAX INTPTR_MAX |
18 | #define LUA_NBITS 32 | 17 | #define LUA_INTEGER_MIN INTPTR_MIN |
19 | #endif | ||
20 | |||
21 | |||
22 | #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) | ||
23 | |||
24 | /* macro to trim extra bits */ | ||
25 | #define trim(x) ((x) & ALLONES) | ||
26 | |||
27 | |||
28 | /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ | ||
29 | #define mask(n) (~((ALLONES << 1) << ((n) - 1))) | ||
30 | 18 | ||
19 | /* FIXME: Assume size_t is an unsigned lua_Integer */ | ||
20 | typedef size_t lua_UInteger; | ||
21 | #define LUA_UINTEGER_MAX UINT_MAX | ||
31 | 22 | ||
32 | typedef lua_Unsigned b_uint; | ||
33 | 23 | ||
24 | /* Bit type size and limits */ | ||
34 | 25 | ||
26 | #define BIT_BITS (CHAR_BIT * sizeof(lua_Integer)) | ||
35 | 27 | ||
36 | static b_uint andaux (lua_State *L) { | 28 | /* This code may give warnings if BITLIB_FLOAT_* are too big to fit in |
37 | int i, n = lua_gettop(L); | 29 | long, but that doesn't matter since in that case they won't be |
38 | b_uint r = ~(b_uint)0; | 30 | used. */ |
39 | for (i = 1; i <= n; i++) | 31 | #define BIT_MAX (LUA_INTEGER_MAX) |
40 | r &= luaL_checkunsigned(L, i); | ||
41 | return trim(r); | ||
42 | } | ||
43 | 32 | ||
33 | #define BIT_MIN (LUA_INTEGER_MIN) | ||
44 | 34 | ||
45 | static int b_and (lua_State *L) { | 35 | #define BIT_UMAX (LUA_UINTEGER_MAX) |
46 | b_uint r = andaux(L); | ||
47 | lua_pushunsigned(L, r); | ||
48 | return 1; | ||
49 | } | ||
50 | 36 | ||
51 | 37 | ||
52 | static int b_test (lua_State *L) { | 38 | /* Define TOBIT to get a bit value */ |
53 | b_uint r = andaux(L); | 39 | #ifdef BUILTIN_CAST |
54 | lua_pushboolean(L, r != 0); | 40 | #define |
55 | return 1; | 41 | #define TOBIT(L, n, res) \ |
56 | } | 42 | ((void)(res), luaL_checkinteger((L), (n))) |
43 | #else | ||
57 | 44 | ||
45 | #define TOBIT(L, n, res) \ | ||
46 | ((lua_Integer)(((res) = luaL_checknumber(L, (n)) % BIT_UMAX), \ | ||
47 | (res) > BIT_MAX ? ((res) -= BIT_UMAX, (res) -= 1) : \ | ||
48 | ((res) < BIT_MIN ? ((res) += BIT_UMAX, (res) += 1) : (res)))) | ||
49 | #endif | ||
58 | 50 | ||
59 | static int b_or (lua_State *L) { | ||
60 | int i, n = lua_gettop(L); | ||
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 | } | ||
67 | 51 | ||
52 | #define BIT_TRUNCATE(i) \ | ||
53 | ((i) & BIT_UMAX) | ||
68 | 54 | ||
69 | static int b_xor (lua_State *L) { | ||
70 | int i, n = lua_gettop(L); | ||
71 | b_uint r = 0; | ||
72 | for (i = 1; i <= n; i++) | ||
73 | r ^= luaL_checkunsigned(L, i); | ||
74 | lua_pushunsigned(L, trim(r)); | ||
75 | return 1; | ||
76 | } | ||
77 | 55 | ||
56 | /* Operations | ||
78 | 57 | ||
79 | static int b_not (lua_State *L) { | 58 | The macros MONADIC and VARIADIC only deal with bitwise operations. |
80 | b_uint r = ~luaL_checkunsigned(L, 1); | ||
81 | lua_pushunsigned(L, trim(r)); | ||
82 | return 1; | ||
83 | } | ||
84 | 59 | ||
60 | LOGICAL_SHIFT truncates its left-hand operand before shifting so | ||
61 | that any extra bits at the most-significant end are not shifted | ||
62 | into the result. | ||
85 | 63 | ||
86 | static int b_shift (lua_State *L, b_uint r, int i) { | 64 | ARITHMETIC_SHIFT does not truncate its left-hand operand, so that |
87 | if (i < 0) { /* shift right? */ | 65 | the sign bits are not removed and right shift work properly. |
88 | i = -i; | 66 | */ |
89 | r = trim(r); | 67 | |
90 | if (i >= LUA_NBITS) r = 0; | 68 | #define MONADIC(name, op) \ |
91 | else r >>= i; | 69 | static int bit_ ## name(lua_State *L) { \ |
92 | } | 70 | lua_Number f; \ |
93 | else { /* shift left */ | 71 | lua_pushinteger(L, BIT_TRUNCATE(op TOBIT(L, 1, f))); \ |
94 | if (i >= LUA_NBITS) r = 0; | 72 | return 1; \ |
95 | else r <<= i; | ||
96 | r = trim(r); | ||
97 | } | 73 | } |
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 | } | ||
111 | 74 | ||
112 | 75 | #define VARIADIC(name, op) \ | |
113 | static int b_arshift (lua_State *L) { | 76 | static int bit_ ## name(lua_State *L) { \ |
114 | b_uint r = luaL_checkunsigned(L, 1); | 77 | lua_Number f; \ |
115 | int i = luaL_checkint(L, 2); | 78 | int n = lua_gettop(L), i; \ |
116 | if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1)))) | 79 | lua_Integer w = TOBIT(L, 1, f); \ |
117 | return b_shift(L, r, -i); | 80 | for (i = 2; i <= n; i++) \ |
118 | else { /* arithmetic shift for 'negative' number */ | 81 | w op TOBIT(L, i, f); \ |
119 | if (i >= LUA_NBITS) r = ALLONES; | 82 | lua_pushinteger(L, BIT_TRUNCATE(w)); \ |
120 | else | 83 | return 1; \ |
121 | r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */ | ||
122 | lua_pushunsigned(L, r); | ||
123 | return 1; | ||
124 | } | 84 | } |
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 | |||
138 | 85 | ||
139 | static int b_lrot (lua_State *L) { | 86 | #define LOGICAL_SHIFT(name, op) \ |
140 | return b_rot(L, luaL_checkint(L, 2)); | 87 | static int bit_ ## name(lua_State *L) { \ |
141 | } | 88 | lua_Number f; \ |
142 | 89 | lua_pushinteger(L, BIT_TRUNCATE(BIT_TRUNCATE((lua_UInteger)TOBIT(L, 1, f)) op \ | |
143 | 90 | (unsigned)luaL_checknumber(L, 2))); \ | |
144 | static int b_rrot (lua_State *L) { | 91 | return 1; \ |
145 | return b_rot(L, -luaL_checkint(L, 2)); | 92 | } |
146 | } | ||
147 | |||
148 | |||
149 | /* | ||
150 | ** get field and width arguments for field-manipulation functions, | ||
151 | ** checking whether they are valid. | ||
152 | ** ('luaL_error' called without 'return' to avoid later warnings about | ||
153 | ** 'width' being used uninitialized.) | ||
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 | 93 | ||
94 | #define ARITHMETIC_SHIFT(name, op) \ | ||
95 | static int bit_ ## name(lua_State *L) { \ | ||
96 | lua_Number f; \ | ||
97 | lua_pushinteger(L, BIT_TRUNCATE((lua_Integer)TOBIT(L, 1, f) op \ | ||
98 | (unsigned)luaL_checknumber(L, 2))); \ | ||
99 | return 1; \ | ||
100 | } | ||
189 | 101 | ||
190 | static const luaL_Reg bitlib[] = { | 102 | MONADIC(bnot, ~) |
191 | {"arshift", b_arshift}, | 103 | VARIADIC(band, &=) |
192 | {"band", b_and}, | 104 | VARIADIC(bor, |=) |
193 | {"bnot", b_not}, | 105 | VARIADIC(bxor, ^=) |
194 | {"bor", b_or}, | 106 | ARITHMETIC_SHIFT(lshift, <<) |
195 | {"bxor", b_xor}, | 107 | LOGICAL_SHIFT(rshift, >>) |
196 | {"btest", b_test}, | 108 | ARITHMETIC_SHIFT(arshift, >>) |
197 | {"extract", b_extract}, | 109 | |
198 | {"lrotate", b_lrot}, | 110 | static const struct luaL_reg bitlib[] = { |
199 | {"lshift", b_lshift}, | 111 | {"bnot", bit_bnot}, |
200 | {"replace", b_replace}, | 112 | {"band", bit_band}, |
201 | {"rrotate", b_rrot}, | 113 | {"bor", bit_bor}, |
202 | {"rshift", b_rshift}, | 114 | {"bxor", bit_bxor}, |
115 | {"lshift", bit_lshift}, | ||
116 | {"rshift", bit_rshift}, | ||
117 | {"arshift", bit_arshift}, | ||
203 | {NULL, NULL} | 118 | {NULL, NULL} |
204 | }; | 119 | }; |
205 | 120 | ||
206 | 121 | LUALIB_API int luaopen_bit (lua_State *L) { | |
207 | 122 | luaL_register(L, "bit", bitlib); | |
208 | LUAMOD_API int luaopen_bit32 (lua_State *L) { | 123 | lua_pushnumber(L, BIT_BITS); |
209 | luaL_newlib(L, bitlib); | 124 | lua_setfield(L, -2, "bits"); |
210 | return 1; | 125 | return 1; |
211 | } | 126 | } |
212 | |||