2 * nixio - Linux I/O library for lua
4 * Copyright (C) 2009 Steven Barth <steven@midlink.org>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 /* 52 bit maximum precision */
24 #define NIXIO_BIT_BMAX 52
25 #define NIXIO_BIT_NMAX 0xfffffffffffff
27 #define NIXIO_BIT_XOP(BIT_XOP) \
28 uint64_t oper = luaL_checknumber(L, 1); \
29 const int args = lua_gettop(L); \
31 for (int i = 2; i <= args; i++) { \
32 uint64_t oper2 = luaL_checknumber(L, i); \
36 lua_pushnumber(L, oper); \
40 static int nixio_bit_or(lua_State *L) {
44 static int nixio_bit_and(lua_State *L) {
48 static int nixio_bit_xor(lua_State *L) {
52 static int nixio_bit_unset(lua_State *L) {
56 static int nixio_bit_not(lua_State *L) {
57 lua_pushnumber(L, (~((uint64_t)luaL_checknumber(L, 1))) & NIXIO_BIT_NMAX);
61 static int nixio_bit_shl(lua_State *L) {
62 uint64_t oper = luaL_checknumber(L, 1);
63 oper <<= luaL_checkinteger(L, 2);
64 if (oper > NIXIO_BIT_NMAX) {
65 return luaL_error(L, "arithmetic overflow");
67 lua_pushnumber(L, oper);
72 static int nixio_bit_ashr(lua_State *L) {
73 int64_t oper = luaL_checknumber(L, 1);
74 lua_pushnumber(L, oper >> luaL_checkinteger(L, 2));
78 static int nixio_bit_shr(lua_State *L) {
79 uint64_t oper = luaL_checknumber(L, 1);
80 lua_pushnumber(L, oper >> luaL_checkinteger(L, 2));
84 static int nixio_bit_div(lua_State *L) {
88 static int nixio_bit_check(lua_State *L) {
89 uint64_t oper = luaL_checknumber(L, 1);
90 uint64_t oper2 = luaL_checknumber(L, 2);
91 lua_pushboolean(L, (oper & oper2) == oper2);
95 static int nixio_bit_cast(lua_State *L) {
96 lua_pushnumber(L, ((uint64_t)luaL_checknumber(L, 1)) & NIXIO_BIT_NMAX);
100 static int nixio_bit_swap(lua_State *L) {
101 uint64_t op = luaL_checknumber(L, 1);
102 op = (op >> 24) | ((op >> 8) & 0xff00) | ((op & 0xff00) << 8) | (op << 24);
103 lua_pushnumber(L, op);
108 static const luaL_reg R[] = {
109 {"bor", nixio_bit_or},
110 {"set", nixio_bit_or},
111 {"band", nixio_bit_and},
112 {"bxor", nixio_bit_xor},
113 {"unset", nixio_bit_unset},
114 {"bnot", nixio_bit_not},
115 {"rshift", nixio_bit_shr},
116 {"arshift", nixio_bit_ashr},
117 {"lshift", nixio_bit_shl},
118 {"div", nixio_bit_div},
119 {"check", nixio_bit_check},
120 {"cast", nixio_bit_cast},
121 {"tobit", nixio_bit_cast},
122 {"bswap", nixio_bit_swap},
126 void nixio_open_bit(lua_State *L) {
128 luaL_register(L, NULL, R);
129 lua_pushnumber(L, NIXIO_BIT_BMAX);
130 lua_setfield(L, -2, "bits");
131 lua_pushnumber(L, NIXIO_BIT_NMAX);
132 lua_setfield(L, -2, "max");
133 lua_setfield(L, -2, "bit");