1 /* Bitwise operations library */
2 /* (c) Reuben Thomas 2000-2008 */
3 /* See README for license */
5 /* Metalua: no automake/autoconf goo pleaaaze! :) */
6 /* #include "config.h" */
11 /* FIXME: Should really use limits of lua_Integer (currently not given
12 by Lua); the code below assumes that lua_Integer is ptrdiff_t, that
13 size_t is the same as unsigned ptrdiff_t, and that lua_Number is
14 floating-point and fits in a double (use of fmod). */
16 #define TOINTEGER(L, n, f) \
18 luaL_checkinteger((L), (n)))
23 #define TOINTEGER(L, n, f) \
24 ((ptrdiff_t)(((f) = fmod(luaL_checknumber((L), (n)), (double)SIZE_MAX)), \
25 (f) > PTRDIFF_MAX ? ((f) -= SIZE_MAX + 1) : \
26 ((f) < PTRDIFF_MIN ? ((f) += SIZE_MAX + 1) : (f))))
29 #define TDYADIC(name, op) \
30 static int bit_ ## name(lua_State *L) { \
32 lua_Integer w = TOINTEGER(L, 1, f); \
33 lua_pushinteger(L, w op TOINTEGER(L, 2, f)); \
37 #define MONADIC(name, op) \
38 static int bit_ ## name(lua_State *L) { \
40 lua_pushinteger(L, op TOINTEGER(L, 1, f)); \
44 #define VARIADIC(name, op) \
45 static int bit_ ## name(lua_State *L) { \
47 int n = lua_gettop(L), i; \
48 lua_Integer w = TOINTEGER(L, 1, f); \
49 for (i = 2; i <= n; i++) \
50 w op TOINTEGER(L, i, f); \
51 lua_pushinteger(L, w); \
64 static const struct luaL_reg bitlib[] = {
70 {"lshift", bit_lshift},
71 {"rshift", bit_rshift},
72 {"arshift", bit_arshift},
76 LUALIB_API int luaopen_bit (lua_State *L) {
77 luaL_openlib(L, "bit", bitlib, 0);