Skip to content

Commit ae567a1

Browse files
committed
increase performance of bit, createMatFromArray, tolist
1 parent 91f225d commit ae567a1

File tree

5 files changed

+277
-294
lines changed

5 files changed

+277
-294
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ execute_process(
201201
)
202202

203203
file(GLOB_RECURSE project_source_headers "src/*.h*")
204-
file(GLOB_RECURSE project_sources_files "src/*.cpp")
204+
file(GLOB_RECURSE project_sources_files "src/*.c" "src/*.cpp")
205205

206206
file(GLOB_RECURSE project_generated_headers "generated/*.h*")
207-
file(GLOB_RECURSE project_generated_files "generated/*.cpp")
207+
file(GLOB_RECURSE project_generated_files "generated/*.c" "generated/*.cpp")
208208

209209
source_group("Source Headers" FILES ${project_source_headers})
210210
source_group("Source Files" FILES ${project_sources_files})

src/bit.cpp

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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:
14+
**
15+
** The above copyright notice and this permission notice shall be
16+
** included in all copies or substantial portions of the Software.
17+
**
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.
25+
**
26+
** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
27+
*/
28+
29+
#include <bit.hpp>
30+
31+
#define LUA_BITOP_VERSION "1.0.2"
32+
33+
#define LUA_LIB
34+
#include "lua.h"
35+
#include "lauxlib.h"
36+
37+
#ifdef _MSC_VER
38+
/* MSVC is stuck in the last century and doesn't have C99's stdint.h. */
39+
typedef __int32 int32_t;
40+
typedef unsigned __int32 uint32_t;
41+
typedef unsigned __int64 uint64_t;
42+
#else
43+
#include <stdint.h>
44+
#endif
45+
46+
typedef int32_t SBits;
47+
typedef uint32_t UBits;
48+
49+
typedef union {
50+
lua_Number n;
51+
#ifdef LUA_NUMBER_DOUBLE
52+
uint64_t b;
53+
#else
54+
UBits b;
55+
#endif
56+
} BitNum;
57+
58+
/* Convert argument to bit type. */
59+
static UBits barg(lua_State *L, int idx)
60+
{
61+
BitNum bn;
62+
UBits b;
63+
#if LUA_VERSION_NUM < 502
64+
bn.n = lua_tonumber(L, idx);
65+
#else
66+
bn.n = luaL_checknumber(L, idx);
67+
#endif
68+
#if defined(LUA_NUMBER_DOUBLE)
69+
bn.n += 6755399441055744.0; /* 2^52+2^51 */
70+
#ifdef SWAPPED_DOUBLE
71+
b = (UBits)(bn.b >> 32);
72+
#else
73+
b = (UBits)bn.b;
74+
#endif
75+
#elif defined(LUA_NUMBER_INT) || defined(LUA_NUMBER_LONG) || \
76+
defined(LUA_NUMBER_LONGLONG) || defined(LUA_NUMBER_LONG_LONG) || \
77+
defined(LUA_NUMBER_LLONG)
78+
if (sizeof(UBits) == sizeof(lua_Number))
79+
b = bn.b;
80+
else
81+
b = (UBits)(SBits)bn.n;
82+
#elif defined(LUA_NUMBER_FLOAT)
83+
#error "A 'float' lua_Number type is incompatible with this library"
84+
#else
85+
#error "Unknown number type, check LUA_NUMBER_* in luaconf.h"
86+
#endif
87+
#if LUA_VERSION_NUM < 502
88+
if (b == 0 && !lua_isnumber(L, idx)) {
89+
luaL_typerror(L, idx, "number");
90+
}
91+
#endif
92+
return b;
93+
}
94+
95+
/* Return bit type. */
96+
#define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1;
97+
98+
static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) }
99+
static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) }
100+
101+
#define BIT_OP(func, opr) \
102+
static int func(lua_State *L) { int i; UBits b = barg(L, 1); \
103+
for (i = lua_gettop(L); i > 1; i--) b opr barg(L, i); BRET(b) }
104+
BIT_OP(bit_band, &=)
105+
BIT_OP(bit_bor, |=)
106+
BIT_OP(bit_bxor, ^=)
107+
108+
#define bshl(b, n) (b << n)
109+
#define bshr(b, n) (b >> n)
110+
#define bsar(b, n) ((SBits)b >> n)
111+
#define brol(b, n) ((b << n) | (b >> (32-n)))
112+
#define bror(b, n) ((b << (32-n)) | (b >> n))
113+
#define BIT_SH(func, fn) \
114+
static int func(lua_State *L) { \
115+
UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) }
116+
BIT_SH(bit_lshift, bshl)
117+
BIT_SH(bit_rshift, bshr)
118+
BIT_SH(bit_arshift, bsar)
119+
BIT_SH(bit_rol, brol)
120+
BIT_SH(bit_ror, bror)
121+
122+
static int bit_bswap(lua_State *L)
123+
{
124+
UBits b = barg(L, 1);
125+
b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24);
126+
BRET(b)
127+
}
128+
129+
static int bit_tohex(lua_State *L)
130+
{
131+
UBits b = barg(L, 1);
132+
SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2);
133+
const char *hexdigits = "0123456789abcdef";
134+
char buf[8];
135+
int i;
136+
if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; }
137+
if (n > 8) n = 8;
138+
for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; }
139+
lua_pushlstring(L, buf, (size_t)n);
140+
return 1;
141+
}
142+
143+
static const struct luaL_Reg bit_funcs[] = {
144+
{ "tobit", bit_tobit },
145+
{ "bnot", bit_bnot },
146+
{ "band", bit_band },
147+
{ "bor", bit_bor },
148+
{ "bxor", bit_bxor },
149+
{ "lshift", bit_lshift },
150+
{ "rshift", bit_rshift },
151+
{ "arshift", bit_arshift },
152+
{ "rol", bit_rol },
153+
{ "ror", bit_ror },
154+
{ "bswap", bit_bswap },
155+
{ "tohex", bit_tohex },
156+
{ NULL, NULL }
157+
};
158+
159+
/* Signed right-shifts are implementation-defined per C89/C99.
160+
** But the de facto standard are arithmetic right-shifts on two's
161+
** complement CPUs. This behaviour is required here, so test for it.
162+
*/
163+
#define BAD_SAR (bsar(-8, 2) != (SBits)-2)
164+
165+
int LUA_MODULE_NAME::luaopen_bit(lua_State *L)
166+
{
167+
UBits b;
168+
lua_pushnumber(L, (lua_Number)1437217655L);
169+
b = barg(L, -1);
170+
lua_pop(L, 1);
171+
if (b != (UBits)1437217655L || BAD_SAR) { /* Perform a simple self-test. */
172+
const char *msg = "compiled with incompatible luaconf.h";
173+
#ifdef LUA_NUMBER_DOUBLE
174+
#ifdef _WIN32
175+
if (b == (UBits)1610612736L)
176+
msg = "use D3DCREATE_FPU_PRESERVE with DirectX";
177+
#endif
178+
if (b == (UBits)1127743488L)
179+
msg = "not compiled with SWAPPED_DOUBLE";
180+
#endif
181+
if (BAD_SAR)
182+
msg = "arithmetic right-shift broken";
183+
luaL_error(L, "bit library self-test failed (%s)", msg);
184+
}
185+
#if LUA_VERSION_NUM < 502
186+
luaL_register(L, NULL, bit_funcs);
187+
#else
188+
luaL_setfuncs(L, bit_funcs, 0);
189+
#endif
190+
return 1;
191+
}

0 commit comments

Comments
 (0)