Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 1 | // |
| 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 | |
| 23 | static 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 | |
Thomas | eb684d3 | 2024-07-28 15:32:23 +0200 | [diff] [blame^] | 30 | static 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 Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 37 | static 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 | |
Thomas | eb684d3 | 2024-07-28 15:32:23 +0200 | [diff] [blame^] | 45 | static 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 Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 54 | static 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 | |
Thomas | eb684d3 | 2024-07-28 15:32:23 +0200 | [diff] [blame^] | 62 | static 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 Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 70 | static 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 | |
Thomas | eb684d3 | 2024-07-28 15:32:23 +0200 | [diff] [blame^] | 78 | static 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 Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 86 | static 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 | |
Thomas | eb684d3 | 2024-07-28 15:32:23 +0200 | [diff] [blame^] | 94 | static 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 Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 102 | static 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 | |
Thomas | eb684d3 | 2024-07-28 15:32:23 +0200 | [diff] [blame^] | 110 | static 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 Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 118 | static const struct luaL_Reg funcs[] = { |
| 119 | {"band", l_and}, |
Thomas | eb684d3 | 2024-07-28 15:32:23 +0200 | [diff] [blame^] | 120 | {"buand", l_uand}, |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 121 | {"bor", l_or}, |
Thomas | eb684d3 | 2024-07-28 15:32:23 +0200 | [diff] [blame^] | 122 | {"buor", l_uor}, |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 123 | {"bxor", l_xor}, |
Thomas | eb684d3 | 2024-07-28 15:32:23 +0200 | [diff] [blame^] | 124 | {"buxor", l_uxor}, |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 125 | {"bnot", l_not}, |
Thomas | eb684d3 | 2024-07-28 15:32:23 +0200 | [diff] [blame^] | 126 | {"bunot", l_unot}, |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 127 | {"shiftl", l_shiftl}, |
Thomas | eb684d3 | 2024-07-28 15:32:23 +0200 | [diff] [blame^] | 128 | {"ushiftl", l_ushiftl}, |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 129 | {"shiftr", l_shiftr}, |
Thomas | eb684d3 | 2024-07-28 15:32:23 +0200 | [diff] [blame^] | 130 | {"ushiftr", l_ushiftr}, |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 131 | {NULL, NULL} |
| 132 | }; |
| 133 | |
| 134 | int luaopen_libluabitwise(lua_State *L) { |
Thomas | eb684d3 | 2024-07-28 15:32:23 +0200 | [diff] [blame^] | 135 | #if LUA_VERSION_NUM >= 502 |
| 136 | lua_newtable(L); |
| 137 | luaL_setfuncs(L, funcs, 0); |
| 138 | #else |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 139 | luaL_register(L, "libluabitwise", funcs); |
Thomas | eb684d3 | 2024-07-28 15:32:23 +0200 | [diff] [blame^] | 140 | #endif |
Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame] | 141 | return 1; |
| 142 | } |