2 ** Lua BitOp -- a bit operations library for Lua 5.1/5.2.
3 ** http://bitop.luajit.org/
5 ** Copyright (C) 2008-2012 Mike Pall. All rights reserved.
7 ** SPDX-License-Identifier: MIT
11 #define LUA_BITOP_VERSION "1.0.2"
17 #include "lua_bitop.h"
18 #include "ws_diag_control.h"
22 typedef int32_t SBits
;
23 typedef uint32_t UBits
;
27 #if defined(LUA_NUMBER_DOUBLE) || defined(LUA_FLOAT_DOUBLE)
34 /* Convert argument to bit type. */
35 static UBits
barg(lua_State
*L
, int idx
)
39 bn
.n
= luaL_checknumber(L
, idx
);
40 #if defined(LUA_NUMBER_DOUBLE) || defined(LUA_FLOAT_DOUBLE)
41 bn
.n
+= 6755399441055744.0; /* 2^52+2^51 */
43 b
= (UBits
)(bn
.b
>> 32);
47 #elif defined(LUA_NUMBER_INT) || defined(LUA_INT_INT) || \
48 defined(LUA_NUMBER_LONG) || defined(LUA_INT_LONG) || \
49 defined(LUA_NUMBER_LONGLONG) || defined(LUA_INT_LONGLONG) || \
50 defined(LUA_NUMBER_LONG_LONG) || defined(LUA_NUMBER_LLONG)
51 if (sizeof(UBits
) == sizeof(lua_Number
))
54 b
= (UBits
)(SBits
)bn
.n
;
55 #elif defined(LUA_NUMBER_FLOAT) || defined(LUA_FLOAT_FLOAT)
56 #error "A 'float' lua_Number type is incompatible with this library"
58 #error "Unknown number type, check LUA_NUMBER_*, LUA_FLOAT_*, LUA_INT_* in luaconf.h"
63 /* Return bit type. */
64 #if LUA_VERSION_NUM < 503
65 #define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1;
67 #define BRET(b) lua_pushinteger(L, (lua_Integer)(SBits)(b)); return 1;
70 static int bit_tobit(lua_State
*L
) { BRET(barg(L
, 1)) }
71 static int bit_bnot(lua_State
*L
) { BRET(~barg(L
, 1)) }
73 #define BIT_OP(func, opr) \
74 static int func(lua_State *L) { int i; UBits b = barg(L, 1); \
75 for (i = lua_gettop(L); i > 1; i--) { b opr barg(L, i); } BRET(b) }
80 #define bshl(b, n) (b << n)
81 #define bshr(b, n) (b >> n)
82 #define bsar(b, n) ((SBits)b >> n)
83 #define brol(b, n) ((b << n) | (b >> (32-n)))
84 #define bror(b, n) ((b << (32-n)) | (b >> n))
85 #define BIT_SH(func, fn) \
86 static int func(lua_State *L) { \
87 UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) }
88 BIT_SH(bit_lshift
, bshl
)
89 BIT_SH(bit_rshift
, bshr
)
90 BIT_SH(bit_arshift
, bsar
)
94 static int bit_bswap(lua_State
*L
)
97 b
= (b
>> 24) | ((b
>> 8) & 0xff00) | ((b
& 0xff00) << 8) | (b
<< 24);
101 static int bit_tohex(lua_State
*L
)
103 UBits b
= barg(L
, 1);
104 SBits n
= lua_isnone(L
, 2) ? 8 : (SBits
)barg(L
, 2);
105 const char *hexdigits
= "0123456789abcdef";
108 if (n
< 0) { n
= -n
; hexdigits
= "0123456789ABCDEF"; }
110 for (i
= (int)n
; --i
>= 0; ) { buf
[i
] = hexdigits
[b
& 15]; b
>>= 4; }
111 lua_pushlstring(L
, buf
, (size_t)n
);
115 static const struct luaL_Reg bit_funcs
[] = {
116 { "tobit", bit_tobit
},
117 { "bnot", bit_bnot
},
118 { "band", bit_band
},
120 { "bxor", bit_bxor
},
121 { "lshift", bit_lshift
},
122 { "rshift", bit_rshift
},
123 { "arshift", bit_arshift
},
126 { "bswap", bit_bswap
},
127 { "tohex", bit_tohex
},
131 /* Signed right-shifts are implementation-defined per C89/C99.
132 ** But the de facto standard are arithmetic right-shifts on two's
133 ** complement CPUs. This behaviour is required here, so test for it.
135 #define BAD_SAR (bsar(-8, 2) != (SBits)-2)
137 LUALIB_API
int luaopen_bit(lua_State
*L
)
140 #if LUA_VERSION_NUM < 503
141 lua_pushnumber(L
, (lua_Number
)1437217655L);
143 lua_pushinteger(L
, (lua_Integer
)1437217655L);
146 if (b
!= (UBits
)1437217655L || BAD_SAR
) { /* Perform a simple self-test. */
147 const char *msg
= "compiled with incompatible luaconf.h";
148 #if defined(LUA_NUMBER_DOUBLE) || defined(LUA_FLOAT_DOUBLE)
150 if (b
== (UBits
)1610612736L)
151 msg
= "use D3DCREATE_FPU_PRESERVE with DirectX";
153 if (b
== (UBits
)1127743488L)
154 msg
= "not compiled with SWAPPED_DOUBLE";
156 DIAG_OFF(unreachable
-code
)
158 msg
= "arithmetic right-shift broken";
159 DIAG_ON(unreachable
-code
)
160 luaL_error(L
, "bit library self-test failed (%s)", msg
);
163 luaL_newlib(L
, bit_funcs
);