diff options
author | Mike Pall <mike> | 2012-07-08 16:24:53 +0200 |
---|---|---|
committer | Mike Pall <mike> | 2012-07-08 16:24:53 +0200 |
commit | e3dec0438d50dbdf0184078d4e0233e399725787 (patch) | |
tree | c6bb83ac81679b5904a909f301852f36a873d63d /src/host/minilua.c | |
parent | 6a67fa8a4b2b038e0dca676303545da47f2b9773 (diff) | |
download | luajit-e3dec0438d50dbdf0184078d4e0233e399725787.tar.gz luajit-e3dec0438d50dbdf0184078d4e0233e399725787.tar.bz2 luajit-e3dec0438d50dbdf0184078d4e0233e399725787.zip |
Add Lua BitOp to minilua.
Diffstat (limited to '')
-rw-r--r-- | src/host/minilua.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/host/minilua.c b/src/host/minilua.c index 77e6349c..93e9273e 100644 --- a/src/host/minilua.c +++ b/src/host/minilua.c | |||
@@ -22,6 +22,11 @@ | |||
22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | 22 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | 23 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
24 | ******************************************************************************/ | 24 | ******************************************************************************/ |
25 | #ifdef _MSC_VER | ||
26 | typedef unsigned __int64 U64; | ||
27 | #else | ||
28 | typedef unsigned long long U64; | ||
29 | #endif | ||
25 | #include <stddef.h> | 30 | #include <stddef.h> |
26 | #include <stdarg.h> | 31 | #include <stdarg.h> |
27 | #include <limits.h> | 32 | #include <limits.h> |
@@ -7683,10 +7688,68 @@ lua_pushstring(L,lib->name); | |||
7683 | lua_call(L,1,0); | 7688 | lua_call(L,1,0); |
7684 | } | 7689 | } |
7685 | } | 7690 | } |
7691 | typedef unsigned int UB; | ||
7692 | static UB barg(lua_State*L,int idx){ | ||
7693 | union{lua_Number n;U64 b;}bn; | ||
7694 | bn.n=lua_tonumber(L,idx)+6755399441055744.0; | ||
7695 | if(bn.n==0.0&&!lua_isnumber(L,idx))luaL_typerror(L,idx,"number"); | ||
7696 | return(UB)bn.b; | ||
7697 | } | ||
7698 | #define BRET(b)lua_pushnumber(L,(lua_Number)(int)(b));return 1; | ||
7699 | static int tobit(lua_State*L){ | ||
7700 | BRET(barg(L,1))} | ||
7701 | static int bnot(lua_State*L){ | ||
7702 | BRET(~barg(L,1))} | ||
7703 | static int band(lua_State*L){ | ||
7704 | int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b&=barg(L,i);BRET(b)} | ||
7705 | static int bor(lua_State*L){ | ||
7706 | int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b|=barg(L,i);BRET(b)} | ||
7707 | static int bxor(lua_State*L){ | ||
7708 | int i;UB b=barg(L,1);for(i=lua_gettop(L);i>1;i--)b^=barg(L,i);BRET(b)} | ||
7709 | static int lshift(lua_State*L){ | ||
7710 | UB b=barg(L,1),n=barg(L,2)&31;BRET(b<<n)} | ||
7711 | static int rshift(lua_State*L){ | ||
7712 | UB b=barg(L,1),n=barg(L,2)&31;BRET(b>>n)} | ||
7713 | static int arshift(lua_State*L){ | ||
7714 | UB b=barg(L,1),n=barg(L,2)&31;BRET((int)b>>n)} | ||
7715 | static int rol(lua_State*L){ | ||
7716 | UB b=barg(L,1),n=barg(L,2)&31;BRET((b<<n)|(b>>(32-n)))} | ||
7717 | static int ror(lua_State*L){ | ||
7718 | UB b=barg(L,1),n=barg(L,2)&31;BRET((b>>n)|(b<<(32-n)))} | ||
7719 | static int bswap(lua_State*L){ | ||
7720 | UB b=barg(L,1);b=(b>>24)|((b>>8)&0xff00)|((b&0xff00)<<8)|(b<<24);BRET(b)} | ||
7721 | static int tohex(lua_State*L){ | ||
7722 | UB b=barg(L,1); | ||
7723 | int n=lua_isnone(L,2)?8:(int)barg(L,2); | ||
7724 | const char*hexdigits="0123456789abcdef"; | ||
7725 | char buf[8]; | ||
7726 | int i; | ||
7727 | if(n<0){n=-n;hexdigits="0123456789ABCDEF";} | ||
7728 | if(n>8)n=8; | ||
7729 | for(i=(int)n;--i>=0;){buf[i]=hexdigits[b&15];b>>=4;} | ||
7730 | lua_pushlstring(L,buf,(size_t)n); | ||
7731 | return 1; | ||
7732 | } | ||
7733 | static const struct luaL_Reg bitlib[]={ | ||
7734 | {"tobit",tobit}, | ||
7735 | {"bnot",bnot}, | ||
7736 | {"band",band}, | ||
7737 | {"bor",bor}, | ||
7738 | {"bxor",bxor}, | ||
7739 | {"lshift",lshift}, | ||
7740 | {"rshift",rshift}, | ||
7741 | {"arshift",arshift}, | ||
7742 | {"rol",rol}, | ||
7743 | {"ror",ror}, | ||
7744 | {"bswap",bswap}, | ||
7745 | {"tohex",tohex}, | ||
7746 | {NULL,NULL} | ||
7747 | }; | ||
7686 | int main(int argc,char**argv){ | 7748 | int main(int argc,char**argv){ |
7687 | lua_State*L=luaL_newstate(); | 7749 | lua_State*L=luaL_newstate(); |
7688 | int i; | 7750 | int i; |
7689 | luaL_openlibs(L); | 7751 | luaL_openlibs(L); |
7752 | luaL_register(L,"bit",bitlib); | ||
7690 | if(argc<2)return sizeof(void*); | 7753 | if(argc<2)return sizeof(void*); |
7691 | lua_createtable(L,0,1); | 7754 | lua_createtable(L,0,1); |
7692 | lua_pushstring(L,argv[1]); | 7755 | lua_pushstring(L,argv[1]); |