Localize a few variables

This commit is contained in:
Novatux 2015-01-25 10:31:23 +01:00
parent 9d43c1c90e
commit 13a946a868
3 changed files with 42 additions and 42 deletions

View File

@ -3,96 +3,96 @@ bit32 = {}
local N = 32 local N = 32
local P = 2^N local P = 2^N
bit32.bnot = function(x) function bit32.bnot(x)
x = x%P x = x % P
return P-1-x return P - 1 - x
end end
bit32.band = function(x, y) function bit32.band(x, y)
-- Common usecases, they deserve to be optimized -- Common usecases, they deserve to be optimized
if y == 0xff then return x%0x100 end if y == 0xff then return x % 0x100 end
if y == 0xffff then return x%0x10000 end if y == 0xffff then return x % 0x10000 end
if y == 0xffffffff then return x%0x100000000 end if y == 0xffffffff then return x % 0x100000000 end
x, y = x%P, y%P x, y = x % P, y % P
local r = 0 local r = 0
local p = 1 local p = 1
for i = 1, N do for i = 1, N do
a, b = x%2, y%2 local a, b = x % 2, y % 2
x, y = math.floor(x/2), math.floor(y/2) x, y = math.floor(x / 2), math.floor(y / 2)
if a+b == 2 then if a + b == 2 then
r = r + p r = r + p
end end
p = 2*p p = 2 * p
end end
return r return r
end end
bit32.bor = function(x, y) function bit32.bor(x, y)
-- Common usecases, they deserve to be optimized -- Common usecases, they deserve to be optimized
if y == 0xff then return x - (x%0x100) + 0xff end if y == 0xff then return x - (x%0x100) + 0xff end
if y == 0xffff then return x - (x%0x10000) + 0xffff end if y == 0xffff then return x - (x%0x10000) + 0xffff end
if y == 0xffffffff then return 0xffffffff end if y == 0xffffffff then return 0xffffffff end
x, y = x%P, y%P x, y = x % P, y % P
local r = 0 local r = 0
local p = 1 local p = 1
for i = 1, N do for i = 1, N do
a, b = x%2, y%2 local a, b = x % 2, y % 2
x, y = math.floor(x/2), math.floor(y/2) x, y = math.floor(x / 2), math.floor(y / 2)
if a+b >= 1 then if a + b >= 1 then
r = r + p r = r + p
end end
p = 2*p p = 2 * p
end end
return r return r
end end
bit32.bxor = function(x, y) function bit32.bxor(x, y)
x, y = x%P, y%P x, y = x % P, y % P
local r = 0 local r = 0
local p = 1 local p = 1
for i = 1, N do for i = 1, N do
a, b = x%2, y%2 local a, b = x%2, y%2
x, y = math.floor(x/2), math.floor(y/2) x, y = math.floor(x/2), math.floor(y/2)
if a+b == 1 then if a + b == 1 then
r = r + p r = r + p
end end
p = 2*p p = 2 * p
end end
return r return r
end end
bit32.lshift = function(x, s_amount) function bit32.lshift(x, s_amount)
if math.abs(s_amount) >= N then return 0 end if math.abs(s_amount) >= N then return 0 end
x = x%P x = x % P
if s_amount < 0 then if s_amount < 0 then
return math.floor(x*(2^s_amount)) return math.floor(x * (2 ^ s_amount))
else else
return (x*(2^s_amount))%P return (x * (2 ^ s_amount)) % P
end end
end end
bit32.rshift = function(x, s_amount) function bit32.rshift(x, s_amount)
if math.abs(s_amount) >= N then return 0 end if math.abs(s_amount) >= N then return 0 end
x = x%P x = x % P
if s_amount > 0 then if s_amount > 0 then
return math.floor(x*(2^-s_amount)) return math.floor(x * (2 ^ - s_amount))
else else
return (x*(2^-s_amount))%P return (x * (2 ^ -s_amount)) % P
end end
end end
bit32.arshift = function(x, s_amount) function bit32.arshift(x, s_amount)
if math.abs(s_amount) >= N then return 0 end if math.abs(s_amount) >= N then return 0 end
x = x%P x = x % P
if s_amount > 0 then if s_amount > 0 then
local add = 0 local add = 0
if x >= P/2 then if x >= P/2 then
add = P - 2^(N-s_amount) add = P - 2 ^ (N - s_amount)
end end
return math.floor(x*(2^-s_amount))+add return math.floor(x * (2 ^ -s_amount)) + add
else else
return (x*(2^-s_amount))%P return (x * (2 ^ -s_amount)) % P
end end
end end

View File

@ -159,7 +159,7 @@ function run_computer(turtle, cptr)
if cptr.stopped then return end if cptr.stopped then return end
cptr.cycles = math.max(MAX_CYCLES, cptr.cycles + CYCLES_PER_STEP) cptr.cycles = math.max(MAX_CYCLES, cptr.cycles + CYCLES_PER_STEP)
while true do while true do
instr = cptr[cptr.PC] local instr = cptr[cptr.PC]
local f = ITABLE[instr] local f = ITABLE[instr]
if f == nil then return end if f == nil then return end
cptr.PC = u16(cptr.PC + 1) cptr.PC = u16(cptr.PC + 1)

View File

@ -28,8 +28,8 @@ end
local function escape(text) local function escape(text)
-- Remove all \0's in the string, that cannot be done using string.gsub as there can't be \0's in a pattern -- Remove all \0's in the string, that cannot be done using string.gsub as there can't be \0's in a pattern
text2 = "" local text2 = ""
for i=1, string.len(text) do for i = 1, string.len(text) do
if string.byte(text, i) ~= 0 then text2 = text2 .. string.sub(text, i, i) end if string.byte(text, i) ~= 0 then text2 = text2 .. string.sub(text, i, i) end
end end
return minetest.formspec_escape(text2) return minetest.formspec_escape(text2)
@ -48,9 +48,9 @@ end
function screen.create_text_formspec(text, basex, basey) function screen.create_text_formspec(text, basex, basey)
local f = lines(text) local f = lines(text)
s = "" local s = ""
i = basey - 0.25 local i = basey - 0.25
for _,x in ipairs(f) do for _, x in ipairs(f) do
s = s .. "]label[" .. basex .. "," .. tostring(i) .. ";" .. escape(x) s = s .. "]label[" .. basex .. "," .. tostring(i) .. ";" .. escape(x)
i = i + 0.3 i = i + 0.3
end end