2012-07-20 21:57:34 +00:00
|
|
|
#!/usr/bin/env luajit
|
2012-07-20 21:57:28 +00:00
|
|
|
|
2012-07-20 21:57:40 +00:00
|
|
|
-- Usage: luajit bittest.lua <number or "x"> [-ffi] [-bchk]
|
|
|
|
|
2012-07-20 21:57:28 +00:00
|
|
|
local string = require "string"
|
|
|
|
|
|
|
|
local getticks
|
|
|
|
|
|
|
|
local bit = require("bit")
|
2012-07-20 21:57:40 +00:00
|
|
|
local bitar = require "bitar"
|
|
|
|
|
2012-07-20 21:57:28 +00:00
|
|
|
local print = print
|
2012-07-20 21:57:40 +00:00
|
|
|
local tonumber = tonumber
|
2012-07-20 21:57:28 +00:00
|
|
|
|
|
|
|
if (string.dump) then
|
|
|
|
-- stand-alone
|
|
|
|
local os = require "os"
|
|
|
|
function getticks()
|
|
|
|
return os.clock()*1000
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- embedded
|
|
|
|
getticks = gv.gethitickms
|
|
|
|
|
|
|
|
module(...)
|
|
|
|
end
|
|
|
|
|
2012-07-20 21:57:40 +00:00
|
|
|
-- based on example from http://bitop.luajit.org/api.html
|
|
|
|
|
|
|
|
local isset, set0 = bitar.isset, bitar.set0
|
2012-07-20 21:57:28 +00:00
|
|
|
|
2012-07-20 21:57:40 +00:00
|
|
|
local m = string.dump and tonumber(arg[1]) or 1e7
|
2012-07-20 21:57:28 +00:00
|
|
|
|
2012-07-20 21:57:40 +00:00
|
|
|
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
|
2012-07-20 21:57:28 +00:00
|
|
|
|
|
|
|
function sieve()
|
|
|
|
local count = 0
|
|
|
|
local p = {}
|
|
|
|
|
2012-07-20 21:57:40 +00:00
|
|
|
if (ffiar_p) then
|
2012-07-20 21:57:34 +00:00
|
|
|
-- stand-alone using unchecked int32_t array instead of table:
|
|
|
|
-- on x86_64 approx. 100 vs. 160 ms for m = 1e7
|
|
|
|
-- (enabling bound checking makes it be around 170 ms)
|
|
|
|
local ffi = require "ffi"
|
|
|
|
local pp = ffi.new("int32_t [?]", (m+31)/32 + 1)
|
|
|
|
|
|
|
|
p = pp
|
2012-07-20 21:57:28 +00:00
|
|
|
|
2012-07-20 21:57:40 +00:00
|
|
|
if (boundchk_p) then
|
|
|
|
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 (idx >= 0 and idx <= (m+31)/32) then
|
|
|
|
pp[idx] = val
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
p = setmetatable({}, mt)
|
2012-07-20 21:57:34 +00:00
|
|
|
end
|
2012-07-20 21:57:40 +00:00
|
|
|
|
|
|
|
for i=0,(m+31)/32 do p[i] = -1; end
|
|
|
|
else
|
|
|
|
p = bitar.new(m, 1)
|
2012-07-20 21:57:34 +00:00
|
|
|
end
|
|
|
|
|
2012-07-20 21:57:40 +00:00
|
|
|
local t = getticks()
|
|
|
|
|
2012-07-20 21:57:28 +00:00
|
|
|
for i=2,m do
|
2012-07-20 21:57:40 +00:00
|
|
|
if (isset(p, i)) then
|
2012-07-20 21:57:28 +00:00
|
|
|
count = count + 1
|
2012-07-20 21:57:40 +00:00
|
|
|
for j=i+i,m,i do set0(p, j); end
|
2012-07-20 21:57:28 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-20 21:57:40 +00:00
|
|
|
print(string.format("[%s] Found %d primes up to %d (%.02f ms)",
|
|
|
|
ffiar_p and "ffi-ar"..(boundchk_p and ", bchk" or "") or "tab-ar",
|
|
|
|
count, m, getticks()-t))
|
2012-07-20 21:57:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if (string.dump) then
|
|
|
|
sieve()
|
|
|
|
end
|