SHARE
TWEET

Credit your sources

ulfben Sep 8th, 2016 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //10 times faster than (Math.log(x) / Math.LN
  2. //but only for integers
  3. //http://www.nbilyk.com/optimizing-actionscript-3
  4. public function log2(x:int):int {
  5.     var num:int = x >> 16;
  6.     var sign:int = int(!num);
  7.     var ans:int = (sign << 4) ^ 24;
  8.  
  9.     num = x >> (ans);
  10.     sign = int(!num);
  11.     ans = (sign << 3) ^ (ans + 4);     
  12.  
  13.     num = x >> (ans);
  14.     sign = int(!num);
  15.     ans = (sign << 2) ^ (ans + 2);
  16.  
  17.     num = x >> (ans);
  18.     sign = int(!num);
  19.     ans = (sign << 1) ^ (ans + 1);     
  20.  
  21.     num = x >> (ans);
  22.     sign = int(!num);
  23.     ans = sign ^ ans;
  24.  
  25.     return ans;
  26. }
RAW Paste Data
Top