forked from tux-mind/platform_external_nmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nse_bit.cc
75 lines (63 loc) · 1.98 KB
/
nse_bit.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* Bitwise operations library
* by Reuben Thomas ([email protected])
* bitlib is a C library for Lua 5.x that provides bitwise operations
* It is copyright Reuben Thomas 2000-2006, and is released under the
* MIT license, like Lua (see http://www.lua.org/copyright.html for the
* full license; it's basically the same as the BSD license). There is no
* warranty.
* the most recent copy can be found at http://rrt.sc3d.org/Software/Lua/
**/
extern "C" {
#include "lauxlib.h"
#include "lua.h"
}
#include "nse_bit.h"
typedef long long Integer;
typedef unsigned long long UInteger;
#define luaL_checkbit(L, n) ((Integer)luaL_checknumber(L, n))
#define luaL_checkubit(L, n) ((UInteger)luaL_checkbit(L, n))
#define TDYADIC(name, op, checkbit1, checkbit2) \
static int bit_ ## name(lua_State* L) { \
lua_pushnumber(L, \
(lua_Number)(checkbit1(L, 1) op checkbit2(L, 2))); \
return 1; \
}
#define DYADIC(name, op) \
TDYADIC(name, op, luaL_checkbit, luaL_checkbit)
#define MONADIC(name, op) \
static int bit_ ## name(lua_State* L) { \
lua_pushnumber(L, (lua_Number)(op luaL_checkbit(L, 1))); \
return 1; \
}
#define VARIADIC(name, op) \
static int bit_ ## name(lua_State *L) { \
int n = lua_gettop(L), i; \
Integer w = luaL_checkbit(L, 1); \
for (i = 2; i <= n; i++) \
w op luaL_checkbit(L, i); \
lua_pushnumber(L, (lua_Number)w); \
return 1; \
}
MONADIC(bnot, ~)
VARIADIC(band, &=)
VARIADIC(bor, |=)
VARIADIC(bxor, ^=)
TDYADIC(lshift, <<, luaL_checkbit, luaL_checkubit)
TDYADIC(rshift, >>, luaL_checkubit, luaL_checkubit)
TDYADIC(arshift, >>, luaL_checkbit, luaL_checkubit)
DYADIC(mod, %)
static const struct luaL_Reg bitlib[] = {
{"bnot", bit_bnot},
{"band", bit_band},
{"bor", bit_bor},
{"bxor", bit_bxor},
{"lshift", bit_lshift},
{"rshift", bit_rshift},
{"arshift", bit_arshift},
{"mod", bit_mod},
{NULL, NULL}
};
LUALIB_API int luaopen_bit(lua_State *L) {
luaL_newlib(L, bitlib);
return 1;
}