Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.49 KB | None | 0 0
  1. do
  2.  
  3.     local char = string.char
  4.     local floor = math.floor
  5.  
  6.     -- Bit manipulation
  7.     -----------------------------
  8.  
  9.     local band32 = bit32.band32
  10.     local lshift32 = bit32.lshift
  11.     local rshift32 = bit32.rshift
  12.  
  13.     local function lshift64 (num, shifts)
  14.         local result = num
  15.         while ( shifts > 0 ) do
  16.             result = result + result
  17.             shifts = shifts - 1
  18.         end
  19.         return result
  20.     end
  21.  
  22.     local function rshift64 (num, shifts)
  23.         local result = num
  24.         while ( shifts > 0 ) do
  25.             result = result / 2
  26.             shifts = shifts - 1
  27.         end
  28.         return floor(result)
  29.     end
  30.  
  31.     local function multilshift (num, shifts)
  32.         if ( num > 0x7FFFFFFF ) then
  33.             return lshift64(num, shifts)
  34.         end
  35.  
  36.         return lshift32(num, shifts)
  37.     end
  38.  
  39.     local function multirshift (num, shifts)
  40.         if ( num > 0x7FFFFFFF ) then
  41.             return rshift64(num, shifts)
  42.         end
  43.  
  44.         return rshift32(num, shifts)
  45.     end
  46.  
  47.     --- Internal facilities
  48.     ---------------------------------
  49.  
  50.     --[=[
  51.      * A stack stores numbers, and numbers store octet bytes.
  52.      * Octet bytes are accessed by a location holding their respective
  53.      * number index and initial bit index.
  54.     ]=]
  55.  
  56.     local MAX_BYTES =
  57.         (rshift64(0xFFFFFFFF + 1, 1) == 0x80000000) and 6 or 3
  58.  
  59.     local function nextlocation (locNum, locBit)
  60.         if ( locBit >= 40 ) then
  61.             return locNum + 1, 0
  62.         end
  63.         return locNum, locBit + 8
  64.     end
  65.  
  66.     local function reachlocation
  67.     ( targetPos
  68.     , locNum
  69.     , locBit
  70.     , curPos )
  71.         while ( curPos < targetPos ) do
  72.             locNum, locBit =
  73.                 nextlocation(locNum, locBit)
  74.         end
  75.  
  76.         return locNum, locBit
  77.     end
  78.  
  79.     local function resize (stack, len, curlen)
  80.         if ( len > curlen ) then
  81.             for i = curlen + 1, len do
  82.                 stack[i] = 0
  83.             end
  84.         else
  85.             for i = len + 1, curlen do
  86.                 stack[i] = nil
  87.             end
  88.         end
  89.     end
  90.  
  91.     local function updnummid (stack, index, ahead, rest)
  92.         local num = stack[index]
  93.         local left = rshift64(num, ahead)
  94.         stack[locNum] = left + value +
  95.             band32(locNum, rest)
  96.     end
  97.  
  98.     local function setbyte (stack, locNum, locBit, value)
  99.  
  100.         -- Intentionally repeated conditions
  101.         -- may decrease some operations.
  102.  
  103.         if ( locBit >= 40 ) then
  104.             -- Assign sixth byte
  105.             stack[locNum] =
  106.                 value + rshift64(num, 40)
  107.  
  108.         elseif ( locBit >= 32 ) then
  109.             -- Assign fifth byte
  110.             local left = rshift64(num, 40)
  111.             stack[locNum] = left + value +
  112.                 band32(locNum, 0xFFFFFFFF)
  113.             updnummid(stack, locNum, 40, )
  114.  
  115.         elseif ( locBit >= 24 ) then
  116.             -- Assign fourth byte
  117.             local left = rshift64(num, 32)
  118.             stack[locNum] = left + value +
  119.                 band32(locNum, 0xFFFFFF)
  120.  
  121.         elseif ( locBit >= 16 ) then
  122.             -- Assign third byte
  123.             local left = rshift64(num, 24)
  124.             stack[locNum] = left + value +
  125.                 band32(locNum, 0xFFFF)
  126.  
  127.         elseif ( locBit >= 8 ) then
  128.             -- Assign second byte
  129.             local left = rshift64(num, 24)
  130.             stack[locNum] = left + value +
  131.                 band32(locNum, 0xFFFF)
  132.  
  133.         else
  134.  
  135.         end
  136.     end
  137.  
  138. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement