aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-12-17 11:26:38 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2010-12-17 11:26:38 -0200
commit1a92c2b354bd08031a744cb39125a8c5d04349c3 (patch)
tree70608af5d9f305936f06145a045c684d826db250
parentfe0d54d37edd3d28d42a01db013e04cc12e97f2f (diff)
downloadlua-1a92c2b354bd08031a744cb39125a8c5d04349c3.tar.gz
lua-1a92c2b354bd08031a744cb39125a8c5d04349c3.tar.bz2
lua-1a92c2b354bd08031a744cb39125a8c5d04349c3.zip
allows number of bits used by the library to be changed from outside
-rw-r--r--lbitlib.c25
1 files changed, 14 insertions, 11 deletions
diff --git a/lbitlib.c b/lbitlib.c
index 3e86bab1..22796a25 100644
--- a/lbitlib.c
+++ b/lbitlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbitlib.c,v 1.13 2010/11/22 18:06:33 roberto Exp roberto $ 2** $Id: lbitlib.c,v 1.14 2010/11/29 15:19:28 roberto Exp roberto $
3** Standard library for bitwise operations 3** Standard library for bitwise operations
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -14,15 +14,18 @@
14 14
15 15
16/* number of bits to consider in a number */ 16/* number of bits to consider in a number */
17#define NBITS 32 17#if !defined(LUA_NBITS)
18#define LUA_NBITS 32
19#endif
18 20
19#define ALLONES (~(((~(lua_Unsigned)0) << (NBITS - 1)) << 1)) 21
22#define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
20 23
21/* macro to trim extra bits */ 24/* macro to trim extra bits */
22#define trim(x) ((x) & ALLONES) 25#define trim(x) ((x) & ALLONES)
23 26
24 27
25/* builds a number with 'n' ones (1 <= n <= NBITS) */ 28/* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */
26#define mask(n) (~((ALLONES << 1) << ((n) - 1))) 29#define mask(n) (~((ALLONES << 1) << ((n) - 1)))
27 30
28 31
@@ -84,11 +87,11 @@ static int b_shift (lua_State *L, b_uint r, int i) {
84 if (i < 0) { /* shift right? */ 87 if (i < 0) { /* shift right? */
85 i = -i; 88 i = -i;
86 r = trim(r); 89 r = trim(r);
87 if (i >= NBITS) r = 0; 90 if (i >= LUA_NBITS) r = 0;
88 else r >>= i; 91 else r >>= i;
89 } 92 }
90 else { /* shift left */ 93 else { /* shift left */
91 if (i >= NBITS) r = 0; 94 if (i >= LUA_NBITS) r = 0;
92 else r <<= i; 95 else r <<= i;
93 r = trim(r); 96 r = trim(r);
94 } 97 }
@@ -110,10 +113,10 @@ static int b_rshift (lua_State *L) {
110static int b_arshift (lua_State *L) { 113static int b_arshift (lua_State *L) {
111 b_uint r = luaL_checkunsigned(L, 1); 114 b_uint r = luaL_checkunsigned(L, 1);
112 int i = luaL_checkint(L, 2); 115 int i = luaL_checkint(L, 2);
113 if (i < 0 || !(r & ((b_uint)1 << (NBITS - 1)))) 116 if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1))))
114 return b_shift(L, r, -i); 117 return b_shift(L, r, -i);
115 else { /* arithmetic shift for 'negative' number */ 118 else { /* arithmetic shift for 'negative' number */
116 if (i >= NBITS) r = ALLONES; 119 if (i >= LUA_NBITS) r = ALLONES;
117 else 120 else
118 r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */ 121 r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */
119 lua_pushunsigned(L, r); 122 lua_pushunsigned(L, r);
@@ -124,9 +127,9 @@ static int b_arshift (lua_State *L) {
124 127
125static int b_rot (lua_State *L, int i) { 128static int b_rot (lua_State *L, int i) {
126 b_uint r = luaL_checkunsigned(L, 1); 129 b_uint r = luaL_checkunsigned(L, 1);
127 i &= (NBITS - 1); /* i = i % NBITS */ 130 i &= (LUA_NBITS - 1); /* i = i % NBITS */
128 r = trim(r); 131 r = trim(r);
129 r = (r << i) | (r >> (NBITS - i)); 132 r = (r << i) | (r >> (LUA_NBITS - i));
130 lua_pushunsigned(L, trim(r)); 133 lua_pushunsigned(L, trim(r));
131 return 1; 134 return 1;
132} 135}
@@ -151,7 +154,7 @@ static int fieldargs (lua_State *L, int farg, int *width) {
151 int w = luaL_optint(L, farg + 1, 1); 154 int w = luaL_optint(L, farg + 1, 1);
152 luaL_argcheck(L, 0 <= f, farg, "field cannot be netative"); 155 luaL_argcheck(L, 0 <= f, farg, "field cannot be netative");
153 luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); 156 luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
154 if (f + w > NBITS) 157 if (f + w > LUA_NBITS)
155 luaL_error(L, "trying to access non-existent bits"); 158 luaL_error(L, "trying to access non-existent bits");
156 *width = w; 159 *width = w;
157 return f; 160 return f;