1 /* Bitwise operations library */
2 /* (c) Reuben Thomas 2000-2008 */
3 /* See README for license */
9 #include "pm3_bit_limits.h"
10 #include "pm3_bitlib.h"
13 /* FIXME: Assumes lua_Integer is ptrdiff_t */
14 #define LUA_INTEGER_MAX PTRDIFF_MAX
15 #define LUA_INTEGER_MIN PTRDIFF_MIN
17 /* FIXME: Assumes size_t is an unsigned lua_Integer */
18 typedef size_t lua_UInteger
;
19 #define LUA_UINTEGER_MAX SIZE_MAX
22 /* Bit type size and limits */
25 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? \
26 BITLIB_FLOAT_BITS : (CHAR_BIT * sizeof(lua_Integer)))
28 /* This code may give warnings if BITLIB_FLOAT_* are too big to fit in
29 long, but that doesn't matter since in that case they won't be
32 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_MAX : LUA_INTEGER_MAX)
35 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_MIN : LUA_INTEGER_MIN)
38 (CHAR_BIT * sizeof(lua_Integer) > BITLIB_FLOAT_BITS ? BITLIB_FLOAT_UMAX : LUA_UINTEGER_MAX)
41 /* Define TOBIT to get a bit value */
44 #define TOBIT(L, n, res) \
45 ((void)(res), luaL_checkinteger((L), (n)))
50 /* FIXME: Assumes lua_Number fits in a double (use of fmod). */
51 #define TOBIT(L, n, res) \
52 ((lua_Integer)(((res) = fmod(luaL_checknumber(L, (n)), (double)BIT_UMAX + 1.0)), \
53 (res) > BIT_MAX ? ((res) -= (double)BIT_UMAX, (res) -= 1) : \
54 ((res) < BIT_MIN ? ((res) += (double)BIT_UMAX, (res) += 1) : (res))))
58 #define BIT_TRUNCATE(i) \
64 The macros MONADIC and VARIADIC only deal with bitwise operations.
66 LOGICAL_SHIFT truncates its left-hand operand before shifting so
67 that any extra bits at the most-significant end are not shifted
70 ARITHMETIC_SHIFT does not truncate its left-hand operand, so that
71 the sign bits are not removed and right shift work properly.
74 #define MONADIC(name, op) \
75 static int bit_ ## name(lua_State *L) { \
77 lua_pushinteger(L, BIT_TRUNCATE(op TOBIT(L, 1, f))); \
81 #define VARIADIC(name, op) \
82 static int bit_ ## name(lua_State *L) { \
84 int n = lua_gettop(L), i; \
85 lua_Integer w = TOBIT(L, 1, f); \
86 for (i = 2; i <= n; i++) \
87 w op TOBIT(L, i, f); \
88 lua_pushinteger(L, BIT_TRUNCATE(w)); \
92 #define LOGICAL_SHIFT(name, op) \
93 static int bit_ ## name(lua_State *L) { \
95 lua_Number n = luaL_checknumber(L, 2); \
96 lua_pushinteger(L, BIT_TRUNCATE(BIT_TRUNCATE((lua_UInteger)TOBIT(L, 1, f)) op \
101 #define ARITHMETIC_SHIFT(name, op) \
102 static int bit_ ## name(lua_State *L) { \
104 lua_Number n = luaL_checknumber(L, 2); \
105 lua_pushinteger(L, BIT_TRUNCATE((lua_Integer)TOBIT(L, 1, f) op \
115 ARITHMETIC_SHIFT(lshift
, <<)
116 LOGICAL_SHIFT(rshift
, >>)
117 ARITHMETIC_SHIFT(arshift
, >>)
119 static const struct luaL_Reg bitlib
[] = {
125 {"lshift", bit_lshift
},
126 {"rshift", bit_rshift
},
127 {"arshift", bit_arshift
},
131 LUALIB_API
int luaopen_bit(lua_State
*L
);
132 LUALIB_API
int luaopen_bit(lua_State
*L
) {
133 luaL_newlib(L
, bitlib
);
134 //luaL_register(L, "bit", bitlib);
135 lua_pushnumber(L
, BIT_BITS
);
136 lua_setfield(L
, -2, "bits");
141 LUALIB_API int luaopen_bit (lua_State *L) {
142 luaL_register(L, "bit", bitlib);
143 lua_pushnumber(L, BIT_BITS);
144 lua_setfield(L, -2, "bits");
151 int set_bit_library(lua_State
*L
) {
153 luaL_requiref(L
, "bit", luaopen_bit
, 1);