-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #751 from ScorexFoundation/develop
Release v4.0.5
- Loading branch information
Showing
93 changed files
with
4,634 additions
and
1,055 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Oracle designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
package java7.compat; | ||
|
||
/** | ||
* Contains methods introduced since Java 1.8 which are not available in Java 1.7. | ||
* Using this methods supports compatibility with Java 1.7 in non-JVM contexts like | ||
* RoboVM. | ||
* The implementations are copies from JDK 1.8 sources. | ||
* <p> | ||
* See | ||
* <a href="https://github.com/ScorexFoundation/sigmastate-interpreter/issues/735">this issue</a> | ||
*/ | ||
public final class Math { | ||
/** | ||
* Returns the sum of its arguments, | ||
* throwing an exception if the result overflows an {@code int}. | ||
* | ||
* @param x the first value | ||
* @param y the second value | ||
* @return the result | ||
* @throws ArithmeticException if the result overflows an int | ||
* @since 1.8 | ||
*/ | ||
public static int addExact(int x, int y) { | ||
int r = x + y; | ||
// HD 2-12 Overflow iff both arguments have the opposite sign of the result | ||
if (((x ^ r) & (y ^ r)) < 0) { | ||
throw new ArithmeticException("integer overflow"); | ||
} | ||
return r; | ||
} | ||
|
||
/** | ||
* Returns the sum of its arguments, | ||
* throwing an exception if the result overflows a {@code long}. | ||
* | ||
* @param x the first value | ||
* @param y the second value | ||
* @return the result | ||
* @throws ArithmeticException if the result overflows a long | ||
* @since 1.8 | ||
*/ | ||
public static long addExact(long x, long y) { | ||
long r = x + y; | ||
// HD 2-12 Overflow iff both arguments have the opposite sign of the result | ||
if (((x ^ r) & (y ^ r)) < 0) { | ||
throw new ArithmeticException("long overflow"); | ||
} | ||
return r; | ||
} | ||
|
||
/** | ||
* Returns the difference of the arguments, | ||
* throwing an exception if the result overflows an {@code int}. | ||
* | ||
* @param x the first value | ||
* @param y the second value to subtract from the first | ||
* @return the result | ||
* @throws ArithmeticException if the result overflows an int | ||
* @since 1.8 | ||
*/ | ||
public static int subtractExact(int x, int y) { | ||
int r = x - y; | ||
// HD 2-12 Overflow iff the arguments have different signs and | ||
// the sign of the result is different than the sign of x | ||
if (((x ^ y) & (x ^ r)) < 0) { | ||
throw new ArithmeticException("integer overflow"); | ||
} | ||
return r; | ||
} | ||
|
||
/** | ||
* Returns the difference of the arguments, | ||
* throwing an exception if the result overflows a {@code long}. | ||
* | ||
* @param x the first value | ||
* @param y the second value to subtract from the first | ||
* @return the result | ||
* @throws ArithmeticException if the result overflows a long | ||
* @since 1.8 | ||
*/ | ||
public static long subtractExact(long x, long y) { | ||
long r = x - y; | ||
// HD 2-12 Overflow iff the arguments have different signs and | ||
// the sign of the result is different than the sign of x | ||
if (((x ^ y) & (x ^ r)) < 0) { | ||
throw new ArithmeticException("long overflow"); | ||
} | ||
return r; | ||
} | ||
|
||
/** | ||
* Returns the product of the arguments, | ||
* throwing an exception if the result overflows an {@code int}. | ||
* | ||
* @param x the first value | ||
* @param y the second value | ||
* @return the result | ||
* @throws ArithmeticException if the result overflows an int | ||
* @since 1.8 | ||
*/ | ||
public static int multiplyExact(int x, int y) { | ||
long r = (long)x * (long)y; | ||
if ((int)r != r) { | ||
throw new ArithmeticException("integer overflow"); | ||
} | ||
return (int)r; | ||
} | ||
|
||
/** | ||
* Returns the product of the arguments, | ||
* throwing an exception if the result overflows a {@code long}. | ||
* | ||
* @param x the first value | ||
* @param y the second value | ||
* @return the result | ||
* @throws ArithmeticException if the result overflows a long | ||
* @since 1.8 | ||
*/ | ||
public static long multiplyExact(long x, long y) { | ||
long r = x * y; | ||
long ax = java.lang.Math.abs(x); | ||
long ay = java.lang.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)) { | ||
throw new ArithmeticException("long overflow"); | ||
} | ||
} | ||
return r; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.