mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-12-23 03:55:01 +00:00
Add bit polyfill from https://github.com/Rabios/polyfill.lua
This commit is contained in:
parent
a80ab3b657
commit
5c7b5432d9
2 changed files with 247 additions and 44 deletions
|
@ -6,57 +6,60 @@
|
|||
-- @author Starbeamrainbowlabs
|
||||
|
||||
worldeditadditions = {}
|
||||
worldeditadditions.modpath = minetest.get_modpath("worldeditadditions")
|
||||
dofile(worldeditadditions.modpath.."/utils/vector.lua")
|
||||
worldeditadditions.Vector3 = dofile(worldeditadditions.modpath.."/utils/vector3.lua")
|
||||
worldeditadditions.Mesh,
|
||||
worldeditadditions.Face = dofile(worldeditadditions.modpath.."/utils/mesh.lua")
|
||||
local wea = worldeditadditions
|
||||
wea.modpath = minetest.get_modpath("worldeditadditions")
|
||||
|
||||
worldeditadditions.Queue = dofile(worldeditadditions.modpath.."/utils/queue.lua")
|
||||
worldeditadditions.LRU = dofile(worldeditadditions.modpath.."/utils/lru.lua")
|
||||
wea.Vector3 = dofile(wea.modpath.."/utils/vector3.lua")
|
||||
wea.Mesh, wea.Face = dofile(wea.modpath.."/utils/mesh.lua")
|
||||
|
||||
wea.Queue = dofile(wea.modpath.."/utils/queue.lua")
|
||||
wea.LRU = dofile(wea.modpath.."/utils/lru.lua")
|
||||
|
||||
wea.bit = dofile(wea.modpath.."/utils/bit.lua")
|
||||
|
||||
|
||||
dofile(worldeditadditions.modpath.."/utils/strings/init.lua")
|
||||
dofile(worldeditadditions.modpath.."/utils/format/init.lua")
|
||||
dofile(worldeditadditions.modpath.."/utils/parse/init.lua")
|
||||
dofile(worldeditadditions.modpath.."/utils/tables/init.lua")
|
||||
dofile(wea.modpath.."/utils/vector.lua")
|
||||
dofile(wea.modpath.."/utils/strings/init.lua")
|
||||
dofile(wea.modpath.."/utils/format/init.lua")
|
||||
dofile(wea.modpath.."/utils/parse/init.lua")
|
||||
dofile(wea.modpath.."/utils/tables/init.lua")
|
||||
|
||||
dofile(worldeditadditions.modpath.."/utils/numbers.lua")
|
||||
dofile(worldeditadditions.modpath.."/utils/nodes.lua")
|
||||
dofile(worldeditadditions.modpath.."/utils/node_identification.lua")
|
||||
dofile(worldeditadditions.modpath.."/utils/terrain.lua")
|
||||
dofile(worldeditadditions.modpath.."/utils/raycast_adv.lua") -- For the farwand
|
||||
dofile(worldeditadditions.modpath.."/utils/axes.lua")
|
||||
dofile(wea.modpath.."/utils/numbers.lua")
|
||||
dofile(wea.modpath.."/utils/nodes.lua")
|
||||
dofile(wea.modpath.."/utils/node_identification.lua")
|
||||
dofile(wea.modpath.."/utils/terrain.lua")
|
||||
dofile(wea.modpath.."/utils/raycast_adv.lua") -- For the farwand
|
||||
dofile(wea.modpath.."/utils/axes.lua")
|
||||
|
||||
dofile(worldeditadditions.modpath.."/lib/compat/saplingnames.lua")
|
||||
dofile(wea.modpath.."/lib/compat/saplingnames.lua")
|
||||
|
||||
dofile(worldeditadditions.modpath.."/lib/floodfill.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/overlay.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/layers.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/fillcaves.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/ellipsoid.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/torus.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/line.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/walls.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/replacemix.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/maze2d.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/maze3d.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/hollow.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/scale_up.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/scale_down.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/scale.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/conv/conv.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/erode/erode.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/noise/init.lua")
|
||||
dofile(wea.modpath.."/lib/floodfill.lua")
|
||||
dofile(wea.modpath.."/lib/overlay.lua")
|
||||
dofile(wea.modpath.."/lib/layers.lua")
|
||||
dofile(wea.modpath.."/lib/fillcaves.lua")
|
||||
dofile(wea.modpath.."/lib/ellipsoid.lua")
|
||||
dofile(wea.modpath.."/lib/torus.lua")
|
||||
dofile(wea.modpath.."/lib/line.lua")
|
||||
dofile(wea.modpath.."/lib/walls.lua")
|
||||
dofile(wea.modpath.."/lib/replacemix.lua")
|
||||
dofile(wea.modpath.."/lib/maze2d.lua")
|
||||
dofile(wea.modpath.."/lib/maze3d.lua")
|
||||
dofile(wea.modpath.."/lib/hollow.lua")
|
||||
dofile(wea.modpath.."/lib/scale_up.lua")
|
||||
dofile(wea.modpath.."/lib/scale_down.lua")
|
||||
dofile(wea.modpath.."/lib/scale.lua")
|
||||
dofile(wea.modpath.."/lib/conv/conv.lua")
|
||||
dofile(wea.modpath.."/lib/erode/erode.lua")
|
||||
dofile(wea.modpath.."/lib/noise/init.lua")
|
||||
|
||||
dofile(worldeditadditions.modpath.."/lib/count.lua")
|
||||
dofile(wea.modpath.."/lib/count.lua")
|
||||
|
||||
dofile(worldeditadditions.modpath.."/lib/bonemeal.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/forest.lua")
|
||||
dofile(wea.modpath.."/lib/bonemeal.lua")
|
||||
dofile(wea.modpath.."/lib/forest.lua")
|
||||
|
||||
dofile(worldeditadditions.modpath.."/lib/ellipsoidapply.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/airapply.lua")
|
||||
dofile(wea.modpath.."/lib/ellipsoidapply.lua")
|
||||
dofile(wea.modpath.."/lib/airapply.lua")
|
||||
|
||||
dofile(worldeditadditions.modpath.."/lib/subdivide.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/selection/stack.lua")
|
||||
dofile(worldeditadditions.modpath.."/lib/selection/cloud.lua")
|
||||
dofile(wea.modpath.."/lib/subdivide.lua")
|
||||
dofile(wea.modpath.."/lib/selection/stack.lua")
|
||||
dofile(wea.modpath.."/lib/selection/cloud.lua")
|
||||
|
|
200
worldeditadditions/utils/bit.lua
Normal file
200
worldeditadditions/utils/bit.lua
Normal file
|
@ -0,0 +1,200 @@
|
|||
-- ██████ ██ ████████
|
||||
-- ██ ██ ██ ██
|
||||
-- ██████ ██ ██
|
||||
-- ██ ██ ██ ██
|
||||
-- ██████ ██ ██
|
||||
|
||||
-- LuaJIT bit polyfill
|
||||
-- @source https://github.com/Rabios/polyfill.lua/blob/master/polyfill.lua
|
||||
-- Adapted for Minetest by Starbeamrainbowlabs
|
||||
-- Note that this file is MIT licenced, and NOT MPL-2.0!
|
||||
-- @licence MIT
|
||||
-- Docs: http://bitop.luajit.org/api.html
|
||||
|
||||
-- module: bit
|
||||
local bit = bit or {}
|
||||
|
||||
bit.bits = 32
|
||||
bit.powtab = { 1 }
|
||||
|
||||
for b = 1, bit.bits - 1 do
|
||||
bit.powtab[#bit.powtab + 1] = math.pow(2, b)
|
||||
end
|
||||
|
||||
-- Functions
|
||||
-- bit.band
|
||||
if not bit.band then
|
||||
bit.band = function(a, b)
|
||||
local result = 0
|
||||
for x = 1, bit.bits do
|
||||
result = result + result
|
||||
if (a < 0) then
|
||||
if (b < 0) then
|
||||
result = result + 1
|
||||
end
|
||||
end
|
||||
a = a + a
|
||||
b = b + b
|
||||
end
|
||||
return result
|
||||
end
|
||||
end
|
||||
|
||||
-- bit.bor
|
||||
if not bit.bor then
|
||||
bit.bor = function(a, b)
|
||||
local result = 0
|
||||
for x = 1, bit.bits do
|
||||
result = result + result
|
||||
if (a < 0) then
|
||||
result = result + 1
|
||||
elseif (b < 0) then
|
||||
result = result + 1
|
||||
end
|
||||
a = a + a
|
||||
b = b + b
|
||||
end
|
||||
return result
|
||||
end
|
||||
end
|
||||
|
||||
-- bit.bnot
|
||||
if not bit.bnot then
|
||||
bit.bnot = function(x)
|
||||
return bit.bxor(x, math.pow((bit.bits or math.floor(math.log(x, 2))), 2) - 1)
|
||||
end
|
||||
end
|
||||
|
||||
-- bit.lshift
|
||||
if not bit.lshift then
|
||||
bit.lshift = function(a, n)
|
||||
if (n > bit.bits) then
|
||||
a = 0
|
||||
else
|
||||
a = a * bit.powtab[n]
|
||||
end
|
||||
return a
|
||||
end
|
||||
end
|
||||
|
||||
-- bit.rshift
|
||||
if not bit.rshift then
|
||||
bit.rshift = function(a, n)
|
||||
if (n > bit.bits) then
|
||||
a = 0
|
||||
elseif (n > 0) then
|
||||
if (a < 0) then
|
||||
a = a - bit.powtab[#bit.powtab]
|
||||
a = a / bit.powtab[n]
|
||||
a = a + bit.powtab[bit.bits - n]
|
||||
else
|
||||
a = a / bit.powtab[n]
|
||||
end
|
||||
end
|
||||
return a
|
||||
end
|
||||
end
|
||||
|
||||
-- bit.arshift
|
||||
if not bit.arshift then
|
||||
bit.arshift = function(a, n)
|
||||
if (n >= bit.bits) then
|
||||
if (a < 0) then
|
||||
a = -1
|
||||
else
|
||||
a = 0
|
||||
end
|
||||
elseif (n > 0) then
|
||||
if (a < 0) then
|
||||
a = a - bit.powtab[#bit.powtab]
|
||||
a = a / bit.powtab[n]
|
||||
a = a - bit.powtab[bit.bits - n]
|
||||
else
|
||||
a = a / bit.powtab[n]
|
||||
end
|
||||
end
|
||||
return a
|
||||
end
|
||||
end
|
||||
|
||||
-- bit.bxor
|
||||
if not bit.bxor then
|
||||
bit.bxor = function(a, b)
|
||||
local result = 0
|
||||
for x = 1, bit.bits, 1 do
|
||||
result = result + result
|
||||
if (a < 0) then
|
||||
if (b >= 0) then
|
||||
result = result + 1
|
||||
end
|
||||
elseif (b < 0) then
|
||||
result = result + 1
|
||||
end
|
||||
a = a + a
|
||||
b = b + b
|
||||
end
|
||||
return result
|
||||
end
|
||||
end
|
||||
|
||||
-- bit.rol
|
||||
if not bit.rol then
|
||||
bit.rol = function(a, b)
|
||||
local bits = bit.band(b, bit.bits - 1)
|
||||
a = bit.band(a, 0xffffffff)
|
||||
a = bit.bor(bit.lshift(a, b), bit.rshift(a, ((bit.bits - 1) - b)))
|
||||
return bit.band(n, 0xffffffff)
|
||||
end
|
||||
end
|
||||
|
||||
-- bit.ror
|
||||
if not bit.ror then
|
||||
bit.ror = function(a, b)
|
||||
return bit.rol(a, - b)
|
||||
end
|
||||
end
|
||||
|
||||
-- bit.bswap
|
||||
if not bit.bswap then
|
||||
bit.bswap = function(n)
|
||||
local a = bit.band(n, 0xff)
|
||||
n = bit.rshift(n, 8)
|
||||
local b = bit.band(n, 0xff)
|
||||
n = bit.rshift(n, 8)
|
||||
local c = bit.band(n, 0xff)
|
||||
n = bit.rshift(n, 8)
|
||||
local d = bit.band(n, 0xff)
|
||||
return bit.lshift(bit.lshift(bit.lshift(a, 8) + b, 8) + c, 8) + d
|
||||
end
|
||||
end
|
||||
|
||||
-- bit.tobit
|
||||
if not bit.tobit then
|
||||
bit.tobit = function(n)
|
||||
local MOD = 2^32
|
||||
n = n % MOD
|
||||
if (n >= 0x80000000) then
|
||||
n = n - MOD
|
||||
end
|
||||
return n
|
||||
end
|
||||
end
|
||||
|
||||
-- bit.tohex
|
||||
if not bit.tohex then
|
||||
bit.tohex = function(x, n)
|
||||
n = n or 8
|
||||
local up
|
||||
if n <= 0 then
|
||||
if n == 0 then
|
||||
return ''
|
||||
end
|
||||
up = true
|
||||
n = -n
|
||||
end
|
||||
x = bit.band(x, 16^n - 1)
|
||||
return ('%0'..n..(up and 'X' or 'x')):format(x)
|
||||
end
|
||||
end
|
||||
|
||||
return bit
|
Loading…
Reference in a new issue