summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lbitlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/lbitlib.c')
-rw-r--r--apps/plugins/lua/lbitlib.c268
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 */
20typedef 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) 32typedef 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) \ 36static 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) \ 45static 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 52static 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 59static 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) \ 69static 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) { \ 79static 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
86static 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
103static int b_lshift (lua_State *L) {
104 return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2));
105}
106
107
108static 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) { \ 113static 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
128static 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
102MONADIC(bnot, ~) 139static int b_lrot (lua_State *L) {
103VARIADIC(band, &=) 140 return b_rot(L, luaL_checkint(L, 2));
104VARIADIC(bor, |=) 141}
105VARIADIC(bxor, ^=) 142
106ARITHMETIC_SHIFT(lshift, <<) 143
107LOGICAL_SHIFT(rshift, >>) 144static int b_rrot (lua_State *L) {
108ARITHMETIC_SHIFT(arshift, >>) 145 return b_rot(L, -luaL_checkint(L, 2));
109 146}
110static 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*/
155static 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
167static 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
177static 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
190static 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
121LUALIB_API int luaopen_bit (lua_State *L) { 206
122 luaL_register(L, "bit", bitlib); 207
123 lua_pushnumber(L, BIT_BITS); 208LUAMOD_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