From 33253a4baa6279f81a73425b49dfb6abe5f5416e Mon Sep 17 00:00:00 2001 From: Neil Fuller Date: Wed, 1 Oct 2014 11:55:10 +0100 Subject: Switch from FloatMath -> Math and Math.hypot where possible MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The motivation is an API change: FloatMath is going to be deprecated and/or removed. Performance is not the goal of this change. That said... Math is faster than FloatMath with AOT compilation. While making the change, occurances of: {Float}Math.sqrt(x * x + y * y) and {Float}Math.sqrt({Float}Math.pow(x, 2) + {Float}Math.pow(y, 2)) have been replaced with: {(float)} Math.hypot(x, y) Right now there is no runtime intrinsic for hypot so is not faster in all cases for AOT compilation: Math.sqrt(x * x + y * y) is faster than Math.hypot(x, y) with AOT, but all other combinations of FloatMath, use of pow() etc. are slower than hypot(). hypot() has the advantage of being self documenting and could be optimized in future. None of the behavior differences around NaN and rounding appear to be important for the cases looked at: they all assume results and arguments are in range and usually the results are cast to float. Different implementations measured on hammerhead / L: AOT compiled: [FloatMath.hypot(x, y)] benchmark=Hypot_FloatMathHypot} 633.85 ns; σ=0.32 ns @ 3 trials [FloatMath.sqrt(x*x + y*y)] benchmark=Hypot_FloatMathSqrtMult} 684.17 ns; σ=4.83 ns @ 3 trials [FloatMath.sqrt(FloatMath.pow(x, 2) + FloatMath.pow(y, 2))] benchmark=Hypot_FloatMathSqrtPow} 1270.65 ns; σ=12.20 ns @ 6 trials [(float) Math.hypot(x, y)] benchmark=Hypot_MathHypot} 96.80 ns; σ=0.05 ns @ 3 trials [(float) Math.sqrt(x*x + y*y)] benchmark=Hypot_MathSqrtMult} 23.97 ns; σ=0.01 ns @ 3 trials [(float) Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2))] benchmark=Hypot_MathSqrtPow} 156.19 ns; σ=0.12 ns @ 3 trials Interpreter: benchmark=Hypot_FloatMathHypot} 1180.54 ns; σ=5.13 ns @ 3 trials benchmark=Hypot_FloatMathSqrtMult} 1121.05 ns; σ=3.80 ns @ 3 trials benchmark=Hypot_FloatMathSqrtPow} 3327.14 ns; σ=7.33 ns @ 3 trials benchmark=Hypot_MathHypot} 856.57 ns; σ=1.41 ns @ 3 trials benchmark=Hypot_MathSqrtMult} 1028.92 ns; σ=9.11 ns @ 3 trials benchmark=Hypot_MathSqrtPow} 2539.47 ns; σ=24.44 ns @ 3 trials Bug: https://code.google.com/p/android/issues/detail?id=36199 Change-Id: I06c91f682095e627cb547d60d936ef87941be692 --- graphics/java/android/graphics/ColorMatrix.java | 8 +++----- graphics/java/android/graphics/PointF.java | 3 +-- graphics/java/android/graphics/RectF.java | 5 ++--- 3 files changed, 6 insertions(+), 10 deletions(-) (limited to 'graphics/java') diff --git a/graphics/java/android/graphics/ColorMatrix.java b/graphics/java/android/graphics/ColorMatrix.java index e3596c8..0bfd07be 100644 --- a/graphics/java/android/graphics/ColorMatrix.java +++ b/graphics/java/android/graphics/ColorMatrix.java @@ -16,8 +16,6 @@ package android.graphics; -import android.util.FloatMath; - /** * 4x5 matrix for transforming the color+alpha components of a Bitmap. * The matrix is stored in a single array, and its treated as follows: @@ -118,9 +116,9 @@ public class ColorMatrix { */ public void setRotate(int axis, float degrees) { reset(); - float radians = degrees * (float)Math.PI / 180; - float cosine = FloatMath.cos(radians); - float sine = FloatMath.sin(radians); + double radians = degrees * Math.PI / 180d; + float cosine = (float) Math.cos(radians); + float sine = (float) Math.sin(radians); switch (axis) { // Rotation around the red color case 0: diff --git a/graphics/java/android/graphics/PointF.java b/graphics/java/android/graphics/PointF.java index ee38dbb..8e4288e 100644 --- a/graphics/java/android/graphics/PointF.java +++ b/graphics/java/android/graphics/PointF.java @@ -18,7 +18,6 @@ package android.graphics; import android.os.Parcel; import android.os.Parcelable; -import android.util.FloatMath; /** @@ -109,7 +108,7 @@ public class PointF implements Parcelable { * Returns the euclidian distance from (0,0) to (x,y) */ public static float length(float x, float y) { - return FloatMath.sqrt(x * x + y * y); + return (float) Math.hypot(x, y); } /** diff --git a/graphics/java/android/graphics/RectF.java b/graphics/java/android/graphics/RectF.java index 53178b0..f5cedfa 100644 --- a/graphics/java/android/graphics/RectF.java +++ b/graphics/java/android/graphics/RectF.java @@ -20,7 +20,6 @@ import java.io.PrintWriter; import android.os.Parcel; import android.os.Parcelable; -import android.util.FloatMath; import com.android.internal.util.FastMath; /** @@ -450,8 +449,8 @@ public class RectF implements Parcelable { * floor of top and left, and the ceiling of right and bottom. */ public void roundOut(Rect dst) { - dst.set((int) FloatMath.floor(left), (int) FloatMath.floor(top), - (int) FloatMath.ceil(right), (int) FloatMath.ceil(bottom)); + dst.set((int) Math.floor(left), (int) Math.floor(top), + (int) Math.ceil(right), (int) Math.ceil(bottom)); } /** -- cgit v1.1