1 /* Bitwise operations library */
2 /* (c) Reuben Thomas 2000-2008 */
3 /* See README for license */
9 #include "pm3_bit_limits.h"
12 /* FIXME: Assumes lua_Integer is ptrdiff_t */
13 #define LUA_INTEGER_MAX PTRDIFF_MAX
14 #define LUA_INTEGER_MIN PTRDIFF_MIN
16 /* FIXME: Assumes size_t is an unsigned lua_Integer */
17 typedef size_t lua_UInteger
;
18 #define LUA_UINTEGER_MAX SIZE_MAX
21 /* Bit type size and limits */
24 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? \
25 BITLIB_FLOAT_BITS : (CHAR_BIT * sizeof(lua_Integer)))
27 /* This code may give warnings if BITLIB_FLOAT_* are too big to fit in
28 long, but that doesn't matter since in that case they won't be
31 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_MAX : LUA_INTEGER_MAX)
34 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_MIN : LUA_INTEGER_MIN)
37 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_UMAX : LUA_UINTEGER_MAX)
40 /* Define TOBIT to get a bit value */
43 #define TOBIT(L, n, res) \
44 ((void)(res), luaL_checkinteger((L), (n)))
49 /* FIXME: Assumes lua_Number fits in a double (use of fmod). */
50 #define TOBIT(L, n, res) \
51 ((lua_Integer)(((res) = fmod(luaL_checknumber(L, (n)), (double)BIT_UMAX + 1.0)), \
52 (res) > BIT_MAX ? ((res) -= (double)BIT_UMAX, (res) -= 1) : \
53 ((res) < BIT_MIN ? ((res) += (double)BIT_UMAX, (res) += 1) : (res))))
57 #define BIT_TRUNCATE(i) \
63 The macros MONADIC and VARIADIC only deal with bitwise operations.
65 LOGICAL_SHIFT truncates its left-hand operand before shifting so
66 that any extra bits at the most-significant end are not shifted
69 ARITHMETIC_SHIFT does not truncate its left-hand operand, so that
70 the sign bits are not removed and right shift work properly.
73 #define MONADIC(name, op) \
74 static int bit_ ## name(lua_State *L) { \
76 lua_pushinteger(L, BIT_TRUNCATE(op TOBIT(L, 1, f))); \
80 #define VARIADIC(name, op) \
81 static int bit_ ## name(lua_State *L) { \
83 int n = lua_gettop(L), i; \
84 lua_Integer w = TOBIT(L, 1, f); \
85 for (i = 2; i <= n; i++) \
86 w op TOBIT(L, i, f); \
87 lua_pushinteger(L, BIT_TRUNCATE(w)); \
91 #define LOGICAL_SHIFT(name, op) \
92 static int bit_ ## name(lua_State *L) { \
94 lua_pushinteger(L, BIT_TRUNCATE(BIT_TRUNCATE((lua_UInteger)TOBIT(L, 1, f)) op \
95 (unsigned)luaL_checknumber(L, 2))); \
99 #define ARITHMETIC_SHIFT(name, op) \
100 static int bit_ ## name(lua_State *L) { \
102 lua_pushinteger(L, BIT_TRUNCATE((lua_Integer)TOBIT(L, 1, f) op \
103 (unsigned)luaL_checknumber(L, 2))); \
112 ARITHMETIC_SHIFT(lshift
, <<)
113 LOGICAL_SHIFT(rshift
, >>)
114 ARITHMETIC_SHIFT(arshift
, >>)
116 static const struct luaL_Reg bitlib
[] = {
122 {"lshift", bit_lshift
},
123 {"rshift", bit_rshift
},
124 {"arshift", bit_arshift
},
128 LUALIB_API
int luaopen_bit (lua_State
*L
) {
129 luaL_newlib(L
, bitlib
);
130 //luaL_register(L, "bit", bitlib);
131 lua_pushnumber(L
, BIT_BITS
);
132 lua_setfield(L
, -2, "bits");
137 LUALIB_API int luaopen_bit (lua_State *L) {
138 luaL_register(L, "bit", bitlib);
139 lua_pushnumber(L, BIT_BITS);
140 lua_setfield(L, -2, "bits");
147 int set_bit_library (lua_State
*L
) {
149 luaL_requiref(L
, "bit", luaopen_bit
, 1);