glegris / pulpcore

Automatically exported from code.google.com/p/pulpcore
0 stars 1 forks source link

[improvement] CoreMath sin(), cos(), tan() performance. #23

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
replace the un-needed multiplications by their constant equivalent in
CoreMath.sin() function (CoreMath.java, lines 799 and below):

  int d = mul((1 << INTERNAL_BITS) / (2*3*4*5*6*7*8*9), fxSquared);
  int c = mul(d - (1 << INTERNAL_BITS) / (2*3*4*5*6*7), fxSquared);
  int b = mul(c + (1 << INTERNAL_BITS) / (2*3*4*5), fxSquared);
  int a = mul(b - (1 << INTERNAL_BITS) / (2*3), fxSquared);
  return sine >> (INTERNAL_BITS - FRACTION_BITS);

Maybe detailed multiplications should be kept in comments.

+ same idea for bit shifts of constant value INTERNAL_BITS and
(INTERNAL_BITS - FRACTION_BITS)

Original issue reported on code.google.com by alexandr...@gmail.com on 9 Jul 2009 at 5:32

GoogleCodeExporter commented 9 years ago
Javac does all the dirty work here. Decompile the code, and you'll see what 
javac does: 
            int d = mul(46, fxSquared);
            int c = mul(d - 3328, fxSquared);
            int b = mul(c + 0x22222, fxSquared);
            int a = mul(b - 0x2aaaaa, fxSquared);
            int sine = mul(a + 0x1000000, fx);
            return sine >> 8;

Original comment by brack...@gmail.com on 9 Jul 2009 at 5:45

GoogleCodeExporter commented 9 years ago
Didn't know.

Thanks for the explanation.

Original comment by alexandr...@gmail.com on 9 Jul 2009 at 5:48