lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


DirectX switches the FPU to single precision by default [at least used
to]. So, even though Lua number is defined as double, all one has to
work with is 23 bits of precision. This means that full 32 bit integers
are impossible to store in a Lua number under a DirectX app. On one
project [with Lua 4.x] we had discovered this only too late and resorted
to making our Lua wrapper changing FPU precission back and forth [lookup
fldcw instruction on x86 FPUs] on every Lua entry. Lua was used in a not
time cricical part of the project there. Doing this FPU massage in a
time critical part will be pretty slow as an atomic operation done over
and over every frame, but compared to searching for a single Lua key in
a table it would likely amount to nothing [benchmark it]. Anyhow, you'd
want to cache the state of FPU so you'd only change it when you need to.
Also, instead of doing it per Lua call you can do this at a higher
application level where you know you don't make any DirectX calls, but
do enter Lua. Single precision on FPU makes divides and sqrts go about
twice as fast and this is one reason to use it instead of double
precision. 

AB

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Roberto
Ierusalimschy
Sent: Thursday, May 25, 2006 8:53 AM
To: Lua list
Subject: Re: D3DCREATE_FPU_PRESERVE


> I'm not really skilled enough to attempt more than a few approaches - 
> those listed - and I'm not really sure I'm up to the task of creating 
> effective benchmarks.  Basically, I (and others, apparently) am simply

> curious to know if anyone has checked to see exactly what impact this 
> flag creates vs. changing the Lua number to float, etc.  From what 
> I've heard everyone has simply adopted the flag, and damn the cost.

Do not forget these two approaches:

1) change lua_number2int in luaconf.h to this:

  #define lua_number2int(i,d)   __asm fld d   __asm fistp i

(Should have no performance penalty, but only works on selected
compilers.)


2) change lua_number2int in luaconf.h to its default definition:

  #define lua_number2int(i,d)     ((i)=(int)(d))

(Should always work, but may have a performance penalty in Lua; but how
much??)

-- Roberto