blob: ea9110a9c277291bee62bd5bf2ea2c26066949dc [file] [log] [blame]
Roger Meier6cf0ffc2014-04-05 00:45:42 +02001//
2// Licensed to the Apache Software Foundation (ASF) under one
3// or more contributor license agreements. See the NOTICE file
4// distributed with this work for additional information
5// regarding copyright ownership. The ASF licenses this file
6// to you under the Apache License, Version 2.0 (the
7// "License"); you may not use this file except in compliance
8// with the License. You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing,
13// software distributed under the License is distributed on an
14// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15// KIND, either express or implied. See the License for the
16// specific language governing permissions and limitations
17// under the License.
18//
19
20#include <lua.h>
21#include <lauxlib.h>
22
23static int l_not(lua_State *L) {
24 int a = luaL_checkinteger(L, 1);
25 a = ~a;
26 lua_pushnumber(L, a);
27 return 1;
28}
29
Thomaseb684d32024-07-28 15:32:23 +020030static int l_unot(lua_State *L) {
31 unsigned int a = luaL_checkinteger(L, 1);
32 a = ~a;
33 lua_pushnumber(L, a);
34 return 1;
35}
36
Roger Meier6cf0ffc2014-04-05 00:45:42 +020037static int l_xor(lua_State *L) {
38 int a = luaL_checkinteger(L, 1);
39 int b = luaL_checkinteger(L, 2);
40 a ^= b;
41 lua_pushnumber(L, a);
42 return 1;
43}
44
Thomaseb684d32024-07-28 15:32:23 +020045static int l_uxor(lua_State *L) {
46 unsigned int a = luaL_checkinteger(L, 1);
47 unsigned int b = luaL_checkinteger(L, 2);
48 a ^= b;
49 lua_pushnumber(L, a);
50 return 1;
51}
52
53
Roger Meier6cf0ffc2014-04-05 00:45:42 +020054static int l_and(lua_State *L) {
55 int a = luaL_checkinteger(L, 1);
56 int b = luaL_checkinteger(L, 2);
57 a &= b;
58 lua_pushnumber(L, a);
59 return 1;
60}
61
Thomaseb684d32024-07-28 15:32:23 +020062static int l_uand(lua_State *L) {
63 unsigned int a = luaL_checkinteger(L, 1);
64 unsigned int b = luaL_checkinteger(L, 2);
65 a &= b;
66 lua_pushnumber(L, a);
67 return 1;
68}
69
Roger Meier6cf0ffc2014-04-05 00:45:42 +020070static int l_or(lua_State *L) {
71 int a = luaL_checkinteger(L, 1);
72 int b = luaL_checkinteger(L, 2);
73 a |= b;
74 lua_pushnumber(L, a);
75 return 1;
76}
77
Thomaseb684d32024-07-28 15:32:23 +020078static int l_uor(lua_State *L) {
79 unsigned int a = luaL_checkinteger(L, 1);
80 unsigned int b = luaL_checkinteger(L, 2);
81 a |= b;
82 lua_pushnumber(L, a);
83 return 1;
84}
85
Roger Meier6cf0ffc2014-04-05 00:45:42 +020086static int l_shiftr(lua_State *L) {
87 int a = luaL_checkinteger(L, 1);
88 int b = luaL_checkinteger(L, 2);
89 a = a >> b;
90 lua_pushnumber(L, a);
91 return 1;
92}
93
Thomaseb684d32024-07-28 15:32:23 +020094static int l_ushiftr(lua_State *L) {
95 unsigned int a = luaL_checkinteger(L, 1);
96 unsigned int b = luaL_checkinteger(L, 2);
97 a = a >> b;
98 lua_pushnumber(L, a);
99 return 1;
100}
101
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200102static int l_shiftl(lua_State *L) {
103 int a = luaL_checkinteger(L, 1);
104 int b = luaL_checkinteger(L, 2);
105 a = a << b;
106 lua_pushnumber(L, a);
107 return 1;
108}
109
Thomaseb684d32024-07-28 15:32:23 +0200110static int l_ushiftl(lua_State *L) {
111 unsigned int a = luaL_checkinteger(L, 1);
112 unsigned int b = luaL_checkinteger(L, 2);
113 a = a << b;
114 lua_pushnumber(L, a);
115 return 1;
116}
117
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200118static const struct luaL_Reg funcs[] = {
119 {"band", l_and},
Thomaseb684d32024-07-28 15:32:23 +0200120 {"buand", l_uand},
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200121 {"bor", l_or},
Thomaseb684d32024-07-28 15:32:23 +0200122 {"buor", l_uor},
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200123 {"bxor", l_xor},
Thomaseb684d32024-07-28 15:32:23 +0200124 {"buxor", l_uxor},
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200125 {"bnot", l_not},
Thomaseb684d32024-07-28 15:32:23 +0200126 {"bunot", l_unot},
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200127 {"shiftl", l_shiftl},
Thomaseb684d32024-07-28 15:32:23 +0200128 {"ushiftl", l_ushiftl},
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200129 {"shiftr", l_shiftr},
Thomaseb684d32024-07-28 15:32:23 +0200130 {"ushiftr", l_ushiftr},
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200131 {NULL, NULL}
132};
133
134int luaopen_libluabitwise(lua_State *L) {
Thomaseb684d32024-07-28 15:32:23 +0200135#if LUA_VERSION_NUM >= 502
136 lua_newtable(L);
137 luaL_setfuncs(L, funcs, 0);
138#else
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200139 luaL_register(L, "libluabitwise", funcs);
Thomaseb684d32024-07-28 15:32:23 +0200140#endif
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200141 return 1;
142}