This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
gamax92's profile picture

[CC1.74] Bit api bugs

Started by gamax92, 21 July 2015 - 04:30 PM
gamax92 #1
Posted 21 July 2015 - 06:30 PM
When the bit api changed up in CC1.74, it became a bit less useful and even break various functions:

All functions will limit the number to the range of a signed 32bit number, but give numbers that are unsigned 32bit numbers.
And by limit to a range, it'll do something more like
math.min(math.max(number, -2^31), 2^31-1)
instead of wrapping around

This will for example, break colors.subtract:
bit.bnot will generally return a number larger than 2^31-1, but when it's passed to bit.band it gets rounded down to 2^31-1, and no bits will change.
This results in the color not being subtracted

I'm assuming the api was changed up for the eventual migration towards Lua 5.2, but numbers should wrap around instead or being range limited

EDIT: I guess this is more Java's fault instead of ComputerCraft's fault, casting a double directly to an int will range limit it, but intermediately casting it to a long and then to an int gets the correct wrapped result.
Edited on 21 July 2015 - 04:54 PM
ElvishJerricco #2
Posted 19 September 2015 - 04:56 AM
This bug has broken asm.lua and subsequently JVML-JIT, and now I have to find a different, pure Lua (and therefore slow) bit API. Anyone have any suggestions?
Edited on 19 September 2015 - 02:56 AM
SquidDev #3
Posted 19 September 2015 - 08:27 AM
This bug has broken asm.lua and subsequently JVML-JIT, and now I have to find a different, pure Lua (and therefore slow) bit API. Anyone have any suggestions?

I just added some unsigned integer to integer casts for my AES's bit library, its faster than a pure-Lua version.
Edited on 19 September 2015 - 06:28 AM
ElvishJerricco #4
Posted 19 September 2015 - 08:55 PM
Doesn't seem to have a distinction between logical and arithmetic shift.

I didn't even manage to convert everything to the new bit library. Just anything that could have been vulnerable. It'd be great if you could go through asm.lua and JVML-JIT and see about improving the bit stuff.

Although honestly, the emit time didn't go up at all with the adoption of the pure lua one.