Faster normalXAngle --> faster sin,cos,tan

Jump to navigation Jump to search
Revision as of 8 April 2013 at 08:21.
The highlighted comment was created in this revision.

Faster normalXAngle --> faster sin,cos,tan

Inspired by recent discussion I did a little profiling of DrussGT, and was horrified to find that plain old robocode.util.Utils.normalRelativeAngle() was using a huge percentage of my processing time. So, I tried various methods to see how they were for speed, and came up with one which takes about 1/4 of the time:

   public static final double normalRelativeAngle(double d){
      d += Math.PI;
      double i = Math.floor(d*(1/(2*Math.PI)));
      d -= i*(2*Math.PI);
      return d-Math.PI;
   }
   public static final double normalAbsoluteAngle(double d){
      double i = Math.floor(d*(1/(2*Math.PI)));
      d -= i*(2*Math.PI);
      return d;
   }

I think the big improvement comes from no divisions, no % and no decisions, all of which are slow. Of course, it isn't ulp-accurate (although I'm not sure the previous one was either?), but over 1M calls the largest difference I saw between this and the old one was -3.552713678800501E-15 which is small enough for me to call 0.

Of course, this technique can then be applied to sin and cos, which then run about 2x the speed of previously, and on my system this is now ~4x the speed of Math.sin/cos:

   public static final double sin(double value) {
      value = value*K + 0.5;
      double i = Math.floor(value*(1.0/TRIG_DIVISIONS));
      value -= i*TRIG_DIVISIONS;
      return sineTable[(int)value];
   }

   public static final double cos(double value) {
      value = value*K + 0.5 + Math.PI*0.5*K;
      double i = Math.floor(value*(1.0/TRIG_DIVISIONS));
      value -= i*TRIG_DIVISIONS;
      return sineTable[(int)value];
   }

It can also be applied to the tan method, but I hardly use tan so I haven't bothered with it. Additionally, TRIG_DIVISIONS is no longer restricted to power-of-2 numbers because we lost the &.

This code is public domain, do with it as you like, and give credit if you think I deserve it! Enjoy, and write speedy bots!

    Skilgannon11:15, 7 April 2013

    Further experimentation with the profiler tells me that our lookup-table sin and cos are actually slower than an expanded polynomial version, I suspect this is due to occasional cache misses. However, the faster normalRelativeAngle code is still useful, here are my latest sin/cos implementations:

     public static final double sin(double d) {
          d += Math.PI;
          double x2 = Math.floor(d*(1/(2*Math.PI)));
          d -= x2*(2*Math.PI);
          d-=Math.PI;
       
          x2 = d * d;
       
          return //accurate to 6.82e-8, 3.3x faster than Math.sin, 
             //faster than lookup table in real-world conditions due to no cache misses
             //all values from "Fast Polynomial Approximations to Sine and Cosine", Garret, C. K., 2012
             (((((-2.05342856289746600727e-08*x2 + 2.70405218307799040084e-06)*x2
             - 1.98125763417806681909e-04)*x2 + 8.33255814755188010464e-03)*x2
             - 1.66665772196961623983e-01)*x2 + 9.99999707044156546685e-01)*d;
       }
    
    
       public static final double cos(double d) {
          d += Math.PI;
          double x2 = Math.floor(d*(1/(2*Math.PI)));
          d -= x2*(2*Math.PI);
          d-=Math.PI;
       
          d *= d;
       
          return //max error 5.6e-7, 4x faster than Math.cos, 
             //faster than lookup table in real-world conditions due to less cache misses
             //all values from "Fast Polynomial Approximations to Sine and Cosine", Garret, C. K., 2012
             ((((- 2.21941782786353727022e-07*d + 2.42532401381033027481e-05)*d
             - 1.38627507062573673756e-03)*d + 4.16610337354021107429e-02)*d
             - 4.99995582499065048420e-01)*d + 9.99999443739537210853e-01;
       }
    
      Skilgannon22:48, 7 April 2013
       

      Table lookups are slow I suspect due to array index out-of-bounds checking.

      It also happens in kNN search. I was able to double the speed of my bot by replacing a few arrays with objects with hardcoded number of attributes. Only works with constant sized arrays, like data point coordinates.

        MN00:18, 8 April 2013

        No, I'm fairly sure it's caching, because profiling shows LUT twice as slow as polynomial, but a micro-benchmark shows LUT ~30% faster.

          Skilgannon10:21, 8 April 2013
           

          Just a thought, but why not incorporate the faster normalRelativeAngle and normalAbsoluteAngle into the codebase of Robocode. That way everybody profits from it in the future when a newer version than 1.8.1.0 is standard.

            GrubbmGait01:52, 8 April 2013
             

            Fast-math classes/functions have lower precision.

              MN03:51, 8 April 2013