Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6ff62406 authored by Junyu Lai's avatar Junyu Lai Committed by Gerrit Code Review
Browse files

Merge "[MS10.1] Move multiplySafeByRational to NetworkStatsUtils"

parents 4548fa72 b241707f
Loading
Loading
Loading
Loading
+0 −31
Original line number Diff line number Diff line
@@ -74,35 +74,4 @@ public class NetworkUtilsInternal {

        return true;
    }

    /**
     * Safely multiple a value by a rational.
     * <p>
     * Internally it uses integer-based math whenever possible, but switches
     * over to double-based math if values would overflow.
     * @hide
     */
    public static long multiplySafeByRational(long value, long num, long den) {
        if (den == 0) {
            throw new ArithmeticException("Invalid Denominator");
        }
        long x = value;
        long y = num;

        // Logic shamelessly borrowed from Math.multiplyExact()
        long r = x * y;
        long ax = Math.abs(x);
        long ay = Math.abs(y);
        if (((ax | ay) >>> 31 != 0)) {
            // Some bits greater than 2^31 that might cause overflow
            // Check the result using the divide operator
            // and check for the special case of Long.MIN_VALUE * -1
            if (((y != 0) && (r / y != x))
                    || (x == Long.MIN_VALUE && y == -1)) {
                // Use double math to avoid overflowing
                return (long) (((double) num / den) * value);
            }
        }
        return r / den;
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@

package android.net;

import static com.android.internal.net.NetworkUtilsInternal.multiplySafeByRational;
import static com.android.net.module.util.NetworkStatsUtils.multiplySafeByRational;

import android.annotation.IntDef;
import android.annotation.NonNull;
+1 −1
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@ import static android.net.NetworkStats.UID_ALL;
import static android.net.TrafficStats.UID_REMOVED;
import static android.text.format.DateUtils.WEEK_IN_MILLIS;

import static com.android.internal.net.NetworkUtilsInternal.multiplySafeByRational;
import static com.android.net.module.util.NetworkStatsUtils.multiplySafeByRational;

import android.os.Binder;
import android.service.NetworkStatsCollectionKeyProto;
+1 −1
Original line number Diff line number Diff line
@@ -28,8 +28,8 @@ import static android.net.NetworkStatsHistory.ParcelUtils.readLongArray;
import static android.net.NetworkStatsHistory.ParcelUtils.writeLongArray;
import static android.text.format.DateUtils.SECOND_IN_MILLIS;

import static com.android.internal.net.NetworkUtilsInternal.multiplySafeByRational;
import static com.android.internal.util.ArrayUtils.total;
import static com.android.net.module.util.NetworkStatsUtils.multiplySafeByRational;

import android.compat.annotation.UnsupportedAppUsage;
import android.os.Build;