1 /*
2 ** Lua BitOp -- a bit operations library for Lua 5.1/5.2.
3 ** http://bitop.luajit.org/
4 **
5 ** Copyright (C) 2008-2012 Mike Pall. All rights reserved.
6 **
7 ** Permission is hereby granted, free of charge, to any person obtaining
8 ** a copy of this software and associated documentation files (the
9 ** "Software"), to deal in the Software without restriction, including
10 ** without limitation the rights to use, copy, modify, merge, publish,
11 ** distribute, sublicense, and/or sell copies of the Software, and to
12 ** permit persons to whom the Software is furnished to do so, subject to
13 ** the following conditions:
14 **
15 ** The above copyright notice and this permission notice shall be
16 ** included in all copies or substantial portions of the Software.
17 **
18 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 **
26 ** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
27 **
28 ** Lua 5.3 changes are based on https://github.com/LuaJIT/LuaJIT/issues/384
29 ** added libsize and updated luaopen_bit to load for Lua 5.2 and Lua 5.3
30 */
31
32 #define LUA_BITOP_VERSION "1.0.3"
33
34 #define LUA_LIB
35 #include "lua.h"
36 #include "lauxlib.h"
37
38 #ifdef _MSC_VER
39 /* MSVC is stuck in the last century and doesn't have C99's stdint.h. */
40 typedef __int32 int32_t;
41 typedef unsigned __int32 uint32_t;
42 typedef unsigned __int64 uint64_t;
43 #else
44 #include <stdint.h>
45 #endif
46
47 typedef int32_t SBits;
48 typedef uint32_t UBits;
49
50 typedef union {
51 lua_Number n;
52 #if defined(LUA_NUMBER_DOUBLE) || defined(LUA_FLOAT_DOUBLE)
53 uint64_t b;
54 #else
55 UBits b;
56 #endif
57 } BitNum;
58
59 /* Convert argument to bit type. */
barg(lua_State * L,int idx)60 static UBits barg(lua_State *L, int idx)
61 {
62 BitNum bn;
63 UBits b;
64 #if LUA_VERSION_NUM < 502
65 bn.n = lua_tonumber(L, idx);
66 #else
67 bn.n = luaL_checknumber(L, idx);
68 #endif
69 #if defined(LUA_NUMBER_DOUBLE) || defined(LUA_FLOAT_DOUBLE)
70 bn.n += 6755399441055744.0; /* 2^52+2^51 */
71 #ifdef SWAPPED_DOUBLE
72 b = (UBits)(bn.b >> 32);
73 #else
74 b = (UBits)bn.b;
75 #endif
76 #elif defined(LUA_NUMBER_INT) || defined(LUA_INT_INT) || \
77 defined(LUA_NUMBER_LONG) || defined(LUA_INT_LONG) || \
78 defined(LUA_NUMBER_LONGLONG) || defined(LUA_INT_LONGLONG) || \
79 defined(LUA_NUMBER_LONG_LONG) || defined(LUA_NUMBER_LLONG)
80 if (sizeof(UBits) == sizeof(lua_Number))
81 b = bn.b;
82 else
83 b = (UBits)(SBits)bn.n;
84 #elif defined(LUA_NUMBER_FLOAT) || defined(LUA_FLOAT_FLOAT)
85 #error "A 'float' lua_Number type is incompatible with this library"
86 #else
87 #error "Unknown number type, check LUA_NUMBER_*, LUA_FLOAT_*, LUA_INT_* in luaconf.h"
88 #endif
89 #if LUA_VERSION_NUM < 502
90 if (b == 0 && !lua_isnumber(L, idx)) {
91 luaL_typerror(L, idx, "number");
92 }
93 #endif
94 return b;
95 }
96
97 /* Return bit type. */
98 #if LUA_VERSION_NUM < 503
99 #define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1;
100 #else
101 #define BRET(b) lua_pushinteger(L, (lua_Integer)(SBits)(b)); return 1;
102 #endif
103
bit_tobit(lua_State * L)104 static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) }
bit_bnot(lua_State * L)105 static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) }
106
107 #define BIT_OP(func, opr) \
108 static int func(lua_State *L) { int i; UBits b = barg(L, 1); \
109 for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b) }
110 BIT_OP(bit_band, &=)
111 BIT_OP(bit_bor, |=)
112 BIT_OP(bit_bxor, ^=)
113
114 #define bshl(b, n) (b << n)
115 #define bshr(b, n) (b >> n)
116 #define bsar(b, n) ((SBits)b >> n)
117 #define brol(b, n) ((b << n) | (b >> (32-n)))
118 #define bror(b, n) ((b << (32-n)) | (b >> n))
119 #define BIT_SH(func, fn) \
120 static int func(lua_State *L) { \
121 UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) }
BIT_SH(bit_lshift,bshl)122 BIT_SH(bit_lshift, bshl)
123 BIT_SH(bit_rshift, bshr)
124 BIT_SH(bit_arshift, bsar)
125 BIT_SH(bit_rol, brol)
126 BIT_SH(bit_ror, bror)
127
128 static int bit_bswap(lua_State *L)
129 {
130 UBits b = barg(L, 1);
131 b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24);
132 BRET(b)
133 }
134
bit_tohex(lua_State * L)135 static int bit_tohex(lua_State *L)
136 {
137 UBits b = barg(L, 1);
138 SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2);
139 const char *hexdigits = "0123456789abcdef";
140 char buf[8];
141 int i;
142 if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; }
143 if (n > 8) n = 8;
144 for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; }
145 lua_pushlstring(L, buf, (size_t)n);
146 return 1;
147 }
148
149 static const struct luaL_Reg bit_funcs[] = {
150 { "tobit", bit_tobit },
151 { "bnot", bit_bnot },
152 { "band", bit_band },
153 { "bor", bit_bor },
154 { "bxor", bit_bxor },
155 { "lshift", bit_lshift },
156 { "rshift", bit_rshift },
157 { "arshift", bit_arshift },
158 { "rol", bit_rol },
159 { "ror", bit_ror },
160 { "bswap", bit_bswap },
161 { "tohex", bit_tohex },
162 { NULL, NULL }
163 };
164
165 /* Signed right-shifts are implementation-defined per C89/C99.
166 ** But the de facto standard are arithmetic right-shifts on two's
167 ** complement CPUs. This behaviour is required here, so test for it.
168 */
169 #define BAD_SAR (bsar(-8, 2) != (SBits)-2)
170
171 #if LUA_VERSION_NUM >= 502
libsize(const luaL_Reg * l)172 static int libsize (const luaL_Reg *l) {
173 int size = 0;
174 for (; l && l->name; l++) size++;
175 return size;
176 }
pushmodule(lua_State * L,const char * modname,int sizehint)177 static void pushmodule (lua_State *L, const char *modname, int sizehint) {
178 lua_pushglobaltable(L);
179 lua_pushstring(L, modname);
180 lua_rawget(L, -2);
181 if (!lua_isnil(L, -1)) {
182 luaL_error(L, "name conflict for module '%s'", modname);
183 }
184 lua_pop(L, 1); /* remove this nil */
185 lua_createtable(L, 0, sizehint); /* new table for field */
186 lua_pushstring(L, modname);
187 lua_pushvalue(L, -2);
188 lua_settable(L, -4); /* set new table into field */
189 lua_pushvalue(L, -1);
190 }
191 #endif
192
luaopen_bit(lua_State * L)193 LUALIB_API int luaopen_bit(lua_State *L)
194 {
195 UBits b;
196 #if LUA_VERSION_NUM < 503
197 lua_pushnumber(L, (lua_Number)1437217655L);
198 #else
199 lua_pushinteger(L, (lua_Integer)1437217655L);
200 #endif
201 b = barg(L, -1);
202 if (b != (UBits)1437217655L || BAD_SAR) { /* Perform a simple self-test. */
203 const char *msg = "compiled with incompatible luaconf.h";
204 #if defined(LUA_NUMBER_DOUBLE) || defined(LUA_FLOAT_DOUBLE)
205 #ifdef _WIN32
206 if (b == (UBits)1610612736L)
207 msg = "use D3DCREATE_FPU_PRESERVE with DirectX";
208 #endif
209 if (b == (UBits)1127743488L)
210 msg = "not compiled with SWAPPED_DOUBLE";
211 #endif
212 if (BAD_SAR)
213 msg = "arithmetic right-shift broken";
214 luaL_error(L, "bit library self-test failed (%s)", msg);
215 }
216 #if LUA_VERSION_NUM < 502
217 luaL_register(L, "bit", bit_funcs);
218 #else
219 pushmodule(L, "bit", libsize(bit_funcs)); /* get/create library table */
220 lua_insert(L, -1); /* move library table to below upvalues */
221 luaL_setfuncs(L, bit_funcs, 0);
222 #endif
223 return 1;
224 }
225
226