< prev index next >

src/java.base/share/classes/java/lang/Math.java

Print this page
rev 53028 : 8212043: Add floating-point Math.min/max intrinsics
Summary: Floating-point Math.min() and Math.max() intrinsics are enabled on AArch64 platform
Reviewed-by: adinn, aph


1443     }
1444 
1445     // Use raw bit-wise conversions on guaranteed non-NaN arguments.
1446     private static final long negativeZeroFloatBits  = Float.floatToRawIntBits(-0.0f);
1447     private static final long negativeZeroDoubleBits = Double.doubleToRawLongBits(-0.0d);
1448 
1449     /**
1450      * Returns the greater of two {@code float} values.  That is,
1451      * the result is the argument closer to positive infinity. If the
1452      * arguments have the same value, the result is that same
1453      * value. If either value is NaN, then the result is NaN.  Unlike
1454      * the numerical comparison operators, this method considers
1455      * negative zero to be strictly smaller than positive zero. If one
1456      * argument is positive zero and the other negative zero, the
1457      * result is positive zero.
1458      *
1459      * @param   a   an argument.
1460      * @param   b   another argument.
1461      * @return  the larger of {@code a} and {@code b}.
1462      */

1463     public static float max(float a, float b) {
1464         if (a != a)
1465             return a;   // a is NaN
1466         if ((a == 0.0f) &&
1467             (b == 0.0f) &&
1468             (Float.floatToRawIntBits(a) == negativeZeroFloatBits)) {
1469             // Raw conversion ok since NaN can't map to -0.0.
1470             return b;
1471         }
1472         return (a >= b) ? a : b;
1473     }
1474 
1475     /**
1476      * Returns the greater of two {@code double} values.  That
1477      * is, the result is the argument closer to positive infinity. If
1478      * the arguments have the same value, the result is that same
1479      * value. If either value is NaN, then the result is NaN.  Unlike
1480      * the numerical comparison operators, this method considers
1481      * negative zero to be strictly smaller than positive zero. If one
1482      * argument is positive zero and the other negative zero, the
1483      * result is positive zero.
1484      *
1485      * @param   a   an argument.
1486      * @param   b   another argument.
1487      * @return  the larger of {@code a} and {@code b}.
1488      */

1489     public static double max(double a, double b) {
1490         if (a != a)
1491             return a;   // a is NaN
1492         if ((a == 0.0d) &&
1493             (b == 0.0d) &&
1494             (Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {
1495             // Raw conversion ok since NaN can't map to -0.0.
1496             return b;
1497         }
1498         return (a >= b) ? a : b;
1499     }
1500 
1501     /**
1502      * Returns the smaller of two {@code int} values. That is,
1503      * the result the argument closer to the value of
1504      * {@link Integer#MIN_VALUE}.  If the arguments have the same
1505      * value, the result is that same value.
1506      *
1507      * @param   a   an argument.
1508      * @param   b   another argument.


1524      * @return  the smaller of {@code a} and {@code b}.
1525      */
1526     public static long min(long a, long b) {
1527         return (a <= b) ? a : b;
1528     }
1529 
1530     /**
1531      * Returns the smaller of two {@code float} values.  That is,
1532      * the result is the value closer to negative infinity. If the
1533      * arguments have the same value, the result is that same
1534      * value. If either value is NaN, then the result is NaN.  Unlike
1535      * the numerical comparison operators, this method considers
1536      * negative zero to be strictly smaller than positive zero.  If
1537      * one argument is positive zero and the other is negative zero,
1538      * the result is negative zero.
1539      *
1540      * @param   a   an argument.
1541      * @param   b   another argument.
1542      * @return  the smaller of {@code a} and {@code b}.
1543      */

1544     public static float min(float a, float b) {
1545         if (a != a)
1546             return a;   // a is NaN
1547         if ((a == 0.0f) &&
1548             (b == 0.0f) &&
1549             (Float.floatToRawIntBits(b) == negativeZeroFloatBits)) {
1550             // Raw conversion ok since NaN can't map to -0.0.
1551             return b;
1552         }
1553         return (a <= b) ? a : b;
1554     }
1555 
1556     /**
1557      * Returns the smaller of two {@code double} values.  That
1558      * is, the result is the value closer to negative infinity. If the
1559      * arguments have the same value, the result is that same
1560      * value. If either value is NaN, then the result is NaN.  Unlike
1561      * the numerical comparison operators, this method considers
1562      * negative zero to be strictly smaller than positive zero. If one
1563      * argument is positive zero and the other is negative zero, the
1564      * result is negative zero.
1565      *
1566      * @param   a   an argument.
1567      * @param   b   another argument.
1568      * @return  the smaller of {@code a} and {@code b}.
1569      */

1570     public static double min(double a, double b) {
1571         if (a != a)
1572             return a;   // a is NaN
1573         if ((a == 0.0d) &&
1574             (b == 0.0d) &&
1575             (Double.doubleToRawLongBits(b) == negativeZeroDoubleBits)) {
1576             // Raw conversion ok since NaN can't map to -0.0.
1577             return b;
1578         }
1579         return (a <= b) ? a : b;
1580     }
1581 
1582     /**
1583      * Returns the fused multiply add of the three arguments; that is,
1584      * returns the exact product of the first two arguments summed
1585      * with the third argument and then rounded once to the nearest
1586      * {@code double}.
1587      *
1588      * The rounding is done using the {@linkplain
1589      * java.math.RoundingMode#HALF_EVEN round to nearest even




1443     }
1444 
1445     // Use raw bit-wise conversions on guaranteed non-NaN arguments.
1446     private static final long negativeZeroFloatBits  = Float.floatToRawIntBits(-0.0f);
1447     private static final long negativeZeroDoubleBits = Double.doubleToRawLongBits(-0.0d);
1448 
1449     /**
1450      * Returns the greater of two {@code float} values.  That is,
1451      * the result is the argument closer to positive infinity. If the
1452      * arguments have the same value, the result is that same
1453      * value. If either value is NaN, then the result is NaN.  Unlike
1454      * the numerical comparison operators, this method considers
1455      * negative zero to be strictly smaller than positive zero. If one
1456      * argument is positive zero and the other negative zero, the
1457      * result is positive zero.
1458      *
1459      * @param   a   an argument.
1460      * @param   b   another argument.
1461      * @return  the larger of {@code a} and {@code b}.
1462      */
1463     @HotSpotIntrinsicCandidate
1464     public static float max(float a, float b) {
1465         if (a != a)
1466             return a;   // a is NaN
1467         if ((a == 0.0f) &&
1468             (b == 0.0f) &&
1469             (Float.floatToRawIntBits(a) == negativeZeroFloatBits)) {
1470             // Raw conversion ok since NaN can't map to -0.0.
1471             return b;
1472         }
1473         return (a >= b) ? a : b;
1474     }
1475 
1476     /**
1477      * Returns the greater of two {@code double} values.  That
1478      * is, the result is the argument closer to positive infinity. If
1479      * the arguments have the same value, the result is that same
1480      * value. If either value is NaN, then the result is NaN.  Unlike
1481      * the numerical comparison operators, this method considers
1482      * negative zero to be strictly smaller than positive zero. If one
1483      * argument is positive zero and the other negative zero, the
1484      * result is positive zero.
1485      *
1486      * @param   a   an argument.
1487      * @param   b   another argument.
1488      * @return  the larger of {@code a} and {@code b}.
1489      */
1490     @HotSpotIntrinsicCandidate
1491     public static double max(double a, double b) {
1492         if (a != a)
1493             return a;   // a is NaN
1494         if ((a == 0.0d) &&
1495             (b == 0.0d) &&
1496             (Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {
1497             // Raw conversion ok since NaN can't map to -0.0.
1498             return b;
1499         }
1500         return (a >= b) ? a : b;
1501     }
1502 
1503     /**
1504      * Returns the smaller of two {@code int} values. That is,
1505      * the result the argument closer to the value of
1506      * {@link Integer#MIN_VALUE}.  If the arguments have the same
1507      * value, the result is that same value.
1508      *
1509      * @param   a   an argument.
1510      * @param   b   another argument.


1526      * @return  the smaller of {@code a} and {@code b}.
1527      */
1528     public static long min(long a, long b) {
1529         return (a <= b) ? a : b;
1530     }
1531 
1532     /**
1533      * Returns the smaller of two {@code float} values.  That is,
1534      * the result is the value closer to negative infinity. If the
1535      * arguments have the same value, the result is that same
1536      * value. If either value is NaN, then the result is NaN.  Unlike
1537      * the numerical comparison operators, this method considers
1538      * negative zero to be strictly smaller than positive zero.  If
1539      * one argument is positive zero and the other is negative zero,
1540      * the result is negative zero.
1541      *
1542      * @param   a   an argument.
1543      * @param   b   another argument.
1544      * @return  the smaller of {@code a} and {@code b}.
1545      */
1546     @HotSpotIntrinsicCandidate
1547     public static float min(float a, float b) {
1548         if (a != a)
1549             return a;   // a is NaN
1550         if ((a == 0.0f) &&
1551             (b == 0.0f) &&
1552             (Float.floatToRawIntBits(b) == negativeZeroFloatBits)) {
1553             // Raw conversion ok since NaN can't map to -0.0.
1554             return b;
1555         }
1556         return (a <= b) ? a : b;
1557     }
1558 
1559     /**
1560      * Returns the smaller of two {@code double} values.  That
1561      * is, the result is the value closer to negative infinity. If the
1562      * arguments have the same value, the result is that same
1563      * value. If either value is NaN, then the result is NaN.  Unlike
1564      * the numerical comparison operators, this method considers
1565      * negative zero to be strictly smaller than positive zero. If one
1566      * argument is positive zero and the other is negative zero, the
1567      * result is negative zero.
1568      *
1569      * @param   a   an argument.
1570      * @param   b   another argument.
1571      * @return  the smaller of {@code a} and {@code b}.
1572      */
1573     @HotSpotIntrinsicCandidate
1574     public static double min(double a, double b) {
1575         if (a != a)
1576             return a;   // a is NaN
1577         if ((a == 0.0d) &&
1578             (b == 0.0d) &&
1579             (Double.doubleToRawLongBits(b) == negativeZeroDoubleBits)) {
1580             // Raw conversion ok since NaN can't map to -0.0.
1581             return b;
1582         }
1583         return (a <= b) ? a : b;
1584     }
1585 
1586     /**
1587      * Returns the fused multiply add of the three arguments; that is,
1588      * returns the exact product of the first two arguments summed
1589      * with the third argument and then rounded once to the nearest
1590      * {@code double}.
1591      *
1592      * The rounding is done using the {@linkplain
1593      * java.math.RoundingMode#HALF_EVEN round to nearest even


< prev index next >