really fix #72

This commit is contained in:
Starbeamrainbowlabs 2021-12-26 22:40:00 +00:00
parent 502595579e
commit f7a5e223d4
Signed by: sbrl
GPG key ID: 1BE5172E637709C2

View file

@ -13,23 +13,27 @@
-- module: bit -- module: bit
if not minetest.global_exists("bit") then local bit_local
bit = {
if minetest.global_exists("bit") then
bit_local = bit
else
bit_local = {
bits = 32, bits = 32,
powtab = { 1 } powtab = { 1 }
} }
for b = 1, bit.bits - 1 do for b = 1, bit_local.bits - 1 do
bit.powtab[#bit.powtab + 1] = math.pow(2, b) bit_local.powtab[#bit_local.powtab + 1] = math.pow(2, b)
end end
end end
-- Functions -- Functions
-- bit.band -- bit_local.band
if not bit.band then if not bit_local.band then
bit.band = function(a, b) bit_local.band = function(a, b)
local result = 0 local result = 0
for x = 1, bit.bits do for x = 1, bit_local.bits do
result = result + result result = result + result
if (a < 0) then if (a < 0) then
if (b < 0) then if (b < 0) then
@ -43,11 +47,11 @@ if not bit.band then
end end
end end
-- bit.bor -- bit_local.bor
if not bit.bor then if not bit_local.bor then
bit.bor = function(a, b) bit_local.bor = function(a, b)
local result = 0 local result = 0
for x = 1, bit.bits do for x = 1, bit_local.bits do
result = result + result result = result + result
if (a < 0) then if (a < 0) then
result = result + 1 result = result + 1
@ -61,47 +65,47 @@ if not bit.bor then
end end
end end
-- bit.bnot -- bit_local.bnot
if not bit.bnot then if not bit_local.bnot then
bit.bnot = function(x) bit_local.bnot = function(x)
return bit.bxor(x, math.pow((bit.bits or math.floor(math.log(x, 2))), 2) - 1) return bit_local.bxor(x, math.pow((bit_local.bits or math.floor(math.log(x, 2))), 2) - 1)
end end
end end
-- bit.lshift -- bit_local.lshift
if not bit.lshift then if not bit_local.lshift then
bit.lshift = function(a, n) bit_local.lshift = function(a, n)
if (n > bit.bits) then if (n > bit_local.bits) then
a = 0 a = 0
else else
a = a * bit.powtab[n] a = a * bit_local.powtab[n]
end end
return a return a
end end
end end
-- bit.rshift -- bit_local.rshift
if not bit.rshift then if not bit_local.rshift then
bit.rshift = function(a, n) bit_local.rshift = function(a, n)
if (n > bit.bits) then if (n > bit_local.bits) then
a = 0 a = 0
elseif (n > 0) then elseif (n > 0) then
if (a < 0) then if (a < 0) then
a = a - bit.powtab[#bit.powtab] a = a - bit_local.powtab[#bit_local.powtab]
a = a / bit.powtab[n] a = a / bit_local.powtab[n]
a = a + bit.powtab[bit.bits - n] a = a + bit_local.powtab[bit_local.bits - n]
else else
a = a / bit.powtab[n] a = a / bit_local.powtab[n]
end end
end end
return a return a
end end
end end
-- bit.arshift -- bit_local.arshift
if not bit.arshift then if not bit_local.arshift then
bit.arshift = function(a, n) bit_local.arshift = function(a, n)
if (n >= bit.bits) then if (n >= bit_local.bits) then
if (a < 0) then if (a < 0) then
a = -1 a = -1
else else
@ -109,22 +113,22 @@ if not bit.arshift then
end end
elseif (n > 0) then elseif (n > 0) then
if (a < 0) then if (a < 0) then
a = a - bit.powtab[#bit.powtab] a = a - bit_local.powtab[#bit_local.powtab]
a = a / bit.powtab[n] a = a / bit_local.powtab[n]
a = a - bit.powtab[bit.bits - n] a = a - bit_local.powtab[bit_local.bits - n]
else else
a = a / bit.powtab[n] a = a / bit_local.powtab[n]
end end
end end
return a return a
end end
end end
-- bit.bxor -- bit_local.bxor
if not bit.bxor then if not bit_local.bxor then
bit.bxor = function(a, b) bit_local.bxor = function(a, b)
local result = 0 local result = 0
for x = 1, bit.bits, 1 do for x = 1, bit_local.bits, 1 do
result = result + result result = result + result
if (a < 0) then if (a < 0) then
if (b >= 0) then if (b >= 0) then
@ -140,40 +144,40 @@ if not bit.bxor then
end end
end end
-- bit.rol -- bit_local.rol
if not bit.rol then if not bit_local.rol then
bit.rol = function(a, b) bit_local.rol = function(a, b)
local bits = bit.band(b, bit.bits - 1) local bits = bit_local.band(b, bit_local.bits - 1)
a = bit.band(a, 0xffffffff) a = bit_local.band(a, 0xffffffff)
a = bit.bor(bit.lshift(a, b), bit.rshift(a, ((bit.bits - 1) - b))) a = bit_local.bor(bit_local.lshift(a, b), bit_local.rshift(a, ((bit_local.bits - 1) - b)))
return bit.band(n, 0xffffffff) return bit_local.band(n, 0xffffffff)
end end
end end
-- bit.ror -- bit_local.ror
if not bit.ror then if not bit_local.ror then
bit.ror = function(a, b) bit_local.ror = function(a, b)
return bit.rol(a, - b) return bit_local.rol(a, - b)
end end
end end
-- bit.bswap -- bit_local.bswap
if not bit.bswap then if not bit_local.bswap then
bit.bswap = function(n) bit_local.bswap = function(n)
local a = bit.band(n, 0xff) local a = bit_local.band(n, 0xff)
n = bit.rshift(n, 8) n = bit_local.rshift(n, 8)
local b = bit.band(n, 0xff) local b = bit_local.band(n, 0xff)
n = bit.rshift(n, 8) n = bit_local.rshift(n, 8)
local c = bit.band(n, 0xff) local c = bit_local.band(n, 0xff)
n = bit.rshift(n, 8) n = bit_local.rshift(n, 8)
local d = bit.band(n, 0xff) local d = bit_local.band(n, 0xff)
return bit.lshift(bit.lshift(bit.lshift(a, 8) + b, 8) + c, 8) + d return bit_local.lshift(bit_local.lshift(bit_local.lshift(a, 8) + b, 8) + c, 8) + d
end end
end end
-- bit.tobit -- bit_local.tobit
if not bit.tobit then if not bit_local.tobit then
bit.tobit = function(n) bit_local.tobit = function(n)
local MOD = 2^32 local MOD = 2^32
n = n % MOD n = n % MOD
if (n >= 0x80000000) then if (n >= 0x80000000) then
@ -183,9 +187,9 @@ if not bit.tobit then
end end
end end
-- bit.tohex -- bit_local.tohex
if not bit.tohex then if not bit_local.tohex then
bit.tohex = function(x, n) bit_local.tohex = function(x, n)
n = n or 8 n = n or 8
local up local up
if n <= 0 then if n <= 0 then
@ -195,9 +199,9 @@ if not bit.tohex then
up = true up = true
n = -n n = -n
end end
x = bit.band(x, 16^n - 1) x = bit_local.band(x, 16^n - 1)
return ('%0'..n..(up and 'X' or 'x')):format(x) return ('%0'..n..(up and 'X' or 'x')):format(x)
end end
end end
return bit return bit_local