Lua: register entire LuaBitOp library.
This commit is contained in:
parent
36d61a2b2c
commit
313ed75b55
|
@ -1698,72 +1698,207 @@ use_console:
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// the following bit operations are ported from LuaBitOp 1.0.1,
|
||||||
|
// because it can handle the sign bit (bit 31) correctly.
|
||||||
|
|
||||||
// int AND(int one, int two, ..., int n)
|
/*
|
||||||
//
|
** Lua BitOp -- a bit operations library for Lua 5.1.
|
||||||
// Since Lua doesn't provide binary, I provide this function.
|
** http://bitop.luajit.org/
|
||||||
// Does a full binary AND on all parameters and returns the result.
|
**
|
||||||
static int base_AND(lua_State *L) {
|
** Copyright (C) 2008-2009 Mike Pall. All rights reserved.
|
||||||
int count = lua_gettop(L);
|
**
|
||||||
|
** Permission is hereby granted, free of charge, to any person obtaining
|
||||||
lua_Integer result = ~((lua_Integer)0);
|
** a copy of this software and associated documentation files (the
|
||||||
int i;
|
** "Software"), to deal in the Software without restriction, including
|
||||||
for (i=1; i <= count; i++)
|
** without limitation the rights to use, copy, modify, merge, publish,
|
||||||
result &= luaL_checkinteger(L,i);
|
** distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
lua_settop(L,0);
|
** permit persons to whom the Software is furnished to do so, subject to
|
||||||
lua_pushinteger(L, result);
|
** the following conditions:
|
||||||
return 1;
|
**
|
||||||
|
** The above copyright notice and this permission notice shall be
|
||||||
|
** included in all copies or substantial portions of the Software.
|
||||||
|
**
|
||||||
|
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||||
|
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||||
|
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||||
|
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
**
|
||||||
|
** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */
|
||||||
|
typedef __int32 int32_t;
|
||||||
|
typedef unsigned __int32 uint32_t;
|
||||||
|
typedef unsigned __int64 uint64_t;
|
||||||
|
#else
|
||||||
|
#include <stdint.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef int32_t SBits;
|
||||||
|
typedef uint32_t UBits;
|
||||||
|
|
||||||
|
typedef union {
|
||||||
|
lua_Number n;
|
||||||
|
#ifdef LUA_NUMBER_DOUBLE
|
||||||
|
uint64_t b;
|
||||||
|
#else
|
||||||
|
UBits b;
|
||||||
|
#endif
|
||||||
|
} BitNum;
|
||||||
|
|
||||||
|
/* Convert argument to bit type. */
|
||||||
|
static UBits barg(lua_State *L, int idx)
|
||||||
|
{
|
||||||
|
BitNum bn;
|
||||||
|
UBits b;
|
||||||
|
bn.n = lua_tonumber(L, idx);
|
||||||
|
#if defined(LUA_NUMBER_DOUBLE)
|
||||||
|
bn.n += 6755399441055744.0; /* 2^52+2^51 */
|
||||||
|
#ifdef SWAPPED_DOUBLE
|
||||||
|
b = (UBits)(bn.b >> 32);
|
||||||
|
#else
|
||||||
|
b = (UBits)bn.b;
|
||||||
|
#endif
|
||||||
|
#elif defined(LUA_NUMBER_INT) || defined(LUA_NUMBER_LONG) || \
|
||||||
|
defined(LUA_NUMBER_LONGLONG) || defined(LUA_NUMBER_LONG_LONG) || \
|
||||||
|
defined(LUA_NUMBER_LLONG)
|
||||||
|
if (sizeof(UBits) == sizeof(lua_Number))
|
||||||
|
b = bn.b;
|
||||||
|
else
|
||||||
|
b = (UBits)(SBits)bn.n;
|
||||||
|
#elif defined(LUA_NUMBER_FLOAT)
|
||||||
|
#error "A 'float' lua_Number type is incompatible with this library"
|
||||||
|
#else
|
||||||
|
#error "Unknown number type, check LUA_NUMBER_* in luaconf.h"
|
||||||
|
#endif
|
||||||
|
if (b == 0 && !lua_isnumber(L, idx))
|
||||||
|
luaL_typerror(L, idx, "number");
|
||||||
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Return bit type. */
|
||||||
|
#define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1;
|
||||||
|
|
||||||
// int OR(int one, int two, ..., int n)
|
static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) }
|
||||||
//
|
static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) }
|
||||||
// ..and similarly for a binary OR
|
|
||||||
static int base_OR(lua_State *L) {
|
#define BIT_OP(func, opr) \
|
||||||
int count = lua_gettop(L);
|
static int func(lua_State *L) { int i; UBits b = barg(L, 1); \
|
||||||
|
for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b) }
|
||||||
lua_Integer result = 0;
|
BIT_OP(bit_band, &=)
|
||||||
int i;
|
BIT_OP(bit_bor, |=)
|
||||||
for (i=1; i <= count; i++)
|
BIT_OP(bit_bxor, ^=)
|
||||||
result |= luaL_checkinteger(L,i);
|
|
||||||
lua_settop(L,0);
|
#define bshl(b, n) (b << n)
|
||||||
lua_pushinteger(L, result);
|
#define bshr(b, n) (b >> n)
|
||||||
return 1;
|
#define bsar(b, n) ((SBits)b >> n)
|
||||||
|
#define brol(b, n) ((b << n) | (b >> (32-n)))
|
||||||
|
#define bror(b, n) ((b << (32-n)) | (b >> n))
|
||||||
|
#define BIT_SH(func, fn) \
|
||||||
|
static int func(lua_State *L) { \
|
||||||
|
UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) }
|
||||||
|
BIT_SH(bit_lshift, bshl)
|
||||||
|
BIT_SH(bit_rshift, bshr)
|
||||||
|
BIT_SH(bit_arshift, bsar)
|
||||||
|
BIT_SH(bit_rol, brol)
|
||||||
|
BIT_SH(bit_ror, bror)
|
||||||
|
|
||||||
|
static int bit_bswap(lua_State *L)
|
||||||
|
{
|
||||||
|
UBits b = barg(L, 1);
|
||||||
|
b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24);
|
||||||
|
BRET(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int bit_tohex(lua_State *L)
|
||||||
// int XOR(int one, int two, ..., int n)
|
{
|
||||||
//
|
UBits b = barg(L, 1);
|
||||||
// ..and similarly for a binary XOR
|
SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2);
|
||||||
static int base_XOR(lua_State *L) {
|
const char *hexdigits = "0123456789abcdef";
|
||||||
int count = lua_gettop(L);
|
char buf[8];
|
||||||
|
int i;
|
||||||
lua_Integer result = 0;
|
if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; }
|
||||||
int i;
|
if (n > 8) n = 8;
|
||||||
for (i=1; i <= count; i++)
|
for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; }
|
||||||
result ^= luaL_checkinteger(L,i);
|
lua_pushlstring(L, buf, (size_t)n);
|
||||||
lua_settop(L,0);
|
return 1;
|
||||||
lua_pushinteger(L, result);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const struct luaL_Reg bit_funcs[] = {
|
||||||
|
{ "tobit", bit_tobit },
|
||||||
|
{ "bnot", bit_bnot },
|
||||||
|
{ "band", bit_band },
|
||||||
|
{ "bor", bit_bor },
|
||||||
|
{ "bxor", bit_bxor },
|
||||||
|
{ "lshift", bit_lshift },
|
||||||
|
{ "rshift", bit_rshift },
|
||||||
|
{ "arshift", bit_arshift },
|
||||||
|
{ "rol", bit_rol },
|
||||||
|
{ "ror", bit_ror },
|
||||||
|
{ "bswap", bit_bswap },
|
||||||
|
{ "tohex", bit_tohex },
|
||||||
|
{ NULL, NULL }
|
||||||
|
};
|
||||||
|
|
||||||
// int BIT(int one, int two, ..., int n)
|
/* Signed right-shifts are implementation-defined per C89/C99.
|
||||||
//
|
** But the de facto standard are arithmetic right-shifts on two's
|
||||||
// Returns a number with the specified bit(s) set.
|
** complement CPUs. This behaviour is required here, so test for it.
|
||||||
static int base_BIT(lua_State *L) {
|
*/
|
||||||
int count = lua_gettop(L);
|
#define BAD_SAR (bsar(-8, 2) != (SBits)-2)
|
||||||
|
|
||||||
lua_Integer result = 0;
|
bool luabitop_validate(lua_State *L) // originally named as luaopen_bit
|
||||||
int i;
|
{
|
||||||
for (i=1; i <= count; i++)
|
UBits b;
|
||||||
result |= (lua_Integer)1 << luaL_checkinteger(L,i);
|
lua_pushnumber(L, (lua_Number)1437217655L);
|
||||||
lua_settop(L,0);
|
b = barg(L, -1);
|
||||||
lua_pushinteger(L, result);
|
if (b != (UBits)1437217655L || BAD_SAR) { /* Perform a simple self-test. */
|
||||||
return 1;
|
const char *msg = "compiled with incompatible luaconf.h";
|
||||||
|
#ifdef LUA_NUMBER_DOUBLE
|
||||||
|
#ifdef _WIN32
|
||||||
|
if (b == (UBits)1610612736L)
|
||||||
|
msg = "use D3DCREATE_FPU_PRESERVE with DirectX";
|
||||||
|
#endif
|
||||||
|
if (b == (UBits)1127743488L)
|
||||||
|
msg = "not compiled with SWAPPED_DOUBLE";
|
||||||
|
#endif
|
||||||
|
if (BAD_SAR)
|
||||||
|
msg = "arithmetic right-shift broken";
|
||||||
|
luaL_error(L, "bit library self-test failed (%s)", msg);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LuaBitOp ends here
|
||||||
|
|
||||||
|
static int bit_bshift_emulua(lua_State *L)
|
||||||
|
{
|
||||||
|
int shift = luaL_checkinteger(L,2);
|
||||||
|
if (shift < 0) {
|
||||||
|
lua_pushinteger(L, -shift);
|
||||||
|
lua_replace(L, 2);
|
||||||
|
return bit_lshift(L);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return bit_rshift(L);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int bitbit(lua_State *L)
|
||||||
|
{
|
||||||
|
int rv = 0;
|
||||||
|
int numArgs = lua_gettop(L);
|
||||||
|
for(int i = 1; i <= numArgs; i++) {
|
||||||
|
int where = luaL_checkinteger(L,i);
|
||||||
|
if (where >= 0 && where < 32)
|
||||||
|
rv |= (1 << where);
|
||||||
|
}
|
||||||
|
lua_settop(L,0);
|
||||||
|
BRET(rv);
|
||||||
|
}
|
||||||
|
|
||||||
// The function called periodically to ensure Lua doesn't run amok.
|
// The function called periodically to ensure Lua doesn't run amok.
|
||||||
static void FCEU_LuaHookFunction(lua_State *L, lua_Debug *dbg) {
|
static void FCEU_LuaHookFunction(lua_State *L, lua_Debug *dbg) {
|
||||||
|
@ -2076,15 +2211,17 @@ int FCEU_LoadLuaCode(const char *filename) {
|
||||||
luaL_register(L, "savestate", savestatelib);
|
luaL_register(L, "savestate", savestatelib);
|
||||||
luaL_register(L, "movie", movielib);
|
luaL_register(L, "movie", movielib);
|
||||||
luaL_register(L, "gui", guilib);
|
luaL_register(L, "gui", guilib);
|
||||||
|
luaL_register(L, "bit", bit_funcs); // LuaBitOp library
|
||||||
|
lua_settop(L, 0); // clean the stack, because each call to luaL_register leaves a table on top
|
||||||
|
|
||||||
lua_pushcfunction(L, base_AND);
|
// old bit operation functions
|
||||||
lua_setfield(L, LUA_GLOBALSINDEX, "AND");
|
lua_register(L, "AND", bit_band);
|
||||||
lua_pushcfunction(L, base_OR);
|
lua_register(L, "OR", bit_bor);
|
||||||
lua_setfield(L, LUA_GLOBALSINDEX, "OR");
|
lua_register(L, "XOR", bit_bxor);
|
||||||
lua_pushcfunction(L, base_XOR);
|
lua_register(L, "SHIFT", bit_bshift_emulua);
|
||||||
lua_setfield(L, LUA_GLOBALSINDEX, "XOR");
|
lua_register(L, "BIT", bitbit);
|
||||||
lua_pushcfunction(L, base_BIT);
|
|
||||||
lua_setfield(L, LUA_GLOBALSINDEX, "BIT");
|
luabitop_validate(L);
|
||||||
|
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
lua_setglobal(L,"emu");
|
lua_setglobal(L,"emu");
|
||||||
|
|
Loading…
Reference in New Issue