mirror of
https://github.com/LuaJIT/LuaJIT.git
synced 2025-02-07 23:24:09 +00:00
Add Lua BitOp to minilua.
This commit is contained in:
parent
6a67fa8a4b
commit
e3dec0438d
@ -58,10 +58,68 @@ end)
|
|||||||
local REMOVE_EXTINC = { ["<assert.h>"] = true, ["<locale.h>"] = true, }
|
local REMOVE_EXTINC = { ["<assert.h>"] = true, ["<locale.h>"] = true, }
|
||||||
|
|
||||||
local CUSTOM_MAIN = [[
|
local CUSTOM_MAIN = [[
|
||||||
|
typedef unsigned int UB;
|
||||||
|
static UB barg(lua_State *L,int idx){
|
||||||
|
union{lua_Number n;U64 b;}bn;
|
||||||
|
bn.n=lua_tonumber(L,idx)+6755399441055744.0;
|
||||||
|
if (bn.n==0.0&&!lua_isnumber(L,idx))luaL_typerror(L,idx,"number");
|
||||||
|
return(UB)bn.b;
|
||||||
|
}
|
||||||
|
#define BRET(b) lua_pushnumber(L,(lua_Number)(int)(b));return 1;
|
||||||
|
static int tobit(lua_State *L){
|
||||||
|
BRET(barg(L,1))}
|
||||||
|
static int bnot(lua_State *L){
|
||||||
|
BRET(~barg(L,1))}
|
||||||
|
static int band(lua_State *L){
|
||||||
|
int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b&=barg(L,i);BRET(b)}
|
||||||
|
static int bor(lua_State *L){
|
||||||
|
int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b|=barg(L,i);BRET(b)}
|
||||||
|
static int bxor(lua_State *L){
|
||||||
|
int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b^=barg(L,i);BRET(b)}
|
||||||
|
static int lshift(lua_State *L){
|
||||||
|
UB b=barg(L,1),n=barg(L,2)&31;BRET(b<<n)}
|
||||||
|
static int rshift(lua_State *L){
|
||||||
|
UB b=barg(L,1),n=barg(L,2)&31;BRET(b>>n)}
|
||||||
|
static int arshift(lua_State *L){
|
||||||
|
UB b=barg(L,1),n=barg(L,2)&31;BRET((int)b>>n)}
|
||||||
|
static int rol(lua_State *L){
|
||||||
|
UB b=barg(L,1),n=barg(L,2)&31;BRET((b<<n)|(b>>(32-n)))}
|
||||||
|
static int ror(lua_State *L){
|
||||||
|
UB b=barg(L,1),n=barg(L,2)&31;BRET((b>>n)|(b<<(32-n)))}
|
||||||
|
static int bswap(lua_State *L){
|
||||||
|
UB b=barg(L,1);b=(b>>24)|((b>>8)&0xff00)|((b&0xff00)<<8)|(b<<24);BRET(b)}
|
||||||
|
static int tohex(lua_State *L){
|
||||||
|
UB b=barg(L,1);
|
||||||
|
int n=lua_isnone(L,2)?8:(int)barg(L,2);
|
||||||
|
const char *hexdigits="0123456789abcdef";
|
||||||
|
char buf[8];
|
||||||
|
int i;
|
||||||
|
if(n<0){n=-n;hexdigits="0123456789ABCDEF";}
|
||||||
|
if(n>8)n=8;
|
||||||
|
for(i=(int)n;--i>=0;){buf[i]=hexdigits[b&15];b>>=4;}
|
||||||
|
lua_pushlstring(L,buf,(size_t)n);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
static const struct luaL_Reg bitlib[] = {
|
||||||
|
{"tobit",tobit},
|
||||||
|
{"bnot",bnot},
|
||||||
|
{"band",band},
|
||||||
|
{"bor",bor},
|
||||||
|
{"bxor",bxor},
|
||||||
|
{"lshift",lshift},
|
||||||
|
{"rshift",rshift},
|
||||||
|
{"arshift",arshift},
|
||||||
|
{"rol",rol},
|
||||||
|
{"ror",ror},
|
||||||
|
{"bswap",bswap},
|
||||||
|
{"tohex",tohex},
|
||||||
|
{NULL,NULL}
|
||||||
|
};
|
||||||
int main(int argc, char **argv){
|
int main(int argc, char **argv){
|
||||||
lua_State *L = luaL_newstate();
|
lua_State *L = luaL_newstate();
|
||||||
int i;
|
int i;
|
||||||
luaL_openlibs(L);
|
luaL_openlibs(L);
|
||||||
|
luaL_register(L, "bit", bitlib);
|
||||||
if (argc < 2) return sizeof(void *);
|
if (argc < 2) return sizeof(void *);
|
||||||
lua_createtable(L, 0, 1);
|
lua_createtable(L, 0, 1);
|
||||||
lua_pushstring(L, argv[1]);
|
lua_pushstring(L, argv[1]);
|
||||||
@ -146,7 +204,13 @@ local function def_istrue(def)
|
|||||||
def == "UNUSED"
|
def == "UNUSED"
|
||||||
end
|
end
|
||||||
|
|
||||||
local head, defs = {}, {}
|
local head, defs = {[[
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
typedef unsigned __int64 U64;
|
||||||
|
#else
|
||||||
|
typedef unsigned long long U64;
|
||||||
|
#endif
|
||||||
|
]]}, {}
|
||||||
|
|
||||||
local function preprocess(src)
|
local function preprocess(src)
|
||||||
local t = { match(src, "^(.-)#") }
|
local t = { match(src, "^(.-)#") }
|
||||||
|
@ -22,6 +22,11 @@
|
|||||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
typedef unsigned __int64 U64;
|
||||||
|
#else
|
||||||
|
typedef unsigned long long U64;
|
||||||
|
#endif
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
@ -7683,10 +7688,68 @@ lua_pushstring(L,lib->name);
|
|||||||
lua_call(L,1,0);
|
lua_call(L,1,0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
typedef unsigned int UB;
|
||||||
|
static UB barg(lua_State*L,int idx){
|
||||||
|
union{lua_Number n;U64 b;}bn;
|
||||||
|
bn.n=lua_tonumber(L,idx)+6755399441055744.0;
|
||||||
|
if(bn.n==0.0&&!lua_isnumber(L,idx))luaL_typerror(L,idx,"number");
|
||||||
|
return(UB)bn.b;
|
||||||
|
}
|
||||||
|
#define BRET(b)lua_pushnumber(L,(lua_Number)(int)(b));return 1;
|
||||||
|
static int tobit(lua_State*L){
|
||||||
|
BRET(barg(L,1))}
|
||||||
|
static int bnot(lua_State*L){
|
||||||
|
BRET(~barg(L,1))}
|
||||||
|
static int band(lua_State*L){
|
||||||
|
int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b&=barg(L,i);BRET(b)}
|
||||||
|
static int bor(lua_State*L){
|
||||||
|
int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b|=barg(L,i);BRET(b)}
|
||||||
|
static int bxor(lua_State*L){
|
||||||
|
int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b^=barg(L,i);BRET(b)}
|
||||||
|
static int lshift(lua_State*L){
|
||||||
|
UB b=barg(L,1),n=barg(L,2)&31;BRET(b<<n)}
|
||||||
|
static int rshift(lua_State*L){
|
||||||
|
UB b=barg(L,1),n=barg(L,2)&31;BRET(b>>n)}
|
||||||
|
static int arshift(lua_State*L){
|
||||||
|
UB b=barg(L,1),n=barg(L,2)&31;BRET((int)b>>n)}
|
||||||
|
static int rol(lua_State*L){
|
||||||
|
UB b=barg(L,1),n=barg(L,2)&31;BRET((b<<n)|(b>>(32-n)))}
|
||||||
|
static int ror(lua_State*L){
|
||||||
|
UB b=barg(L,1),n=barg(L,2)&31;BRET((b>>n)|(b<<(32-n)))}
|
||||||
|
static int bswap(lua_State*L){
|
||||||
|
UB b=barg(L,1);b=(b>>24)|((b>>8)&0xff00)|((b&0xff00)<<8)|(b<<24);BRET(b)}
|
||||||
|
static int tohex(lua_State*L){
|
||||||
|
UB b=barg(L,1);
|
||||||
|
int n=lua_isnone(L,2)?8:(int)barg(L,2);
|
||||||
|
const char*hexdigits="0123456789abcdef";
|
||||||
|
char buf[8];
|
||||||
|
int i;
|
||||||
|
if(n<0){n=-n;hexdigits="0123456789ABCDEF";}
|
||||||
|
if(n>8)n=8;
|
||||||
|
for(i=(int)n;--i>=0;){buf[i]=hexdigits[b&15];b>>=4;}
|
||||||
|
lua_pushlstring(L,buf,(size_t)n);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
static const struct luaL_Reg bitlib[]={
|
||||||
|
{"tobit",tobit},
|
||||||
|
{"bnot",bnot},
|
||||||
|
{"band",band},
|
||||||
|
{"bor",bor},
|
||||||
|
{"bxor",bxor},
|
||||||
|
{"lshift",lshift},
|
||||||
|
{"rshift",rshift},
|
||||||
|
{"arshift",arshift},
|
||||||
|
{"rol",rol},
|
||||||
|
{"ror",ror},
|
||||||
|
{"bswap",bswap},
|
||||||
|
{"tohex",tohex},
|
||||||
|
{NULL,NULL}
|
||||||
|
};
|
||||||
int main(int argc,char**argv){
|
int main(int argc,char**argv){
|
||||||
lua_State*L=luaL_newstate();
|
lua_State*L=luaL_newstate();
|
||||||
int i;
|
int i;
|
||||||
luaL_openlibs(L);
|
luaL_openlibs(L);
|
||||||
|
luaL_register(L,"bit",bitlib);
|
||||||
if(argc<2)return sizeof(void*);
|
if(argc<2)return sizeof(void*);
|
||||||
lua_createtable(L,0,1);
|
lua_createtable(L,0,1);
|
||||||
lua_pushstring(L,argv[1]);
|
lua_pushstring(L,argv[1]);
|
||||||
|
Loading…
Reference in New Issue
Block a user