Bit32Helper.cs 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. using System.Runtime.CompilerServices;
  2. namespace Lua.Standard.Internal;
  3. static class Bit32Helper
  4. {
  5. static readonly double Bit32 = 4294967296;
  6. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  7. public static uint ToUInt32(double d)
  8. {
  9. return (uint)ToInt32(d);
  10. }
  11. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  12. public static int ToInt32(double d)
  13. {
  14. return (int)(long)Math.IEEERemainder(d, Bit32);
  15. }
  16. public static void ValidateFieldAndWidth(LuaState thread, string functionName, int argumentId, int field, int width)
  17. {
  18. if (field > 31 || field + width > 32)
  19. throw new LuaRuntimeException(thread, "trying to access non-existent bits");
  20. if (field < 0)
  21. throw new LuaRuntimeException(thread, $"bad argument #{argumentId} to '{functionName}' (field cannot be negative)");
  22. if (width <= 0)
  23. throw new LuaRuntimeException(thread, $"bad argument #{argumentId} to '{functionName}' (width must be positive)");
  24. }
  25. }