2009-07-01 20:17:08 +04:00
|
|
|
/*
|
2014-10-01 15:54:56 +04:00
|
|
|
** $Id: lbitlib.c,v 1.26 2014/05/15 19:28:34 roberto Exp roberto $
|
2009-07-01 20:17:08 +04:00
|
|
|
** Standard library for bitwise operations
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2010-01-12 22:40:02 +03:00
|
|
|
#define lbitlib_c
|
|
|
|
#define LUA_LIB
|
|
|
|
|
2009-07-01 20:17:08 +04:00
|
|
|
#include "lua.h"
|
|
|
|
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "lualib.h"
|
|
|
|
|
|
|
|
|
2014-03-18 22:27:08 +04:00
|
|
|
#if defined(LUA_COMPAT_BITLIB) /* { */
|
|
|
|
|
|
|
|
|
2010-10-28 19:17:29 +04:00
|
|
|
/* number of bits to consider in a number */
|
2010-12-17 16:26:38 +03:00
|
|
|
#if !defined(LUA_NBITS)
|
|
|
|
#define LUA_NBITS 32
|
|
|
|
#endif
|
2009-07-01 20:17:08 +04:00
|
|
|
|
2010-12-17 16:26:38 +03:00
|
|
|
|
2014-02-26 16:38:18 +04:00
|
|
|
/*
|
|
|
|
** a lua_Unsigned with its first LUA_NBITS bits equal to 1. (Shift must
|
|
|
|
** be made in two parts to avoid problems when LUA_NBITS is equal to the
|
|
|
|
** number of bits in a lua_Unsigned.)
|
|
|
|
*/
|
2013-07-09 21:49:50 +04:00
|
|
|
#define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
|
2013-04-16 22:39:37 +04:00
|
|
|
|
2009-07-01 20:17:08 +04:00
|
|
|
|
2010-11-29 18:19:28 +03:00
|
|
|
/* macro to trim extra bits */
|
2010-10-28 19:17:29 +04:00
|
|
|
#define trim(x) ((x) & ALLONES)
|
|
|
|
|
|
|
|
|
2010-12-17 16:26:38 +03:00
|
|
|
/* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */
|
2010-11-29 18:19:28 +03:00
|
|
|
#define mask(n) (~((ALLONES << 1) << ((n) - 1)))
|
|
|
|
|
2009-07-01 20:17:08 +04:00
|
|
|
|
|
|
|
|
2013-07-09 21:49:50 +04:00
|
|
|
static lua_Unsigned andaux (lua_State *L) {
|
2009-07-01 20:17:08 +04:00
|
|
|
int i, n = lua_gettop(L);
|
2013-07-09 21:49:50 +04:00
|
|
|
lua_Unsigned r = ~(lua_Unsigned)0;
|
2009-07-01 20:17:08 +04:00
|
|
|
for (i = 1; i <= n; i++)
|
2010-11-29 18:19:28 +03:00
|
|
|
r &= luaL_checkunsigned(L, i);
|
2010-10-28 19:17:29 +04:00
|
|
|
return trim(r);
|
2009-07-01 20:17:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_and (lua_State *L) {
|
2013-07-09 21:49:50 +04:00
|
|
|
lua_Unsigned r = andaux(L);
|
2010-10-26 00:31:11 +04:00
|
|
|
lua_pushunsigned(L, r);
|
2009-07-01 20:17:08 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_test (lua_State *L) {
|
2013-07-09 21:49:50 +04:00
|
|
|
lua_Unsigned r = andaux(L);
|
2009-07-01 20:17:08 +04:00
|
|
|
lua_pushboolean(L, r != 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_or (lua_State *L) {
|
|
|
|
int i, n = lua_gettop(L);
|
2013-07-09 21:49:50 +04:00
|
|
|
lua_Unsigned r = 0;
|
2009-07-01 20:17:08 +04:00
|
|
|
for (i = 1; i <= n; i++)
|
2010-11-29 18:19:28 +03:00
|
|
|
r |= luaL_checkunsigned(L, i);
|
2010-10-28 19:17:29 +04:00
|
|
|
lua_pushunsigned(L, trim(r));
|
2009-07-01 20:17:08 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_xor (lua_State *L) {
|
|
|
|
int i, n = lua_gettop(L);
|
2013-07-09 21:49:50 +04:00
|
|
|
lua_Unsigned r = 0;
|
2009-07-01 20:17:08 +04:00
|
|
|
for (i = 1; i <= n; i++)
|
2010-11-29 18:19:28 +03:00
|
|
|
r ^= luaL_checkunsigned(L, i);
|
2010-10-28 19:17:29 +04:00
|
|
|
lua_pushunsigned(L, trim(r));
|
2009-07-01 20:17:08 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_not (lua_State *L) {
|
2013-07-09 21:49:50 +04:00
|
|
|
lua_Unsigned r = ~luaL_checkunsigned(L, 1);
|
2010-10-28 19:17:29 +04:00
|
|
|
lua_pushunsigned(L, trim(r));
|
2009-07-01 20:17:08 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-01 15:54:56 +04:00
|
|
|
static int b_shift (lua_State *L, lua_Unsigned r, lua_Integer i) {
|
2009-07-01 20:17:08 +04:00
|
|
|
if (i < 0) { /* shift right? */
|
|
|
|
i = -i;
|
2010-10-28 19:17:29 +04:00
|
|
|
r = trim(r);
|
2010-12-17 16:26:38 +03:00
|
|
|
if (i >= LUA_NBITS) r = 0;
|
2009-07-01 20:17:08 +04:00
|
|
|
else r >>= i;
|
|
|
|
}
|
|
|
|
else { /* shift left */
|
2010-12-17 16:26:38 +03:00
|
|
|
if (i >= LUA_NBITS) r = 0;
|
2009-07-01 20:17:08 +04:00
|
|
|
else r <<= i;
|
2010-10-28 19:17:29 +04:00
|
|
|
r = trim(r);
|
2009-07-01 20:17:08 +04:00
|
|
|
}
|
2010-10-26 00:31:11 +04:00
|
|
|
lua_pushunsigned(L, r);
|
2009-07-01 20:17:08 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-11 18:55:29 +03:00
|
|
|
static int b_lshift (lua_State *L) {
|
2014-10-01 15:54:56 +04:00
|
|
|
return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkinteger(L, 2));
|
2010-02-11 18:55:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_rshift (lua_State *L) {
|
2014-10-01 15:54:56 +04:00
|
|
|
return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkinteger(L, 2));
|
2010-10-25 18:32:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_arshift (lua_State *L) {
|
2013-07-09 21:49:50 +04:00
|
|
|
lua_Unsigned r = luaL_checkunsigned(L, 1);
|
2014-10-01 15:54:56 +04:00
|
|
|
lua_Integer i = luaL_checkinteger(L, 2);
|
2013-07-09 21:49:50 +04:00
|
|
|
if (i < 0 || !(r & ((lua_Unsigned)1 << (LUA_NBITS - 1))))
|
2010-10-25 18:32:36 +04:00
|
|
|
return b_shift(L, r, -i);
|
|
|
|
else { /* arithmetic shift for 'negative' number */
|
2010-12-17 16:26:38 +03:00
|
|
|
if (i >= LUA_NBITS) r = ALLONES;
|
2010-10-25 18:32:36 +04:00
|
|
|
else
|
2013-07-09 21:49:50 +04:00
|
|
|
r = trim((r >> i) | ~(trim(~(lua_Unsigned)0) >> i)); /* add signal bit */
|
2010-10-26 00:31:11 +04:00
|
|
|
lua_pushunsigned(L, r);
|
2010-10-25 18:32:36 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2010-02-11 18:55:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-01 15:54:56 +04:00
|
|
|
static int b_rot (lua_State *L, lua_Integer d) {
|
2013-07-09 21:49:50 +04:00
|
|
|
lua_Unsigned r = luaL_checkunsigned(L, 1);
|
2014-10-01 15:54:56 +04:00
|
|
|
int i = d & (LUA_NBITS - 1); /* i = d % NBITS */
|
2010-10-28 19:17:29 +04:00
|
|
|
r = trim(r);
|
2013-07-09 22:31:01 +04:00
|
|
|
if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */
|
|
|
|
r = (r << i) | (r >> (LUA_NBITS - i));
|
2010-10-28 19:17:29 +04:00
|
|
|
lua_pushunsigned(L, trim(r));
|
2009-07-01 20:17:08 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-22 19:39:20 +03:00
|
|
|
static int b_lrot (lua_State *L) {
|
2014-10-01 15:54:56 +04:00
|
|
|
return b_rot(L, luaL_checkinteger(L, 2));
|
2010-02-11 18:55:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-22 19:39:20 +03:00
|
|
|
static int b_rrot (lua_State *L) {
|
2014-10-01 15:54:56 +04:00
|
|
|
return b_rot(L, -luaL_checkinteger(L, 2));
|
2010-02-11 18:55:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-29 18:19:28 +03:00
|
|
|
/*
|
|
|
|
** get field and width arguments for field-manipulation functions,
|
2013-03-19 17:19:12 +04:00
|
|
|
** checking whether they are valid.
|
|
|
|
** ('luaL_error' called without 'return' to avoid later warnings about
|
|
|
|
** 'width' being used uninitialized.)
|
2010-11-29 18:19:28 +03:00
|
|
|
*/
|
|
|
|
static int fieldargs (lua_State *L, int farg, int *width) {
|
2014-10-01 15:54:56 +04:00
|
|
|
lua_Integer f = luaL_checkinteger(L, farg);
|
|
|
|
lua_Integer w = luaL_optinteger(L, farg + 1, 1);
|
2011-06-20 20:35:23 +04:00
|
|
|
luaL_argcheck(L, 0 <= f, farg, "field cannot be negative");
|
2010-11-29 18:19:28 +03:00
|
|
|
luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
|
2010-12-17 16:26:38 +03:00
|
|
|
if (f + w > LUA_NBITS)
|
2013-03-19 17:19:12 +04:00
|
|
|
luaL_error(L, "trying to access non-existent bits");
|
2014-10-01 15:54:56 +04:00
|
|
|
*width = (int)w;
|
|
|
|
return (int)f;
|
2010-11-29 18:19:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int b_extract (lua_State *L) {
|
|
|
|
int w;
|
2013-07-09 21:49:50 +04:00
|
|
|
lua_Unsigned r = trim(luaL_checkunsigned(L, 1));
|
2010-11-29 18:19:28 +03:00
|
|
|
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;
|
2013-07-09 21:49:50 +04:00
|
|
|
lua_Unsigned r = trim(luaL_checkunsigned(L, 1));
|
|
|
|
lua_Unsigned v = luaL_checkunsigned(L, 2);
|
2010-11-29 18:19:28 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-01 20:17:08 +04:00
|
|
|
static const luaL_Reg bitlib[] = {
|
2010-11-22 19:39:20 +03:00
|
|
|
{"arshift", b_arshift},
|
|
|
|
{"band", b_and},
|
|
|
|
{"bnot", b_not},
|
|
|
|
{"bor", b_or},
|
|
|
|
{"bxor", b_xor},
|
2010-11-29 18:19:28 +03:00
|
|
|
{"btest", b_test},
|
|
|
|
{"extract", b_extract},
|
2010-11-22 19:39:20 +03:00
|
|
|
{"lrotate", b_lrot},
|
|
|
|
{"lshift", b_lshift},
|
2010-11-29 18:19:28 +03:00
|
|
|
{"replace", b_replace},
|
2010-11-22 19:39:20 +03:00
|
|
|
{"rrotate", b_rrot},
|
|
|
|
{"rshift", b_rshift},
|
2009-07-01 20:17:08 +04:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-25 18:32:36 +04:00
|
|
|
LUAMOD_API int luaopen_bit32 (lua_State *L) {
|
2010-07-02 15:38:13 +04:00
|
|
|
luaL_newlib(L, bitlib);
|
2009-07-01 20:17:08 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2010-10-28 19:17:29 +04:00
|
|
|
|
2014-03-18 22:27:08 +04:00
|
|
|
|
|
|
|
#else /* }{ */
|
|
|
|
|
|
|
|
|
|
|
|
LUAMOD_API int luaopen_bit32 (lua_State *L) {
|
2014-05-15 23:28:34 +04:00
|
|
|
return luaL_error(L, "library 'bit32' has been deprecated");
|
2014-03-18 22:27:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* } */
|