BitwiseLibrary.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. using Lua.Standard.Internal;
  2. namespace Lua.Standard;
  3. public sealed class BitwiseLibrary
  4. {
  5. public static readonly BitwiseLibrary Instance = new();
  6. public BitwiseLibrary()
  7. {
  8. Functions = [
  9. new("arshift", ArShift),
  10. new("band", BAnd),
  11. new("bnot", BNot),
  12. new("bor", BOr),
  13. new("btest", BTest),
  14. new("bxor", BXor),
  15. new("extract", Extract),
  16. new("lrotate", LRotate),
  17. new("lshift", LShift),
  18. new("replace", Replace),
  19. new("rrotate", RRotate),
  20. new("rshift", RShift),
  21. ];
  22. }
  23. public readonly LuaFunction[] Functions;
  24. public ValueTask<int> ArShift(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
  25. {
  26. var x = context.GetArgument<double>(0);
  27. var disp = context.GetArgument<double>(1);
  28. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "arshift", 1, x);
  29. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "arshift", 2, disp);
  30. var v = Bit32Helper.ToInt32(x);
  31. var a = (int)disp;
  32. if (a < 0)
  33. {
  34. v <<= -a;
  35. }
  36. else
  37. {
  38. v >>= a;
  39. }
  40. buffer.Span[0] = (uint)v;
  41. return new(1);
  42. }
  43. public ValueTask<int> BAnd(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
  44. {
  45. if (context.ArgumentCount == 0)
  46. {
  47. buffer.Span[0] = uint.MaxValue;
  48. return new(1);
  49. }
  50. var arg0 = context.GetArgument<double>(0);
  51. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "band", 1, arg0);
  52. var value = Bit32Helper.ToUInt32(arg0);
  53. for (int i = 1; i < context.ArgumentCount; i++)
  54. {
  55. var arg = context.GetArgument<double>(i);
  56. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "band", 1 + i, arg);
  57. var v = Bit32Helper.ToUInt32(arg);
  58. value &= v;
  59. }
  60. buffer.Span[0] = value;
  61. return new(1);
  62. }
  63. public ValueTask<int> BNot(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
  64. {
  65. var arg0 = context.GetArgument<double>(0);
  66. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "bnot", 1, arg0);
  67. var value = Bit32Helper.ToUInt32(arg0);
  68. buffer.Span[0] = ~value;
  69. return new(1);
  70. }
  71. public ValueTask<int> BOr(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
  72. {
  73. if (context.ArgumentCount == 0)
  74. {
  75. buffer.Span[0] = 0;
  76. return new(1);
  77. }
  78. var arg0 = context.GetArgument<double>(0);
  79. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "bor", 1, arg0);
  80. var value = Bit32Helper.ToUInt32(arg0);
  81. for (int i = 1; i < context.ArgumentCount; i++)
  82. {
  83. var arg = context.GetArgument<double>(i);
  84. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "bor", 1 + i, arg);
  85. var v = Bit32Helper.ToUInt32(arg);
  86. value |= v;
  87. }
  88. buffer.Span[0] = value;
  89. return new(1);
  90. }
  91. public ValueTask<int> BTest(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
  92. {
  93. if (context.ArgumentCount == 0)
  94. {
  95. buffer.Span[0] = true;
  96. return new(1);
  97. }
  98. var arg0 = context.GetArgument<double>(0);
  99. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "btest", 1, arg0);
  100. var value = Bit32Helper.ToUInt32(arg0);
  101. for (int i = 1; i < context.ArgumentCount; i++)
  102. {
  103. var arg = context.GetArgument<double>(i);
  104. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "btest", 1 + i, arg);
  105. var v = Bit32Helper.ToUInt32(arg);
  106. value &= v;
  107. }
  108. buffer.Span[0] = value != 0;
  109. return new(1);
  110. }
  111. public ValueTask<int> BXor(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
  112. {
  113. if (context.ArgumentCount == 0)
  114. {
  115. buffer.Span[0] = 0;
  116. return new(1);
  117. }
  118. var arg0 = context.GetArgument<double>(0);
  119. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "bxor", 1, arg0);
  120. var value = Bit32Helper.ToUInt32(arg0);
  121. for (int i = 1; i < context.ArgumentCount; i++)
  122. {
  123. var arg = context.GetArgument<double>(i);
  124. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "bxor", 1 + i, arg);
  125. var v = Bit32Helper.ToUInt32(arg);
  126. value ^= v;
  127. }
  128. buffer.Span[0] = value;
  129. return new(1);
  130. }
  131. public ValueTask<int> Extract(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
  132. {
  133. var arg0 = context.GetArgument<double>(0);
  134. var arg1 = context.GetArgument<double>(1);
  135. var arg2 = context.HasArgument(2)
  136. ? context.GetArgument<double>(2)
  137. : 1;
  138. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "extract", 1, arg0);
  139. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "extract", 2, arg1);
  140. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "extract", 3, arg2);
  141. var n = Bit32Helper.ToUInt32(arg0);
  142. var field = (int)arg1;
  143. var width = (int)arg2;
  144. Bit32Helper.ValidateFieldAndWidth(context.State, "extract", 2, field, width);
  145. if (field == 0 && width == 32)
  146. {
  147. buffer.Span[0] = n;
  148. }
  149. else
  150. {
  151. var mask = (uint)((1 << width) - 1);
  152. buffer.Span[0] = (n >> field) & mask;
  153. }
  154. return new(1);
  155. }
  156. public ValueTask<int> LRotate(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
  157. {
  158. var x = context.GetArgument<double>(0);
  159. var disp = context.GetArgument<double>(1);
  160. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "lrotate", 1, x);
  161. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "lrotate", 2, disp);
  162. var v = Bit32Helper.ToUInt32(x);
  163. var a = ((int)disp) % 32;
  164. if (a < 0)
  165. {
  166. v = (v >> (-a)) | (v << (32 + a));
  167. }
  168. else
  169. {
  170. v = (v << a) | (v >> (32 - a));
  171. }
  172. buffer.Span[0] = v;
  173. return new(1);
  174. }
  175. public ValueTask<int> LShift(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
  176. {
  177. var x = context.GetArgument<double>(0);
  178. var disp = context.GetArgument<double>(1);
  179. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "lshift", 1, x);
  180. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "lshift", 2, disp);
  181. var v = Bit32Helper.ToUInt32(x);
  182. var a = (int)disp;
  183. if (Math.Abs(a) >= 32)
  184. {
  185. v = 0;
  186. }
  187. else if (a < 0)
  188. {
  189. v >>= -a;
  190. }
  191. else
  192. {
  193. v <<= a;
  194. }
  195. buffer.Span[0] = v;
  196. return new(1);
  197. }
  198. public ValueTask<int> Replace(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
  199. {
  200. var arg0 = context.GetArgument<double>(0);
  201. var arg1 = context.GetArgument<double>(1);
  202. var arg2 = context.GetArgument<double>(2);
  203. var arg3 = context.HasArgument(3)
  204. ? context.GetArgument<double>(3)
  205. : 1;
  206. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "replace", 1, arg0);
  207. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "replace", 2, arg1);
  208. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "replace", 3, arg2);
  209. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "replace", 4, arg3);
  210. var n = Bit32Helper.ToUInt32(arg0);
  211. var v = Bit32Helper.ToUInt32(arg1);
  212. var field = (int)arg2;
  213. var width = (int)arg3;
  214. Bit32Helper.ValidateFieldAndWidth(context.State, "replace", 2, field, width);
  215. uint mask;
  216. if (width == 32)
  217. {
  218. mask = 0xFFFFFFFF;
  219. }
  220. else
  221. {
  222. mask = (uint)((1 << width) - 1);
  223. }
  224. v = v & mask;
  225. n = (n & ~(mask << field)) | (v << field);
  226. buffer.Span[0] = n;
  227. return new(1);
  228. }
  229. public ValueTask<int> RRotate(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
  230. {
  231. var x = context.GetArgument<double>(0);
  232. var disp = context.GetArgument<double>(1);
  233. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "rrotate", 1, x);
  234. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "rrotate", 2, disp);
  235. var v = Bit32Helper.ToUInt32(x);
  236. var a = ((int)disp) % 32;
  237. if (a < 0)
  238. {
  239. v = (v << (-a)) | (v >> (32 + a));
  240. }
  241. else
  242. {
  243. v = (v >> a) | (v << (32 - a));
  244. }
  245. buffer.Span[0] = v;
  246. return new(1);
  247. }
  248. public ValueTask<int> RShift(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
  249. {
  250. var x = context.GetArgument<double>(0);
  251. var disp = context.GetArgument<double>(1);
  252. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "rshift", 1, x);
  253. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, "rshift", 2, disp);
  254. var v = Bit32Helper.ToUInt32(x);
  255. var a = (int)disp;
  256. if (Math.Abs(a) >= 32)
  257. {
  258. v = 0;
  259. }
  260. else if (a < 0)
  261. {
  262. v <<= -a;
  263. }
  264. else
  265. {
  266. v >>= a;
  267. }
  268. buffer.Span[0] = v;
  269. return new(1);
  270. }
  271. }