-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbit.cpp
More file actions
218 lines (189 loc) · 4.52 KB
/
bit.cpp
File metadata and controls
218 lines (189 loc) · 4.52 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "bit.hpp"
#include <lua.hpp>
#include <bit>
#include <bitset>
#include <sstream>
#include "textmodule_lua.hpp"
int bit_lshift(lua_State* L) {
try {
if (tm_callmetan(L, 1, "__shl"))
return 1;
lua_pushnumber(L, tm_tounsigned(L, 1) << tm_tointeger(L, 2));
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int bit_rshift(lua_State* L) {
try {
if (tm_callmetan(L, 1, "__shr"))
return 1;
lua_pushnumber(L, tm_tounsigned(L, 1) >> tm_tointeger(L, 2));
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int bit_lrotate(lua_State* L) {
try {
if (tm_callmetan(L, 1, "__lrot"))
return 1;
lua_pushnumber(L, std::rotl(tm_tounsigned(L, 1), tm_tointeger(L, 2)));
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int bit_rrotate(lua_State* L) {
try {
if (tm_callmetan(L, 1, "__rrot"))
return 1;
lua_pushnumber(L, std::rotr(tm_tounsigned(L, 1), tm_tointeger(L, 2)));
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int bit_not(lua_State* L) {
try {
if (tm_callmetan(L, 1, "__bnot"))
return 1;
lua_pushnumber(L, ~tm_tounsigned(L, 1));
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int bit_and(lua_State* L) {
try {
if (tm_callmetan(L, 1, "__band"))
return 1;
lua_Unsigned ret = tm_tounsigned(L, 1) & tm_tounsigned(L, 2);
int idx = 3;
while (lua_type(L, idx) == LUA_TNUMBER) {
ret = ret & tm_tounsigned(L, 3);
idx++;
}
lua_pushnumber(L, ret);
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int bit_or(lua_State* L) {
try {
if (tm_callmetan(L, 1, "__bor"))
return 1;
unsigned long ret = tm_tounsigned(L, 1) | tm_tounsigned(L, 2);
int idx = 3;
while (lua_type(L, idx) == LUA_TNUMBER) {
ret = ret | tm_tounsigned(L, 3);
idx++;
}
lua_pushnumber(L, ret);
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int bit_xor(lua_State* L) {
try {
if (tm_callmetan(L, 1, "__bxor"))
return 1;
unsigned long ret = tm_tounsigned(L, 1) ^ tm_tounsigned(L, 2);
int idx = 3;
while (lua_type(L, idx) == LUA_TNUMBER) {
ret = ret ^ tm_tounsigned(L, 3);
idx++;
}
lua_pushnumber(L, ret);
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int bit_ceil(lua_State* L) {
try {
lua_pushnumber(L, std::bit_ceil(tm_tounsigned(L, 1)));
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int bit_floor(lua_State* L) {
try {
lua_pushnumber(L, std::bit_floor(tm_tounsigned(L, 1)));
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int bit_popcount(lua_State* L) {
try {
lua_pushnumber(L, std::popcount(tm_tounsigned(L, 1)));
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int bit_binary(lua_State* L) {
try {
lua_Unsigned v = tm_tounsigned(L, 1);
std::wstringstream ss;
ss << std::bitset<sizeof(v) * CHAR_BIT>(v);
lua_pushwstring(L, L"0b"+ss.str());
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
static luaL_Reg TEXTMODULE_BIT_REG[] = {
{"rshift", bit_rshift},
{"lshift", bit_lshift},
{"rrotate", bit_rrotate},
{"lrotate", bit_lrotate},
{"band", bit_and},
{"bnot", bit_not},
{"bor", bit_or},
{"bxor", bit_xor},
{"ceil", bit_ceil},
{"floor", bit_floor},
{"popcount", bit_popcount},
{"binary", bit_binary},
{nullptr, nullptr}
};
void luaReg_bit(lua_State* L, lua_Option opt) {
if (opt["api"]["bit"]) {
tm_debuglog_apiloaded(opt, "bit");
lua_newtable(L);
luaL_register(L, NULL, TEXTMODULE_BIT_REG);
lua_setfield(L, -2, "bit");
}
else {
tm_debuglog_apinoloaded(opt, "bit");
}
}