Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 76 lines (63 sloc) 2.046 kb
7b25129 Updated NSE's bit library to no longer be a shared library.
batrick authored
1 /* Bitwise operations library
2 * by Reuben Thomas (rrt@sc3d.org)
3 * bitlib is a C library for Lua 5.x that provides bitwise operations
4 * It is copyright Reuben Thomas 2000-2006, and is released under the
5 * MIT license, like Lua (see http://www.lua.org/copyright.html for the
6 * full license; it's basically the same as the BSD license). There is no
7 * warranty.
8 * the most recent copy can be found at http://rrt.sc3d.org/Software/Lua/
9 **/
10
2582156 Corrected many #includes for header files (where they are included).
batrick authored
11 extern "C" {
12 #include "lauxlib.h"
13 #include "lua.h"
14 }
15
7b25129 Updated NSE's bit library to no longer be a shared library.
batrick authored
16 #include "nse_bit.h"
17
18 typedef long long Integer;
19 typedef unsigned long long UInteger;
20
21 #define luaL_checkbit(L, n) ((Integer)luaL_checknumber(L, n))
22 #define luaL_checkubit(L, n) ((UInteger)luaL_checkbit(L, n))
23
24 #define TDYADIC(name, op, checkbit1, checkbit2) \
25 static int bit_ ## name(lua_State* L) { \
26 lua_pushnumber(L, \
27 (lua_Number)(checkbit1(L, 1) op checkbit2(L, 2))); \
28 return 1; \
29 }
30
31 #define DYADIC(name, op) \
32 TDYADIC(name, op, luaL_checkbit, luaL_checkbit)
33
34 #define MONADIC(name, op) \
35 static int bit_ ## name(lua_State* L) { \
36 lua_pushnumber(L, (lua_Number)(op luaL_checkbit(L, 1))); \
37 return 1; \
38 }
39
40 #define VARIADIC(name, op) \
41 static int bit_ ## name(lua_State *L) { \
42 int n = lua_gettop(L), i; \
43 Integer w = luaL_checkbit(L, 1); \
44 for (i = 2; i <= n; i++) \
45 w op luaL_checkbit(L, i); \
46 lua_pushnumber(L, (lua_Number)w); \
47 return 1; \
48 }
49
50 MONADIC(bnot, ~)
51 VARIADIC(band, &=)
52 VARIADIC(bor, |=)
53 VARIADIC(bxor, ^=)
54 TDYADIC(lshift, <<, luaL_checkbit, luaL_checkubit)
55 TDYADIC(rshift, >>, luaL_checkubit, luaL_checkubit)
56 TDYADIC(arshift, >>, luaL_checkbit, luaL_checkubit)
57 DYADIC(mod, %)
58
59 static const struct luaL_reg bitlib[] = {
60 {"bnot", bit_bnot},
61 {"band", bit_band},
62 {"bor", bit_bor},
63 {"bxor", bit_bxor},
64 {"lshift", bit_lshift},
65 {"rshift", bit_rshift},
66 {"arshift", bit_arshift},
67 {"mod", bit_mod},
68 {NULL, NULL}
69 };
70
71 LUALIB_API int luaopen_bit(lua_State *L) {
84dbd58 replace deprecated use of luaL_openlib with luaL_register
sven authored
72 luaL_register(L, BITLIBNAME, bitlib);
7b25129 Updated NSE's bit library to no longer be a shared library.
batrick authored
73 return 1;
74 }
75
Something went wrong with that request. Please try again.