2012-08-04 10:54:37 +00:00
|
|
|
/*
|
2013-04-13 21:24:24 +00:00
|
|
|
** $Id: lbitlib.c,v 1.18 2013/03/19 13:19:12 roberto Exp $
|
2012-08-04 10:54:37 +00:00
|
|
|
** Standard library for bitwise operations
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define lbitlib_c
|
|
|
|
#define LUA_LIB
|
|
|
|
|
|
|
|
#include "lua.h"
|
|
|
|
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "lualib.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* number of bits to consider in a number */
|
2013-04-13 21:24:24 +00:00
|
|
|
#if !defined(LUA_NBITS)
|
|
|
|
#define LUA_NBITS 32
|
|
|
|
#endif
|
2012-08-04 10:54:37 +00:00
|
|
|
|
|
|
|
|
2013-04-13 21:24:24 +00:00
|
|
|
#define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
|
|
|
|
|
|
|
|
/* macro to trim extra bits */
|
2012-08-04 10:54:37 +00:00
|
|
|
#define trim(x) ((x) & ALLONES)
|
|
|
|
|
|
|
|
|
2013-04-13 21:24:24 +00:00
|
|
|
/* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */
|
|
|
|
#define mask(n) (~((ALLONES << 1) << ((n) - 1)))
|
|
|
|
|
2012-08-04 10:54:37 +00:00
|
|
|
|
2013-04-13 21:24:24 +00:00
|
|
|
typedef lua_Unsigned b_uint;
|
2012-08-04 10:54:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static b_uint andaux (lua_State *L) {
|
|
|
|
int i, n = lua_gettop(L);
|
|
|
|
b_uint r = ~(b_uint)0;
|
|
|
|
for (i = 1; i <= n; i++)
|
2013-04-13 21:24:24 +00:00
|
|
|
r &= luaL_checkunsigned(L, i);
|
2012-08-04 10:54:37 +00:00
|
|
|
return trim(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_and (lua_State *L) {
|
|
|
|
b_uint r = andaux(L);
|
|
|
|
lua_pushunsigned(L, r);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_test (lua_State *L) {
|
|
|
|
b_uint r = andaux(L);
|
|
|
|
lua_pushboolean(L, r != 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_or (lua_State *L) {
|
|
|
|
int i, n = lua_gettop(L);
|
|
|
|
b_uint r = 0;
|
|
|
|
for (i = 1; i <= n; i++)
|
2013-04-13 21:24:24 +00:00
|
|
|
r |= luaL_checkunsigned(L, i);
|
2012-08-04 10:54:37 +00:00
|
|
|
lua_pushunsigned(L, trim(r));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_xor (lua_State *L) {
|
|
|
|
int i, n = lua_gettop(L);
|
|
|
|
b_uint r = 0;
|
|
|
|
for (i = 1; i <= n; i++)
|
2013-04-13 21:24:24 +00:00
|
|
|
r ^= luaL_checkunsigned(L, i);
|
2012-08-04 10:54:37 +00:00
|
|
|
lua_pushunsigned(L, trim(r));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_not (lua_State *L) {
|
2013-04-13 21:24:24 +00:00
|
|
|
b_uint r = ~luaL_checkunsigned(L, 1);
|
2012-08-04 10:54:37 +00:00
|
|
|
lua_pushunsigned(L, trim(r));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_shift (lua_State *L, b_uint r, int i) {
|
|
|
|
if (i < 0) { /* shift right? */
|
|
|
|
i = -i;
|
|
|
|
r = trim(r);
|
2013-04-13 21:24:24 +00:00
|
|
|
if (i >= LUA_NBITS) r = 0;
|
2012-08-04 10:54:37 +00:00
|
|
|
else r >>= i;
|
|
|
|
}
|
|
|
|
else { /* shift left */
|
2013-04-13 21:24:24 +00:00
|
|
|
if (i >= LUA_NBITS) r = 0;
|
2012-08-04 10:54:37 +00:00
|
|
|
else r <<= i;
|
|
|
|
r = trim(r);
|
|
|
|
}
|
|
|
|
lua_pushunsigned(L, r);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_lshift (lua_State *L) {
|
2013-04-13 21:24:24 +00:00
|
|
|
return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2));
|
2012-08-04 10:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_rshift (lua_State *L) {
|
2013-04-13 21:24:24 +00:00
|
|
|
return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2));
|
2012-08-04 10:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_arshift (lua_State *L) {
|
2013-04-13 21:24:24 +00:00
|
|
|
b_uint r = luaL_checkunsigned(L, 1);
|
2012-08-04 10:54:37 +00:00
|
|
|
int i = luaL_checkint(L, 2);
|
2013-04-13 21:24:24 +00:00
|
|
|
if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1))))
|
2012-08-04 10:54:37 +00:00
|
|
|
return b_shift(L, r, -i);
|
|
|
|
else { /* arithmetic shift for 'negative' number */
|
2013-04-13 21:24:24 +00:00
|
|
|
if (i >= LUA_NBITS) r = ALLONES;
|
2012-08-04 10:54:37 +00:00
|
|
|
else
|
|
|
|
r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */
|
|
|
|
lua_pushunsigned(L, r);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_rot (lua_State *L, int i) {
|
2013-04-13 21:24:24 +00:00
|
|
|
b_uint r = luaL_checkunsigned(L, 1);
|
|
|
|
i &= (LUA_NBITS - 1); /* i = i % NBITS */
|
2012-08-04 10:54:37 +00:00
|
|
|
r = trim(r);
|
2013-04-13 21:24:24 +00:00
|
|
|
r = (r << i) | (r >> (LUA_NBITS - i));
|
2012-08-04 10:54:37 +00:00
|
|
|
lua_pushunsigned(L, trim(r));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_lrot (lua_State *L) {
|
|
|
|
return b_rot(L, luaL_checkint(L, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_rrot (lua_State *L) {
|
|
|
|
return b_rot(L, -luaL_checkint(L, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-13 21:24:24 +00:00
|
|
|
/*
|
|
|
|
** get field and width arguments for field-manipulation functions,
|
|
|
|
** checking whether they are valid.
|
|
|
|
** ('luaL_error' called without 'return' to avoid later warnings about
|
|
|
|
** 'width' being used uninitialized.)
|
|
|
|
*/
|
|
|
|
static int fieldargs (lua_State *L, int farg, int *width) {
|
|
|
|
int f = luaL_checkint(L, farg);
|
|
|
|
int w = luaL_optint(L, farg + 1, 1);
|
|
|
|
luaL_argcheck(L, 0 <= f, farg, "field cannot be negative");
|
|
|
|
luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
|
|
|
|
if (f + w > LUA_NBITS)
|
|
|
|
luaL_error(L, "trying to access non-existent bits");
|
|
|
|
*width = w;
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_extract (lua_State *L) {
|
|
|
|
int w;
|
|
|
|
b_uint r = luaL_checkunsigned(L, 1);
|
|
|
|
int f = fieldargs(L, 2, &w);
|
|
|
|
r = (r >> f) & mask(w);
|
|
|
|
lua_pushunsigned(L, r);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_replace (lua_State *L) {
|
|
|
|
int w;
|
|
|
|
b_uint r = luaL_checkunsigned(L, 1);
|
|
|
|
b_uint v = luaL_checkunsigned(L, 2);
|
|
|
|
int f = fieldargs(L, 3, &w);
|
|
|
|
int m = mask(w);
|
|
|
|
v &= m; /* erase bits outside given width */
|
|
|
|
r = (r & ~(m << f)) | (v << f);
|
|
|
|
lua_pushunsigned(L, r);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-04 10:54:37 +00:00
|
|
|
static const luaL_Reg bitlib[] = {
|
|
|
|
{"arshift", b_arshift},
|
|
|
|
{"band", b_and},
|
|
|
|
{"bnot", b_not},
|
|
|
|
{"bor", b_or},
|
|
|
|
{"bxor", b_xor},
|
2013-04-13 21:24:24 +00:00
|
|
|
{"btest", b_test},
|
|
|
|
{"extract", b_extract},
|
2012-08-04 10:54:37 +00:00
|
|
|
{"lrotate", b_lrot},
|
|
|
|
{"lshift", b_lshift},
|
2013-04-13 21:24:24 +00:00
|
|
|
{"replace", b_replace},
|
2012-08-04 10:54:37 +00:00
|
|
|
{"rrotate", b_rrot},
|
|
|
|
{"rshift", b_rshift},
|
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LUAMOD_API int luaopen_bit32 (lua_State *L) {
|
|
|
|
luaL_newlib(L, bitlib);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|