Re: LuaJIT and Floating Point Determinism

  • From: Mike Pall <mike-1401@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Mon, 13 Jan 2014 20:13:22 +0100

Alex wrote:
> The article states "you can’t use SSE or SSE2 for floating point, because
> it’s too under-specified to be deterministic", though I find that somewhat
> suspect, and LuaJIT seems to compile with SSE2 enabled by default.

Yes, that statement is uninformed. SSE *is* the way to get
deterministic IEEE-conformant behavior on x86.

In other words: if you use LuaJIT 2.0 on a 32 bit x86 platform,
you need to explicitly enable SSE in the Makefile. It's on by
default for x64 (and in general for LuaJIT 2.1).

> My main concern is whether the trace optimizations or trace recording will
> affect the order of operations or other things that may affect the output.

That shouldn't affect the outcome, except if you invoke undefined
or unspecified behavior. E.g. converting an out-of-range double to
an integer may yield different results.

> I think I've also read somewhere that trig functions and square root
> implementations vary as well.

Yup, that's pretty hopeless. You'll get to enjoy the differences
between hardware implementations, different math libraries etc.
Basically forget the whole idea, if it needs to be cross-platform
and you intend to use anything other than + - * /.

Oh, and stay away from denormals and infinities, too. Although
there are standard rules for that, many mobile and embedded
platforms take a shortcut and give you 'interesting' results. No
way to tell without trying on each hardware/software combination.

--Mike

Other related posts: