There are several things wrong here:
1. The "least significant 32 bits" mean the rightmost bits. A theoretical
bit.band(result,0xFFFFFFFF00000000) would give you the
most significant 32 bits of a 64 bit number, not the least significant bits.
2. Despite that misunderstanding, your code does give you the least significant 32 bits of
result. But:
3. In your code,
result is created by multiplying two 32 bit numbers, which in BizHawk's Lua (5.1) are doubles, meaning once the result is over 2^53, you will have inaccuracies.
Example: 0xFFFFFFFF*0x00200001 equals 0x200000FFDFFFFF. But when using doubles that multiplication gives you 0x200000FFE00000.
Since your goal is the least significant 32 bits, it can easily be done using the mod operator (%):
number % 0x100000000.
The bigger problem is an accurate 32 bit multiplication. The result of that multiplication can reach 64 bits, so the 53 bits of doubles is not enough. Luckily, you can simply split one of the numbers into two 16 bit parts, giving you two 32 bit with 16 bit multiplications, where the result is only 48 bits.
Language: lua
function MUL(A,B)
local reslow = A * (B%0x10000) -- A multiplied with lower 16 bits of B
local reshigh = A * (math.floor(B/0x10000)%0x10000) -- A multiplied with higher 16 bits of B (shifted down)
reshigh = reshigh%0x10000 -- only 16 bits can matter here if result is 32 bits
return (reshigh*0x10000 + reslow)%0x100000000 -- recombine and cut off to 32 bits
end
(The final recombining step adds a 32 bit and a 48 bit number, so technically requires 49 bits. But we have 53 bits so we're good.)