Files
frameworks_base/services/java/com/android/server/TwilightCalculator.java
Neil Fuller 33253a4baa Switch from FloatMath -> Math and Math.hypot where possible
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
2014-10-01 14:04:15 +01:00

123 lines
4.1 KiB
Java

/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server;
import android.text.format.DateUtils;
/** @hide */
public class TwilightCalculator {
/** Value of {@link #mState} if it is currently day */
public static final int DAY = 0;
/** Value of {@link #mState} if it is currently night */
public static final int NIGHT = 1;
private static final float DEGREES_TO_RADIANS = (float) (Math.PI / 180.0f);
// element for calculating solar transit.
private static final float J0 = 0.0009f;
// correction for civil twilight
private static final float ALTIDUTE_CORRECTION_CIVIL_TWILIGHT = -0.104719755f;
// coefficients for calculating Equation of Center.
private static final float C1 = 0.0334196f;
private static final float C2 = 0.000349066f;
private static final float C3 = 0.000005236f;
private static final float OBLIQUITY = 0.40927971f;
// Java time on Jan 1, 2000 12:00 UTC.
private static final long UTC_2000 = 946728000000L;
/**
* Time of sunset (civil twilight) in milliseconds or -1 in the case the day
* or night never ends.
*/
public long mSunset;
/**
* Time of sunrise (civil twilight) in milliseconds or -1 in the case the
* day or night never ends.
*/
public long mSunrise;
/** Current state */
public int mState;
/**
* calculates the civil twilight bases on time and geo-coordinates.
*
* @param time time in milliseconds.
* @param latiude latitude in degrees.
* @param longitude latitude in degrees.
*/
public void calculateTwilight(long time, double latiude, double longitude) {
final float daysSince2000 = (float) (time - UTC_2000) / DateUtils.DAY_IN_MILLIS;
// mean anomaly
final float meanAnomaly = 6.240059968f + daysSince2000 * 0.01720197f;
// true anomaly
final double trueAnomaly = meanAnomaly + C1 * Math.sin(meanAnomaly) + C2
* Math.sin(2 * meanAnomaly) + C3 * Math.sin(3 * meanAnomaly);
// ecliptic longitude
final double solarLng = trueAnomaly + 1.796593063d + Math.PI;
// solar transit in days since 2000
final double arcLongitude = -longitude / 360;
float n = Math.round(daysSince2000 - J0 - arcLongitude);
double solarTransitJ2000 = n + J0 + arcLongitude + 0.0053d * Math.sin(meanAnomaly)
+ -0.0069d * Math.sin(2 * solarLng);
// declination of sun
double solarDec = Math.asin(Math.sin(solarLng) * Math.sin(OBLIQUITY));
final double latRad = latiude * DEGREES_TO_RADIANS;
double cosHourAngle = (Math.sin(ALTIDUTE_CORRECTION_CIVIL_TWILIGHT) - Math.sin(latRad)
* Math.sin(solarDec)) / (Math.cos(latRad) * Math.cos(solarDec));
// The day or night never ends for the given date and location, if this value is out of
// range.
if (cosHourAngle >= 1) {
mState = NIGHT;
mSunset = -1;
mSunrise = -1;
return;
} else if (cosHourAngle <= -1) {
mState = DAY;
mSunset = -1;
mSunrise = -1;
return;
}
float hourAngle = (float) (Math.acos(cosHourAngle) / (2 * Math.PI));
mSunset = Math.round((solarTransitJ2000 + hourAngle) * DateUtils.DAY_IN_MILLIS) + UTC_2000;
mSunrise = Math.round((solarTransitJ2000 - hourAngle) * DateUtils.DAY_IN_MILLIS) + UTC_2000;
if (mSunrise < time && mSunset > time) {
mState = DAY;
} else {
mState = NIGHT;
}
}
}