src/java.base/share/classes/java/lang/Math.java
author redestad
Wed, 10 Apr 2019 20:03:07 +0200
changeset 54492 9d0ae9508d53
parent 53041 f15af1e2c683
child 54750 1851a532ddfe
permissions -rw-r--r--
8222029: Optimize Math.floorMod Reviewed-by: aph, darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
44924
dbfcc317533b 8138672: Math. negativeZeroFloatBits and Math. negativeZeroDoubleBits should be final
darcy
parents: 44263
diff changeset
     2
 * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
    27
37364
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
    28
import java.math.BigDecimal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Random;
34781
479b1724ab80 8145990: Move sun.misc math support classes to jdk.internal.math
chegar
parents: 31671
diff changeset
    30
import jdk.internal.math.FloatConsts;
479b1724ab80 8145990: Move sun.misc math support classes to jdk.internal.math
chegar
parents: 31671
diff changeset
    31
import jdk.internal.math.DoubleConsts;
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
    32
import jdk.internal.HotSpotIntrinsicCandidate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * The class {@code Math} contains methods for performing basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * numeric operations such as the elementary exponential, logarithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * square root, and trigonometric functions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p>Unlike some of the numeric methods of class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * {@code StrictMath}, all implementations of the equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * functions of class {@code Math} are not defined to return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * bit-for-bit same results.  This relaxation permits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * better-performing implementations where strict reproducibility is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * not required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>By default many of the {@code Math} methods simply call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * the equivalent method in {@code StrictMath} for their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * implementation.  Code generators are encouraged to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * platform-specific native libraries or microprocessor instructions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * where available, to provide higher-performance implementations of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * {@code Math} methods.  Such higher-performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * implementations still must conform to the specification for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * {@code Math}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>The quality of implementation specifications concern two
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * properties, accuracy of the returned result and monotonicity of the
10122
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    57
 * method.  Accuracy of the floating-point {@code Math} methods is
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    58
 * measured in terms of <i>ulps</i>, units in the last place.  For a
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    59
 * given floating-point format, an {@linkplain #ulp(double) ulp} of a
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    60
 * specific real number value is the distance between the two
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    61
 * floating-point values bracketing that numerical value.  When
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    62
 * discussing the accuracy of a method as a whole rather than at a
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    63
 * specific argument, the number of ulps cited is for the worst-case
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    64
 * error at any argument.  If a method always has an error less than
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    65
 * 0.5 ulps, the method always returns the floating-point number
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    66
 * nearest the exact result; such a method is <i>correctly
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    67
 * rounded</i>.  A correctly rounded method is generally the best a
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    68
 * floating-point approximation can be; however, it is impractical for
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    69
 * many floating-point methods to be correctly rounded.  Instead, for
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    70
 * the {@code Math} class, a larger error bound of 1 or 2 ulps is
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    71
 * allowed for certain methods.  Informally, with a 1 ulp error bound,
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    72
 * when the exact result is a representable number, the exact result
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    73
 * should be returned as the computed result; otherwise, either of the
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    74
 * two floating-point values which bracket the exact result may be
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    75
 * returned.  For exact results large in magnitude, one of the
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    76
 * endpoints of the bracket may be infinite.  Besides accuracy at
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    77
 * individual arguments, maintaining proper relations between the
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    78
 * method at different arguments is also important.  Therefore, most
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    79
 * methods with more than 0.5 ulp errors are required to be
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    80
 * <i>semi-monotonic</i>: whenever the mathematical function is
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    81
 * non-decreasing, so is the floating-point approximation, likewise,
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    82
 * whenever the mathematical function is non-increasing, so is the
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    83
 * floating-point approximation.  Not all approximations that have 1
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
    84
 * ulp accuracy will automatically meet the monotonicity requirements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    86
 * <p>
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    87
 * The platform uses signed two's complement integer arithmetic with
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    88
 * int and long primitive types.  The developer should choose
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    89
 * the primitive type to ensure that arithmetic operations consistently
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    90
 * produce correct results, which in some cases means the operations
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    91
 * will not overflow the range of values of the computation.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    92
 * The best practice is to choose the primitive type and algorithm to avoid
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    93
 * overflow. In cases where the size is {@code int} or {@code long} and
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    94
 * overflow errors need to be detected, the methods {@code addExact},
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    95
 * {@code subtractExact}, {@code multiplyExact}, and {@code toIntExact}
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    96
 * throw an {@code ArithmeticException} when the results overflow.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    97
 * For other arithmetic operations such as divide, absolute value,
38455
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
    98
 * increment by one, decrement by one, and negation, overflow occurs only with
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
    99
 * a specific minimum or maximum value and should be checked against
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   100
 * the minimum or maximum as appropriate.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   101
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @author  unascribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * @author  Joseph D. Darcy
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24367
diff changeset
   104
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
public final class Math {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Don't let anyone instantiate this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private Math() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * The {@code double} value that is closer than any other to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <i>e</i>, the base of the natural logarithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public static final double E = 2.7182818284590452354;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * The {@code double} value that is closer than any other to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <i>pi</i>, the ratio of the circumference of a circle to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * diameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static final double PI = 3.14159265358979323846;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
26727
b4e26e7f964e 4477961: java.lang.Math.toDegrees(double) could be optimized
bpb
parents: 25859
diff changeset
   128
     * Constant by which to multiply an angular value in degrees to obtain an
b4e26e7f964e 4477961: java.lang.Math.toDegrees(double) could be optimized
bpb
parents: 25859
diff changeset
   129
     * angular value in radians.
b4e26e7f964e 4477961: java.lang.Math.toDegrees(double) could be optimized
bpb
parents: 25859
diff changeset
   130
     */
b4e26e7f964e 4477961: java.lang.Math.toDegrees(double) could be optimized
bpb
parents: 25859
diff changeset
   131
    private static final double DEGREES_TO_RADIANS = 0.017453292519943295;
b4e26e7f964e 4477961: java.lang.Math.toDegrees(double) could be optimized
bpb
parents: 25859
diff changeset
   132
b4e26e7f964e 4477961: java.lang.Math.toDegrees(double) could be optimized
bpb
parents: 25859
diff changeset
   133
    /**
b4e26e7f964e 4477961: java.lang.Math.toDegrees(double) could be optimized
bpb
parents: 25859
diff changeset
   134
     * Constant by which to multiply an angular value in radians to obtain an
b4e26e7f964e 4477961: java.lang.Math.toDegrees(double) could be optimized
bpb
parents: 25859
diff changeset
   135
     * angular value in degrees.
b4e26e7f964e 4477961: java.lang.Math.toDegrees(double) could be optimized
bpb
parents: 25859
diff changeset
   136
     */
b4e26e7f964e 4477961: java.lang.Math.toDegrees(double) could be optimized
bpb
parents: 25859
diff changeset
   137
    private static final double RADIANS_TO_DEGREES = 57.29577951308232;
b4e26e7f964e 4477961: java.lang.Math.toDegrees(double) could be optimized
bpb
parents: 25859
diff changeset
   138
b4e26e7f964e 4477961: java.lang.Math.toDegrees(double) could be optimized
bpb
parents: 25859
diff changeset
   139
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Returns the trigonometric sine of an angle.  Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * <ul><li>If the argument is NaN or an infinity, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * same sign as the argument.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param   a   an angle, in radians.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @return  the sine of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   152
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public static double sin(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return StrictMath.sin(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Returns the trigonometric cosine of an angle. Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <ul><li>If the argument is NaN or an infinity, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * result is NaN.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param   a   an angle, in radians.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @return  the cosine of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   168
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public static double cos(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return StrictMath.cos(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Returns the trigonometric tangent of an angle.  Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <ul><li>If the argument is NaN or an infinity, then the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * same sign as the argument.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param   a   an angle, in radians.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @return  the tangent of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   186
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public static double tan(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return StrictMath.tan(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Returns the arc sine of a value; the returned angle is in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * range -<i>pi</i>/2 through <i>pi</i>/2.  Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * <ul><li>If the argument is NaN or its absolute value is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * than 1, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * same sign as the argument.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param   a   the value whose arc sine is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @return  the arc sine of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public static double asin(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return StrictMath.asin(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Returns the arc cosine of a value; the returned angle is in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * range 0.0 through <i>pi</i>.  Special case:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <ul><li>If the argument is NaN or its absolute value is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * than 1, then the result is NaN.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param   a   the value whose arc cosine is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @return  the arc cosine of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public static double acos(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return StrictMath.acos(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Returns the arc tangent of a value; the returned angle is in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * range -<i>pi</i>/2 through <i>pi</i>/2.  Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * <ul><li>If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * same sign as the argument.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param   a   the value whose arc tangent is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @return  the arc tangent of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public static double atan(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        return StrictMath.atan(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Converts an angle measured in degrees to an approximately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * equivalent angle measured in radians.  The conversion from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * degrees to radians is generally inexact.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param   angdeg   an angle, in degrees
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @return  the measurement of the angle {@code angdeg}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *          in radians.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public static double toRadians(double angdeg) {
26727
b4e26e7f964e 4477961: java.lang.Math.toDegrees(double) could be optimized
bpb
parents: 25859
diff changeset
   253
        return angdeg * DEGREES_TO_RADIANS;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Converts an angle measured in radians to an approximately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * equivalent angle measured in degrees.  The conversion from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * radians to degrees is generally inexact; users should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <i>not</i> expect {@code cos(toRadians(90.0))} to exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * equal {@code 0.0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param   angrad   an angle, in radians
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return  the measurement of the angle {@code angrad}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *          in degrees.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public static double toDegrees(double angrad) {
26727
b4e26e7f964e 4477961: java.lang.Math.toDegrees(double) could be optimized
bpb
parents: 25859
diff changeset
   269
        return angrad * RADIANS_TO_DEGREES;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Returns Euler's number <i>e</i> raised to the power of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * {@code double} value.  Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <ul><li>If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * <li>If the argument is positive infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <li>If the argument is negative infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * positive zero.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param   a   the exponent to raise <i>e</i> to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @return  the value <i>e</i><sup>{@code a}</sup>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *          where <i>e</i> is the base of the natural logarithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   288
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public static double exp(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return StrictMath.exp(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Returns the natural logarithm (base <i>e</i>) of a {@code double}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * value.  Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <ul><li>If the argument is NaN or less than zero, then the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * <li>If the argument is positive infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * <li>If the argument is positive zero or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * result is negative infinity.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @param   a   a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @return  the value ln&nbsp;{@code a}, the natural logarithm of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *          {@code a}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   310
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public static double log(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return StrictMath.log(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Returns the base 10 logarithm of a {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <ul><li>If the argument is NaN or less than zero, then the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * <li>If the argument is positive infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * <li>If the argument is positive zero or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * result is negative infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * <li> If the argument is equal to 10<sup><i>n</i></sup> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * integer <i>n</i>, then the result is <i>n</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @param   a   a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @return  the base 10 logarithm of  {@code a}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   336
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public static double log10(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        return StrictMath.log10(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Returns the correctly rounded positive square root of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <ul><li>If the argument is NaN or less than zero, then the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * <li>If the argument is positive infinity, then the result is positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * <li>If the argument is positive zero or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * result is the same as the argument.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Otherwise, the result is the {@code double} value closest to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * the true mathematical square root of the argument value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @param   a   a value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @return  the positive square root of {@code a}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *          If the argument is NaN or less than zero, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   358
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public static double sqrt(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return StrictMath.sqrt(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                   // Note that hardware sqrt instructions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                                   // frequently can be directly used by JITs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                   // and should be much faster than doing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                                   // Math.sqrt in software.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Returns the cube root of a {@code double} value.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * positive finite {@code x}, {@code cbrt(-x) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * -cbrt(x)}; that is, the cube root of a negative value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * the negative of the cube root of that value's magnitude.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * <li>If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * <li>If the argument is infinite, then the result is an infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * with the same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @param   a   a value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @return  the cube root of {@code a}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public static double cbrt(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        return StrictMath.cbrt(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * Computes the remainder operation on two arguments as prescribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * by the IEEE 754 standard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * The remainder value is mathematically equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * <code>f1&nbsp;-&nbsp;f2</code>&nbsp;&times;&nbsp;<i>n</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * where <i>n</i> is the mathematical integer closest to the exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * mathematical value of the quotient {@code f1/f2}, and if two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * mathematical integers are equally close to {@code f1/f2},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * then <i>n</i> is the integer that is even. If the remainder is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * zero, its sign is the same as the sign of the first argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * <ul><li>If either argument is NaN, or the first argument is infinite,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * or the second argument is positive zero or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * <li>If the first argument is finite and the second argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * infinite, then the result is the same as the first argument.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @param   f1   the dividend.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @param   f2   the divisor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @return  the remainder when {@code f1} is divided by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *          {@code f2}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public static double IEEEremainder(double f1, double f2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        return StrictMath.IEEEremainder(f1, f2); // delegate to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Returns the smallest (closest to negative infinity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * {@code double} value that is greater than or equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * argument and is equal to a mathematical integer. Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * <ul><li>If the argument value is already equal to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * mathematical integer, then the result is the same as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * argument.  <li>If the argument is NaN or an infinity or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * positive zero or negative zero, then the result is the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * the argument.  <li>If the argument value is less than zero but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * greater than -1.0, then the result is negative zero.</ul> Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * that the value of {@code Math.ceil(x)} is exactly the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * value of {@code -Math.floor(-x)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @param   a   a value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @return  the smallest (closest to negative infinity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *          floating-point value that is greater than or equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *          the argument and is equal to a mathematical integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public static double ceil(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        return StrictMath.ceil(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * Returns the largest (closest to positive infinity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * {@code double} value that is less than or equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * argument and is equal to a mathematical integer. Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * <ul><li>If the argument value is already equal to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * mathematical integer, then the result is the same as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * argument.  <li>If the argument is NaN or an infinity or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * positive zero or negative zero, then the result is the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * the argument.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @param   a   a value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @return  the largest (closest to positive infinity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *          floating-point value that less than or equal to the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *          and is equal to a mathematical integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    public static double floor(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        return StrictMath.floor(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * Returns the {@code double} value that is closest in value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * to the argument and is equal to a mathematical integer. If two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * {@code double} values that are mathematical integers are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * equally close, the result is the integer value that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * even. Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * <ul><li>If the argument value is already equal to a mathematical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * integer, then the result is the same as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * <li>If the argument is NaN or an infinity or positive zero or negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * zero, then the result is the same as the argument.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @param   a   a {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @return  the closest floating-point value to {@code a} that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *          equal to a mathematical integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    public static double rint(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        return StrictMath.rint(a); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * Returns the angle <i>theta</i> from the conversion of rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * coordinates ({@code x},&nbsp;{@code y}) to polar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * coordinates (r,&nbsp;<i>theta</i>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * This method computes the phase <i>theta</i> by computing an arc tangent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * of {@code y/x} in the range of -<i>pi</i> to <i>pi</i>. Special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * <ul><li>If either argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * <li>If the first argument is positive zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * is positive, or the first argument is positive and finite and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * second argument is positive infinity, then the result is positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * <li>If the first argument is negative zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * is positive, or the first argument is negative and finite and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * second argument is positive infinity, then the result is negative zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * <li>If the first argument is positive zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * is negative, or the first argument is positive and finite and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * second argument is negative infinity, then the result is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * {@code double} value closest to <i>pi</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * <li>If the first argument is negative zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * is negative, or the first argument is negative and finite and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * second argument is negative infinity, then the result is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * {@code double} value closest to -<i>pi</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * <li>If the first argument is positive and the second argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * positive zero or negative zero, or the first argument is positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * infinity and the second argument is finite, then the result is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * {@code double} value closest to <i>pi</i>/2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * <li>If the first argument is negative and the second argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * positive zero or negative zero, or the first argument is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * infinity and the second argument is finite, then the result is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * {@code double} value closest to -<i>pi</i>/2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * <li>If both arguments are positive infinity, then the result is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * {@code double} value closest to <i>pi</i>/4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * <li>If the first argument is positive infinity and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * is negative infinity, then the result is the {@code double}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * value closest to 3*<i>pi</i>/4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * <li>If the first argument is negative infinity and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * is positive infinity, then the result is the {@code double} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * closest to -<i>pi</i>/4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * <li>If both arguments are negative infinity, then the result is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * {@code double} value closest to -3*<i>pi</i>/4.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * <p>The computed result must be within 2 ulps of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @param   y   the ordinate coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @param   x   the abscissa coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @return  the <i>theta</i> component of the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *          (<i>r</i>,&nbsp;<i>theta</i>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *          in polar coordinates that corresponds to the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *          (<i>x</i>,&nbsp;<i>y</i>) in Cartesian coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   537
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    public static double atan2(double y, double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        return StrictMath.atan2(y, x); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * Returns the value of the first argument raised to the power of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * second argument. Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * <ul><li>If the second argument is positive or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * result is 1.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * <li>If the second argument is 1.0, then the result is the same as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * first argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * <li>If the second argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * <li>If the first argument is NaN and the second argument is nonzero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * <li>If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * <li>the absolute value of the first argument is greater than 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * and the second argument is positive infinity, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * <li>the absolute value of the first argument is less than 1 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * the second argument is negative infinity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * then the result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * <li>If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * <li>the absolute value of the first argument is greater than 1 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * the second argument is negative infinity, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * <li>the absolute value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * first argument is less than 1 and the second argument is positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * infinity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * then the result is positive zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * <li>If the absolute value of the first argument equals 1 and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * second argument is infinite, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * <li>If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * <li>the first argument is positive zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * is greater than zero, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * <li>the first argument is positive infinity and the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * argument is less than zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * then the result is positive zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * <li>If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * <li>the first argument is positive zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * is less than zero, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * <li>the first argument is positive infinity and the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * argument is greater than zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * then the result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * <li>If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * <li>the first argument is negative zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * is greater than zero but not a finite odd integer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * <li>the first argument is negative infinity and the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * argument is less than zero but not a finite odd integer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * then the result is positive zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * <li>If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * <li>the first argument is negative zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * is a positive finite odd integer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * <li>the first argument is negative infinity and the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * argument is a negative finite odd integer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * then the result is negative zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * <li>If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * <li>the first argument is negative zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * is less than zero but not a finite odd integer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * <li>the first argument is negative infinity and the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * argument is greater than zero but not a finite odd integer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * then the result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * <li>If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * <li>the first argument is negative zero and the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * is a negative finite odd integer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * <li>the first argument is negative infinity and the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * argument is a positive finite odd integer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * then the result is negative infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * <li>If the first argument is finite and less than zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * <li> if the second argument is a finite even integer, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * result is equal to the result of raising the absolute value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * the first argument to the power of the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * <li>if the second argument is a finite odd integer, the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * is equal to the negative of the result of raising the absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * value of the first argument to the power of the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * <li>if the second argument is finite and not an integer, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * <li>If both arguments are integers, then the result is exactly equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * to the mathematical result of raising the first argument to the power
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * of the second argument if that result can in fact be represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * exactly as a {@code double} value.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * <p>(In the foregoing descriptions, a floating-point value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * considered to be an integer if and only if it is finite and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * fixed point of the method {@link #ceil ceil} or,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * equivalently, a fixed point of the method {@link #floor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * floor}. A value is a fixed point of a one-argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * method if and only if the result of applying the method to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * value is equal to the value.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @param   a   the base.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * @param   b   the exponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @return  the value {@code a}<sup>{@code b}</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   665
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    public static double pow(double a, double b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        return StrictMath.pow(a, b); // default impl. delegates to StrictMath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    /**
9269
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   671
     * Returns the closest {@code int} to the argument, with ties
19851
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   672
     * rounding to positive infinity.
9269
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   673
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * <ul><li>If the argument is NaN, the result is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * <li>If the argument is negative infinity or any value less than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * equal to the value of {@code Integer.MIN_VALUE}, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * equal to the value of {@code Integer.MIN_VALUE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * <li>If the argument is positive infinity or any value greater than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * equal to the value of {@code Integer.MAX_VALUE}, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * equal to the value of {@code Integer.MAX_VALUE}.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @param   a   a floating-point value to be rounded to an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * @return  the value of the argument rounded to the nearest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *          {@code int} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @see     java.lang.Integer#MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * @see     java.lang.Integer#MIN_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    public static int round(float a) {
19851
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   691
        int intBits = Float.floatToRawIntBits(a);
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   692
        int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   693
                >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   694
        int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   695
                + FloatConsts.EXP_BIAS) - biasedExp;
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   696
        if ((shift & -32) == 0) { // shift >= 0 && shift < 32
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   697
            // a is a finite number such that pow(2,-32) <= ulp(a) < 1
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   698
            int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   699
                    | (FloatConsts.SIGNIF_BIT_MASK + 1));
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   700
            if (intBits < 0) {
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   701
                r = -r;
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   702
            }
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   703
            // In the comments below each Java expression evaluates to the value
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   704
            // the corresponding mathematical expression:
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   705
            // (r) evaluates to a / ulp(a)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   706
            // (r >> shift) evaluates to floor(a * 2)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   707
            // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   708
            // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   709
            return ((r >> shift) + 1) >> 1;
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   710
        } else {
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   711
            // a is either
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   712
            // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   713
            // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   714
            // - an infinity or NaN
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   715
            return (int) a;
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   716
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    /**
9269
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   720
     * Returns the closest {@code long} to the argument, with ties
19851
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   721
     * rounding to positive infinity.
9269
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   722
     *
f66626469aa8 6430675: Math.round has surprising behavior for 0x1.fffffffffffffp-2
darcy
parents: 7668
diff changeset
   723
     * <p>Special cases:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * <ul><li>If the argument is NaN, the result is 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * <li>If the argument is negative infinity or any value less than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * equal to the value of {@code Long.MIN_VALUE}, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * equal to the value of {@code Long.MIN_VALUE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * <li>If the argument is positive infinity or any value greater than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * equal to the value of {@code Long.MAX_VALUE}, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * equal to the value of {@code Long.MAX_VALUE}.</ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @param   a   a floating-point value to be rounded to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     *          {@code long}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * @return  the value of the argument rounded to the nearest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     *          {@code long} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * @see     java.lang.Long#MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * @see     java.lang.Long#MIN_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    public static long round(double a) {
19851
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   740
        long longBits = Double.doubleToRawLongBits(a);
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   741
        long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   742
                >> (DoubleConsts.SIGNIFICAND_WIDTH - 1);
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   743
        long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   744
                + DoubleConsts.EXP_BIAS) - biasedExp;
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   745
        if ((shift & -64) == 0) { // shift >= 0 && shift < 64
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   746
            // a is a finite number such that pow(2,-64) <= ulp(a) < 1
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   747
            long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   748
                    | (DoubleConsts.SIGNIF_BIT_MASK + 1));
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   749
            if (longBits < 0) {
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   750
                r = -r;
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   751
            }
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   752
            // In the comments below each Java expression evaluates to the value
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   753
            // the corresponding mathematical expression:
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   754
            // (r) evaluates to a / ulp(a)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   755
            // (r >> shift) evaluates to floor(a * 2)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   756
            // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   757
            // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   758
            return ((r >> shift) + 1) >> 1;
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   759
        } else {
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   760
            // a is either
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   761
            // - a finite number with abs(a) < exp(2,DoubleConsts.SIGNIFICAND_WIDTH-64) < 1/2
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   762
            // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   763
            // - an infinity or NaN
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   764
            return (long) a;
7b6ff45c39ce 8010430: Math.round has surprising behavior for odd values of ulp 1
bpb
parents: 19583
diff changeset
   765
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
19583
828d85603705 6470700: Math.random() / Math.initRNG() uses "double checked locking"
bpb
parents: 19406
diff changeset
   768
    private static final class RandomNumberGeneratorHolder {
828d85603705 6470700: Math.random() / Math.initRNG() uses "double checked locking"
bpb
parents: 19406
diff changeset
   769
        static final Random randomNumberGenerator = new Random();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * Returns a {@code double} value with a positive sign, greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * than or equal to {@code 0.0} and less than {@code 1.0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * Returned values are chosen pseudorandomly with (approximately)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * uniform distribution from that range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * <p>When this method is first called, it creates a single new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * pseudorandom-number generator, exactly as if by the expression
5781
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   780
     *
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   781
     * <blockquote>{@code new java.util.Random()}</blockquote>
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   782
     *
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   783
     * This new pseudorandom-number generator is used thereafter for
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   784
     * all calls to this method and is used nowhere else.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * <p>This method is properly synchronized to allow correct use by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * more than one thread. However, if many threads need to generate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * pseudorandom numbers at a great rate, it may reduce contention
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * for each thread to have its own pseudorandom-number generator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     *
23745
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   791
     * @apiNote
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   792
     * As the largest {@code double} value less than {@code 1.0}
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   793
     * is {@code Math.nextDown(1.0)}, a value {@code x} in the closed range
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   794
     * {@code [x1,x2]} where {@code x1<=x2} may be defined by the statements
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   795
     *
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   796
     * <blockquote><pre>{@code
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   797
     * double f = Math.random()/Math.nextDown(1.0);
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   798
     * double x = x1*(1.0 - f) + x2*f;
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   799
     * }</pre></blockquote>
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   800
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * @return  a pseudorandom {@code double} greater than or equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * to {@code 0.0} and less than {@code 1.0}.
23745
7898c52fcfb4 8035427: Math.random() JavaDoc: missing maximum returned value
bpb
parents: 19851
diff changeset
   803
     * @see #nextDown(double)
5781
11a42d91eb56 6959259: Minor improvements to static Random field caching
martin
parents: 5506
diff changeset
   804
     * @see Random#nextDouble()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    public static double random() {
19583
828d85603705 6470700: Math.random() / Math.initRNG() uses "double checked locking"
bpb
parents: 19406
diff changeset
   807
        return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    /**
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   811
     * Returns the sum of its arguments,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   812
     * throwing an exception if the result overflows an {@code int}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   813
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   814
     * @param x the first value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   815
     * @param y the second value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   816
     * @return the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   817
     * @throws ArithmeticException if the result overflows an int
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
   818
     * @since 1.8
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   819
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   820
    @HotSpotIntrinsicCandidate
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   821
    public static int addExact(int x, int y) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   822
        int r = x + y;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   823
        // HD 2-12 Overflow iff both arguments have the opposite sign of the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   824
        if (((x ^ r) & (y ^ r)) < 0) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   825
            throw new ArithmeticException("integer overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   826
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   827
        return r;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   828
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   829
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   830
    /**
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   831
     * Returns the sum of its arguments,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   832
     * throwing an exception if the result overflows a {@code long}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   833
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   834
     * @param x the first value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   835
     * @param y the second value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   836
     * @return the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   837
     * @throws ArithmeticException if the result overflows a long
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
   838
     * @since 1.8
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   839
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   840
    @HotSpotIntrinsicCandidate
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   841
    public static long addExact(long x, long y) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   842
        long r = x + y;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   843
        // HD 2-12 Overflow iff both arguments have the opposite sign of the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   844
        if (((x ^ r) & (y ^ r)) < 0) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   845
            throw new ArithmeticException("long overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   846
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   847
        return r;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   848
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   849
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   850
    /**
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   851
     * Returns the difference of the arguments,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   852
     * throwing an exception if the result overflows an {@code int}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   853
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   854
     * @param x the first value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   855
     * @param y the second value to subtract from the first
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   856
     * @return the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   857
     * @throws ArithmeticException if the result overflows an int
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
   858
     * @since 1.8
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   859
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   860
    @HotSpotIntrinsicCandidate
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   861
    public static int subtractExact(int x, int y) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   862
        int r = x - y;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   863
        // HD 2-12 Overflow iff the arguments have different signs and
38455
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
   864
        // the sign of the result is different from the sign of x
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   865
        if (((x ^ y) & (x ^ r)) < 0) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   866
            throw new ArithmeticException("integer overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   867
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   868
        return r;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   869
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   870
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   871
    /**
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   872
     * Returns the difference of the arguments,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   873
     * throwing an exception if the result overflows a {@code long}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   874
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   875
     * @param x the first value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   876
     * @param y the second value to subtract from the first
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   877
     * @return the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   878
     * @throws ArithmeticException if the result overflows a long
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
   879
     * @since 1.8
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   880
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   881
    @HotSpotIntrinsicCandidate
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   882
    public static long subtractExact(long x, long y) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   883
        long r = x - y;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   884
        // HD 2-12 Overflow iff the arguments have different signs and
38455
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
   885
        // the sign of the result is different from the sign of x
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   886
        if (((x ^ y) & (x ^ r)) < 0) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   887
            throw new ArithmeticException("long overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   888
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   889
        return r;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   890
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   891
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   892
    /**
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   893
     * Returns the product of the arguments,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   894
     * throwing an exception if the result overflows an {@code int}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   895
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   896
     * @param x the first value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   897
     * @param y the second value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   898
     * @return the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   899
     * @throws ArithmeticException if the result overflows an int
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
   900
     * @since 1.8
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   901
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   902
    @HotSpotIntrinsicCandidate
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   903
    public static int multiplyExact(int x, int y) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   904
        long r = (long)x * (long)y;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   905
        if ((int)r != r) {
19406
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   906
            throw new ArithmeticException("integer overflow");
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   907
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   908
        return (int)r;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   909
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   910
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   911
    /**
38455
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
   912
     * Returns the product of the arguments, throwing an exception if the result
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
   913
     * overflows a {@code long}.
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
   914
     *
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
   915
     * @param x the first value
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
   916
     * @param y the second value
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
   917
     * @return the result
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
   918
     * @throws ArithmeticException if the result overflows a long
38460
f521277035db 8157527: Replace @since 1.9 with @since 9 on new math methods
darcy
parents: 38455
diff changeset
   919
     * @since 9
38455
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
   920
     */
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
   921
    public static long multiplyExact(long x, int y) {
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
   922
        return multiplyExact(x, (long)y);
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
   923
    }
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
   924
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
   925
    /**
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   926
     * Returns the product of the arguments,
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   927
     * throwing an exception if the result overflows a {@code long}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   928
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   929
     * @param x the first value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   930
     * @param y the second value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   931
     * @return the result
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   932
     * @throws ArithmeticException if the result overflows a long
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
   933
     * @since 1.8
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   934
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   935
    @HotSpotIntrinsicCandidate
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   936
    public static long multiplyExact(long x, long y) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   937
        long r = x * y;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   938
        long ax = Math.abs(x);
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   939
        long ay = Math.abs(y);
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   940
        if (((ax | ay) >>> 31 != 0)) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   941
            // Some bits greater than 2^31 that might cause overflow
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   942
            // Check the result using the divide operator
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   943
            // and check for the special case of Long.MIN_VALUE * -1
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   944
           if (((y != 0) && (r / y != x)) ||
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   945
               (x == Long.MIN_VALUE && y == -1)) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   946
                throw new ArithmeticException("long overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   947
            }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   948
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   949
        return r;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   950
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   951
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
   952
    /**
19406
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   953
     * Returns the argument incremented by one, throwing an exception if the
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   954
     * result overflows an {@code int}.
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   955
     *
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   956
     * @param a the value to increment
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   957
     * @return the result
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   958
     * @throws ArithmeticException if the result overflows an int
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   959
     * @since 1.8
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   960
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   961
    @HotSpotIntrinsicCandidate
19406
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   962
    public static int incrementExact(int a) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   963
        if (a == Integer.MAX_VALUE) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   964
            throw new ArithmeticException("integer overflow");
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   965
        }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   966
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   967
        return a + 1;
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   968
    }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   969
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   970
    /**
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   971
     * Returns the argument incremented by one, throwing an exception if the
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   972
     * result overflows a {@code long}.
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   973
     *
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   974
     * @param a the value to increment
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   975
     * @return the result
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   976
     * @throws ArithmeticException if the result overflows a long
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   977
     * @since 1.8
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   978
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   979
    @HotSpotIntrinsicCandidate
19406
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   980
    public static long incrementExact(long a) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   981
        if (a == Long.MAX_VALUE) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   982
            throw new ArithmeticException("long overflow");
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   983
        }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   984
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   985
        return a + 1L;
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   986
    }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   987
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   988
    /**
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   989
     * Returns the argument decremented by one, throwing an exception if the
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   990
     * result overflows an {@code int}.
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   991
     *
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   992
     * @param a the value to decrement
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   993
     * @return the result
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   994
     * @throws ArithmeticException if the result overflows an int
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   995
     * @since 1.8
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   996
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   997
    @HotSpotIntrinsicCandidate
19406
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   998
    public static int decrementExact(int a) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
   999
        if (a == Integer.MIN_VALUE) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1000
            throw new ArithmeticException("integer overflow");
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1001
        }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1002
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1003
        return a - 1;
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1004
    }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1005
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1006
    /**
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1007
     * Returns the argument decremented by one, throwing an exception if the
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1008
     * result overflows a {@code long}.
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1009
     *
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1010
     * @param a the value to decrement
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1011
     * @return the result
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1012
     * @throws ArithmeticException if the result overflows a long
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1013
     * @since 1.8
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1014
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
  1015
    @HotSpotIntrinsicCandidate
19406
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1016
    public static long decrementExact(long a) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1017
        if (a == Long.MIN_VALUE) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1018
            throw new ArithmeticException("long overflow");
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1019
        }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1020
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1021
        return a - 1L;
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1022
    }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1023
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1024
    /**
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1025
     * Returns the negation of the argument, throwing an exception if the
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1026
     * result overflows an {@code int}.
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1027
     *
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1028
     * @param a the value to negate
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1029
     * @return the result
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1030
     * @throws ArithmeticException if the result overflows an int
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1031
     * @since 1.8
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1032
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
  1033
    @HotSpotIntrinsicCandidate
19406
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1034
    public static int negateExact(int a) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1035
        if (a == Integer.MIN_VALUE) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1036
            throw new ArithmeticException("integer overflow");
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1037
        }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1038
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1039
        return -a;
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1040
    }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1041
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1042
    /**
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1043
     * Returns the negation of the argument, throwing an exception if the
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1044
     * result overflows a {@code long}.
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1045
     *
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1046
     * @param a the value to negate
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1047
     * @return the result
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1048
     * @throws ArithmeticException if the result overflows a long
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1049
     * @since 1.8
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1050
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
  1051
    @HotSpotIntrinsicCandidate
19406
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1052
    public static long negateExact(long a) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1053
        if (a == Long.MIN_VALUE) {
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1054
            throw new ArithmeticException("long overflow");
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1055
        }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1056
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1057
        return -a;
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1058
    }
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1059
10093962bbf3 8022109: Evaluate adding incrementExact, decrementExact, negateExact to java.lang.Math
bpb
parents: 14420
diff changeset
  1060
    /**
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1061
     * Returns the value of the {@code long} argument;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1062
     * throwing an exception if the value overflows an {@code int}.
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1063
     *
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1064
     * @param value the long value
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1065
     * @return the argument as an int
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1066
     * @throws ArithmeticException if the {@code argument} overflows an int
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1067
     * @since 1.8
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1068
     */
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1069
    public static int toIntExact(long value) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1070
        if ((int)value != value) {
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1071
            throw new ArithmeticException("integer overflow");
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1072
        }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1073
        return (int)value;
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1074
    }
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1075
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  1076
    /**
38452
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1077
     * Returns the exact mathematical product of the arguments.
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1078
     *
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1079
     * @param x the first value
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1080
     * @param y the second value
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1081
     * @return the result
44263
7a9297d467e7 8176563: @since value errors in apis of java.base/java.logging module
mli
parents: 41362
diff changeset
  1082
     * @since 9
38452
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1083
     */
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1084
    public static long multiplyFull(int x, int y) {
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1085
        return (long)x * (long)y;
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1086
    }
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1087
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1088
    /**
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1089
     * Returns as a {@code long} the most significant 64 bits of the 128-bit
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1090
     * product of two 64-bit factors.
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1091
     *
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1092
     * @param x the first value
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1093
     * @param y the second value
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1094
     * @return the result
44263
7a9297d467e7 8176563: @since value errors in apis of java.base/java.logging module
mli
parents: 41362
diff changeset
  1095
     * @since 9
38452
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1096
     */
47587
bf73ca31add9 8187684: Intrinsify Math.multiplyHigh(long, long)
dpochepk
parents: 47216
diff changeset
  1097
    @HotSpotIntrinsicCandidate
38452
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1098
    public static long multiplyHigh(long x, long y) {
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1099
        if (x < 0 || y < 0) {
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1100
            // Use technique from section 8-2 of Henry S. Warren, Jr.,
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1101
            // Hacker's Delight (2nd ed.) (Addison Wesley, 2013), 173-174.
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1102
            long x1 = x >> 32;
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1103
            long x2 = x & 0xFFFFFFFFL;
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1104
            long y1 = y >> 32;
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1105
            long y2 = y & 0xFFFFFFFFL;
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1106
            long z2 = x2 * y2;
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1107
            long t = x1 * y2 + (z2 >>> 32);
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1108
            long z1 = t & 0xFFFFFFFFL;
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1109
            long z0 = t >> 32;
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1110
            z1 += x2 * y1;
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1111
            return x1 * y1 + z0 + (z1 >> 32);
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1112
        } else {
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1113
            // Use Karatsuba technique with two base 2^32 digits.
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1114
            long x1 = x >>> 32;
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1115
            long y1 = y >>> 32;
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1116
            long x2 = x & 0xFFFFFFFFL;
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1117
            long y2 = y & 0xFFFFFFFFL;
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1118
            long A = x1 * y1;
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1119
            long B = x2 * y2;
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1120
            long C = (x1 + x2) * (y1 + y2);
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1121
            long K = C - A - B;
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1122
            return (((B >>> 32) + K) >>> 32) + A;
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1123
        }
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1124
    }
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1125
ca210bc11ed7 5100935: No way to access the 64-bit integer multiplication of 64-bit CPUs efficiently
bpb
parents: 37364
diff changeset
  1126
    /**
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1127
     * Returns the largest (closest to positive infinity)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1128
     * {@code int} value that is less than or equal to the algebraic quotient.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1129
     * There is one special case, if the dividend is the
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1130
     * {@linkplain Integer#MIN_VALUE Integer.MIN_VALUE} and the divisor is {@code -1},
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1131
     * then integer overflow occurs and
38455
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1132
     * the result is equal to {@code Integer.MIN_VALUE}.
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1133
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1134
     * Normal integer division operates under the round to zero rounding mode
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1135
     * (truncation).  This operation instead acts under the round toward
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1136
     * negative infinity (floor) rounding mode.
38455
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1137
     * The floor rounding mode gives different results from truncation
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1138
     * when the exact result is negative.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1139
     * <ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1140
     *   <li>If the signs of the arguments are the same, the results of
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1141
     *       {@code floorDiv} and the {@code /} operator are the same.  <br>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1142
     *       For example, {@code floorDiv(4, 3) == 1} and {@code (4 / 3) == 1}.</li>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1143
     *   <li>If the signs of the arguments are different,  the quotient is negative and
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1144
     *       {@code floorDiv} returns the integer less than or equal to the quotient
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1145
     *       and the {@code /} operator returns the integer closest to zero.<br>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1146
     *       For example, {@code floorDiv(-4, 3) == -2},
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1147
     *       whereas {@code (-4 / 3) == -1}.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1148
     *   </li>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1149
     * </ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1150
     *
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1151
     * @param x the dividend
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1152
     * @param y the divisor
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1153
     * @return the largest (closest to positive infinity)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1154
     * {@code int} value that is less than or equal to the algebraic quotient.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1155
     * @throws ArithmeticException if the divisor {@code y} is zero
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1156
     * @see #floorMod(int, int)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1157
     * @see #floor(double)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1158
     * @since 1.8
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1159
     */
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1160
    public static int floorDiv(int x, int y) {
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1161
        int r = x / y;
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1162
        // if the signs are different and modulo not zero, round down
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1163
        if ((x ^ y) < 0 && (r * y != x)) {
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1164
            r--;
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1165
        }
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1166
        return r;
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1167
    }
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1168
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1169
    /**
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1170
     * Returns the largest (closest to positive infinity)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1171
     * {@code long} value that is less than or equal to the algebraic quotient.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1172
     * There is one special case, if the dividend is the
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1173
     * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1},
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1174
     * then integer overflow occurs and
38455
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1175
     * the result is equal to {@code Long.MIN_VALUE}.
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1176
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1177
     * Normal integer division operates under the round to zero rounding mode
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1178
     * (truncation).  This operation instead acts under the round toward
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1179
     * negative infinity (floor) rounding mode.
38455
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1180
     * The floor rounding mode gives different results from truncation
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1181
     * when the exact result is negative.
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1182
     * <p>
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1183
     * For examples, see {@link #floorDiv(int, int)}.
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1184
     *
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1185
     * @param x the dividend
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1186
     * @param y the divisor
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1187
     * @return the largest (closest to positive infinity)
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1188
     * {@code int} value that is less than or equal to the algebraic quotient.
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1189
     * @throws ArithmeticException if the divisor {@code y} is zero
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1190
     * @see #floorMod(long, int)
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1191
     * @see #floor(double)
38460
f521277035db 8157527: Replace @since 1.9 with @since 9 on new math methods
darcy
parents: 38455
diff changeset
  1192
     * @since 9
38455
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1193
     */
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1194
    public static long floorDiv(long x, int y) {
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1195
        return floorDiv(x, (long)y);
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1196
    }
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1197
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1198
    /**
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1199
     * Returns the largest (closest to positive infinity)
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1200
     * {@code long} value that is less than or equal to the algebraic quotient.
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1201
     * There is one special case, if the dividend is the
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1202
     * {@linkplain Long#MIN_VALUE Long.MIN_VALUE} and the divisor is {@code -1},
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1203
     * then integer overflow occurs and
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1204
     * the result is equal to {@code Long.MIN_VALUE}.
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1205
     * <p>
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1206
     * Normal integer division operates under the round to zero rounding mode
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1207
     * (truncation).  This operation instead acts under the round toward
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1208
     * negative infinity (floor) rounding mode.
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1209
     * The floor rounding mode gives different results from truncation
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1210
     * when the exact result is negative.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1211
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1212
     * For examples, see {@link #floorDiv(int, int)}.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1213
     *
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1214
     * @param x the dividend
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1215
     * @param y the divisor
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1216
     * @return the largest (closest to positive infinity)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1217
     * {@code long} value that is less than or equal to the algebraic quotient.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1218
     * @throws ArithmeticException if the divisor {@code y} is zero
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1219
     * @see #floorMod(long, long)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1220
     * @see #floor(double)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1221
     * @since 1.8
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1222
     */
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1223
    public static long floorDiv(long x, long y) {
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1224
        long r = x / y;
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1225
        // if the signs are different and modulo not zero, round down
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1226
        if ((x ^ y) < 0 && (r * y != x)) {
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1227
            r--;
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1228
        }
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1229
        return r;
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1230
    }
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1231
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1232
    /**
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1233
     * Returns the floor modulus of the {@code int} arguments.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1234
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1235
     * The floor modulus is {@code x - (floorDiv(x, y) * y)},
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1236
     * has the same sign as the divisor {@code y}, and
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1237
     * is in the range of {@code -abs(y) < r < +abs(y)}.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1238
     *
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1239
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1240
     * The relationship between {@code floorDiv} and {@code floorMod} is such that:
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1241
     * <ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1242
     *   <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1243
     * </ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1244
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1245
     * The difference in values between {@code floorMod} and
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1246
     * the {@code %} operator is due to the difference between
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1247
     * {@code floorDiv} that returns the integer less than or equal to the quotient
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1248
     * and the {@code /} operator that returns the integer closest to zero.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1249
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1250
     * Examples:
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1251
     * <ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1252
     *   <li>If the signs of the arguments are the same, the results
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1253
     *       of {@code floorMod} and the {@code %} operator are the same.  <br>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1254
     *       <ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1255
     *       <li>{@code floorMod(4, 3) == 1}; &nbsp; and {@code (4 % 3) == 1}</li>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1256
     *       </ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1257
     *   <li>If the signs of the arguments are different, the results differ from the {@code %} operator.<br>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1258
     *      <ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1259
     *      <li>{@code floorMod(+4, -3) == -2}; &nbsp; and {@code (+4 % -3) == +1} </li>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1260
     *      <li>{@code floorMod(-4, +3) == +2}; &nbsp; and {@code (-4 % +3) == -1} </li>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1261
     *      <li>{@code floorMod(-4, -3) == -1}; &nbsp; and {@code (-4 % -3) == -1 } </li>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1262
     *      </ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1263
     *   </li>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1264
     * </ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1265
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1266
     * If the signs of arguments are unknown and a positive modulus
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1267
     * is needed it can be computed as {@code (floorMod(x, y) + abs(y)) % abs(y)}.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1268
     *
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1269
     * @param x the dividend
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1270
     * @param y the divisor
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1271
     * @return the floor modulus {@code x - (floorDiv(x, y) * y)}
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1272
     * @throws ArithmeticException if the divisor {@code y} is zero
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1273
     * @see #floorDiv(int, int)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1274
     * @since 1.8
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1275
     */
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1276
    public static int floorMod(int x, int y) {
54492
9d0ae9508d53 8222029: Optimize Math.floorMod
redestad
parents: 53041
diff changeset
  1277
        int mod = x % y;
9d0ae9508d53 8222029: Optimize Math.floorMod
redestad
parents: 53041
diff changeset
  1278
        // if the signs are different and modulo not zero, adjust result
9d0ae9508d53 8222029: Optimize Math.floorMod
redestad
parents: 53041
diff changeset
  1279
        if ((mod ^ y) < 0 && mod != 0) {
9d0ae9508d53 8222029: Optimize Math.floorMod
redestad
parents: 53041
diff changeset
  1280
            mod += y;
9d0ae9508d53 8222029: Optimize Math.floorMod
redestad
parents: 53041
diff changeset
  1281
        }
9d0ae9508d53 8222029: Optimize Math.floorMod
redestad
parents: 53041
diff changeset
  1282
        return mod;
38455
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1283
    }
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1284
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1285
    /**
44851
3439a92526a0 8179413: Fix remaining minor HTML5 issues in java.base module
jjg
parents: 44263
diff changeset
  1286
     * Returns the floor modulus of the {@code long} and {@code int} arguments.
38455
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1287
     * <p>
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1288
     * The floor modulus is {@code x - (floorDiv(x, y) * y)},
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1289
     * has the same sign as the divisor {@code y}, and
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1290
     * is in the range of {@code -abs(y) < r < +abs(y)}.
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1291
     *
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1292
     * <p>
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1293
     * The relationship between {@code floorDiv} and {@code floorMod} is such that:
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1294
     * <ul>
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1295
     *   <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1296
     * </ul>
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1297
     * <p>
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1298
     * For examples, see {@link #floorMod(int, int)}.
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1299
     *
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1300
     * @param x the dividend
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1301
     * @param y the divisor
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1302
     * @return the floor modulus {@code x - (floorDiv(x, y) * y)}
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1303
     * @throws ArithmeticException if the divisor {@code y} is zero
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1304
     * @see #floorDiv(long, int)
38460
f521277035db 8157527: Replace @since 1.9 with @since 9 on new math methods
darcy
parents: 38455
diff changeset
  1305
     * @since 9
38455
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1306
     */
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1307
    public static int floorMod(long x, int y) {
43fec67d51a3 8023217: Additional floorDiv/floorMod/multiplyExact methods for java.lang.Math
bpb
parents: 38452
diff changeset
  1308
        // Result cannot overflow the range of int.
54492
9d0ae9508d53 8222029: Optimize Math.floorMod
redestad
parents: 53041
diff changeset
  1309
        return (int)floorMod(x, (long)y);
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1310
    }
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1311
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1312
    /**
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1313
     * Returns the floor modulus of the {@code long} arguments.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1314
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1315
     * The floor modulus is {@code x - (floorDiv(x, y) * y)},
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1316
     * has the same sign as the divisor {@code y}, and
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1317
     * is in the range of {@code -abs(y) < r < +abs(y)}.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1318
     *
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1319
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1320
     * The relationship between {@code floorDiv} and {@code floorMod} is such that:
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1321
     * <ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1322
     *   <li>{@code floorDiv(x, y) * y + floorMod(x, y) == x}
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1323
     * </ul>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1324
     * <p>
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1325
     * For examples, see {@link #floorMod(int, int)}.
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1326
     *
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1327
     * @param x the dividend
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1328
     * @param y the divisor
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1329
     * @return the floor modulus {@code x - (floorDiv(x, y) * y)}
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1330
     * @throws ArithmeticException if the divisor {@code y} is zero
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1331
     * @see #floorDiv(long, long)
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1332
     * @since 1.8
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1333
     */
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1334
    public static long floorMod(long x, long y) {
54492
9d0ae9508d53 8222029: Optimize Math.floorMod
redestad
parents: 53041
diff changeset
  1335
        long mod = x % y;
9d0ae9508d53 8222029: Optimize Math.floorMod
redestad
parents: 53041
diff changeset
  1336
        // if the signs are different and modulo not zero, adjust result
9d0ae9508d53 8222029: Optimize Math.floorMod
redestad
parents: 53041
diff changeset
  1337
        if ((x ^ y) < 0 && mod != 0) {
9d0ae9508d53 8222029: Optimize Math.floorMod
redestad
parents: 53041
diff changeset
  1338
            mod += y;
9d0ae9508d53 8222029: Optimize Math.floorMod
redestad
parents: 53041
diff changeset
  1339
        }
9d0ae9508d53 8222029: Optimize Math.floorMod
redestad
parents: 53041
diff changeset
  1340
        return mod;
14420
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1341
    }
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1342
5cbeeccf4a40 6282196: There should be Math.mod(number, modulo) methods
sherman
parents: 14342
diff changeset
  1343
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * Returns the absolute value of an {@code int} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * If the argument is not negative, the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * If the argument is negative, the negation of the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * <p>Note that if the argument is equal to the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * {@link Integer#MIN_VALUE}, the most negative representable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * {@code int} value, the result is that same value, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * @param   a   the argument whose absolute value is to be determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * @return  the absolute value of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    public static int abs(int a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        return (a < 0) ? -a : a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * Returns the absolute value of a {@code long} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     * If the argument is not negative, the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * If the argument is negative, the negation of the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * <p>Note that if the argument is equal to the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * {@link Long#MIN_VALUE}, the most negative representable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * {@code long} value, the result is that same value, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * @param   a   the argument whose absolute value is to be determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * @return  the absolute value of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    public static long abs(long a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        return (a < 0) ? -a : a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * Returns the absolute value of a {@code float} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     * If the argument is not negative, the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     * If the argument is negative, the negation of the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     * <ul><li>If the argument is positive zero or negative zero, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * result is positive zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * <li>If the argument is infinite, the result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * <li>If the argument is NaN, the result is NaN.</ul>
40530
53bc022a137b 8164524: Correct inconsistencies in floating-point abs spec
darcy
parents: 39656
diff changeset
  1386
     *
53bc022a137b 8164524: Correct inconsistencies in floating-point abs spec
darcy
parents: 39656
diff changeset
  1387
     * @apiNote As implied by the above, one valid implementation of
53bc022a137b 8164524: Correct inconsistencies in floating-point abs spec
darcy
parents: 39656
diff changeset
  1388
     * this method is given by the expression below which computes a
53bc022a137b 8164524: Correct inconsistencies in floating-point abs spec
darcy
parents: 39656
diff changeset
  1389
     * {@code float} with the same exponent and significand as the
53bc022a137b 8164524: Correct inconsistencies in floating-point abs spec
darcy
parents: 39656
diff changeset
  1390
     * argument but with a guaranteed zero sign bit indicating a
53bc022a137b 8164524: Correct inconsistencies in floating-point abs spec
darcy
parents: 39656
diff changeset
  1391
     * positive value:<br>
53bc022a137b 8164524: Correct inconsistencies in floating-point abs spec
darcy
parents: 39656
diff changeset
  1392
     * {@code Float.intBitsToFloat(0x7fffffff & Float.floatToRawIntBits(a))}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * @param   a   the argument whose absolute value is to be determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * @return  the absolute value of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    public static float abs(float a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        return (a <= 0.0F) ? 0.0F - a : a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * Returns the absolute value of a {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * If the argument is not negative, the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * If the argument is negative, the negation of the argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     * <ul><li>If the argument is positive zero or negative zero, the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * is positive zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     * <li>If the argument is infinite, the result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     * <li>If the argument is NaN, the result is NaN.</ul>
40530
53bc022a137b 8164524: Correct inconsistencies in floating-point abs spec
darcy
parents: 39656
diff changeset
  1410
     *
53bc022a137b 8164524: Correct inconsistencies in floating-point abs spec
darcy
parents: 39656
diff changeset
  1411
     * @apiNote As implied by the above, one valid implementation of
53bc022a137b 8164524: Correct inconsistencies in floating-point abs spec
darcy
parents: 39656
diff changeset
  1412
     * this method is given by the expression below which computes a
53bc022a137b 8164524: Correct inconsistencies in floating-point abs spec
darcy
parents: 39656
diff changeset
  1413
     * {@code double} with the same exponent and significand as the
53bc022a137b 8164524: Correct inconsistencies in floating-point abs spec
darcy
parents: 39656
diff changeset
  1414
     * argument but with a guaranteed zero sign bit indicating a
53bc022a137b 8164524: Correct inconsistencies in floating-point abs spec
darcy
parents: 39656
diff changeset
  1415
     * positive value:<br>
53bc022a137b 8164524: Correct inconsistencies in floating-point abs spec
darcy
parents: 39656
diff changeset
  1416
     * {@code Double.longBitsToDouble((Double.doubleToRawLongBits(a)<<1)>>>1)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     * @param   a   the argument whose absolute value is to be determined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * @return  the absolute value of the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
  1421
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    public static double abs(double a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        return (a <= 0.0D) ? 0.0D - a : a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     * Returns the greater of two {@code int} values. That is, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     * result is the argument closer to the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * {@link Integer#MAX_VALUE}. If the arguments have the same value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * the result is that same value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * @return  the larger of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
  1436
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
    public static int max(int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        return (a >= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     * Returns the greater of two {@code long} values. That is, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     * result is the argument closer to the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
     * {@link Long#MAX_VALUE}. If the arguments have the same value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     * the result is that same value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * @return  the larger of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    public static long max(long a, long b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        return (a >= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
11512
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1455
    // Use raw bit-wise conversions on guaranteed non-NaN arguments.
44924
dbfcc317533b 8138672: Math. negativeZeroFloatBits and Math. negativeZeroDoubleBits should be final
darcy
parents: 44263
diff changeset
  1456
    private static final long negativeZeroFloatBits  = Float.floatToRawIntBits(-0.0f);
dbfcc317533b 8138672: Math. negativeZeroFloatBits and Math. negativeZeroDoubleBits should be final
darcy
parents: 44263
diff changeset
  1457
    private static final long negativeZeroDoubleBits = Double.doubleToRawLongBits(-0.0d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     * Returns the greater of two {@code float} values.  That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     * the result is the argument closer to positive infinity. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     * arguments have the same value, the result is that same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     * value. If either value is NaN, then the result is NaN.  Unlike
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     * the numerical comparison operators, this method considers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * negative zero to be strictly smaller than positive zero. If one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
     * argument is positive zero and the other negative zero, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * result is positive zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     * @return  the larger of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     */
53041
f15af1e2c683 8212043: Add floating-point Math.min/max intrinsics
pli
parents: 47587
diff changeset
  1473
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    public static float max(float a, float b) {
11512
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1475
        if (a != a)
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1476
            return a;   // a is NaN
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1477
        if ((a == 0.0f) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1478
            (b == 0.0f) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1479
            (Float.floatToRawIntBits(a) == negativeZeroFloatBits)) {
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1480
            // Raw conversion ok since NaN can't map to -0.0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        return (a >= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * Returns the greater of two {@code double} values.  That
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     * is, the result is the argument closer to positive infinity. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * the arguments have the same value, the result is that same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     * value. If either value is NaN, then the result is NaN.  Unlike
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     * the numerical comparison operators, this method considers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     * negative zero to be strictly smaller than positive zero. If one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * argument is positive zero and the other negative zero, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     * result is positive zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     * @return  the larger of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     */
53041
f15af1e2c683 8212043: Add floating-point Math.min/max intrinsics
pli
parents: 47587
diff changeset
  1500
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    public static double max(double a, double b) {
11512
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1502
        if (a != a)
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1503
            return a;   // a is NaN
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1504
        if ((a == 0.0d) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1505
            (b == 0.0d) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1506
            (Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1507
            // Raw conversion ok since NaN can't map to -0.0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        return (a >= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     * Returns the smaller of two {@code int} values. That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * the result the argument closer to the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     * {@link Integer#MIN_VALUE}.  If the arguments have the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * value, the result is that same value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
     * @return  the smaller of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
  1523
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
    public static int min(int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        return (a <= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     * Returns the smaller of two {@code long} values. That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * the result is the argument closer to the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * {@link Long#MIN_VALUE}. If the arguments have the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * value, the result is that same value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     * @return  the smaller of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
    public static long min(long a, long b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        return (a <= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     * Returns the smaller of two {@code float} values.  That is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     * the result is the value closer to negative infinity. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * arguments have the same value, the result is that same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     * value. If either value is NaN, then the result is NaN.  Unlike
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     * the numerical comparison operators, this method considers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     * negative zero to be strictly smaller than positive zero.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * one argument is positive zero and the other is negative zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     * the result is negative zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
     * @return  the smaller of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     */
53041
f15af1e2c683 8212043: Add floating-point Math.min/max intrinsics
pli
parents: 47587
diff changeset
  1556
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    public static float min(float a, float b) {
11512
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1558
        if (a != a)
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1559
            return a;   // a is NaN
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1560
        if ((a == 0.0f) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1561
            (b == 0.0f) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1562
            (Float.floatToRawIntBits(b) == negativeZeroFloatBits)) {
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1563
            // Raw conversion ok since NaN can't map to -0.0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        return (a <= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     * Returns the smaller of two {@code double} values.  That
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * is, the result is the value closer to negative infinity. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * arguments have the same value, the result is that same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * value. If either value is NaN, then the result is NaN.  Unlike
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * the numerical comparison operators, this method considers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * negative zero to be strictly smaller than positive zero. If one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     * argument is positive zero and the other is negative zero, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     * result is negative zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     * @param   a   an argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     * @param   b   another argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     * @return  the smaller of {@code a} and {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     */
53041
f15af1e2c683 8212043: Add floating-point Math.min/max intrinsics
pli
parents: 47587
diff changeset
  1583
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
    public static double min(double a, double b) {
11512
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1585
        if (a != a)
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1586
            return a;   // a is NaN
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1587
        if ((a == 0.0d) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1588
            (b == 0.0d) &&
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1589
            (Double.doubleToRawLongBits(b) == negativeZeroDoubleBits)) {
ac5944feab25 7128441: StrictMath performance improvement note shared with Math
darcy
parents: 11510
diff changeset
  1590
            // Raw conversion ok since NaN can't map to -0.0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        return (a <= b) ? a : b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
    /**
37364
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1597
     * Returns the fused multiply add of the three arguments; that is,
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1598
     * returns the exact product of the first two arguments summed
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1599
     * with the third argument and then rounded once to the nearest
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1600
     * {@code double}.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1601
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1602
     * The rounding is done using the {@linkplain
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1603
     * java.math.RoundingMode#HALF_EVEN round to nearest even
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1604
     * rounding mode}.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1605
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1606
     * In contrast, if {@code a * b + c} is evaluated as a regular
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1607
     * floating-point expression, two rounding errors are involved,
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1608
     * the first for the multiply operation, the second for the
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1609
     * addition operation.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1610
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1611
     * <p>Special cases:
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1612
     * <ul>
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1613
     * <li> If any argument is NaN, the result is NaN.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1614
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1615
     * <li> If one of the first two arguments is infinite and the
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1616
     * other is zero, the result is NaN.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1617
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1618
     * <li> If the exact product of the first two arguments is infinite
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1619
     * (in other words, at least one of the arguments is infinite and
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1620
     * the other is neither zero nor NaN) and the third argument is an
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1621
     * infinity of the opposite sign, the result is NaN.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1622
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1623
     * </ul>
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1624
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1625
     * <p>Note that {@code fma(a, 1.0, c)} returns the same
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1626
     * result as ({@code a + c}).  However,
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1627
     * {@code fma(a, b, +0.0)} does <em>not</em> always return the
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1628
     * same result as ({@code a * b}) since
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1629
     * {@code fma(-0.0, +0.0, +0.0)} is {@code +0.0} while
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1630
     * ({@code -0.0 * +0.0}) is {@code -0.0}; {@code fma(a, b, -0.0)} is
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1631
     * equivalent to ({@code a * b}) however.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1632
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1633
     * @apiNote This method corresponds to the fusedMultiplyAdd
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1634
     * operation defined in IEEE 754-2008.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1635
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1636
     * @param a a value
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1637
     * @param b a value
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1638
     * @param c a value
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1639
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1640
     * @return (<i>a</i>&nbsp;&times;&nbsp;<i>b</i>&nbsp;+&nbsp;<i>c</i>)
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1641
     * computed, as if with unlimited range and precision, and rounded
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1642
     * once to the nearest {@code double} value
39656
25f808289769 8161413: Math.fma javadoc doesn't have @since 9
bpb
parents: 38460
diff changeset
  1643
     *
25f808289769 8161413: Math.fma javadoc doesn't have @since 9
bpb
parents: 38460
diff changeset
  1644
     * @since 9
37364
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1645
     */
41362
e09e871860a7 8154122: Intrinsify fused mac operations
vdeshpande
parents: 40530
diff changeset
  1646
    @HotSpotIntrinsicCandidate
37364
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1647
    public static double fma(double a, double b, double c) {
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1648
        /*
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1649
         * Infinity and NaN arithmetic is not quite the same with two
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1650
         * roundings as opposed to just one so the simple expression
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1651
         * "a * b + c" cannot always be used to compute the correct
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1652
         * result.  With two roundings, the product can overflow and
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1653
         * if the addend is infinite, a spurious NaN can be produced
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1654
         * if the infinity from the overflow and the infinite addend
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1655
         * have opposite signs.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1656
         */
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1657
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1658
        // First, screen for and handle non-finite input values whose
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1659
        // arithmetic is not supported by BigDecimal.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1660
        if (Double.isNaN(a) || Double.isNaN(b) || Double.isNaN(c)) {
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1661
            return Double.NaN;
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1662
        } else { // All inputs non-NaN
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1663
            boolean infiniteA = Double.isInfinite(a);
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1664
            boolean infiniteB = Double.isInfinite(b);
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1665
            boolean infiniteC = Double.isInfinite(c);
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1666
            double result;
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1667
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1668
            if (infiniteA || infiniteB || infiniteC) {
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1669
                if (infiniteA && b == 0.0 ||
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1670
                    infiniteB && a == 0.0 ) {
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1671
                    return Double.NaN;
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1672
                }
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1673
                // Store product in a double field to cause an
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1674
                // overflow even if non-strictfp evaluation is being
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1675
                // used.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1676
                double product = a * b;
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1677
                if (Double.isInfinite(product) && !infiniteA && !infiniteB) {
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1678
                    // Intermediate overflow; might cause a
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1679
                    // spurious NaN if added to infinite c.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1680
                    assert Double.isInfinite(c);
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1681
                    return c;
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1682
                } else {
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1683
                    result = product + c;
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1684
                    assert !Double.isFinite(result);
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1685
                    return result;
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1686
                }
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1687
            } else { // All inputs finite
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1688
                BigDecimal product = (new BigDecimal(a)).multiply(new BigDecimal(b));
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1689
                if (c == 0.0) { // Positive or negative zero
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1690
                    // If the product is an exact zero, use a
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1691
                    // floating-point expression to compute the sign
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1692
                    // of the zero final result. The product is an
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1693
                    // exact zero if and only if at least one of a and
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1694
                    // b is zero.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1695
                    if (a == 0.0 || b == 0.0) {
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1696
                        return a * b + c;
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1697
                    } else {
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1698
                        // The sign of a zero addend doesn't matter if
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1699
                        // the product is nonzero. The sign of a zero
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1700
                        // addend is not factored in the result if the
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1701
                        // exact product is nonzero but underflows to
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1702
                        // zero; see IEEE-754 2008 section 6.3 "The
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1703
                        // sign bit".
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1704
                        return product.doubleValue();
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1705
                    }
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1706
                } else {
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1707
                    return product.add(new BigDecimal(c)).doubleValue();
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1708
                }
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1709
            }
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1710
        }
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1711
    }
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1712
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1713
    /**
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1714
     * Returns the fused multiply add of the three arguments; that is,
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1715
     * returns the exact product of the first two arguments summed
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1716
     * with the third argument and then rounded once to the nearest
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1717
     * {@code float}.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1718
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1719
     * The rounding is done using the {@linkplain
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1720
     * java.math.RoundingMode#HALF_EVEN round to nearest even
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1721
     * rounding mode}.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1722
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1723
     * In contrast, if {@code a * b + c} is evaluated as a regular
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1724
     * floating-point expression, two rounding errors are involved,
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1725
     * the first for the multiply operation, the second for the
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1726
     * addition operation.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1727
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1728
     * <p>Special cases:
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1729
     * <ul>
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1730
     * <li> If any argument is NaN, the result is NaN.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1731
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1732
     * <li> If one of the first two arguments is infinite and the
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1733
     * other is zero, the result is NaN.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1734
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1735
     * <li> If the exact product of the first two arguments is infinite
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1736
     * (in other words, at least one of the arguments is infinite and
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1737
     * the other is neither zero nor NaN) and the third argument is an
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1738
     * infinity of the opposite sign, the result is NaN.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1739
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1740
     * </ul>
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1741
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1742
     * <p>Note that {@code fma(a, 1.0f, c)} returns the same
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1743
     * result as ({@code a + c}).  However,
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1744
     * {@code fma(a, b, +0.0f)} does <em>not</em> always return the
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1745
     * same result as ({@code a * b}) since
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1746
     * {@code fma(-0.0f, +0.0f, +0.0f)} is {@code +0.0f} while
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1747
     * ({@code -0.0f * +0.0f}) is {@code -0.0f}; {@code fma(a, b, -0.0f)} is
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1748
     * equivalent to ({@code a * b}) however.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1749
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1750
     * @apiNote This method corresponds to the fusedMultiplyAdd
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1751
     * operation defined in IEEE 754-2008.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1752
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1753
     * @param a a value
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1754
     * @param b a value
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1755
     * @param c a value
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1756
     *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1757
     * @return (<i>a</i>&nbsp;&times;&nbsp;<i>b</i>&nbsp;+&nbsp;<i>c</i>)
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1758
     * computed, as if with unlimited range and precision, and rounded
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1759
     * once to the nearest {@code float} value
39656
25f808289769 8161413: Math.fma javadoc doesn't have @since 9
bpb
parents: 38460
diff changeset
  1760
     *
25f808289769 8161413: Math.fma javadoc doesn't have @since 9
bpb
parents: 38460
diff changeset
  1761
     * @since 9
37364
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1762
     */
41362
e09e871860a7 8154122: Intrinsify fused mac operations
vdeshpande
parents: 40530
diff changeset
  1763
    @HotSpotIntrinsicCandidate
37364
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1764
    public static float fma(float a, float b, float c) {
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1765
        /*
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1766
         *  Since the double format has more than twice the precision
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1767
         *  of the float format, the multiply of a * b is exact in
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1768
         *  double. The add of c to the product then incurs one
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1769
         *  rounding error. Since the double format moreover has more
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1770
         *  than (2p + 2) precision bits compared to the p bits of the
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1771
         *  float format, the two roundings of (a * b + c), first to
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1772
         *  the double format and then secondarily to the float format,
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1773
         *  are equivalent to rounding the intermediate result directly
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1774
         *  to the float format.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1775
         *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1776
         * In terms of strictfp vs default-fp concerns related to
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1777
         * overflow and underflow, since
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1778
         *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1779
         * (Float.MAX_VALUE * Float.MAX_VALUE) << Double.MAX_VALUE
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1780
         * (Float.MIN_VALUE * Float.MIN_VALUE) >> Double.MIN_VALUE
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1781
         *
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1782
         * neither the multiply nor add will overflow or underflow in
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1783
         * double. Therefore, it is not necessary for this method to
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1784
         * be declared strictfp to have reproducible
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1785
         * behavior. However, it is necessary to explicitly store down
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1786
         * to a float variable to avoid returning a value in the float
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1787
         * extended value set.
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1788
         */
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1789
        float result = (float)(((double) a * (double) b ) + (double) c);
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1790
        return result;
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1791
    }
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1792
80be215c8c51 4851642: Add fused multiply add to Java math library
darcy
parents: 35979
diff changeset
  1793
    /**
10122
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1794
     * Returns the size of an ulp of the argument.  An ulp, unit in
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1795
     * the last place, of a {@code double} value is the positive
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1796
     * distance between this floating-point value and the {@code
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1797
     * double} value next larger in magnitude.  Note that for non-NaN
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1798
     * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     * <li> If the argument is positive or negative infinity, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
     * result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     * <li> If the argument is positive or negative zero, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
     * {@code Double.MIN_VALUE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     * <li> If the argument is &plusmn;{@code Double.MAX_VALUE}, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
     * the result is equal to 2<sup>971</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     * @param d the floating-point value whose ulp is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     * @return the size of an ulp of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
    public static double ulp(double d) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1817
        int exp = getExponent(d);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1818
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1819
        switch(exp) {
35979
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
  1820
        case Double.MAX_EXPONENT + 1:       // NaN or infinity
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1821
            return Math.abs(d);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1822
35979
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
  1823
        case Double.MIN_EXPONENT - 1:       // zero or subnormal
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1824
            return Double.MIN_VALUE;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1825
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1826
        default:
35979
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
  1827
            assert exp <= Double.MAX_EXPONENT && exp >= Double.MIN_EXPONENT;
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1828
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1829
            // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1830
            exp = exp - (DoubleConsts.SIGNIFICAND_WIDTH-1);
35979
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
  1831
            if (exp >= Double.MIN_EXPONENT) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1832
                return powerOfTwoD(exp);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1833
            }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1834
            else {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1835
                // return a subnormal result; left shift integer
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1836
                // representation of Double.MIN_VALUE appropriate
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1837
                // number of positions
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1838
                return Double.longBitsToDouble(1L <<
35979
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
  1839
                (exp - (Double.MIN_EXPONENT - (DoubleConsts.SIGNIFICAND_WIDTH-1)) ));
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1840
            }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1841
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
    /**
10122
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1845
     * Returns the size of an ulp of the argument.  An ulp, unit in
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1846
     * the last place, of a {@code float} value is the positive
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1847
     * distance between this floating-point value and the {@code
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1848
     * float} value next larger in magnitude.  Note that for non-NaN
bf8e763fcceb 7062430: Minor inconsistency in ulp descriptions
darcy
parents: 9269
diff changeset
  1849
     * <i>x</i>, <code>ulp(-<i>x</i>) == ulp(<i>x</i>)</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     * <li> If the argument is positive or negative infinity, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     * result is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     * <li> If the argument is positive or negative zero, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     * {@code Float.MIN_VALUE}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     * <li> If the argument is &plusmn;{@code Float.MAX_VALUE}, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     * the result is equal to 2<sup>104</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     * @param f the floating-point value whose ulp is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     * @return the size of an ulp of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
    public static float ulp(float f) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1868
        int exp = getExponent(f);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1869
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1870
        switch(exp) {
35979
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
  1871
        case Float.MAX_EXPONENT+1:        // NaN or infinity
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1872
            return Math.abs(f);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1873
35979
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
  1874
        case Float.MIN_EXPONENT-1:        // zero or subnormal
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
  1875
            return Float.MIN_VALUE;
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1876
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1877
        default:
35979
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
  1878
            assert exp <= Float.MAX_EXPONENT && exp >= Float.MIN_EXPONENT;
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1879
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1880
            // ulp(x) is usually 2^(SIGNIFICAND_WIDTH-1)*(2^ilogb(x))
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1881
            exp = exp - (FloatConsts.SIGNIFICAND_WIDTH-1);
35979
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
  1882
            if (exp >= Float.MIN_EXPONENT) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1883
                return powerOfTwoF(exp);
35979
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
  1884
            } else {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1885
                // return a subnormal result; left shift integer
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1886
                // representation of FloatConsts.MIN_VALUE appropriate
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1887
                // number of positions
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1888
                return Float.intBitsToFloat(1 <<
35979
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
  1889
                (exp - (Float.MIN_EXPONENT - (FloatConsts.SIGNIFICAND_WIDTH-1)) ));
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1890
            }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1891
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
     * Returns the signum function of the argument; zero if the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     * is zero, 1.0 if the argument is greater than zero, -1.0 if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     * argument is less than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     * <li> If the argument is positive zero or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     *      result is the same as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     * @param d the floating-point value whose signum is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     * @return the signum function of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
    public static double signum(double d) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1912
        return (d == 0.0 || Double.isNaN(d))?d:copySign(1.0, d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     * Returns the signum function of the argument; zero if the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
     * is zero, 1.0f if the argument is greater than zero, -1.0f if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
     * argument is less than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
     * <li> If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
     * <li> If the argument is positive zero or negative zero, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
     *      result is the same as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     * @param f the floating-point value whose signum is to be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
     * @return the signum function of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
     * @author Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
    public static float signum(float f) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  1933
        return (f == 0.0f || Float.isNaN(f))?f:copySign(1.0f, f);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     * Returns the hyperbolic sine of a {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     * The hyperbolic sine of <i>x</i> is defined to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     * (<i>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></i>)/2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     * where <i>e</i> is {@linkplain Math#E Euler's number}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     * <li>If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     * <li>If the argument is infinite, then the result is an infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     * with the same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
     * same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
     * <p>The computed result must be within 2.5 ulps of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
     * @param   x The number whose hyperbolic sine is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     * @return  The hyperbolic sine of {@code x}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
    public static double sinh(double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        return StrictMath.sinh(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     * Returns the hyperbolic cosine of a {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     * The hyperbolic cosine of <i>x</i> is defined to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     * (<i>e<sup>x</sup>&nbsp;+&nbsp;e<sup>-x</sup></i>)/2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
     * where <i>e</i> is {@linkplain Math#E Euler's number}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     * <li>If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     * <li>If the argument is infinite, then the result is positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     * <li>If the argument is zero, then the result is {@code 1.0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
     * <p>The computed result must be within 2.5 ulps of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
     * @param   x The number whose hyperbolic cosine is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
     * @return  The hyperbolic cosine of {@code x}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
    public static double cosh(double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        return StrictMath.cosh(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
     * Returns the hyperbolic tangent of a {@code double} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
     * The hyperbolic tangent of <i>x</i> is defined to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
     * (<i>e<sup>x</sup>&nbsp;-&nbsp;e<sup>-x</sup></i>)/(<i>e<sup>x</sup>&nbsp;+&nbsp;e<sup>-x</sup></i>),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
     * in other words, {@linkplain Math#sinh
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
     * sinh(<i>x</i>)}/{@linkplain Math#cosh cosh(<i>x</i>)}.  Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
     * that the absolute value of the exact tanh is always less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
     * 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
     * <li>If the argument is NaN, then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
     * same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
     * <li>If the argument is positive infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     * {@code +1.0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     * <li>If the argument is negative infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     * {@code -1.0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     * <p>The computed result must be within 2.5 ulps of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
     * The result of {@code tanh} for any finite input must have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
     * an absolute value less than or equal to 1.  Note that once the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
     * exact result of tanh is within 1/2 of an ulp of the limit value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
     * of &plusmn;1, correctly signed &plusmn;{@code 1.0} should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
     * be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
     * @param   x The number whose hyperbolic tangent is to be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     * @return  The hyperbolic tangent of {@code x}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
    public static double tanh(double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
        return StrictMath.tanh(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     * Returns sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     * without intermediate overflow or underflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     * <li> If either argument is infinite, then the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     * is positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
     * <li> If either argument is NaN and neither argument is infinite,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
     * then the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     * <p>The computed result must be within 1 ulp of the exact
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     * result.  If one parameter is held constant, the results must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
     * semi-monotonic in the other parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     * @param x a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     * @param y a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     * @return sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     * without intermediate overflow or underflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
    public static double hypot(double x, double y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
        return StrictMath.hypot(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     * Returns <i>e</i><sup>x</sup>&nbsp;-1.  Note that for values of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     * <i>x</i> near 0, the exact sum of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     * {@code expm1(x)}&nbsp;+&nbsp;1 is much closer to the true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     * result of <i>e</i><sup>x</sup> than {@code exp(x)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     * <li>If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     * <li>If the argument is positive infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     * <li>If the argument is negative infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
     * -1.0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     * same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
     * Results must be semi-monotonic.  The result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
     * {@code expm1} for any finite input must be greater than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
     * equal to {@code -1.0}.  Note that once the exact result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
     * <i>e</i><sup>{@code x}</sup>&nbsp;-&nbsp;1 is within 1/2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
     * ulp of the limit value -1, {@code -1.0} should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     * @param   x   the exponent to raise <i>e</i> to in the computation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     *              <i>e</i><sup>{@code x}</sup>&nbsp;-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     * @return  the value <i>e</i><sup>{@code x}</sup>&nbsp;-&nbsp;1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
    public static double expm1(double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
        return StrictMath.expm1(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
     * Returns the natural logarithm of the sum of the argument and 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     * Note that for small values {@code x}, the result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     * {@code log1p(x)} is much closer to the true result of ln(1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
     * + {@code x}) than the floating-point evaluation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     * {@code log(1.0+x)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
     * <li>If the argument is NaN or less than -1, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
     * NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     * <li>If the argument is positive infinity, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
     * <li>If the argument is negative one, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
     * negative infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
     * <li>If the argument is zero, then the result is a zero with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     * same sign as the argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
     * <p>The computed result must be within 1 ulp of the exact result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
     * Results must be semi-monotonic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
     * @param   x   a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     * @return the value ln({@code x}&nbsp;+&nbsp;1), the natural
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     * log of {@code x}&nbsp;+&nbsp;1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
    public static double log1p(double x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
        return StrictMath.log1p(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
     * Returns the first floating-point argument with the sign of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     * second floating-point argument.  Note that unlike the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
     * StrictMath#copySign(double, double) StrictMath.copySign}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
     * method, this method does not require NaN {@code sign}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     * arguments to be treated as positive values; implementations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     * permitted to treat some NaN arguments as positive and other NaN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
     * arguments as negative to allow greater performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
     * @param magnitude  the parameter providing the magnitude of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
     * @param sign   the parameter providing the sign of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
     * @return a value with the magnitude of {@code magnitude}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
     * and the sign of {@code sign}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
    public static double copySign(double magnitude, double sign) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2153
        return Double.longBitsToDouble((Double.doubleToRawLongBits(sign) &
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2154
                                        (DoubleConsts.SIGN_BIT_MASK)) |
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2155
                                       (Double.doubleToRawLongBits(magnitude) &
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2156
                                        (DoubleConsts.EXP_BIT_MASK |
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2157
                                         DoubleConsts.SIGNIF_BIT_MASK)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
     * Returns the first floating-point argument with the sign of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
     * second floating-point argument.  Note that unlike the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
     * StrictMath#copySign(float, float) StrictMath.copySign}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
     * method, this method does not require NaN {@code sign}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
     * arguments to be treated as positive values; implementations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
     * permitted to treat some NaN arguments as positive and other NaN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
     * arguments as negative to allow greater performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
     * @param magnitude  the parameter providing the magnitude of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
     * @param sign   the parameter providing the sign of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
     * @return a value with the magnitude of {@code magnitude}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
     * and the sign of {@code sign}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
    public static float copySign(float magnitude, float sign) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2176
        return Float.intBitsToFloat((Float.floatToRawIntBits(sign) &
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2177
                                     (FloatConsts.SIGN_BIT_MASK)) |
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2178
                                    (Float.floatToRawIntBits(magnitude) &
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2179
                                     (FloatConsts.EXP_BIT_MASK |
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2180
                                      FloatConsts.SIGNIF_BIT_MASK)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
     * Returns the unbiased exponent used in the representation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
     * {@code float}.  Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
     * <li>If the argument is NaN or infinite, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
     * {@link Float#MAX_EXPONENT} + 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
     * <li>If the argument is zero or subnormal, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
     * {@link Float#MIN_EXPONENT} -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
     * @param f a {@code float} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
     * @return the unbiased exponent of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
    public static int getExponent(float f) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2198
        /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2199
         * Bitwise convert f to integer, mask out exponent bits, shift
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2200
         * to the right and then subtract out float's bias adjust to
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2201
         * get true exponent value
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2202
         */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2203
        return ((Float.floatToRawIntBits(f) & FloatConsts.EXP_BIT_MASK) >>
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2204
                (FloatConsts.SIGNIFICAND_WIDTH - 1)) - FloatConsts.EXP_BIAS;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
     * Returns the unbiased exponent used in the representation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
     * {@code double}.  Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
     * <li>If the argument is NaN or infinite, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
     * {@link Double#MAX_EXPONENT} + 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
     * <li>If the argument is zero or subnormal, then the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
     * {@link Double#MIN_EXPONENT} -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
     * @param d a {@code double} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     * @return the unbiased exponent of the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
    public static int getExponent(double d) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2222
        /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2223
         * Bitwise convert d to long, mask out exponent bits, shift
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2224
         * to the right and then subtract out double's bias adjust to
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2225
         * get true exponent value.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2226
         */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2227
        return (int)(((Double.doubleToRawLongBits(d) & DoubleConsts.EXP_BIT_MASK) >>
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2228
                      (DoubleConsts.SIGNIFICAND_WIDTH - 1)) - DoubleConsts.EXP_BIAS);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
     * Returns the floating-point number adjacent to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
     * argument in the direction of the second argument.  If both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
     * arguments compare as equal the second argument is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
     * <li> If either argument is a NaN, then NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
     * <li> If both arguments are signed zeros, {@code direction}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
     * is returned unchanged (as implied by the requirement of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
     * returning the second argument if the arguments compare as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
     * equal).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
     * <li> If {@code start} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
     * &plusmn;{@link Double#MIN_VALUE} and {@code direction}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
     * has a value such that the result should have a smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
     * magnitude, then a zero with the same sign as {@code start}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
     * <li> If {@code start} is infinite and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
     * {@code direction} has a value such that the result should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
     * have a smaller magnitude, {@link Double#MAX_VALUE} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
     * same sign as {@code start} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
     * <li> If {@code start} is equal to &plusmn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
     * {@link Double#MAX_VALUE} and {@code direction} has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
     * value such that the result should have a larger magnitude, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
     * infinity with same sign as {@code start} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
     * @param start  starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
     * @param direction value indicating which of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
     * {@code start}'s neighbors or {@code start} should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
     * be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
     * @return The floating-point number adjacent to {@code start} in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
     * direction of {@code direction}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
    public static double nextAfter(double start, double direction) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2272
        /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2273
         * The cases:
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2274
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2275
         * nextAfter(+infinity, 0)  == MAX_VALUE
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2276
         * nextAfter(+infinity, +infinity)  == +infinity
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2277
         * nextAfter(-infinity, 0)  == -MAX_VALUE
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2278
         * nextAfter(-infinity, -infinity)  == -infinity
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2279
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2280
         * are naturally handled without any additional testing
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2281
         */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2282
24253
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2283
        /*
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2284
         * IEEE 754 floating-point numbers are lexicographically
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2285
         * ordered if treated as signed-magnitude integers.
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2286
         * Since Java's integers are two's complement,
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2287
         * incrementing the two's complement representation of a
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2288
         * logically negative floating-point value *decrements*
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2289
         * the signed-magnitude representation. Therefore, when
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2290
         * the integer representation of a floating-point value
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2291
         * is negative, the adjustment to the representation is in
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2292
         * the opposite direction from what would initially be expected.
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2293
         */
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2294
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2295
        // Branch to descending case first as it is more costly than ascending
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2296
        // case due to start != 0.0d conditional.
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2297
        if (start > direction) { // descending
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2298
            if (start != 0.0d) {
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2299
                final long transducer = Double.doubleToRawLongBits(start);
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2300
                return Double.longBitsToDouble(transducer + ((transducer > 0L) ? -1L : 1L));
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2301
            } else { // start == 0.0d && direction < 0.0d
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2302
                return -Double.MIN_VALUE;
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2303
            }
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2304
        } else if (start < direction) { // ascending
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2305
            // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0)
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2306
            // then bitwise convert start to integer.
24253
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2307
            final long transducer = Double.doubleToRawLongBits(start + 0.0d);
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2308
            return Double.longBitsToDouble(transducer + ((transducer >= 0L) ? 1L : -1L));
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2309
        } else if (start == direction) {
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2310
            return direction;
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2311
        } else { // isNaN(start) || isNaN(direction)
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2312
            return start + direction;
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2313
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
     * Returns the floating-point number adjacent to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
     * argument in the direction of the second argument.  If both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
     * arguments compare as equal a value equivalent to the second argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
     * Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
     * <li> If either argument is a NaN, then NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
     * <li> If both arguments are signed zeros, a value equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
     * to {@code direction} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
     * <li> If {@code start} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
     * &plusmn;{@link Float#MIN_VALUE} and {@code direction}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
     * has a value such that the result should have a smaller
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
     * magnitude, then a zero with the same sign as {@code start}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
     * <li> If {@code start} is infinite and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
     * {@code direction} has a value such that the result should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
     * have a smaller magnitude, {@link Float#MAX_VALUE} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
     * same sign as {@code start} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
     * <li> If {@code start} is equal to &plusmn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
     * {@link Float#MAX_VALUE} and {@code direction} has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
     * value such that the result should have a larger magnitude, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
     * infinity with same sign as {@code start} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
     * @param start  starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
     * @param direction value indicating which of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
     * {@code start}'s neighbors or {@code start} should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
     * be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
     * @return The floating-point number adjacent to {@code start} in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
     * direction of {@code direction}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
    public static float nextAfter(float start, double direction) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2356
        /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2357
         * The cases:
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2358
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2359
         * nextAfter(+infinity, 0)  == MAX_VALUE
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2360
         * nextAfter(+infinity, +infinity)  == +infinity
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2361
         * nextAfter(-infinity, 0)  == -MAX_VALUE
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2362
         * nextAfter(-infinity, -infinity)  == -infinity
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2363
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2364
         * are naturally handled without any additional testing
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2365
         */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2366
24253
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2367
        /*
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2368
         * IEEE 754 floating-point numbers are lexicographically
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2369
         * ordered if treated as signed-magnitude integers.
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2370
         * Since Java's integers are two's complement,
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2371
         * incrementing the two's complement representation of a
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2372
         * logically negative floating-point value *decrements*
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2373
         * the signed-magnitude representation. Therefore, when
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2374
         * the integer representation of a floating-point value
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2375
         * is negative, the adjustment to the representation is in
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2376
         * the opposite direction from what would initially be expected.
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2377
         */
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2378
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2379
        // Branch to descending case first as it is more costly than ascending
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2380
        // case due to start != 0.0f conditional.
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2381
        if (start > direction) { // descending
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2382
            if (start != 0.0f) {
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2383
                final int transducer = Float.floatToRawIntBits(start);
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2384
                return Float.intBitsToFloat(transducer + ((transducer > 0) ? -1 : 1));
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2385
            } else { // start == 0.0f && direction < 0.0f
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2386
                return -Float.MIN_VALUE;
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2387
            }
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2388
        } else if (start < direction) { // ascending
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2389
            // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0)
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2390
            // then bitwise convert start to integer.
24253
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2391
            final int transducer = Float.floatToRawIntBits(start + 0.0f);
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2392
            return Float.intBitsToFloat(transducer + ((transducer >= 0) ? 1 : -1));
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2393
        } else if (start == direction) {
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2394
            return (float)direction;
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2395
        } else { // isNaN(start) || isNaN(direction)
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2396
            return start + (float)direction;
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2397
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
     * Returns the floating-point value adjacent to {@code d} in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
     * the direction of positive infinity.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
     * semantically equivalent to {@code nextAfter(d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
     * Double.POSITIVE_INFINITY)}; however, a {@code nextUp}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
     * implementation may run faster than its equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
     * {@code nextAfter} call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     * <li> If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
     * <li> If the argument is positive infinity, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
     * <li> If the argument is zero, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
     * {@link Double#MIN_VALUE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
     * @param d starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
     * @return The adjacent floating-point value closer to positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
    public static double nextUp(double d) {
24253
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2426
        // Use a single conditional and handle the likely cases first.
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2427
        if (d < Double.POSITIVE_INFINITY) {
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2428
            // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0).
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2429
            final long transducer = Double.doubleToRawLongBits(d + 0.0D);
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2430
            return Double.longBitsToDouble(transducer + ((transducer >= 0L) ? 1L : -1L));
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2431
        } else { // d is NaN or +Infinity
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2432
            return d;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2433
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
     * Returns the floating-point value adjacent to {@code f} in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
     * the direction of positive infinity.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
     * semantically equivalent to {@code nextAfter(f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
     * Float.POSITIVE_INFINITY)}; however, a {@code nextUp}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
     * implementation may run faster than its equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
     * {@code nextAfter} call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
     * <p>Special Cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
     * <li> If the argument is NaN, the result is NaN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
     * <li> If the argument is positive infinity, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
     * positive infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
     * <li> If the argument is zero, the result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
     * {@link Float#MIN_VALUE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
     * @param f starting floating-point value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
     * @return The adjacent floating-point value closer to positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
     * infinity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
    public static float nextUp(float f) {
24253
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2462
        // Use a single conditional and handle the likely cases first.
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2463
        if (f < Float.POSITIVE_INFINITY) {
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2464
            // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0).
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2465
            final int transducer = Float.floatToRawIntBits(f + 0.0F);
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2466
            return Float.intBitsToFloat(transducer + ((transducer >= 0) ? 1 : -1));
ce29e10e4b41 8032016: Optimizations of Math.next{After,Up}({float,double})
bpb
parents: 23745
diff changeset
  2467
        } else { // f is NaN or +Infinity
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2468
            return f;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2469
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
10608
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2472
    /**
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2473
     * Returns the floating-point value adjacent to {@code d} in
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2474
     * the direction of negative infinity.  This method is
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2475
     * semantically equivalent to {@code nextAfter(d,
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2476
     * Double.NEGATIVE_INFINITY)}; however, a
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2477
     * {@code nextDown} implementation may run faster than its
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2478
     * equivalent {@code nextAfter} call.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2479
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2480
     * <p>Special Cases:
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2481
     * <ul>
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2482
     * <li> If the argument is NaN, the result is NaN.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2483
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2484
     * <li> If the argument is negative infinity, the result is
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2485
     * negative infinity.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2486
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2487
     * <li> If the argument is zero, the result is
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2488
     * {@code -Double.MIN_VALUE}
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2489
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2490
     * </ul>
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2491
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2492
     * @param d  starting floating-point value
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2493
     * @return The adjacent floating-point value closer to negative
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2494
     * infinity.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2495
     * @since 1.8
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2496
     */
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2497
    public static double nextDown(double d) {
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2498
        if (Double.isNaN(d) || d == Double.NEGATIVE_INFINITY)
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2499
            return d;
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2500
        else {
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2501
            if (d == 0.0)
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2502
                return -Double.MIN_VALUE;
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2503
            else
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2504
                return Double.longBitsToDouble(Double.doubleToRawLongBits(d) +
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2505
                                               ((d > 0.0d)?-1L:+1L));
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2506
        }
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2507
    }
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2508
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2509
    /**
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2510
     * Returns the floating-point value adjacent to {@code f} in
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2511
     * the direction of negative infinity.  This method is
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2512
     * semantically equivalent to {@code nextAfter(f,
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2513
     * Float.NEGATIVE_INFINITY)}; however, a
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2514
     * {@code nextDown} implementation may run faster than its
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2515
     * equivalent {@code nextAfter} call.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2516
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2517
     * <p>Special Cases:
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2518
     * <ul>
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2519
     * <li> If the argument is NaN, the result is NaN.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2520
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2521
     * <li> If the argument is negative infinity, the result is
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2522
     * negative infinity.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2523
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2524
     * <li> If the argument is zero, the result is
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2525
     * {@code -Float.MIN_VALUE}
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2526
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2527
     * </ul>
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2528
     *
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2529
     * @param f  starting floating-point value
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2530
     * @return The adjacent floating-point value closer to negative
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2531
     * infinity.
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2532
     * @since 1.8
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2533
     */
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2534
    public static float nextDown(float f) {
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2535
        if (Float.isNaN(f) || f == Float.NEGATIVE_INFINITY)
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2536
            return f;
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2537
        else {
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2538
            if (f == 0.0f)
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2539
                return -Float.MIN_VALUE;
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2540
            else
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2541
                return Float.intBitsToFloat(Float.floatToRawIntBits(f) +
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2542
                                            ((f > 0.0f)?-1:+1));
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2543
        }
7cfca36fc79b 7092404: Add Math.nextDown and Double.isFinite
darcy
parents: 10598
diff changeset
  2544
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
    /**
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  2547
     * Returns {@code d} &times;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
     * 2<sup>{@code scaleFactor}</sup> rounded as if performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
     * by a single correctly rounded floating-point multiply to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
     * member of the double value set.  See the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
     * Language Specification for a discussion of floating-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
     * value sets.  If the exponent of the result is between {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
     * Double#MIN_EXPONENT} and {@link Double#MAX_EXPONENT}, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
     * answer is calculated exactly.  If the exponent of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
     * would be larger than {@code Double.MAX_EXPONENT}, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
     * infinity is returned.  Note that if the result is subnormal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
     * precision may be lost; that is, when {@code scalb(x, n)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
     * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
     * <i>x</i>.  When the result is non-NaN, the result has the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
     * sign as {@code d}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
     * <li> If the first argument is NaN, NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
     * <li> If the first argument is infinite, then an infinity of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
     * same sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
     * <li> If the first argument is zero, then a zero of the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
     * sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
     * @param d number to be scaled by a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
     * @param scaleFactor power of 2 used to scale {@code d}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
     * @return {@code d} &times; 2<sup>{@code scaleFactor}</sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
    public static double scalb(double d, int scaleFactor) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2577
        /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2578
         * This method does not need to be declared strictfp to
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2579
         * compute the same correct result on all platforms.  When
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2580
         * scaling up, it does not matter what order the
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2581
         * multiply-store operations are done; the result will be
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2582
         * finite or overflow regardless of the operation ordering.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2583
         * However, to get the correct result when scaling down, a
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2584
         * particular ordering must be used.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2585
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2586
         * When scaling down, the multiply-store operations are
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2587
         * sequenced so that it is not possible for two consecutive
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2588
         * multiply-stores to return subnormal results.  If one
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2589
         * multiply-store result is subnormal, the next multiply will
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2590
         * round it away to zero.  This is done by first multiplying
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2591
         * by 2 ^ (scaleFactor % n) and then multiplying several
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 26727
diff changeset
  2592
         * times by 2^n as needed where n is the exponent of number
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2593
         * that is a covenient power of two.  In this way, at most one
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2594
         * real rounding error occurs.  If the double value set is
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2595
         * being used exclusively, the rounding will occur on a
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2596
         * multiply.  If the double-extended-exponent value set is
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2597
         * being used, the products will (perhaps) be exact but the
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2598
         * stores to d are guaranteed to round to the double value
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2599
         * set.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2600
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2601
         * It is _not_ a valid implementation to first multiply d by
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2602
         * 2^MIN_EXPONENT and then by 2 ^ (scaleFactor %
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2603
         * MIN_EXPONENT) since even in a strictfp program double
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2604
         * rounding on underflow could occur; e.g. if the scaleFactor
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2605
         * argument was (MIN_EXPONENT - n) and the exponent of d was a
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2606
         * little less than -(MIN_EXPONENT - n), meaning the final
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2607
         * result would be subnormal.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2608
         *
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2609
         * Since exact reproducibility of this method can be achieved
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2610
         * without any undue performance burden, there is no
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2611
         * compelling reason to allow double rounding on underflow in
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2612
         * scalb.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2613
         */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2614
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2615
        // magnitude of a power of two so large that scaling a finite
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2616
        // nonzero value by it would be guaranteed to over or
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 26727
diff changeset
  2617
        // underflow; due to rounding, scaling down takes an
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2618
        // additional power of two which is reflected here
35979
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
  2619
        final int MAX_SCALE = Double.MAX_EXPONENT + -Double.MIN_EXPONENT +
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2620
                              DoubleConsts.SIGNIFICAND_WIDTH + 1;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2621
        int exp_adjust = 0;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2622
        int scale_increment = 0;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2623
        double exp_delta = Double.NaN;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2624
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2625
        // Make sure scaling factor is in a reasonable range
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2626
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2627
        if(scaleFactor < 0) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2628
            scaleFactor = Math.max(scaleFactor, -MAX_SCALE);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2629
            scale_increment = -512;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2630
            exp_delta = twoToTheDoubleScaleDown;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2631
        }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2632
        else {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2633
            scaleFactor = Math.min(scaleFactor, MAX_SCALE);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2634
            scale_increment = 512;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2635
            exp_delta = twoToTheDoubleScaleUp;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2636
        }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2637
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2638
        // Calculate (scaleFactor % +/-512), 512 = 2^9, using
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2639
        // technique from "Hacker's Delight" section 10-2.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2640
        int t = (scaleFactor >> 9-1) >>> 32 - 9;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2641
        exp_adjust = ((scaleFactor + t) & (512 -1)) - t;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2642
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2643
        d *= powerOfTwoD(exp_adjust);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2644
        scaleFactor -= exp_adjust;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2645
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2646
        while(scaleFactor != 0) {
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2647
            d *= exp_delta;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2648
            scaleFactor -= scale_increment;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2649
        }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2650
        return d;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
    /**
11905
646e7e50c2d7 6708398: Support integer overflow
sherman
parents: 11512
diff changeset
  2654
     * Returns {@code f} &times;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
     * 2<sup>{@code scaleFactor}</sup> rounded as if performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
     * by a single correctly rounded floating-point multiply to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
     * member of the float value set.  See the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
     * Language Specification for a discussion of floating-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
     * value sets.  If the exponent of the result is between {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
     * Float#MIN_EXPONENT} and {@link Float#MAX_EXPONENT}, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
     * answer is calculated exactly.  If the exponent of the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
     * would be larger than {@code Float.MAX_EXPONENT}, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
     * infinity is returned.  Note that if the result is subnormal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
     * precision may be lost; that is, when {@code scalb(x, n)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
     * is subnormal, {@code scalb(scalb(x, n), -n)} may not equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
     * <i>x</i>.  When the result is non-NaN, the result has the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
     * sign as {@code f}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
     * <p>Special cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
     * <li> If the first argument is NaN, NaN is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
     * <li> If the first argument is infinite, then an infinity of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
     * same sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
     * <li> If the first argument is zero, then a zero of the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
     * sign is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
     * @param f number to be scaled by a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
     * @param scaleFactor power of 2 used to scale {@code f}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
     * @return {@code f} &times; 2<sup>{@code scaleFactor}</sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
    public static float scalb(float f, int scaleFactor) {
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2684
        // magnitude of a power of two so large that scaling a finite
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2685
        // nonzero value by it would be guaranteed to over or
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 26727
diff changeset
  2686
        // underflow; due to rounding, scaling down takes an
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2687
        // additional power of two which is reflected here
35979
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
  2688
        final int MAX_SCALE = Float.MAX_EXPONENT + -Float.MIN_EXPONENT +
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2689
                              FloatConsts.SIGNIFICAND_WIDTH + 1;
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2690
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2691
        // Make sure scaling factor is in a reasonable range
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2692
        scaleFactor = Math.max(Math.min(scaleFactor, MAX_SCALE), -MAX_SCALE);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2693
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2694
        /*
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2695
         * Since + MAX_SCALE for float fits well within the double
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2696
         * exponent range and + float -> double conversion is exact
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2697
         * the multiplication below will be exact. Therefore, the
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2698
         * rounding that occurs when the double product is cast to
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2699
         * float will be the correctly rounded float result.  Since
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2700
         * all operations other than the final multiply will be exact,
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2701
         * it is not necessary to declare this method strictfp.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2702
         */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2703
        return (float)((double)f*powerOfTwoD(scaleFactor));
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2704
    }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2705
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2706
    // Constants used in scalb
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2707
    static double twoToTheDoubleScaleUp = powerOfTwoD(512);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2708
    static double twoToTheDoubleScaleDown = powerOfTwoD(-512);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2709
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2710
    /**
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2711
     * Returns a floating-point power of two in the normal range.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2712
     */
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2713
    static double powerOfTwoD(int n) {
35979
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
  2714
        assert(n >= Double.MIN_EXPONENT && n <= Double.MAX_EXPONENT);
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2715
        return Double.longBitsToDouble((((long)n + (long)DoubleConsts.EXP_BIAS) <<
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2716
                                        (DoubleConsts.SIGNIFICAND_WIDTH-1))
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2717
                                       & DoubleConsts.EXP_BIT_MASK);
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2718
    }
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2719
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2720
    /**
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2721
     * Returns a floating-point power of two in the normal range.
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2722
     */
11510
f52f50e63c9b 7123649: Remove public modifier from Math.powerOfTwoF.
darcy
parents: 10608
diff changeset
  2723
    static float powerOfTwoF(int n) {
35979
4462913d471a 8149896: Remove unnecessary values in FloatConsts and DoubleConsts
darcy
parents: 34781
diff changeset
  2724
        assert(n >= Float.MIN_EXPONENT && n <= Float.MAX_EXPONENT);
10598
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2725
        return Float.intBitsToFloat(((n + FloatConsts.EXP_BIAS) <<
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2726
                                     (FloatConsts.SIGNIFICAND_WIDTH-1))
efd29b4b3e67 7091682: Move sun.misc.FpUtils code into java.lang.Math
darcy
parents: 10122
diff changeset
  2727
                                    & FloatConsts.EXP_BIT_MASK);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
}