MCPcopy Create free account
hub / github.com/grpc/grpc-java / saturatedAdd

Method saturatedAdd

core/src/main/java/io/grpc/internal/JsonUtil.java:422–432  ·  view source on GitHub ↗

Returns the sum of a and b unless it would overflow or underflow in which case Long.MAX_VALUE or Long.MIN_VALUE is returned, respectively. Copy of com.google.common.math.LongMath#saturatedAdd.

(long a, long b)

Source from the content-addressed store, hash-verified

420 *
421 */
422 @SuppressWarnings("ShortCircuitBoolean")
423 private static long saturatedAdd(long a, long b) {
424 long naiveSum = a + b;
425 if ((a ^ b) < 0 | (a ^ naiveSum) >= 0) {
426 // If a and b have different signs or a has the same sign as the result then there was no
427 // overflow, return.
428 return naiveSum;
429 }
430 // we did over/under flow, if the sign is negative we should return MAX otherwise MIN
431 return Long.MAX_VALUE + ((naiveSum >>> (Long.SIZE - 1)) ^ 1);
432 }
433}

Callers 2

normalizedDurationMethod · 0.95
currentTimeNanosMethod · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected