2014-01-19 17:42:21 +00:00
|
|
|
|
using System;
|
2014-01-25 19:49:29 +00:00
|
|
|
|
|
2014-01-19 17:42:21 +00:00
|
|
|
|
namespace BizHawk.Client.Common
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2013-10-31 00:31:25 +00:00
|
|
|
|
public class BitLuaLibrary : LuaLibraryBase
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2013-10-31 00:31:25 +00:00
|
|
|
|
public override string Name { get { return "bit"; } }
|
2013-10-29 13:52:59 +00:00
|
|
|
|
|
2014-01-25 19:49:29 +00:00
|
|
|
|
[LuaMethodAttributes(
|
|
|
|
|
"band",
|
|
|
|
|
"Bitwise AND of 'val' against 'amt'"
|
|
|
|
|
)]
|
2014-01-25 15:05:53 +00:00
|
|
|
|
public static uint Band(object val, object amt)
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2013-10-31 00:31:25 +00:00
|
|
|
|
return (uint)(LuaInt(val) & LuaInt(amt));
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-25 19:49:29 +00:00
|
|
|
|
[LuaMethodAttributes(
|
|
|
|
|
"bnot",
|
|
|
|
|
"Bitwise NOT of 'val' against 'amt'"
|
|
|
|
|
)]
|
2014-01-25 15:05:53 +00:00
|
|
|
|
public static uint Bnot(object val)
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2013-10-31 00:31:25 +00:00
|
|
|
|
return (uint)(~LuaInt(val));
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-25 19:49:29 +00:00
|
|
|
|
[LuaMethodAttributes(
|
|
|
|
|
"bor",
|
|
|
|
|
"Bitwise OR of 'val' against 'amt'"
|
|
|
|
|
)]
|
2014-01-25 15:05:53 +00:00
|
|
|
|
public static uint Bor(object val, object amt)
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2013-10-31 00:31:25 +00:00
|
|
|
|
return (uint)(LuaInt(val) | LuaInt(amt));
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-25 19:49:29 +00:00
|
|
|
|
[LuaMethodAttributes(
|
|
|
|
|
"bxor",
|
|
|
|
|
"Bitwise XOR of 'val' against 'amt'"
|
|
|
|
|
)]
|
2014-01-25 15:05:53 +00:00
|
|
|
|
public static uint Bxor(object val, object amt)
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2013-10-31 00:31:25 +00:00
|
|
|
|
return (uint)(LuaInt(val) ^ LuaInt(amt));
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-25 19:49:29 +00:00
|
|
|
|
[LuaMethodAttributes(
|
|
|
|
|
"lshift",
|
|
|
|
|
"Logical shift left of 'val' by 'amt' bits"
|
|
|
|
|
)]
|
2014-01-25 15:05:53 +00:00
|
|
|
|
public static uint Lshift(object val, object amt)
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2013-10-31 00:31:25 +00:00
|
|
|
|
return (uint)(LuaInt(val) << LuaInt(amt));
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-25 19:49:29 +00:00
|
|
|
|
[LuaMethodAttributes(
|
|
|
|
|
"rol",
|
|
|
|
|
"Left rotate 'val' by 'amt' bits"
|
|
|
|
|
)]
|
2014-01-25 15:05:53 +00:00
|
|
|
|
public static uint Rol(object val, object amt)
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2014-01-20 15:26:44 +00:00
|
|
|
|
return (uint)((LuaInt(val) << LuaInt(amt))
|
2013-10-31 00:31:25 +00:00
|
|
|
|
| (LuaInt(val) >> (32 - LuaInt(amt))));
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-25 19:49:29 +00:00
|
|
|
|
[LuaMethodAttributes(
|
|
|
|
|
"ror",
|
|
|
|
|
"Right rotate 'val' by 'amt' bits"
|
|
|
|
|
)]
|
2014-01-25 15:05:53 +00:00
|
|
|
|
public static uint Ror(object val, object amt)
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2013-10-31 00:31:25 +00:00
|
|
|
|
return (uint)((LuaInt(val) >> LuaInt(amt))
|
|
|
|
|
| (LuaInt(val) << (32 - LuaInt(amt))));
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-25 19:49:29 +00:00
|
|
|
|
[LuaMethodAttributes(
|
|
|
|
|
"rshift",
|
|
|
|
|
"Logical shift right of 'val' by 'amt' bits"
|
|
|
|
|
)]
|
2014-01-25 15:05:53 +00:00
|
|
|
|
public static uint Rshift(object val, object amt)
|
2013-10-28 19:13:01 +00:00
|
|
|
|
{
|
2013-10-31 00:31:25 +00:00
|
|
|
|
return (uint)(LuaInt(val) >> LuaInt(amt));
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
2014-01-19 17:42:21 +00:00
|
|
|
|
|
2014-01-25 19:49:29 +00:00
|
|
|
|
[LuaMethodAttributes(
|
|
|
|
|
"check",
|
|
|
|
|
"Returns result of bit 'pos' being set in 'num'"
|
|
|
|
|
)]
|
2014-01-25 15:05:53 +00:00
|
|
|
|
public static bool Check(object num, object pos)
|
2014-01-19 17:42:21 +00:00
|
|
|
|
{
|
2014-01-25 15:05:53 +00:00
|
|
|
|
return (LuaLong(num) & (1 << LuaInt(pos))) != 0;
|
2014-01-21 17:28:54 +00:00
|
|
|
|
}
|
2014-01-25 19:49:29 +00:00
|
|
|
|
|
|
|
|
|
[LuaMethodAttributes(
|
|
|
|
|
"set",
|
|
|
|
|
"TODO"
|
|
|
|
|
)]
|
2014-01-25 15:05:53 +00:00
|
|
|
|
public static uint Set(object num, object pos)
|
2014-01-21 17:28:54 +00:00
|
|
|
|
{
|
2014-01-25 15:05:53 +00:00
|
|
|
|
return (uint)(LuaInt(num) | 1 << LuaInt(pos));
|
2014-01-21 17:28:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-25 19:49:29 +00:00
|
|
|
|
[LuaMethodAttributes(
|
|
|
|
|
"clear",
|
|
|
|
|
"TODO"
|
|
|
|
|
)]
|
2014-01-25 15:05:53 +00:00
|
|
|
|
public static uint Clear(object num, object pos)
|
2014-01-21 17:28:54 +00:00
|
|
|
|
{
|
2014-01-25 15:05:53 +00:00
|
|
|
|
return (uint)(LuaInt(num) & ~(1 << LuaInt(pos)));
|
2014-01-19 17:42:21 +00:00
|
|
|
|
}
|
2014-01-20 17:38:31 +00:00
|
|
|
|
|
2014-01-25 19:49:29 +00:00
|
|
|
|
[LuaMethodAttributes(
|
|
|
|
|
"byteswap_16",
|
|
|
|
|
"Byte swaps 'short', i.e. bit.byteswap_16(0xFF00) would return 0x00FF"
|
|
|
|
|
)]
|
2014-01-25 15:05:53 +00:00
|
|
|
|
public static uint Byteswap_16(object _short)
|
2014-01-20 17:38:31 +00:00
|
|
|
|
{
|
2014-01-25 15:05:53 +00:00
|
|
|
|
return (UInt16)((LuaInt(_short) & 0xFFU) << 8 | (LuaInt(_short) & 0xFF00U) >> 8);
|
2014-01-20 17:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-25 19:49:29 +00:00
|
|
|
|
[LuaMethodAttributes(
|
|
|
|
|
"byteswap_32",
|
|
|
|
|
"Byte swaps 'dword'"
|
|
|
|
|
)]
|
2014-01-25 15:05:53 +00:00
|
|
|
|
public static uint Byteswap_32(object _dword)
|
2014-01-20 17:38:31 +00:00
|
|
|
|
{
|
2014-01-25 15:05:53 +00:00
|
|
|
|
return (LuaUInt(_dword) & 0x000000FFU) << 24 | (LuaUInt(_dword) & 0x0000FF00U) << 8 |
|
|
|
|
|
(LuaUInt(_dword) & 0x00FF0000U) >> 8 | (LuaUInt(_dword) & 0xFF000000U) >> 24;
|
2014-01-20 17:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-01-25 19:49:29 +00:00
|
|
|
|
[LuaMethodAttributes(
|
|
|
|
|
"byteswap_64",
|
|
|
|
|
"Byte swaps 'long'"
|
|
|
|
|
)]
|
2014-01-25 15:05:53 +00:00
|
|
|
|
public static UInt64 Byteswap_64(object _long)
|
2014-01-20 17:38:31 +00:00
|
|
|
|
{
|
2014-01-25 15:05:53 +00:00
|
|
|
|
UInt64 value = (UInt64)LuaLong(_long);
|
2014-01-20 17:38:31 +00:00
|
|
|
|
return (value & 0x00000000000000FFUL) << 56 | (value & 0x000000000000FF00UL) << 40 |
|
|
|
|
|
(value & 0x0000000000FF0000UL) << 24 | (value & 0x00000000FF000000UL) << 8 |
|
|
|
|
|
(value & 0x000000FF00000000UL) >> 8 | (value & 0x0000FF0000000000UL) >> 24 |
|
|
|
|
|
(value & 0x00FF000000000000UL) >> 40 | (value & 0xFF00000000000000UL) >> 56;
|
|
|
|
|
}
|
2013-10-28 19:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|