| 1 |
/* |
| 2 |
** $Id: lbitlib.c,v 1.16 2011/06/20 16:35:23 roberto Exp $ |
| 3 |
** Standard library for bitwise operations |
| 4 |
** See Copyright Notice in lua.h |
| 5 |
*/ |
| 6 |
|
| 7 |
#define lbitlib_c |
| 8 |
#define LUA_LIB |
| 9 |
|
| 10 |
#include <limits.h> |
| 11 |
#include "lua.h" |
| 12 |
|
| 13 |
#include "lauxlib.h" |
| 14 |
#include "lualib.h" |
| 15 |
|
| 16 |
#include "lj_obj.h" |
| 17 |
#include "lj_state.h" |
| 18 |
|
| 19 |
/* http://lua-users.org/lists/lua-l/2011-01/msg01039.html */ |
| 20 |
typedef unsigned int lua_Unsigned; |
| 21 |
#define LUA_UNSIGNED unsigned LUA_INT32 |
| 22 |
|
| 23 |
|
| 24 |
#if !defined(lua_unsigned2number) |
| 25 |
/* on several machines, coercion from unsigned to double is slow, |
| 26 |
so it may be worth to avoid */ |
| 27 |
#define lua_unsigned2number(u) \ |
| 28 |
(((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)( unsigned int )(u)) |
| 29 |
#endif |
| 30 |
|
| 31 |
#if !defined(lua_lock) |
| 32 |
#define lua_lock(L) ((void) 0) |
| 33 |
#define lua_unlock(L) ((void) 0) |
| 34 |
#endif |
| 35 |
|
| 36 |
|
| 37 |
LUA_API void lua_pushunsigned (lua_State *L, lua_Unsigned u) { |
| 38 |
lua_Number n=0; |
| 39 |
lua_lock(L); |
| 40 |
/* printf("u=%d,%u,%x\n",u,u,u); |
| 41 |
printf("INT_MAX=%d %u %x, (lua_Unsigned)INT_MAX=%d %u %x (unsigned int)INT_MAX=%d %u %x\n", |
| 42 |
INT_MAX,INT_MAX,INT_MAX, |
| 43 |
(lua_Unsigned)INT_MAX,(lua_Unsigned)INT_MAX,(lua_Unsigned)INT_MAX, |
| 44 |
(unsigned int)INT_MAX,(unsigned int)INT_MAX,(unsigned int)INT_MAX); |
| 45 |
printf("((u) <= (lua_Unsigned)INT_MAX) :%d\n",(((unsigned int)u) <= (lua_Unsigned)INT_MAX)); |
| 46 |
printf("(lua_Number)(int)(u) =%d,%u,%x\n",(lua_Number)(int)(u),(lua_Number)(int)(u),(lua_Number)(int)(u)); |
| 47 |
printf("(lua_Number)(u) =%f,%u,%x\n",(lua_Number)(u),(lua_Number)(u),(lua_Number)(u)); |
| 48 |
printf("u=%d,%u,%x\n",u,u,u); |
| 49 |
*/ |
| 50 |
/*printf("n=%d,%u,%x u=%d,%u,%x\n",n,n,n,u,u,u); */ |
| 51 |
n = lua_unsigned2number(u); |
| 52 |
/* printf("n=%f \n",n); |
| 53 |
printf("u=%d %u %x n=%d %u %x\n",u,u,u,n,n,n); |
| 54 |
printf("n=%d %u %x\n",n,n,n); |
| 55 |
printf("u=%d,%u,%x\n",u,u,u); |
| 56 |
*/ |
| 57 |
/* setintptrV(L->top, n); |
| 58 |
incr_top(L); |
| 59 |
setnumV(L->top, n); |
| 60 |
*/ |
| 61 |
setnumV(L->top, n); |
| 62 |
if (LJ_UNLIKELY(tvisnan(L->top))) |
| 63 |
setnanV(L->top); /* Canonicalize injected NaNs. */ |
| 64 |
incr_top(L); |
| 65 |
/*setnvalue(L->top, n); |
| 66 |
api_incr_top(L); |
| 67 |
*/ |
| 68 |
lua_unlock(L); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
#define luaL_checkunsigned (lua_Unsigned) luaL_checkinteger |
| 77 |
/*#define lua_pushunsigned lua_pushinteger*/ |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
/* number of bits to consider in a number */ |
| 82 |
#if !defined(LUA_NBITS) |
| 83 |
#define LUA_NBITS 32 |
| 84 |
#endif |
| 85 |
|
| 86 |
|
| 87 |
#define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1)) |
| 88 |
|
| 89 |
/* macro to trim extra bits */ |
| 90 |
#define trim(x) ((x) & ALLONES) |
| 91 |
|
| 92 |
|
| 93 |
/* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */ |
| 94 |
#define mask(n) (~((ALLONES << 1) << ((n) - 1))) |
| 95 |
|
| 96 |
|
| 97 |
typedef lua_Unsigned b_uint; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
static b_uint andaux (lua_State *L) { |
| 102 |
int i, n = lua_gettop(L); |
| 103 |
b_uint r = ~(b_uint)0; |
| 104 |
/* printf("1 r=%d,%u,%x\n",r,r,r);*/ |
| 105 |
for (i = 1; i <= n; i++) |
| 106 |
r &= luaL_checkunsigned(L, i); |
| 107 |
/* printf("2 r=%d,%u,%x\n",r,r,r); |
| 108 |
printf("3 r=%d,%u,%x\n",trim(r),trim(r),trim(r)); |
| 109 |
*/ |
| 110 |
return trim(r); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
static int b_and (lua_State *L) { |
| 115 |
b_uint r = andaux(L); |
| 116 |
/* printf("4 r=%d,%u,%x\n",r,r,r);*/ |
| 117 |
lua_pushunsigned(L, r); |
| 118 |
return 1; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
static int b_test (lua_State *L) { |
| 123 |
b_uint r = andaux(L); |
| 124 |
lua_pushboolean(L, r != 0); |
| 125 |
return 1; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
static int b_or (lua_State *L) { |
| 130 |
int i, n = lua_gettop(L); |
| 131 |
b_uint r = 0; |
| 132 |
for (i = 1; i <= n; i++) |
| 133 |
r |= luaL_checkunsigned(L, i); |
| 134 |
lua_pushunsigned(L, trim(r)); |
| 135 |
return 1; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
static int b_xor (lua_State *L) { |
| 140 |
int i, n = lua_gettop(L); |
| 141 |
b_uint r = 0; |
| 142 |
for (i = 1; i <= n; i++) |
| 143 |
r ^= luaL_checkunsigned(L, i); |
| 144 |
lua_pushunsigned(L, trim(r)); |
| 145 |
return 1; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
static int b_not (lua_State *L) { |
| 150 |
b_uint r = ~luaL_checkunsigned(L, 1); |
| 151 |
lua_pushunsigned(L, trim(r)); |
| 152 |
return 1; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
static int b_shift (lua_State *L, b_uint r, int i) { |
| 157 |
if (i < 0) { /* shift right? */ |
| 158 |
i = -i; |
| 159 |
r = trim(r); |
| 160 |
if (i >= LUA_NBITS) r = 0; |
| 161 |
else r >>= i; |
| 162 |
} |
| 163 |
else { /* shift left */ |
| 164 |
if (i >= LUA_NBITS) r = 0; |
| 165 |
else r <<= i; |
| 166 |
r = trim(r); |
| 167 |
} |
| 168 |
lua_pushunsigned(L, r); |
| 169 |
return 1; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
static int b_lshift (lua_State *L) { |
| 174 |
return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2)); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
static int b_rshift (lua_State *L) { |
| 179 |
return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2)); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
static int b_arshift (lua_State *L) { |
| 184 |
b_uint r = luaL_checkunsigned(L, 1); |
| 185 |
int i = luaL_checkint(L, 2); |
| 186 |
if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1)))) |
| 187 |
return b_shift(L, r, -i); |
| 188 |
else { /* arithmetic shift for 'negative' number */ |
| 189 |
if (i >= LUA_NBITS) r = ALLONES; |
| 190 |
else |
| 191 |
r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */ |
| 192 |
lua_pushunsigned(L, r); |
| 193 |
return 1; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
static int b_rot (lua_State *L, int i) { |
| 199 |
b_uint r = luaL_checkunsigned(L, 1); |
| 200 |
i &= (LUA_NBITS - 1); /* i = i % NBITS */ |
| 201 |
r = trim(r); |
| 202 |
r = (r << i) | (r >> (LUA_NBITS - i)); |
| 203 |
lua_pushunsigned(L, trim(r)); |
| 204 |
return 1; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
static int b_lrot (lua_State *L) { |
| 209 |
return b_rot(L, luaL_checkint(L, 2)); |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
static int b_rrot (lua_State *L) { |
| 214 |
return b_rot(L, -luaL_checkint(L, 2)); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
/* |
| 219 |
** get field and width arguments for field-manipulation functions, |
| 220 |
** checking whether they are valid |
| 221 |
*/ |
| 222 |
static int fieldargs (lua_State *L, int farg, int *width) { |
| 223 |
int f = luaL_checkint(L, farg); |
| 224 |
int w = luaL_optint(L, farg + 1, 1); |
| 225 |
luaL_argcheck(L, 0 <= f, farg, "field cannot be negative"); |
| 226 |
luaL_argcheck(L, 0 < w, farg + 1, "width must be positive"); |
| 227 |
if (f + w > LUA_NBITS) |
| 228 |
luaL_error(L, "trying to access non-existent bits"); |
| 229 |
*width = w; |
| 230 |
return f; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
static int b_extract (lua_State *L) { |
| 235 |
int w; |
| 236 |
b_uint r = luaL_checkunsigned(L, 1); |
| 237 |
int f = fieldargs(L, 2, &w); |
| 238 |
r = (r >> f) & mask(w); |
| 239 |
lua_pushunsigned(L, r); |
| 240 |
return 1; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
static int b_replace (lua_State *L) { |
| 245 |
int w; |
| 246 |
b_uint r = luaL_checkunsigned(L, 1); |
| 247 |
b_uint v = luaL_checkunsigned(L, 2); |
| 248 |
int f = fieldargs(L, 3, &w); |
| 249 |
int m = mask(w); |
| 250 |
v &= m; /* erase bits outside given width */ |
| 251 |
r = (r & ~(m << f)) | (v << f); |
| 252 |
lua_pushunsigned(L, r); |
| 253 |
return 1; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
static const luaL_Reg bitlib[] = { |
| 258 |
{"arshift", b_arshift}, |
| 259 |
{"band", b_and}, |
| 260 |
{"bnot", b_not}, |
| 261 |
{"bor", b_or}, |
| 262 |
{"bxor", b_xor}, |
| 263 |
{"btest", b_test}, |
| 264 |
{"extract", b_extract}, |
| 265 |
{"lrotate", b_lrot}, |
| 266 |
{"lshift", b_lshift}, |
| 267 |
{"replace", b_replace}, |
| 268 |
{"rrotate", b_rrot}, |
| 269 |
{"rshift", b_rshift}, |
| 270 |
{NULL, NULL} |
| 271 |
}; |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
/* http://lua-users.org/lists/lua-l/2011-01/msg01039.html */ |
| 276 |
|
| 277 |
/*LUAMOD_API int luaopen_bit32 (lua_State *L) {*/ |
| 278 |
/* luaL_newlib(L, bitlib);*/ |
| 279 |
/* return 1;*/ |
| 280 |
/*} */ |
| 281 |
|
| 282 |
int luaopen_bit32 (lua_State *L) { |
| 283 |
luaL_register(L, "bit32", bitlib); |
| 284 |
return 1; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|