[jira] [Created] (MATH-951) Patch: Faster implementation of double org.apache.commons.math3.FastMath.copySign(double, double)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[jira] [Created] (MATH-951) Patch: Faster implementation of double org.apache.commons.math3.FastMath.copySign(double, double)

ASF GitHub Bot (Jira)
Charles Cooper created MATH-951:
-----------------------------------

             Summary: Patch: Faster implementation of double org.apache.commons.math3.FastMath.copySign(double, double)
                 Key: MATH-951
                 URL: https://issues.apache.org/jira/browse/MATH-951
             Project: Commons Math
          Issue Type: Improvement
    Affects Versions: 3.2
         Environment: java version "1.6.0_27"
OpenJDK Runtime Environment (IcedTea6 1.12.3) (6b27-1.12.3-1)
OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)

Linux debian 3.2.0-4-amd64 #2 SMP Wed Jan 2 01:41:29 PST 2013 x86_64 GNU/Linux

Intel(R) Core(TM) i5-2500 CPU @ 3.30GHz


            Reporter: Charles Cooper
            Priority: Minor
             Fix For: 3.2


Between 40-50% faster than both current implementation and java.lang.Math.copySign by my (sloppy) benchmark. Patch below.

Index: src/main/java/org/apache/commons/math3/util/FastMath.java
===================================================================
--- src/main/java/org/apache/commons/math3/util/FastMath.java (revision 1458590)
+++ src/main/java/org/apache/commons/math3/util/FastMath.java (working copy)
@@ -3632,9 +3632,8 @@
     public static double copySign(double magnitude, double sign){
         long m = Double.doubleToLongBits(magnitude);
         long s = Double.doubleToLongBits(sign);
-        if ((m >= 0 && s >= 0) || (m < 0 && s < 0)) { // Sign is currently OK
+ if ((m^s) >= 0) // have same sign
             return magnitude;
-        }
         return -magnitude; // flip sign
     }
 
@@ -3649,9 +3648,8 @@
     public static float copySign(float magnitude, float sign){
         int m = Float.floatToIntBits(magnitude);
         int s = Float.floatToIntBits(sign);
-        if ((m >= 0 && s >= 0) || (m < 0 && s < 0)) { // Sign is currently OK
+ if ((m^s) >= 0)
             return magnitude;
-        }
         return -magnitude; // flip sign
     }
 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira