Revert "separate lua 5.1 functions into number-based and table-based"

This reverts commit 65d03bceefdfdf3b25f769bc772b422041b8005f.
This commit is contained in:
kikito 2015-04-06 18:30:32 +02:00
parent 1e7902f65b
commit b096b1f3c1

63
md5.lua

@ -75,27 +75,24 @@ else
end end
end end
local bits_not, bits_or, bits_and, bits_not, bits_xor
local to_bits -- needs to be declared before bit_not local to_bits -- needs to be declared before bit_not
bits_not = function(tbl) function bit_not(n)
for i=1, math.max(#tbl, 32) do local tbl = to_bits(n)
if tbl[i] == 1 then local size = math.max(#tbl, 32)
for i = 1, size do
if(tbl[i] == 1) then
tbl[i] = 0 tbl[i] = 0
else else
tbl[i] = 1 tbl[i] = 1
end end
end end
return tbl return tbl2number(tbl)
end
bit_not = function(n)
return tbl2number(bits_not(to_bits(n)))
end end
-- defined as local above -- defined as local above
to_bits = function (n) to_bits = function (n)
if n < 0 then if(n < 0) then
-- negative -- negative
return to_bits(bit_not(math.abs(n)) + 1) return to_bits(bit_not(math.abs(n)) + 1)
end end
@ -113,66 +110,61 @@ else
return tbl return tbl
end end
bits_or = function(tbl_m, tbl_n) function bit_or(m, n)
local tbl_m = to_bits(m)
local tbl_n = to_bits(n)
expand(tbl_m, tbl_n) expand(tbl_m, tbl_n)
local tbl = {} local tbl = {}
for i = 1, #tbl_m do for i = 1, #tbl_m do
if(tbl_m[i] == 0 and tbl_n[i] == 0) then if(tbl_m[i]== 0 and tbl_n[i] == 0) then
tbl[i] = 0 tbl[i] = 0
else else
tbl[i] = 1 tbl[i] = 1
end end
end end
return tbl return tbl2number(tbl)
end end
bit_or = function(m, n) function bit_and(m, n)
return tbl2number(bits_or(tobits(m), tobits(n))) local tbl_m = to_bits(m)
end local tbl_n = to_bits(n)
bits_and = function(tbl_m, tbl_n)
expand(tbl_m, tbl_n) expand(tbl_m, tbl_n)
local tbl = {} local tbl = {}
for i = 1, #tbl_m do for i = 1, #tbl_m do
if tbl_m[i] == 0 or tbl_n[i] == 0 then if(tbl_m[i]== 0 or tbl_n[i] == 0) then
tbl[i] = 0 tbl[i] = 0
else else
tbl[i] = 1 tbl[i] = 1
end end
end end
return tbl return tbl2number(tbl)
end end
bit_and = function(m, n) function bit_xor(m, n)
return tbl2number(bits_and(tobits(m), tobits(n))) local tbl_m = to_bits(m)
end local tbl_n = to_bits(n)
bits_xor = function(tbl_m, tbl_n)
expand(tbl_m, tbl_n) expand(tbl_m, tbl_n)
local tbl = {} local tbl = {}
for i = 1, #tbl_m do for i = 1, #tbl_m do
if tbl_m[i] ~= tbl_n[i] then if(tbl_m[i] ~= tbl_n[i]) then
tbl[i] = 1 tbl[i] = 1
else else
tbl[i] = 0 tbl[i] = 0
end end
end end
return tbl return tbl2number(tbl)
end end
bit_xor = function(m, n) function bit_rshift(n, bits)
return tbl2number(bits_xor(tobits(m), tobits(n)))
end
bit_rshift = function(n, bits)
local high_bit = 0 local high_bit = 0
if n < 0 then if(n < 0) then
-- negative
n = bit_not(math.abs(n)) + 1 n = bit_not(math.abs(n)) + 1
high_bit = 2147483648 -- 0x80000000 high_bit = 2147483648 -- 0x80000000
end end
@ -186,8 +178,9 @@ else
return floor(n) return floor(n)
end end
bit_lshift = function(n, bits) function bit_lshift(n, bits)
if n < 0 then if(n < 0) then
-- negative
n = bit_not(math.abs(n)) + 1 n = bit_not(math.abs(n)) + 1
end end