Rounding-mode correct round to integral value. */
| 1988 | |
| 1989 | /* Rounding-mode correct round to integral value. */ |
| 1990 | IEEEFloat::opStatus IEEEFloat::roundToIntegral(roundingMode rounding_mode) { |
| 1991 | opStatus fs; |
| 1992 | |
| 1993 | if (isInfinity()) |
| 1994 | // [IEEE Std 754-2008 6.1]: |
| 1995 | // The behavior of infinity in floating-point arithmetic is derived from the |
| 1996 | // limiting cases of real arithmetic with operands of arbitrarily |
| 1997 | // large magnitude, when such a limit exists. |
| 1998 | // ... |
| 1999 | // Operations on infinite operands are usually exact and therefore signal no |
| 2000 | // exceptions ... |
| 2001 | return opOK; |
| 2002 | |
| 2003 | if (isNaN()) { |
| 2004 | if (isSignaling()) { |
| 2005 | // [IEEE Std 754-2008 6.2]: |
| 2006 | // Under default exception handling, any operation signaling an invalid |
| 2007 | // operation exception and for which a floating-point result is to be |
| 2008 | // delivered shall deliver a quiet NaN. |
| 2009 | makeQuiet(); |
| 2010 | // [IEEE Std 754-2008 6.2]: |
| 2011 | // Signaling NaNs shall be reserved operands that, under default exception |
| 2012 | // handling, signal the invalid operation exception(see 7.2) for every |
| 2013 | // general-computational and signaling-computational operation except for |
| 2014 | // the conversions described in 5.12. |
| 2015 | return opInvalidOp; |
| 2016 | } else { |
| 2017 | // [IEEE Std 754-2008 6.2]: |
| 2018 | // For an operation with quiet NaN inputs, other than maximum and minimum |
| 2019 | // operations, if a floating-point result is to be delivered the result |
| 2020 | // shall be a quiet NaN which should be one of the input NaNs. |
| 2021 | // ... |
| 2022 | // Every general-computational and quiet-computational operation involving |
| 2023 | // one or more input NaNs, none of them signaling, shall signal no |
| 2024 | // exception, except fusedMultiplyAdd might signal the invalid operation |
| 2025 | // exception(see 7.2). |
| 2026 | return opOK; |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | if (isZero()) { |
| 2031 | // [IEEE Std 754-2008 6.3]: |
| 2032 | // ... the sign of the result of conversions, the quantize operation, the |
| 2033 | // roundToIntegral operations, and the roundToIntegralExact(see 5.3.1) is |
| 2034 | // the sign of the first or only operand. |
| 2035 | return opOK; |
| 2036 | } |
| 2037 | |
| 2038 | // If the exponent is large enough, we know that this value is already |
| 2039 | // integral, and the arithmetic below would potentially cause it to saturate |
| 2040 | // to +/-Inf. Bail out early instead. |
| 2041 | if (exponent+1 >= (int)semanticsPrecision(*semantics)) |
| 2042 | return opOK; |
| 2043 | |
| 2044 | // The algorithm here is quite simple: we add 2^(p-1), where p is the |
| 2045 | // precision of our format, and then subtract it back off again. The choice |
| 2046 | // of rounding modes for the addition/subtraction determines the rounding mode |
| 2047 | // for our integral rounding as well. |
no test coverage detected