RRotateFunction.cs 940 B

12345678910111213141516171819202122232425262728293031
  1. namespace Lua.Standard.Bitwise;
  2. public sealed class RRotateFunction : LuaFunction
  3. {
  4. public override string Name => "rrotate";
  5. public static readonly RRotateFunction Instance = new();
  6. protected override ValueTask<int> InvokeAsyncCore(LuaFunctionExecutionContext context, Memory<LuaValue> buffer, CancellationToken cancellationToken)
  7. {
  8. var x = context.GetArgument<double>(0);
  9. var disp = context.GetArgument<double>(1);
  10. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 1, x);
  11. LuaRuntimeException.ThrowBadArgumentIfNumberIsNotInteger(context.State, this, 2, disp);
  12. var v = Bit32Helper.ToUInt32(x);
  13. var a = ((int)disp) % 32;
  14. if (a < 0)
  15. {
  16. v = (v << (-a)) | (v >> (32 + a));
  17. }
  18. else
  19. {
  20. v = (v >> a) | (v << (32 - a));
  21. }
  22. buffer.Span[0] = v;
  23. return new(1);
  24. }
  25. }