Fix enum setting used as requirement
[minetest.git] / lib / bitop / bit.cpp
blob9957ab5c29b113c0c3170964246d6be29615b11f
1 /*
2 ** Lua BitOp -- a bit operations library for Lua 5.1/5.2.
3 ** http://bitop.luajit.org/
4 **
5 ** Copyright (C) 2008-2012 Mike Pall. All rights reserved.
6 **
7 ** Permission is hereby granted, free of charge, to any person obtaining
8 ** a copy of this software and associated documentation files (the
9 ** "Software"), to deal in the Software without restriction, including
10 ** without limitation the rights to use, copy, modify, merge, publish,
11 ** distribute, sublicense, and/or sell copies of the Software, and to
12 ** permit persons to whom the Software is furnished to do so, subject to
13 ** the following conditions:
15 ** The above copyright notice and this permission notice shall be
16 ** included in all copies or substantial portions of the Software.
18 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 ** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
29 extern "C" {
30 #include "bit.h"
33 #define LUA_BITOP_VERSION "1.0.2"
35 #define LUA_LIB
36 extern "C" {
37 #include "lauxlib.h"
40 #if defined(_MSC_VER) && (_MSC_VER < 1700)
41 /* Old MSVC is stuck in the last century and doesn't have C99's stdint.h. */
42 typedef __int32 int32_t;
43 typedef unsigned __int32 uint32_t;
44 typedef unsigned __int64 uint64_t;
45 #else
46 #include <stdint.h>
47 #endif
49 typedef int32_t SBits;
50 typedef uint32_t UBits;
52 typedef union {
53 lua_Number n;
54 #ifdef LUA_NUMBER_DOUBLE
55 uint64_t b;
56 #else
57 UBits b;
58 #endif
59 } BitNum;
61 /* Convert argument to bit type. */
62 static UBits barg(lua_State *L, int idx)
64 BitNum bn;
65 UBits b;
66 #if LUA_VERSION_NUM < 502
67 bn.n = lua_tonumber(L, idx);
68 #else
69 bn.n = luaL_checknumber(L, idx);
70 #endif
71 #if defined(LUA_NUMBER_DOUBLE)
72 bn.n += 6755399441055744.0; /* 2^52+2^51 */
73 #ifdef SWAPPED_DOUBLE
74 b = (UBits)(bn.b >> 32);
75 #else
76 b = (UBits)bn.b;
77 #endif
78 #elif defined(LUA_NUMBER_INT) || defined(LUA_NUMBER_LONG) || \
79 defined(LUA_NUMBER_LONGLONG) || defined(LUA_NUMBER_LONG_LONG) || \
80 defined(LUA_NUMBER_LLONG)
81 if (sizeof(UBits) == sizeof(lua_Number))
82 b = bn.b;
83 else
84 b = (UBits)(SBits)bn.n;
85 #elif defined(LUA_NUMBER_FLOAT)
86 #error "A 'float' lua_Number type is incompatible with this library"
87 #else
88 #error "Unknown number type, check LUA_NUMBER_* in luaconf.h"
89 #endif
90 #if LUA_VERSION_NUM < 502
91 if (b == 0 && !lua_isnumber(L, idx)) {
92 luaL_typerror(L, idx, "number");
94 #endif
95 return b;
98 /* Return bit type. */
99 #define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1;
101 static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) }
102 static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) }
104 #define BIT_OP(func, opr) \
105 static int func(lua_State *L) { int i; UBits b = barg(L, 1); \
106 for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b) }
107 BIT_OP(bit_band, &=)
108 BIT_OP(bit_bor, |=)
109 BIT_OP(bit_bxor, ^=)
111 #define bshl(b, n) (b << n)
112 #define bshr(b, n) (b >> n)
113 #define bsar(b, n) ((SBits)b >> n)
114 #define brol(b, n) ((b << n) | (b >> (32-n)))
115 #define bror(b, n) ((b << (32-n)) | (b >> n))
116 #define BIT_SH(func, fn) \
117 static int func(lua_State *L) { \
118 UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) }
119 BIT_SH(bit_lshift, bshl)
120 BIT_SH(bit_rshift, bshr)
121 BIT_SH(bit_arshift, bsar)
122 BIT_SH(bit_rol, brol)
123 BIT_SH(bit_ror, bror)
125 static int bit_bswap(lua_State *L)
127 UBits b = barg(L, 1);
128 b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24);
129 BRET(b)
132 static int bit_tohex(lua_State *L)
134 UBits b = barg(L, 1);
135 SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2);
136 const char *hexdigits = "0123456789abcdef";
137 char buf[8];
138 int i;
139 if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; }
140 if (n > 8) n = 8;
141 for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; }
142 lua_pushlstring(L, buf, (size_t)n);
143 return 1;
146 static const struct luaL_Reg bit_funcs[] = {
147 { "tobit", bit_tobit },
148 { "bnot", bit_bnot },
149 { "band", bit_band },
150 { "bor", bit_bor },
151 { "bxor", bit_bxor },
152 { "lshift", bit_lshift },
153 { "rshift", bit_rshift },
154 { "arshift", bit_arshift },
155 { "rol", bit_rol },
156 { "ror", bit_ror },
157 { "bswap", bit_bswap },
158 { "tohex", bit_tohex },
159 { NULL, NULL }
162 /* Signed right-shifts are implementation-defined per C89/C99.
163 ** But the de facto standard are arithmetic right-shifts on two's
164 ** complement CPUs. This behaviour is required here, so test for it.
166 #define BAD_SAR (bsar(-8, 2) != (SBits)-2)
168 LUALIB_API int luaopen_bit(lua_State *L)
170 UBits b;
171 lua_pushnumber(L, (lua_Number)1437217655L);
172 b = barg(L, -1);
173 if (b != (UBits)1437217655L || BAD_SAR) { /* Perform a simple self-test. */
174 const char *msg = "compiled with incompatible luaconf.h";
175 #ifdef LUA_NUMBER_DOUBLE
176 #ifdef _WIN32
177 if (b == (UBits)1610612736L)
178 msg = "use D3DCREATE_FPU_PRESERVE with DirectX";
179 #endif
180 if (b == (UBits)1127743488L)
181 msg = "not compiled with SWAPPED_DOUBLE";
182 #endif
183 if (BAD_SAR)
184 msg = "arithmetic right-shift broken";
185 luaL_error(L, "bit library self-test failed (%s)", msg);
187 #if LUA_VERSION_NUM < 502
188 luaL_register(L, "bit", bit_funcs);
189 #else
190 luaL_newlib(L, bit_funcs);
191 #endif
192 return 1;