lua-users home
lua-l archive

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


Tim Channon wrote:
> I know it is creep-ism, not something I like.

Yes, that's why I dropped some bitfield functions I had originally
intended to add (IMHO this functionality is better left to a
struct (un)packing library).

> It would be good to include a few best-done-at-machine-level set operations.
> 
> include(set, x)
> exclude(set, x)
> in(set, x)

Well, it's not that much longer to write this:

  set = bor(set, lshift(1, x))
  set = band(set, rol(-2, x))
  (band(set, lshift(1, x)) ~= 0)

(and LJ2 knows how to turn it into the optimal machine code)

> count(set)  (number of bits at 1)

Also called popcount. I know this one is ugly, but it's also rare.
And the sum-of-bitmasks solution is the best you can do right now,
anyway. Ok, so Intel added popcnt to SSE4.2 and the first CPU with
it is on the market since a few weeks. But I'm not convinced it
should be part of a general-purpose Lua bit operations library.

Recently I found myself needing find-last-set-bit quite often, but
then I wrote a register allocator (in C, not Lua). :-) Not sure
this is popular enough.

--Mike