47 #define LUAI_INT32 int
49 #define LUA_UNSIGNED unsigned LUAI_INT32
51 #if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI)
55 #if defined(LUA_WIN) && defined(_MSC_VER) && defined(_M_IX86)
63 #define LUA_IEEE754TRICK
71 #if defined(__i386__) || defined(__i386) || defined(__X86__) || \
73 #define LUA_IEEEENDIAN 0
74 #elif defined(__POWERPC__) || defined(__ppc__)
75 #define LUA_IEEEENDIAN 1
93 #if defined(MS_ASMTRICK)
96 #define lua_number2unsigned(i,n) \
97 {__int64 l; __asm {__asm fld n __asm fistp l} i = (unsigned int)l;}
99 #elif defined(LUA_IEEE754TRICK)
103 union luai_Cast2 {
double l_d;
LUAI_INT32 l_p[2]; };
105 #if !defined(LUA_IEEEENDIAN)
106 #define LUAI_EXTRAIEEE \
107 static const union luai_Cast2 ieeeendian = {-(33.0 + 6755399441055744.0)};
108 #define LUA_IEEEENDIAN (ieeeendian.l_p[1] == 33)
110 #define LUAI_EXTRAIEEE
113 #define lua_number2int32(i,n,t) \
115 volatile union luai_Cast2 u; u.l_d = (n) + 6755399441055744.0; \
116 (i) = (t)u.l_p[LUA_IEEEENDIAN]; }
118 #define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned)
122 #if !defined(lua_number2unsigned)
124 #if defined(LUA_NUMBER_DOUBLE)
126 #define SUPUNSIGNED ((lua_Number)(~(lua_Unsigned)0) + 1)
127 #define lua_number2unsigned(i,n) \
128 ((i)=(lua_Unsigned)((n) - floor((n)/SUPUNSIGNED)*SUPUNSIGNED))
130 #define lua_number2unsigned(i,n) ((i)=(lua_Unsigned)(n))
136 #define lua_unsigned2number(u) \
137 (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)(u))
141 static void lua_pushunsigned (lua_State *L,
lua_Unsigned u) {
144 lua_pushnumber(L, n);
149 static lua_Unsigned luaL_checkunsigned (lua_State *L,
int arg) {
151 lua_Number x = lua_tonumber(L, arg);
152 if (x == 0) luaL_checktype(L, arg, LUA_TNUMBER);
159 #define LUAMOD_API LUALIB_API
160 #define LUA_BIT32LIBNAME "bit32"
161 #define luaL_newlib(x, y) luaL_register(x, LUA_BIT32LIBNAME, y)
185 #if !defined(LUA_NBITS)
190 #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
193 #define trim(x) ((x) & ALLONES)
197 #define mask(n) (~((ALLONES << 1) << ((n) - 1)))
204 static b_uint andaux (lua_State *L) {
205 int i, n = lua_gettop(L);
207 for (i = 1; i <= n; i++)
208 r &= luaL_checkunsigned(L, i);
213 static int b_and (lua_State *L) {
215 lua_pushunsigned(L, r);
220 static int b_test (lua_State *L) {
222 lua_pushboolean(L, r != 0);
227 static int b_or (lua_State *L) {
228 int i, n = lua_gettop(L);
230 for (i = 1; i <= n; i++)
231 r |= luaL_checkunsigned(L, i);
232 lua_pushunsigned(L,
trim(r));
237 static int b_xor (lua_State *L) {
238 int i, n = lua_gettop(L);
240 for (i = 1; i <= n; i++)
241 r ^= luaL_checkunsigned(L, i);
242 lua_pushunsigned(L,
trim(r));
247 static int b_not (lua_State *L) {
248 b_uint r = ~luaL_checkunsigned(L, 1);
249 lua_pushunsigned(L,
trim(r));
254 static int b_shift (lua_State *L,
b_uint r,
int i) {
266 lua_pushunsigned(L, r);
271 static int b_lshift (lua_State *L) {
272 return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2));
276 static int b_rshift (lua_State *L) {
277 return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2));
281 static int b_arshift (lua_State *L) {
282 b_uint r = luaL_checkunsigned(L, 1);
283 int i = luaL_checkint(L, 2);
285 return b_shift(L, r, -i);
290 lua_pushunsigned(L, r);
296 static int b_rot (lua_State *L,
int i) {
297 b_uint r = luaL_checkunsigned(L, 1);
301 lua_pushunsigned(L,
trim(r));
306 static int b_lrot (lua_State *L) {
307 return b_rot(L, luaL_checkint(L, 2));
311 static int b_rrot (lua_State *L) {
312 return b_rot(L, -luaL_checkint(L, 2));
320 static int fieldargs (lua_State *L,
int farg,
int *width) {
321 int f = luaL_checkint(L, farg);
322 int w = luaL_optint(L, farg + 1, 1);
323 luaL_argcheck(L, 0 <= f, farg,
"field cannot be negative");
324 luaL_argcheck(L, 0 < w, farg + 1,
"width must be positive");
326 luaL_error(L,
"trying to access non-existent bits");
332 static int b_extract (lua_State *L) {
334 b_uint r = luaL_checkunsigned(L, 1);
335 int f = fieldargs(L, 2, &w);
336 r = (r >> f) &
mask(w);
337 lua_pushunsigned(L, r);
342 static int b_replace (lua_State *L) {
344 b_uint r = luaL_checkunsigned(L, 1);
345 b_uint v = luaL_checkunsigned(L, 2);
346 int f = fieldargs(L, 3, &w);
349 r = (r & ~(m << f)) | (v << f);
350 lua_pushunsigned(L, r);
355 static const luaL_Reg bitlib[] = {
356 {
"arshift", b_arshift},
362 {
"extract", b_extract},
364 {
"lshift", b_lshift},
365 {
"replace", b_replace},
367 {
"rshift", b_rshift},