Пример #1
0
        static int b_extract(LuaState L)
        {
            int    w = 0;
            b_uint r = luaL_checkunsigned(L, 1);
            int    f = fieldargs(L, 2, ref w);

            r = (r >> f) & mask(w);
            lua_pushunsigned(L, r);
            return(1);
        }
Пример #2
0
        static int b_rot(LuaState L, int i)
        {
            b_uint r = luaL_checkunsigned(L, 1);

            i &= (LUA_NBITS - 1);  /* i = i % NBITS */
            r  = trim(r);
            r  = (r << i) | (r >> (LUA_NBITS - i));
            lua_pushunsigned(L, trim(r));
            return(1);
        }
Пример #3
0
        static b_uint andaux(LuaState L)
        {
            int    i, n = lua_gettop(L);
            b_uint r = ~(b_uint)0;

            for (i = 1; i <= n; i++)
            {
                r &= luaL_checkunsigned(L, i);
            }
            return(trim(r));
        }
Пример #4
0
        static int b_xor(LuaState L)
        {
            int    i, n = lua_gettop(L);
            b_uint r = 0;

            for (i = 1; i <= n; i++)
            {
                r ^= luaL_checkunsigned(L, i);
            }
            lua_pushunsigned(L, trim(r));
            return(1);
        }
Пример #5
0
        static int b_or(LuaState L)
        {
            int    i, n = lua_gettop(L);
            b_uint r = 0;

            for (i = 1; i <= n; i++)
            {
                r |= luaL_checkinteger(L, i);
            }
            lua_pushnumber(L, trim(r));
            return(1);
        }
Пример #6
0
        static int b_replace(LuaState L)
        {
            int          w = 0;
            b_uint       r = luaL_checkunsigned(L, 1);
            b_uint       v = luaL_checkunsigned(L, 2);
            int          f = fieldargs(L, 3, ref w);
            lua_Unsigned m = mask(w);

            v &= m;  /* erase bits outside given width */
            r  = (r & ~(m << f)) | (v << f);
            lua_pushunsigned(L, r);
            return(1);
        }
Пример #7
0
        static int b_arshift(LuaState L)
        {
            b_uint r = luaL_checkunsigned(L, 1);
            int    i = luaL_checkint(L, 2);

            if (i < 0 || (r & ((b_uint)1 << (LUA_NBITS - 1))) == 0)
            {
                return(b_shift(L, r, -i));
            }
            else
            {  /* arithmetic shift for 'negative' number */
                if (i >= LUA_NBITS)
                {
                    r = ALLONES;
                }
                else
                {
                    r = trim((r >> i) | ~(~(b_uint)0 >> i));  /* add signal bit */
                }
                lua_pushunsigned(L, r);
                return(1);
            }
        }
Пример #8
0
 static int b_shift(LuaState L, b_uint r, int i)
 {
     if (i < 0)
     {  /* shift right? */
         i = -i;
         r = trim(r);
         if (i >= LUA_NBITS) r = 0;
         else r >>= i;
     }
     else
     {  /* shift left */
         if (i >= LUA_NBITS) r = 0;
         else r <<= i;
         r = trim(r);
     }
     lua_pushnumber(L, r);
     return 1;
 }