Lunatic: ksqrt, with timing and value test.

The latter shows that "int32_t ksqrt(int32_t)" also copes with values in the
range INT32_MIN..-1, effectively interpreting them as uint32_t (i.e. adding
2**32).  However, this should not be relied on from CON.

git-svn-id: https://svn.eduke32.com/eduke32@2790 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2012-07-01 22:11:07 +00:00
parent da4fcec462
commit 9431207c74
3 changed files with 26 additions and 1 deletions

View file

@ -471,6 +471,10 @@ playerdata_t g_player[MAXPLAYERS];
]]
-- functions
ffi.cdef[[
int32_t ksqrt(int32_t num);
]]
ffi.cdef "double gethitickms(void);"
local ffiC = ffi.C

View file

@ -26,6 +26,7 @@ nextspritestat;
headsectbunch;
nextsectbunch;
ksqrt;
hitscan;
actor;

View file

@ -127,6 +127,7 @@ gameevent(gv.EVENT_JUMP,
gameevent(gv.EVENT_ENTERLEVEL,
function()
-- NOTE: times are for helixhorned (Core2Duo 3GHz)
local i
local N = 1e6
local t = gv.gethitickms()
@ -137,9 +138,28 @@ gameevent(gv.EVENT_ENTERLEVEL,
t = gv.gethitickms()-t
-- NOTE: for me (helixhorned), about 40 ns per call
-- x86: 40ns/call, x86_64: 290 ns/call
printf("%d gethitickms() calls took %.03f ms (%.03f us/call)",
N, t, (t*1000)/N)
local sum=0
t = gv.gethitickms()
for i=1,N do sum = sum+gv.ksqrt(i) end
t = gv.gethitickms()-t
-- x86_64: 14ns/call
printf("%d ksqrt() calls took %.03f ms (%.03f us/call) [sum=%f]",
N, t, (t*1000)/N, sum)
sum=0
t = gv.gethitickms()
for i=1,N do sum = sum+math.sqrt(i) end
t = gv.gethitickms()-t
-- x86_64: 7ns/call
printf("%d math.sqrt() calls took %.03f ms (%.03f us/call) [sum=%f]",
N, t, (t*1000)/N, sum)
printf("sqrt(0xffffffff) = %f(ksqrt) %f(math.sqrt)",
gv.ksqrt(-1), math.sqrt(0xffffffff))
end
)