
AS3 Bitwise gems - fast integer math - RWilson
http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/
======
mahmud
Interested parties can also look at how the Tamarin VM implements those
bitwise opcodes natively.

<http://www.mozilla.org/projects/tamarin/>

Flash opcodes into Nanojit IR:

[http://hg.mozilla.org/tamarin-central/raw-
file/4eb9f961a087/...](http://hg.mozilla.org/tamarin-central/raw-
file/4eb9f961a087/nanojit/LIR.h)

Nanojit IR to native x86 translation tables?

[http://hg.mozilla.org/tamarin-central/raw-
file/4eb9f961a087/...](http://hg.mozilla.org/tamarin-central/raw-
file/4eb9f961a087/nanojit/Nativei386.h)

~~~
RWilson
Nice! That's great to hear, since it's always perplexing when native code
ISN'T the fastest... Kind of makes you have to ask, "If there is a better way,
why isn't that the native way?" But, for Tamarin, that question will be moot.

------
pmjordan
I don't do flash, but I was under the impression that there's some kind of
compilation going on and ActionScript isn't just interpreted. How on earth
does a modern compiler not optimise

    
    
      a / 64
    

to

    
    
      a >> 6
    

? I could understand it in a dynamically typed language, where a, and
therefore the result, could be non-integer, but ActionScript seems to have
static type declarations for variables.

~~~
swolchok
This isn't even "modern" in compiler terms, it's a simple peephole
optimization. (grep through the IR, check for "X / 2^n", replace by "X >> n")
To get it right when you have

    
    
        int a;
        int b = 64;
        a / b
    

requires dataflow analysis so you can do constant propagation, copy
propagation, and dead code elimination. This is more work to get right, but
that's not a very good excuse for not at least doing the peephole
optimization.

