14a238c70SJohn Marino /* mpfr_cache -- cache interface for multiple-precision constants in MPFR.
24a238c70SJohn Marino
3*ab6d115fSJohn Marino Copyright 2004, 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 #if 0 /* this function is not used/documented/tested so far, it could be
264a238c70SJohn Marino useful if some user wants to add a new constant to mpfr, and
274a238c70SJohn Marino implement a cache mechanism for that constant */
284a238c70SJohn Marino void
294a238c70SJohn Marino mpfr_init_cache (mpfr_cache_t cache, int (*func)(mpfr_ptr, mpfr_rnd_t))
304a238c70SJohn Marino {
314a238c70SJohn Marino MPFR_PREC (cache->x) = 0; /* Invalid prec to detect that the cache is not
324a238c70SJohn Marino valid. Maybe add a flag? */
334a238c70SJohn Marino cache->func = func;
344a238c70SJohn Marino }
354a238c70SJohn Marino #endif
364a238c70SJohn Marino
374a238c70SJohn Marino void
mpfr_clear_cache(mpfr_cache_t cache)384a238c70SJohn Marino mpfr_clear_cache (mpfr_cache_t cache)
394a238c70SJohn Marino {
404a238c70SJohn Marino if (MPFR_PREC (cache->x) != 0)
414a238c70SJohn Marino mpfr_clear (cache->x);
424a238c70SJohn Marino MPFR_PREC (cache->x) = 0;
434a238c70SJohn Marino }
444a238c70SJohn Marino
454a238c70SJohn Marino int
mpfr_cache(mpfr_ptr dest,mpfr_cache_t cache,mpfr_rnd_t rnd)464a238c70SJohn Marino mpfr_cache (mpfr_ptr dest, mpfr_cache_t cache, mpfr_rnd_t rnd)
474a238c70SJohn Marino {
484a238c70SJohn Marino mpfr_prec_t prec = MPFR_PREC (dest);
494a238c70SJohn Marino mpfr_prec_t pold = MPFR_PREC (cache->x);
504a238c70SJohn Marino int inexact, sign;
514a238c70SJohn Marino MPFR_SAVE_EXPO_DECL (expo);
524a238c70SJohn Marino
534a238c70SJohn Marino MPFR_SAVE_EXPO_MARK (expo);
544a238c70SJohn Marino
554a238c70SJohn Marino if (MPFR_UNLIKELY (prec > pold))
564a238c70SJohn Marino {
574a238c70SJohn Marino /* No previous result in the cache or the precision of the
584a238c70SJohn Marino previous result is not sufficient. */
594a238c70SJohn Marino
604a238c70SJohn Marino if (MPFR_UNLIKELY (pold == 0)) /* No previous result. */
614a238c70SJohn Marino mpfr_init2 (cache->x, prec);
624a238c70SJohn Marino
634a238c70SJohn Marino /* Update the cache. */
644a238c70SJohn Marino pold = prec;
654a238c70SJohn Marino /* no need to keep the previous value */
664a238c70SJohn Marino mpfr_set_prec (cache->x, pold);
674a238c70SJohn Marino cache->inexact = (*cache->func) (cache->x, MPFR_RNDN);
684a238c70SJohn Marino }
694a238c70SJohn Marino
704a238c70SJohn Marino /* now pold >= prec is the precision of cache->x */
714a238c70SJohn Marino
724a238c70SJohn Marino /* First, check if the cache has the exact value (unlikely).
734a238c70SJohn Marino Else the exact value is between (assuming x=cache->x > 0):
744a238c70SJohn Marino x and x+ulp(x) if cache->inexact < 0,
754a238c70SJohn Marino x-ulp(x) and x if cache->inexact > 0,
764a238c70SJohn Marino and abs(x-exact) <= ulp(x)/2. */
774a238c70SJohn Marino
784a238c70SJohn Marino /* we assume all cached constants are positive */
794a238c70SJohn Marino MPFR_ASSERTN (MPFR_IS_POS (cache->x)); /* TODO... */
804a238c70SJohn Marino sign = MPFR_SIGN (cache->x);
814a238c70SJohn Marino MPFR_SET_EXP (dest, MPFR_GET_EXP (cache->x));
824a238c70SJohn Marino MPFR_SET_SIGN (dest, sign);
834a238c70SJohn Marino
844a238c70SJohn Marino /* round cache->x from precision pold down to precision prec */
854a238c70SJohn Marino MPFR_RNDRAW_GEN (inexact, dest,
864a238c70SJohn Marino MPFR_MANT (cache->x), pold, rnd, sign,
874a238c70SJohn Marino if (MPFR_UNLIKELY (cache->inexact == 0))
884a238c70SJohn Marino {
894a238c70SJohn Marino if ((_sp[0] & _ulp) == 0)
904a238c70SJohn Marino {
914a238c70SJohn Marino inexact = -sign;
924a238c70SJohn Marino goto trunc_doit;
934a238c70SJohn Marino }
944a238c70SJohn Marino else
954a238c70SJohn Marino goto addoneulp;
964a238c70SJohn Marino }
974a238c70SJohn Marino else if (cache->inexact < 0)
984a238c70SJohn Marino goto addoneulp;
994a238c70SJohn Marino else /* cache->inexact > 0 */
1004a238c70SJohn Marino {
1014a238c70SJohn Marino inexact = -sign;
1024a238c70SJohn Marino goto trunc_doit;
1034a238c70SJohn Marino },
1044a238c70SJohn Marino if (MPFR_UNLIKELY (++MPFR_EXP (dest) > __gmpfr_emax))
1054a238c70SJohn Marino mpfr_overflow (dest, rnd, sign);
1064a238c70SJohn Marino );
1074a238c70SJohn Marino
1084a238c70SJohn Marino if (MPFR_LIKELY (cache->inexact != 0))
1094a238c70SJohn Marino {
1104a238c70SJohn Marino switch (rnd)
1114a238c70SJohn Marino {
1124a238c70SJohn Marino case MPFR_RNDZ:
1134a238c70SJohn Marino case MPFR_RNDD:
1144a238c70SJohn Marino if (MPFR_UNLIKELY (inexact == 0))
1154a238c70SJohn Marino {
1164a238c70SJohn Marino inexact = cache->inexact;
1174a238c70SJohn Marino if (inexact > 0)
1184a238c70SJohn Marino {
1194a238c70SJohn Marino mpfr_nextbelow (dest);
1204a238c70SJohn Marino inexact = -inexact;
1214a238c70SJohn Marino }
1224a238c70SJohn Marino }
1234a238c70SJohn Marino break;
1244a238c70SJohn Marino case MPFR_RNDU:
1254a238c70SJohn Marino case MPFR_RNDA:
1264a238c70SJohn Marino if (MPFR_UNLIKELY (inexact == 0))
1274a238c70SJohn Marino {
1284a238c70SJohn Marino inexact = cache->inexact;
1294a238c70SJohn Marino if (inexact < 0)
1304a238c70SJohn Marino {
1314a238c70SJohn Marino mpfr_nextabove (dest);
1324a238c70SJohn Marino inexact = -inexact;
1334a238c70SJohn Marino }
1344a238c70SJohn Marino }
1354a238c70SJohn Marino break;
1364a238c70SJohn Marino default: /* MPFR_RNDN */
1374a238c70SJohn Marino if (MPFR_UNLIKELY(inexact == 0))
1384a238c70SJohn Marino inexact = cache->inexact;
1394a238c70SJohn Marino break;
1404a238c70SJohn Marino }
1414a238c70SJohn Marino }
1424a238c70SJohn Marino
1434a238c70SJohn Marino MPFR_SAVE_EXPO_FREE (expo);
1444a238c70SJohn Marino return mpfr_check_range (dest, inexact, rnd);
1454a238c70SJohn Marino }
146