diff options
Diffstat (limited to 'lbitlib.c')
| -rw-r--r-- | lbitlib.c | 25 |
1 files changed, 9 insertions, 16 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbitlib.c,v 1.6 2010/07/02 12:01:53 roberto Exp roberto $ | 2 | ** $Id: lbitlib.c,v 1.7 2010/10/25 14:32:36 roberto Exp roberto $ |
| 3 | ** Standard library for bitwise operations | 3 | ** Standard library for bitwise operations |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -21,14 +21,7 @@ typedef LUA_INT32 b_int; | |||
| 21 | typedef unsigned LUA_INT32 b_uint; | 21 | typedef unsigned LUA_INT32 b_uint; |
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | static b_uint getuintarg (lua_State *L, int arg) { | 24 | #define getuintarg(L,arg) luaL_checkunsigned(L,arg) |
| 25 | b_uint r; | ||
| 26 | int isnum; | ||
| 27 | lua_Number x = lua_tonumberx(L, arg, &isnum); | ||
| 28 | if (!isnum) luaL_typeerror(L, arg, "number"); | ||
| 29 | lua_number2uint(r, x); | ||
| 30 | return r; | ||
| 31 | } | ||
| 32 | 25 | ||
| 33 | 26 | ||
| 34 | static b_uint andaux (lua_State *L) { | 27 | static b_uint andaux (lua_State *L) { |
| @@ -42,7 +35,7 @@ static b_uint andaux (lua_State *L) { | |||
| 42 | 35 | ||
| 43 | static int b_and (lua_State *L) { | 36 | static int b_and (lua_State *L) { |
| 44 | b_uint r = andaux(L); | 37 | b_uint r = andaux(L); |
| 45 | lua_pushnumber(L, lua_uint2number(r)); | 38 | lua_pushunsigned(L, r); |
| 46 | return 1; | 39 | return 1; |
| 47 | } | 40 | } |
| 48 | 41 | ||
| @@ -59,7 +52,7 @@ static int b_or (lua_State *L) { | |||
| 59 | b_uint r = 0; | 52 | b_uint r = 0; |
| 60 | for (i = 1; i <= n; i++) | 53 | for (i = 1; i <= n; i++) |
| 61 | r |= getuintarg(L, i); | 54 | r |= getuintarg(L, i); |
| 62 | lua_pushnumber(L, lua_uint2number(r)); | 55 | lua_pushunsigned(L, r); |
| 63 | return 1; | 56 | return 1; |
| 64 | } | 57 | } |
| 65 | 58 | ||
| @@ -69,14 +62,14 @@ static int b_xor (lua_State *L) { | |||
| 69 | b_uint r = 0; | 62 | b_uint r = 0; |
| 70 | for (i = 1; i <= n; i++) | 63 | for (i = 1; i <= n; i++) |
| 71 | r ^= getuintarg(L, i); | 64 | r ^= getuintarg(L, i); |
| 72 | lua_pushnumber(L, lua_uint2number(r)); | 65 | lua_pushunsigned(L, r); |
| 73 | return 1; | 66 | return 1; |
| 74 | } | 67 | } |
| 75 | 68 | ||
| 76 | 69 | ||
| 77 | static int b_not (lua_State *L) { | 70 | static int b_not (lua_State *L) { |
| 78 | b_uint r = ~getuintarg(L, 1); | 71 | b_uint r = ~getuintarg(L, 1); |
| 79 | lua_pushnumber(L, lua_uint2number(r)); | 72 | lua_pushunsigned(L, r); |
| 80 | return 1; | 73 | return 1; |
| 81 | } | 74 | } |
| 82 | 75 | ||
| @@ -91,7 +84,7 @@ static int b_shift (lua_State *L, b_uint r, int i) { | |||
| 91 | if (i >= NBITS) r = 0; | 84 | if (i >= NBITS) r = 0; |
| 92 | else r <<= i; | 85 | else r <<= i; |
| 93 | } | 86 | } |
| 94 | lua_pushnumber(L, lua_uint2number(r)); | 87 | lua_pushunsigned(L, r); |
| 95 | return 1; | 88 | return 1; |
| 96 | } | 89 | } |
| 97 | 90 | ||
| @@ -115,7 +108,7 @@ static int b_arshift (lua_State *L) { | |||
| 115 | if (i >= NBITS) r = 0xffffffff; | 108 | if (i >= NBITS) r = 0xffffffff; |
| 116 | else | 109 | else |
| 117 | r = (r >> i) | ~(~(b_uint)0 >> i); /* add signal bit */ | 110 | r = (r >> i) | ~(~(b_uint)0 >> i); /* add signal bit */ |
| 118 | lua_pushnumber(L, lua_uint2number(r)); | 111 | lua_pushunsigned(L, r); |
| 119 | return 1; | 112 | return 1; |
| 120 | } | 113 | } |
| 121 | } | 114 | } |
| @@ -125,7 +118,7 @@ static int b_rot (lua_State *L, int i) { | |||
| 125 | b_uint r = getuintarg(L, 1); | 118 | b_uint r = getuintarg(L, 1); |
| 126 | i &= (NBITS - 1); /* i = i % NBITS */ | 119 | i &= (NBITS - 1); /* i = i % NBITS */ |
| 127 | r = (r << i) | (r >> (NBITS - i)); | 120 | r = (r << i) | (r >> (NBITS - i)); |
| 128 | lua_pushnumber(L, lua_uint2number(r)); | 121 | lua_pushunsigned(L, r); |
| 129 | return 1; | 122 | return 1; |
| 130 | } | 123 | } |
| 131 | 124 | ||
