@@ -1768,32 +1768,21 @@ public static double fma(double a, double b, double c) {
17681768 */
17691769 @ HotSpotIntrinsicCandidate
17701770 public static float fma (float a , float b , float c ) {
1771- /*
1772- * Since the double format has more than twice the precision
1773- * of the float format, the multiply of a * b is exact in
1774- * double. The add of c to the product then incurs one
1775- * rounding error. Since the double format moreover has more
1776- * than (2p + 2) precision bits compared to the p bits of the
1777- * float format, the two roundings of (a * b + c), first to
1778- * the double format and then secondarily to the float format,
1779- * are equivalent to rounding the intermediate result directly
1780- * to the float format.
1781- *
1782- * In terms of strictfp vs default-fp concerns related to
1783- * overflow and underflow, since
1784- *
1785- * (Float.MAX_VALUE * Float.MAX_VALUE) << Double.MAX_VALUE
1786- * (Float.MIN_VALUE * Float.MIN_VALUE) >> Double.MIN_VALUE
1787- *
1788- * neither the multiply nor add will overflow or underflow in
1789- * double. Therefore, it is not necessary for this method to
1790- * be declared strictfp to have reproducible
1791- * behavior. However, it is necessary to explicitly store down
1792- * to a float variable to avoid returning a value in the float
1793- * extended value set.
1794- */
1795- float result = (float )(((double ) a * (double ) b ) + (double ) c );
1796- return result ;
1771+ if (Float .isFinite (a ) && Float .isFinite (b ) && Float .isFinite (c )) {
1772+ if (a == 0.0 || b == 0.0 ) {
1773+ return a * b + c ; // Handled signed zero cases
1774+ } else {
1775+ return (new BigDecimal ((double )a * (double )b ) // Exact multiply
1776+ .add (new BigDecimal ((double )c ))) // Exact sum
1777+ .floatValue (); // One rounding
1778+ // to a float value
1779+ }
1780+ } else {
1781+ // At least one of a,b, and c is non-finite. The result
1782+ // will be non-finite as well and will be the same
1783+ // non-finite value under double as float arithmetic.
1784+ return (float )fma ((double )a , (double )b , (double )c );
1785+ }
17971786 }
17981787
17991788 /**
0 commit comments