lua-users home
lua-l archive

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

Pierre Chapuis wrote:
> I have code that uses the FFI that segfaults when I run it in
> LuaJIT-2.0.0-beta8 with hotfix #1 on Mac OS X. It is part of the
> code that implements a bitset.

Thank you for the bug report! A fix for this bug has been
committed to LuaJIT git HEAD.

Unrelated note: your implementation of bitsets is not very
efficient. Have a look at this instead:

local ffi = require("ffi")
local bit = require("bit")
local band, bor =, bit.bor
local lshift, rshift, rol = bit.lshift, bit.rshift, bit.rol

local function bitnew(n)
  return"int32_t[?]", rshift(n+31, 5))

-- Note: the index 'i' is zero-based!
local function bittest(b, i)
  return band(rshift(b[rshift(i, 5)], i), 1) ~= 0

local function bitset(b, i)
  local x = rshift(i, 5); b[x] = bor(b[x], lshift(1, i))

local function bitclear(b, i)
  local x = rshift(i, 5); b[x] = band(b[x], rol(-2, i))

-- Simple test:
local N = tonumber(arg and arg[1]) or 65536
local bset = bitnew(N)
for i=1,N-1,2 do bitset(bset, i) end
for i=0,N-1,3 do bitclear(bset, i) end
for i=0,N-1 do assert(bittest(bset, i) == (i%2 == 1 and i%3 ~= 0)) end