mirror of
https://github.com/ZDoom/raze-gles.git
synced 2025-01-12 03:00:38 +00:00
Lunatic: add "bit array" helper module, rewrite the bit test in terms of that.
git-svn-id: https://svn.eduke32.com/eduke32@2841 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
parent
3c5e36feb4
commit
528ccbca4a
2 changed files with 93 additions and 49 deletions
47
polymer/eduke32/source/lunatic/bitar.lua
Normal file
47
polymer/eduke32/source/lunatic/bitar.lua
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
|
||||||
|
-- "Bit array" module based on LuaJIT's BitOp.
|
||||||
|
|
||||||
|
local bit = require "bit"
|
||||||
|
|
||||||
|
local error = error
|
||||||
|
local type = type
|
||||||
|
|
||||||
|
module(...)
|
||||||
|
|
||||||
|
-- Create new bit array.
|
||||||
|
-- Returns a table p in which entries p[0] through p[floor((size+31)/32)]
|
||||||
|
-- are set to an initialization value: 0 if 0 has been passed, -1 if 1
|
||||||
|
-- has been passed.
|
||||||
|
function new(size, initval)
|
||||||
|
if (type(size) ~= "number" or size < 0) then
|
||||||
|
error("bad argument #1 to newarray (must be a nonnegative number)", 2)
|
||||||
|
end
|
||||||
|
|
||||||
|
if (initval ~= 0 and initval ~= 1) then
|
||||||
|
error("bad argument #2 to newarray (must be either 0 or 1)", 2)
|
||||||
|
end
|
||||||
|
|
||||||
|
local p = {}
|
||||||
|
for i=0,(size+31)/32 do
|
||||||
|
p[i] = -initval
|
||||||
|
end
|
||||||
|
|
||||||
|
return p
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Is bit i set in bit array ar?
|
||||||
|
function isset(ar, i)
|
||||||
|
return bit.band(ar[bit.rshift(i, 5)], bit.lshift(1, i)) ~= 0
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Set bit j in bit array ar.
|
||||||
|
function set0(ar, j)
|
||||||
|
local jx = bit.rshift(j, 5)
|
||||||
|
ar[jx] = bit.band(ar[jx], bit.rol(0xfffffffe, j))
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Clear bit j in bit array ar.
|
||||||
|
function set1(ar, j)
|
||||||
|
local jx = bit.rshift(j, 5)
|
||||||
|
ar[jx] = bit.bor(ar[jx], bit.rol(0x00000001, j))
|
||||||
|
end
|
|
@ -1,11 +1,16 @@
|
||||||
#!/usr/bin/env luajit
|
#!/usr/bin/env luajit
|
||||||
|
|
||||||
|
-- Usage: luajit bittest.lua <number or "x"> [-ffi] [-bchk]
|
||||||
|
|
||||||
local string = require "string"
|
local string = require "string"
|
||||||
|
|
||||||
local getticks
|
local getticks
|
||||||
|
|
||||||
local bit = require("bit")
|
local bit = require("bit")
|
||||||
|
local bitar = require "bitar"
|
||||||
|
|
||||||
local print = print
|
local print = print
|
||||||
|
local tonumber = tonumber
|
||||||
|
|
||||||
if (string.dump) then
|
if (string.dump) then
|
||||||
-- stand-alone
|
-- stand-alone
|
||||||
|
@ -20,18 +25,29 @@ else
|
||||||
module(...)
|
module(...)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- from http://bitop.luajit.org/api.html
|
-- based on example from http://bitop.luajit.org/api.html
|
||||||
|
|
||||||
local band, bxor = bit.band, bit.bxor
|
local isset, set0 = bitar.isset, bitar.set0
|
||||||
local rshift, rol = bit.rshift, bit.rol
|
|
||||||
|
|
||||||
local m = string.dump and arg[1] or 1e7
|
local m = string.dump and tonumber(arg[1]) or 1e7
|
||||||
|
|
||||||
|
local ffiar_p, boundchk_p = false, false
|
||||||
|
|
||||||
|
if (string.dump) then
|
||||||
|
if (arg[2]=="-ffi" or arg[3]=="-ffi") then
|
||||||
|
ffiar_p = true
|
||||||
|
end
|
||||||
|
|
||||||
|
if (arg[2]=="-bchk" or arg[3]=="-bchk") then
|
||||||
|
boundchk_p = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function sieve()
|
function sieve()
|
||||||
local count = 0
|
local count = 0
|
||||||
local p = {}
|
local p = {}
|
||||||
|
|
||||||
if (string.dump) then
|
if (ffiar_p) then
|
||||||
-- stand-alone using unchecked int32_t array instead of table:
|
-- stand-alone using unchecked int32_t array instead of table:
|
||||||
-- on x86_64 approx. 100 vs. 160 ms for m = 1e7
|
-- on x86_64 approx. 100 vs. 160 ms for m = 1e7
|
||||||
-- (enabling bound checking makes it be around 170 ms)
|
-- (enabling bound checking makes it be around 170 ms)
|
||||||
|
@ -39,61 +55,42 @@ function sieve()
|
||||||
local pp = ffi.new("int32_t [?]", (m+31)/32 + 1)
|
local pp = ffi.new("int32_t [?]", (m+31)/32 + 1)
|
||||||
|
|
||||||
p = pp
|
p = pp
|
||||||
--[[
|
|
||||||
local mt = {
|
|
||||||
__index = function(tab,idx)
|
|
||||||
if (idx >= 0 and idx <= (m+31)/32) then
|
|
||||||
return pp[idx]
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
|
|
||||||
__newindex = function(tab,idx,val)
|
if (boundchk_p) then
|
||||||
if (idx >= 0 and idx <= (m+31)/32) then
|
local mt = {
|
||||||
pp[idx] = val
|
__index = function(tab,idx)
|
||||||
end
|
if (idx >= 0 and idx <= (m+31)/32) then
|
||||||
end,
|
return pp[idx]
|
||||||
}
|
end
|
||||||
|
end,
|
||||||
|
|
||||||
p = setmetatable({}, mt)
|
__newindex = function(tab,idx,val)
|
||||||
--]]
|
if (idx >= 0 and idx <= (m+31)/32) then
|
||||||
|
pp[idx] = val
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
|
p = setmetatable({}, mt)
|
||||||
|
end
|
||||||
|
|
||||||
|
for i=0,(m+31)/32 do p[i] = -1; end
|
||||||
|
else
|
||||||
|
p = bitar.new(m, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
for i=0,(m+31)/32 do p[i] = -1 end
|
|
||||||
|
|
||||||
local t = getticks()
|
local t = getticks()
|
||||||
|
|
||||||
-- See http://luajit.org/ext_ffi_tutorial.html,
|
|
||||||
-- "To Cache or Not to Cache"
|
|
||||||
-- bit. - qualified version: with m=1e7 ond x86_64:
|
|
||||||
-- 165 ms embedded, 100 ms stand-alone
|
|
||||||
---[[
|
|
||||||
for i=2,m do
|
for i=2,m do
|
||||||
if bit.band(bit.rshift(p[bit.rshift(i, 5)], i), 1) ~= 0 then
|
if (isset(p, i)) then
|
||||||
count = count + 1
|
count = count + 1
|
||||||
for j=i+i,m,i do
|
for j=i+i,m,i do set0(p, j); end
|
||||||
local jx = bit.rshift(j, 5)
|
|
||||||
p[jx] = bit.band(p[jx], bit.rol(-2, j))
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
--]]
|
|
||||||
|
|
||||||
-- local var version: with m=1e7 on x86_64:
|
print(string.format("[%s] Found %d primes up to %d (%.02f ms)",
|
||||||
-- 205 ms embedded, 90 ms stand-alone
|
ffiar_p and "ffi-ar"..(boundchk_p and ", bchk" or "") or "tab-ar",
|
||||||
--[[
|
count, m, getticks()-t))
|
||||||
for i=2,m do
|
|
||||||
if band(rshift(p[rshift(i, 5)], i), 1) ~= 0 then
|
|
||||||
count = count + 1
|
|
||||||
for j=i+i,m,i do
|
|
||||||
local jx = rshift(j, 5)
|
|
||||||
p[jx] = band(p[jx], rol(-2, j))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
--]]
|
|
||||||
|
|
||||||
print(string.format("Found %d primes up to %d (%.02f ms)", count, m,
|
|
||||||
getticks()-t))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if (string.dump) then
|
if (string.dump) then
|
||||||
|
|
Loading…
Reference in a new issue