diff --git a/includes/engines/LuaCommon/lualib/luabit/bit.lua b/includes/engines/LuaCommon/lualib/luabit/bit.lua index 6bb3f487..e226d72f 100644 --- a/includes/engines/LuaCommon/lualib/luabit/bit.lua +++ b/includes/engines/LuaCommon/lualib/luabit/bit.lua @@ -1,264 +1,264 @@ ---[[--------------- -LuaBit v0.4 -------------------- -a bitwise operation lib for lua. - -http://luaforge.net/projects/bit/ - -How to use: -------------------- - bit.bnot(n) -- bitwise not (~n) - bit.band(m, n) -- bitwise and (m & n) - bit.bor(m, n) -- bitwise or (m | n) - bit.bxor(m, n) -- bitwise xor (m ^ n) - bit.brshift(n, bits) -- right shift (n >> bits) - bit.blshift(n, bits) -- left shift (n << bits) - bit.blogic_rshift(n, bits) -- logic right shift(zero fill >>>) - -Please note that bit.brshift and bit.blshift only support number within -32 bits. - -2 utility functions are provided too: - bit.tobits(n) -- convert n into a bit table(which is a 1/0 sequence) - -- high bits first - bit.tonumb(bit_tbl) -- convert a bit table into a number -------------------- - -Under the MIT license. - -copyright(c) 2006~2007 hanzhao (abrash_han@hotmail.com) - -2013-02-20: Brad Jorsch: Fix to not try messing with globals, doesn't work in Scribunto ---]]--------------- - -do - ------------------------- --- bit lib implementions - -local function check_int(n) - -- checking not float - if(n - math.floor(n) > 0) then - error("trying to use bitwise operation on non-integer!") - end -end - -local function to_bits(n) - check_int(n) - if(n < 0) then - -- negative - return to_bits(bit.bnot(math.abs(n)) + 1) - end - -- to bits table - local tbl = {} - local cnt = 1 - while (n > 0) do - local last = math.mod(n,2) - if(last == 1) then - tbl[cnt] = 1 - else - tbl[cnt] = 0 - end - n = (n-last)/2 - cnt = cnt + 1 - end - - return tbl -end - -local function tbl_to_number(tbl) - local n = table.getn(tbl) - - local rslt = 0 - local power = 1 - for i = 1, n do - rslt = rslt + tbl[i]*power - power = power*2 - end - - return rslt -end - -local function expand(tbl_m, tbl_n) - local big = {} - local small = {} - if(table.getn(tbl_m) > table.getn(tbl_n)) then - big = tbl_m - small = tbl_n - else - big = tbl_n - small = tbl_m - end - -- expand small - for i = table.getn(small) + 1, table.getn(big) do - small[i] = 0 - end - -end - -local function bit_or(m, n) - local tbl_m = to_bits(m) - local tbl_n = to_bits(n) - expand(tbl_m, tbl_n) - - local tbl = {} - local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n)) - for i = 1, rslt do - if(tbl_m[i]== 0 and tbl_n[i] == 0) then - tbl[i] = 0 - else - tbl[i] = 1 - end - end - - return tbl_to_number(tbl) -end - -local function bit_and(m, n) - local tbl_m = to_bits(m) - local tbl_n = to_bits(n) - expand(tbl_m, tbl_n) - - local tbl = {} - local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n)) - for i = 1, rslt do - if(tbl_m[i]== 0 or tbl_n[i] == 0) then - tbl[i] = 0 - else - tbl[i] = 1 - end - end - - return tbl_to_number(tbl) -end - -local function bit_not(n) - - local tbl = to_bits(n) - local size = math.max(table.getn(tbl), 32) - for i = 1, size do - if(tbl[i] == 1) then - tbl[i] = 0 - else - tbl[i] = 1 - end - end - return tbl_to_number(tbl) -end - -local function bit_xor(m, n) - local tbl_m = to_bits(m) - local tbl_n = to_bits(n) - expand(tbl_m, tbl_n) - - local tbl = {} - local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n)) - for i = 1, rslt do - if(tbl_m[i] ~= tbl_n[i]) then - tbl[i] = 1 - else - tbl[i] = 0 - end - end - - --table.foreach(tbl, print) - - return tbl_to_number(tbl) -end - -local function bit_rshift(n, bits) - check_int(n) - - local high_bit = 0 - if(n < 0) then - -- negative - n = bit_not(math.abs(n)) + 1 - high_bit = 2147483648 -- 0x80000000 - end - - for i=1, bits do - n = n/2 - n = bit_or(math.floor(n), high_bit) - end - return math.floor(n) -end - --- logic rightshift assures zero filling shift -local function bit_logic_rshift(n, bits) - check_int(n) - if(n < 0) then - -- negative - n = bit_not(math.abs(n)) + 1 - end - for i=1, bits do - n = n/2 - end - return math.floor(n) -end - -local function bit_lshift(n, bits) - check_int(n) - - if(n < 0) then - -- negative - n = bit_not(math.abs(n)) + 1 - end - - for i=1, bits do - n = n*2 - end - return bit_and(n, 4294967295) -- 0xFFFFFFFF -end - -local function bit_xor2(m, n) - local rhs = bit_or(bit_not(m), bit_not(n)) - local lhs = bit_or(m, n) - local rslt = bit_and(lhs, rhs) - return rslt -end - --------------------- --- bit lib interface - -local bit = { - -- bit operations - bnot = bit_not, - band = bit_and, - bor = bit_or, - bxor = bit_xor, - brshift = bit_rshift, - blshift = bit_lshift, - bxor2 = bit_xor2, - blogic_rshift = bit_logic_rshift, - - -- utility func - tobits = to_bits, - tonumb = tbl_to_number, -} - -return bit - -end - ---[[ -for i = 1, 100 do - for j = 1, 100 do - if(bit.bxor(i, j) ~= bit.bxor2(i, j)) then - error("bit.xor failed.") - end - end -end ---]] - - - - - - - - - - - - - +--[[--------------- +LuaBit v0.4 +------------------- +a bitwise operation lib for lua. + +http://luaforge.net/projects/bit/ + +How to use: +------------------- + bit.bnot(n) -- bitwise not (~n) + bit.band(m, n) -- bitwise and (m & n) + bit.bor(m, n) -- bitwise or (m | n) + bit.bxor(m, n) -- bitwise xor (m ^ n) + bit.brshift(n, bits) -- right shift (n >> bits) + bit.blshift(n, bits) -- left shift (n << bits) + bit.blogic_rshift(n, bits) -- logic right shift(zero fill >>>) + +Please note that bit.brshift and bit.blshift only support number within +32 bits. + +2 utility functions are provided too: + bit.tobits(n) -- convert n into a bit table(which is a 1/0 sequence) + -- high bits first + bit.tonumb(bit_tbl) -- convert a bit table into a number +------------------- + +Under the MIT license. + +copyright(c) 2006~2007 hanzhao (abrash_han@hotmail.com) + +2013-02-20: Brad Jorsch: Fix to not try messing with globals, doesn't work in Scribunto +--]]--------------- + +do + +------------------------ +-- bit lib implementions + +local function check_int(n) + -- checking not float + if(n - math.floor(n) > 0) then + error("trying to use bitwise operation on non-integer!") + end +end + +local function to_bits(n) + check_int(n) + if(n < 0) then + -- negative + return to_bits(bit.bnot(math.abs(n)) + 1) + end + -- to bits table + local tbl = {} + local cnt = 1 + while (n > 0) do + local last = math.mod(n,2) + if(last == 1) then + tbl[cnt] = 1 + else + tbl[cnt] = 0 + end + n = (n-last)/2 + cnt = cnt + 1 + end + + return tbl +end + +local function tbl_to_number(tbl) + local n = table.getn(tbl) + + local rslt = 0 + local power = 1 + for i = 1, n do + rslt = rslt + tbl[i]*power + power = power*2 + end + + return rslt +end + +local function expand(tbl_m, tbl_n) + local big = {} + local small = {} + if(table.getn(tbl_m) > table.getn(tbl_n)) then + big = tbl_m + small = tbl_n + else + big = tbl_n + small = tbl_m + end + -- expand small + for i = table.getn(small) + 1, table.getn(big) do + small[i] = 0 + end + +end + +local function bit_or(m, n) + local tbl_m = to_bits(m) + local tbl_n = to_bits(n) + expand(tbl_m, tbl_n) + + local tbl = {} + local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n)) + for i = 1, rslt do + if(tbl_m[i]== 0 and tbl_n[i] == 0) then + tbl[i] = 0 + else + tbl[i] = 1 + end + end + + return tbl_to_number(tbl) +end + +local function bit_and(m, n) + local tbl_m = to_bits(m) + local tbl_n = to_bits(n) + expand(tbl_m, tbl_n) + + local tbl = {} + local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n)) + for i = 1, rslt do + if(tbl_m[i]== 0 or tbl_n[i] == 0) then + tbl[i] = 0 + else + tbl[i] = 1 + end + end + + return tbl_to_number(tbl) +end + +local function bit_not(n) + + local tbl = to_bits(n) + local size = math.max(table.getn(tbl), 32) + for i = 1, size do + if(tbl[i] == 1) then + tbl[i] = 0 + else + tbl[i] = 1 + end + end + return tbl_to_number(tbl) +end + +local function bit_xor(m, n) + local tbl_m = to_bits(m) + local tbl_n = to_bits(n) + expand(tbl_m, tbl_n) + + local tbl = {} + local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n)) + for i = 1, rslt do + if(tbl_m[i] ~= tbl_n[i]) then + tbl[i] = 1 + else + tbl[i] = 0 + end + end + + --table.foreach(tbl, print) + + return tbl_to_number(tbl) +end + +local function bit_rshift(n, bits) + check_int(n) + + local high_bit = 0 + if(n < 0) then + -- negative + n = bit_not(math.abs(n)) + 1 + high_bit = 2147483648 -- 0x80000000 + end + + for i=1, bits do + n = n/2 + n = bit_or(math.floor(n), high_bit) + end + return math.floor(n) +end + +-- logic rightshift assures zero filling shift +local function bit_logic_rshift(n, bits) + check_int(n) + if(n < 0) then + -- negative + n = bit_not(math.abs(n)) + 1 + end + for i=1, bits do + n = n/2 + end + return math.floor(n) +end + +local function bit_lshift(n, bits) + check_int(n) + + if(n < 0) then + -- negative + n = bit_not(math.abs(n)) + 1 + end + + for i=1, bits do + n = n*2 + end + return bit_and(n, 4294967295) -- 0xFFFFFFFF +end + +local function bit_xor2(m, n) + local rhs = bit_or(bit_not(m), bit_not(n)) + local lhs = bit_or(m, n) + local rslt = bit_and(lhs, rhs) + return rslt +end + +-------------------- +-- bit lib interface + +local bit = { + -- bit operations + bnot = bit_not, + band = bit_and, + bor = bit_or, + bxor = bit_xor, + brshift = bit_rshift, + blshift = bit_lshift, + bxor2 = bit_xor2, + blogic_rshift = bit_logic_rshift, + + -- utility func + tobits = to_bits, + tonumb = tbl_to_number, +} + +return bit + +end + +--[[ +for i = 1, 100 do + for j = 1, 100 do + if(bit.bxor(i, j) ~= bit.bxor2(i, j)) then + error("bit.xor failed.") + end + end +end +--]] + + + + + + + + + + + + + diff --git a/includes/engines/LuaCommon/lualib/luabit/hex.lua b/includes/engines/LuaCommon/lualib/luabit/hex.lua index ee6a69e3..3c5fa072 100644 --- a/includes/engines/LuaCommon/lualib/luabit/hex.lua +++ b/includes/engines/LuaCommon/lualib/luabit/hex.lua @@ -1,99 +1,99 @@ ---[[--------------- -Hex v0.4 -------------------- -Hex conversion lib for lua. - -How to use: - hex.to_hex(n) -- convert a number to a hex string - hex.to_dec(hex) -- convert a hex string(prefix with '0x' or '0X') to number - -Part of LuaBit(http://luaforge.net/projects/bit/). - -Under the MIT license. - -copyright(c) 2006~2007 hanzhao (abrash_han@hotmail.com) - -2013-02-20: Brad Jorsch: Fix to not try messing with globals, doesn't work in Scribunto ---]]--------------- - -local bit = require 'bit' - -do - -local function to_hex(n) - if(type(n) ~= "number") then - error("non-number type passed in.") - end - - -- checking not float - if(n - math.floor(n) > 0) then - error("trying to apply bitwise operation on non-integer!") - end - - if(n < 0) then - -- negative - n = bit.tobits(bit.bnot(math.abs(n)) + 1) - n = bit.tonumb(n) - end - - hex_tbl = {'A', 'B', 'C', 'D', 'E', 'F'} - hex_str = "" - - while(n ~= 0) do - last = math.mod(n, 16) - if(last < 10) then - hex_str = tostring(last) .. hex_str - else - hex_str = hex_tbl[last-10+1] .. hex_str - end - n = math.floor(n/16) - end - if(hex_str == "") then - hex_str = "0" - end - return "0x" .. hex_str -end - -local function to_dec(hex) - if(type(hex) ~= "string") then - error("non-string type passed in.") - end - - head = string.sub(hex, 1, 2) - - if( head ~= "0x" and head ~= "0X") then - error("wrong hex format, should lead by 0x or 0X.") - end - - v = tonumber(string.sub(hex, 3), 16) - - return v; -end - --------------------- --- hex lib interface -local hex = { - to_dec = to_dec, - to_hex = to_hex, -} - -return hex - -end - ---[[ --- test -d = 4341688 -h = to_hex(d) -print(h) -print(to_dec(h)) - - -for i = 1, 100000 do - h = hex.to_hex(i) - d = hex.to_dec(h) - if(d ~= i) then - error("failed " .. i .. ", " .. h) - end -end ---]] +--[[--------------- +Hex v0.4 +------------------- +Hex conversion lib for lua. + +How to use: + hex.to_hex(n) -- convert a number to a hex string + hex.to_dec(hex) -- convert a hex string(prefix with '0x' or '0X') to number + +Part of LuaBit(http://luaforge.net/projects/bit/). + +Under the MIT license. + +copyright(c) 2006~2007 hanzhao (abrash_han@hotmail.com) + +2013-02-20: Brad Jorsch: Fix to not try messing with globals, doesn't work in Scribunto +--]]--------------- + +local bit = require 'bit' + +do + +local function to_hex(n) + if(type(n) ~= "number") then + error("non-number type passed in.") + end + + -- checking not float + if(n - math.floor(n) > 0) then + error("trying to apply bitwise operation on non-integer!") + end + + if(n < 0) then + -- negative + n = bit.tobits(bit.bnot(math.abs(n)) + 1) + n = bit.tonumb(n) + end + + hex_tbl = {'A', 'B', 'C', 'D', 'E', 'F'} + hex_str = "" + + while(n ~= 0) do + last = math.mod(n, 16) + if(last < 10) then + hex_str = tostring(last) .. hex_str + else + hex_str = hex_tbl[last-10+1] .. hex_str + end + n = math.floor(n/16) + end + if(hex_str == "") then + hex_str = "0" + end + return "0x" .. hex_str +end + +local function to_dec(hex) + if(type(hex) ~= "string") then + error("non-string type passed in.") + end + + head = string.sub(hex, 1, 2) + + if( head ~= "0x" and head ~= "0X") then + error("wrong hex format, should lead by 0x or 0X.") + end + + v = tonumber(string.sub(hex, 3), 16) + + return v; +end + +-------------------- +-- hex lib interface +local hex = { + to_dec = to_dec, + to_hex = to_hex, +} + +return hex + +end + +--[[ +-- test +d = 4341688 +h = to_hex(d) +print(h) +print(to_dec(h)) + + +for i = 1, 100000 do + h = hex.to_hex(i) + d = hex.to_dec(h) + if(d ~= i) then + error("failed " .. i .. ", " .. h) + end +end +--]]