14a238c70SJohn Marino /* mpfr_round_near_x -- Round a floating point number nears another one.
24a238c70SJohn Marino
3*ab6d115fSJohn Marino Copyright 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
4*ab6d115fSJohn Marino Contributed by the AriC and Caramel projects, INRIA.
54a238c70SJohn Marino
64a238c70SJohn Marino This file is part of the GNU MPFR Library.
74a238c70SJohn Marino
84a238c70SJohn Marino The GNU MPFR Library is free software; you can redistribute it and/or modify
94a238c70SJohn Marino it under the terms of the GNU Lesser General Public License as published by
104a238c70SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
114a238c70SJohn Marino option) any later version.
124a238c70SJohn Marino
134a238c70SJohn Marino The GNU MPFR Library is distributed in the hope that it will be useful, but
144a238c70SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
154a238c70SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
164a238c70SJohn Marino License for more details.
174a238c70SJohn Marino
184a238c70SJohn Marino You should have received a copy of the GNU Lesser General Public License
194a238c70SJohn Marino along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
204a238c70SJohn Marino http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
214a238c70SJohn Marino 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
224a238c70SJohn Marino
234a238c70SJohn Marino #include "mpfr-impl.h"
244a238c70SJohn Marino
254a238c70SJohn Marino /* Use MPFR_FAST_COMPUTE_IF_SMALL_INPUT instead (a simple wrapper) */
264a238c70SJohn Marino
274a238c70SJohn Marino /* int mpfr_round_near_x (mpfr_ptr y, mpfr_srcptr v, mpfr_uexp_t err, int dir,
284a238c70SJohn Marino mpfr_rnd_t rnd)
294a238c70SJohn Marino
304a238c70SJohn Marino TODO: fix this description.
314a238c70SJohn Marino Assuming y = o(f(x)) = o(x + g(x)) with |g(x)| < 2^(EXP(v)-error)
324a238c70SJohn Marino If x is small enough, y ~= v. This function checks and does this.
334a238c70SJohn Marino
344a238c70SJohn Marino It assumes that f(x) is not representable exactly as a FP number.
354a238c70SJohn Marino v must not be a singular value (NAN, INF or ZERO), usual values are
364a238c70SJohn Marino v=1 or v=x.
374a238c70SJohn Marino
384a238c70SJohn Marino y is the destination (a mpfr_t), v the value to set (a mpfr_t),
394a238c70SJohn Marino err the error term (a mpfr_uexp_t) such that |g(x)| < 2^(EXP(x)-err),
404a238c70SJohn Marino dir (an int) is the direction of the error (if dir = 0,
414a238c70SJohn Marino it rounds toward 0, if dir=1, it rounds away from 0),
424a238c70SJohn Marino rnd the rounding mode.
434a238c70SJohn Marino
444a238c70SJohn Marino It returns 0 if it can't round.
454a238c70SJohn Marino Otherwise it returns the ternary flag (It can't return an exact value).
464a238c70SJohn Marino */
474a238c70SJohn Marino
484a238c70SJohn Marino /* What "small enough" means?
494a238c70SJohn Marino
504a238c70SJohn Marino We work with the positive values.
514a238c70SJohn Marino Assuming err > Prec (y)+1
524a238c70SJohn Marino
534a238c70SJohn Marino i = [ y = o(x)] // i = inexact flag
544a238c70SJohn Marino If i == 0
554a238c70SJohn Marino Setting x in y is exact. We have:
564a238c70SJohn Marino y = [XXXXXXXXX[...]]0[...] + error where [..] are optional zeros
574a238c70SJohn Marino if dirError = ToInf,
584a238c70SJohn Marino x < f(x) < x + 2^(EXP(x)-err)
594a238c70SJohn Marino since x=y, and ulp (y)/2 > 2^(EXP(x)-err), we have:
604a238c70SJohn Marino y < f(x) < y+ulp(y) and |y-f(x)| < ulp(y)/2
614a238c70SJohn Marino if rnd = RNDN, nothing
624a238c70SJohn Marino if rnd = RNDZ, nothing
634a238c70SJohn Marino if rnd = RNDA, addoneulp
644a238c70SJohn Marino elif dirError = ToZero
654a238c70SJohn Marino x -2^(EXP(x)-err) < f(x) < x
664a238c70SJohn Marino since x=y, and ulp (y)/2 > 2^(EXP(x)-err), we have:
674a238c70SJohn Marino y-ulp(y) < f(x) < y and |y-f(x)| < ulp(y)/2
684a238c70SJohn Marino if rnd = RNDN, nothing
694a238c70SJohn Marino if rnd = RNDZ, nexttozero
704a238c70SJohn Marino if rnd = RNDA, nothing
714a238c70SJohn Marino NOTE: err > prec (y)+1 is needed only for RNDN.
724a238c70SJohn Marino elif i > 0 and i = EVEN_ROUNDING
734a238c70SJohn Marino So rnd = RNDN and we have y = x + ulp(y)/2
744a238c70SJohn Marino if dirError = ToZero,
754a238c70SJohn Marino we have x -2^(EXP(x)-err) < f(x) < x
764a238c70SJohn Marino so y - ulp(y)/2 - 2^(EXP(x)-err) < f(x) < y-ulp(y)/2
774a238c70SJohn Marino so y -ulp(y) < f(x) < y-ulp(y)/2
784a238c70SJohn Marino => nexttozero(y)
794a238c70SJohn Marino elif dirError = ToInf
804a238c70SJohn Marino we have x < f(x) < x + 2^(EXP(x)-err)
814a238c70SJohn Marino so y - ulp(y)/2 < f(x) < y+ulp(y)/2-ulp(y)/2
824a238c70SJohn Marino so y - ulp(y)/2 < f(x) < y
834a238c70SJohn Marino => do nothing
844a238c70SJohn Marino elif i < 0 and i = -EVEN_ROUNDING
854a238c70SJohn Marino So rnd = RNDN and we have y = x - ulp(y)/2
864a238c70SJohn Marino if dirError = ToZero,
874a238c70SJohn Marino y < f(x) < y + ulp(y)/2 => do nothing
884a238c70SJohn Marino if dirError = ToInf
894a238c70SJohn Marino y + ulp(y)/2 < f(x) < y + ulp(y) => AddOneUlp
904a238c70SJohn Marino elif i > 0
914a238c70SJohn Marino we can't have rnd = RNDZ, and prec(x) > prec(y), so ulp(x) < ulp(y)
924a238c70SJohn Marino we have y - ulp (y) < x < y
934a238c70SJohn Marino or more exactly y - ulp(y) + ulp(x)/2 <= x <= y - ulp(x)/2
944a238c70SJohn Marino if rnd = RNDA,
954a238c70SJohn Marino if dirError = ToInf,
964a238c70SJohn Marino we have x < f(x) < x + 2^(EXP(x)-err)
974a238c70SJohn Marino if err > prec (x),
984a238c70SJohn Marino we have 2^(EXP(x)-err) < ulp(x), so 2^(EXP(x)-err) <= ulp(x)/2
994a238c70SJohn Marino so f(x) <= y - ulp(x)/2+ulp(x)/2 <= y
1004a238c70SJohn Marino and y - ulp(y) < x < f(x)
1014a238c70SJohn Marino so we have y - ulp(y) < f(x) < y
1024a238c70SJohn Marino so do nothing.
1034a238c70SJohn Marino elif we can round, ie y - ulp(y) < x + 2^(EXP(x)-err) < y
1044a238c70SJohn Marino we have y - ulp(y) < x < f(x) < x + 2^(EXP(x)-err) < y
1054a238c70SJohn Marino so do nothing
1064a238c70SJohn Marino otherwise
1074a238c70SJohn Marino Wrong. Example X=[0.11101]111111110000
1084a238c70SJohn Marino + 1111111111111111111....
1094a238c70SJohn Marino elif dirError = ToZero
1104a238c70SJohn Marino we have x - 2^(EXP(x)-err) < f(x) < x
1114a238c70SJohn Marino so f(x) < x < y
1124a238c70SJohn Marino if err > prec (x)
1134a238c70SJohn Marino x-2^(EXP(x)-err) >= x-ulp(x)/2 >= y - ulp(y) + ulp(x)/2-ulp(x)/2
1144a238c70SJohn Marino so y - ulp(y) < f(x) < y
1154a238c70SJohn Marino so do nothing
1164a238c70SJohn Marino elif we can round, ie y - ulp(y) < x - 2^(EXP(x)-err) < y
1174a238c70SJohn Marino y - ulp(y) < x - 2^(EXP(x)-err) < f(x) < y
1184a238c70SJohn Marino so do nothing
1194a238c70SJohn Marino otherwise
1204a238c70SJohn Marino Wrong. Example: X=[1.111010]00000010
1214a238c70SJohn Marino - 10000001000000000000100....
1224a238c70SJohn Marino elif rnd = RNDN,
1234a238c70SJohn Marino y - ulp(y)/2 < x < y and we can't have x = y-ulp(y)/2:
1244a238c70SJohn Marino so we have:
1254a238c70SJohn Marino y - ulp(y)/2 + ulp(x)/2 <= x <= y - ulp(x)/2
1264a238c70SJohn Marino if dirError = ToInf
1274a238c70SJohn Marino we have x < f(x) < x+2^(EXP(x)-err) and ulp(y) > 2^(EXP(x)-err)
1284a238c70SJohn Marino so y - ulp(y)/2 + ulp (x)/2 < f(x) < y + ulp (y)/2 - ulp (x)/2
1294a238c70SJohn Marino we can round but we can't compute inexact flag.
1304a238c70SJohn Marino if err > prec (x)
1314a238c70SJohn Marino y - ulp(y)/2 + ulp (x)/2 < f(x) < y + ulp(x)/2 - ulp(x)/2
1324a238c70SJohn Marino so y - ulp(y)/2 + ulp (x)/2 < f(x) < y
1334a238c70SJohn Marino we can round and compute inexact flag. do nothing
1344a238c70SJohn Marino elif we can round, ie y - ulp(y)/2 < x + 2^(EXP(x)-err) < y
1354a238c70SJohn Marino we have y - ulp(y)/2 + ulp (x)/2 < f(x) < y
1364a238c70SJohn Marino so do nothing
1374a238c70SJohn Marino otherwise
1384a238c70SJohn Marino Wrong
1394a238c70SJohn Marino elif dirError = ToZero
1404a238c70SJohn Marino we have x -2^(EXP(x)-err) < f(x) < x and ulp(y)/2 > 2^(EXP(x)-err)
1414a238c70SJohn Marino so y-ulp(y)+ulp(x)/2 < f(x) < y - ulp(x)/2
1424a238c70SJohn Marino if err > prec (x)
1434a238c70SJohn Marino x- ulp(x)/2 < f(x) < x
1444a238c70SJohn Marino so y - ulp(y)/2+ulp(x)/2 - ulp(x)/2 < f(x) < x <= y - ulp(x)/2 < y
1454a238c70SJohn Marino do nothing
1464a238c70SJohn Marino elif we can round, ie y-ulp(y)/2 < x-2^(EXP(x)-err) < y
1474a238c70SJohn Marino we have y-ulp(y)/2 < x-2^(EXP(x)-err) < f(x) < x < y
1484a238c70SJohn Marino do nothing
1494a238c70SJohn Marino otherwise
1504a238c70SJohn Marino Wrong
1514a238c70SJohn Marino elif i < 0
1524a238c70SJohn Marino same thing?
1534a238c70SJohn Marino */
1544a238c70SJohn Marino
1554a238c70SJohn Marino int
mpfr_round_near_x(mpfr_ptr y,mpfr_srcptr v,mpfr_uexp_t err,int dir,mpfr_rnd_t rnd)1564a238c70SJohn Marino mpfr_round_near_x (mpfr_ptr y, mpfr_srcptr v, mpfr_uexp_t err, int dir,
1574a238c70SJohn Marino mpfr_rnd_t rnd)
1584a238c70SJohn Marino {
1594a238c70SJohn Marino int inexact, sign;
1604a238c70SJohn Marino unsigned int old_flags = __gmpfr_flags;
1614a238c70SJohn Marino
1624a238c70SJohn Marino MPFR_ASSERTD (!MPFR_IS_SINGULAR (v));
1634a238c70SJohn Marino MPFR_ASSERTD (dir == 0 || dir == 1);
1644a238c70SJohn Marino
1654a238c70SJohn Marino /* First check if we can round. The test is more restrictive than
1664a238c70SJohn Marino necessary. Note that if err is not representable in an mpfr_exp_t,
1674a238c70SJohn Marino then err > MPFR_PREC (v) and the conversion to mpfr_exp_t will not
1684a238c70SJohn Marino occur. */
1694a238c70SJohn Marino if (!(err > MPFR_PREC (y) + 1
1704a238c70SJohn Marino && (err > MPFR_PREC (v)
1714a238c70SJohn Marino || mpfr_round_p (MPFR_MANT (v), MPFR_LIMB_SIZE (v),
1724a238c70SJohn Marino (mpfr_exp_t) err,
1734a238c70SJohn Marino MPFR_PREC (y) + (rnd == MPFR_RNDN)))))
1744a238c70SJohn Marino /* If we assume we can not round, return 0, and y is not modified */
1754a238c70SJohn Marino return 0;
1764a238c70SJohn Marino
1774a238c70SJohn Marino /* First round v in y */
1784a238c70SJohn Marino sign = MPFR_SIGN (v);
1794a238c70SJohn Marino MPFR_SET_EXP (y, MPFR_GET_EXP (v));
1804a238c70SJohn Marino MPFR_SET_SIGN (y, sign);
1814a238c70SJohn Marino MPFR_RNDRAW_GEN (inexact, y, MPFR_MANT (v), MPFR_PREC (v), rnd, sign,
1824a238c70SJohn Marino if (dir == 0)
1834a238c70SJohn Marino {
1844a238c70SJohn Marino inexact = -sign;
1854a238c70SJohn Marino goto trunc_doit;
1864a238c70SJohn Marino }
1874a238c70SJohn Marino else
1884a238c70SJohn Marino goto addoneulp;
1894a238c70SJohn Marino , if (MPFR_UNLIKELY (++MPFR_EXP (y) > __gmpfr_emax))
1904a238c70SJohn Marino mpfr_overflow (y, rnd, sign)
1914a238c70SJohn Marino );
1924a238c70SJohn Marino
1934a238c70SJohn Marino /* Fix it in some cases */
1944a238c70SJohn Marino MPFR_ASSERTD (!MPFR_IS_NAN (y) && !MPFR_IS_ZERO (y));
1954a238c70SJohn Marino /* If inexact == 0, setting y from v is exact but we haven't
1964a238c70SJohn Marino take into account yet the error term */
1974a238c70SJohn Marino if (inexact == 0)
1984a238c70SJohn Marino {
1994a238c70SJohn Marino if (dir == 0) /* The error term is negative for v positive */
2004a238c70SJohn Marino {
2014a238c70SJohn Marino inexact = sign;
2024a238c70SJohn Marino if (MPFR_IS_LIKE_RNDZ (rnd, MPFR_IS_NEG_SIGN (sign)))
2034a238c70SJohn Marino {
2044a238c70SJohn Marino /* case nexttozero */
2054a238c70SJohn Marino /* The underflow flag should be set if the result is zero */
2064a238c70SJohn Marino __gmpfr_flags = old_flags;
2074a238c70SJohn Marino inexact = -sign;
2084a238c70SJohn Marino mpfr_nexttozero (y);
2094a238c70SJohn Marino if (MPFR_UNLIKELY (MPFR_IS_ZERO (y)))
2104a238c70SJohn Marino mpfr_set_underflow ();
2114a238c70SJohn Marino }
2124a238c70SJohn Marino }
2134a238c70SJohn Marino else /* The error term is positive for v positive */
2144a238c70SJohn Marino {
2154a238c70SJohn Marino inexact = -sign;
2164a238c70SJohn Marino /* Round Away */
2174a238c70SJohn Marino if (rnd != MPFR_RNDN && !MPFR_IS_LIKE_RNDZ (rnd, MPFR_IS_NEG_SIGN(sign)))
2184a238c70SJohn Marino {
2194a238c70SJohn Marino /* case nexttoinf */
2204a238c70SJohn Marino /* The overflow flag should be set if the result is infinity */
2214a238c70SJohn Marino inexact = sign;
2224a238c70SJohn Marino mpfr_nexttoinf (y);
2234a238c70SJohn Marino if (MPFR_UNLIKELY (MPFR_IS_INF (y)))
2244a238c70SJohn Marino mpfr_set_overflow ();
2254a238c70SJohn Marino }
2264a238c70SJohn Marino }
2274a238c70SJohn Marino }
2284a238c70SJohn Marino
2294a238c70SJohn Marino /* the inexact flag cannot be 0, since this would mean an exact value,
2304a238c70SJohn Marino and in this case we cannot round correctly */
2314a238c70SJohn Marino MPFR_ASSERTD(inexact != 0);
2324a238c70SJohn Marino MPFR_RET (inexact);
2334a238c70SJohn Marino }
234