lua-users home
lua-l archive

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


I know this is a bit of a sore topic, but can we reduce the amount of undefined behaviour in the official Lua bit library? Prior to 5.2 there's virtually no ill-defined behaviour, yet 5.2 brings in two very common cases. I'm talking about the bit library of course ;)..

1) Negative numbers should be made two's complement unsigned numbers, throw an error, or even cast to zero. Undefined behaviour on an operation as common as operating on bit masks where they're stored as -1, -2, -4 (not that uncommon!) is not acceptable imo, and will catch people out. Especially as most compilers and systems will work exactly the way people expect - but a few others won't - the absolute worst kind of undefined behaviour. Allowing negative numbers may draw attention to the lacking of an bit.arshift, so perhaps throwing an error is the most logical action...

2) Mingling addition and bit operations is common, and so overflow needs to be dealt with in a well mannered way. That 0xFFFFFFFF + 1 could equal 0xABABABAB on some setups certainly violates the principle of least surprise... again, throw an error if modular arithmetic on lua_Numbers is too hard. :)

As an example of these limitations - try going through http://www-graphics.stanford.edu/~seander/bithacks.html. I got as far as population counts before finding my first complete example that didn't instigate undefined behaviour, at which point I tired of it. I realise bit twiddling hacks have no place in Lua, but they make for interesting testing of a bit library..

Anyway, I am curious if any of the authors have tried implementing a bit heavy function in Lua 5.2? It'd be nice to see some example code of its use.

Finally, thanks for splitting bit.shift =).

- Alex

P.S. Don't forget to fix the fistp issue on microsoft compilers! It caught me out.