Probably a bug in LuaJIT x86

  • From: Egor Skriptunoff <egor.skriptunoff@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 15 Aug 2018 22:21:40 +0300

Hi!

The function f() is deterministic, but its result is changing when run on
LuaJIT x86.

   local ffi = require "ffi"
   local dwords = ffi.typeof("uint32_t[?]")
   local ror = bit.ror

   local function f()
      local arr = dwords(100, 0xDEADBEEF)
      for i = 2, 99 do
         arr[i] = arr[i-2] + ror(arr[i-1], 7)
      end
      local a, b = 0, 0
      for i = 0, 99 do
         a, b = arr[i], a
      end
      return a + b
   end

   jit.off()
   print(f())

   jit.on()
   f()  -- warming up
   print(f())


The output on LuaJIT x64 (both Windows/Linux):
   5730702483
   5730702483

On LuaJIT x86 the output is a pair of unequal numbers:

Output on Windows (LuaJIT 2.0/2.1) x86 and Linux (LuaJIT 2.0) x86
   5730702483
   2764532822

Output on Linux (LuaJIT 2.1) x86
   6102788609
   2764532822


*Question*:
Does function f() exhibit some kind of "unspecified behavior"?
Or is it a bug?


This feature/bug was discovered here:
https://github.com/catwell/cw-lua/issues/1




P.S.
A small "bonus" (unrelated):

Compilation warning is displayed when building latest LuaJIT 2.1 under
VS2010:
   lib_base.c(302) : warning C4146: unary minus operator applied to
unsigned type, result still unsigned

The code in lib_base.c:
   unsigned long ul;
   ...
   if (neg) ul = -ul;  // line 302


-- Egor

Other related posts: