| 0 after every math operation to get a 32-bit signed integer result, and
In C if
y are integers then
x / y will always be a floating point division with a floating point result. But if you apply a bitwise operator right afterward, then the result will be rounded down, converted to integer, and you'll get the same number as C did! With a lot more work. Of course you don't want your bitwise operator to change the result, so you can do a no-op bitwise operation such as
>> 0 or
Once asm.js started doing
| 0 everywhere, people started being very interested in optimizing it. Obviously you can easily just skip running the
OR instruction since it does nothing. But the real work is in all that slow conversion to and from floating point. It turns out that if you add a
| 0 converts it to integer again afterward, then instead of emitting two conversions to floating point, a floating point division instruction, and then a conversion to integer, the JIT can emit a single integer division instruction, just like a C compiler would, without changing the result at all. This is pretty miraculous!
>>>. This is essentially an "unsigned right shift" operator, and it is special because its result is an unsigned 32-bit integer. So when you want a signed integer you append
| 0 after your arithmetic operations, and for unsigned you use
Math.imul() was introduced. So for integer multiplication, you have to use that instead of the
* operator followed by
Math.fround used for this purpose by asm.js. I've never tried using it myself.
Putting it all together
| 0 manually to make sure your values stay in integer form. Your code can run fast without resorting to Web Assembly or Emscripten.