summaryrefslogtreecommitdiff
path: root/cache.c
diff options
context:
space:
mode:
authorpelissip <pelissip@280ebfd0-de03-0410-8827-d642c229c3f4>2004-04-21 12:36:30 +0000
committerpelissip <pelissip@280ebfd0-de03-0410-8827-d642c229c3f4>2004-04-21 12:36:30 +0000
commit208bfd1424477764d84b1a72d7c05489efae83a7 (patch)
tree458d0f78d626a9266f3c0de113557dc28bbf18e9 /cache.c
parent392bf02ed16af2d4838b79c397225aa1ff63efbe (diff)
downloadmpfr-208bfd1424477764d84b1a72d7c05489efae83a7.tar.gz
Add preliminary generic code to handle cached const.
Move const_pi, const_log2 to use this generic cache, which fix bugs of the previous implementation. const_euler is now cached too. git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@2876 280ebfd0-de03-0410-8827-d642c229c3f4
Diffstat (limited to 'cache.c')
-rw-r--r--cache.c112
1 files changed, 112 insertions, 0 deletions
diff --git a/cache.c b/cache.c
new file mode 100644
index 000000000..13546ae9d
--- /dev/null
+++ b/cache.c
@@ -0,0 +1,112 @@
+/* mpfr_cache -- cache interface for multi-precision const in MPFR.
+
+Copyright 2004 Free Software Foundation.
+
+This file is part of the MPFR Library.
+
+The MPFR Library is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or (at your
+option) any later version.
+
+The MPFR Library is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+License for more details.
+
+You should have received a copy of the GNU Lesser General Public License
+along with the MPFR Library; see the file COPYING.LIB. If not, write to
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+MA 02111-1307, USA. */
+
+#include "mpfr-impl.h"
+
+/* Global MPFR cache */
+MPFR_DECL_INIT_CACHE(__gmpfr_cache_const_pi, mpfr_const_pi);
+MPFR_DECL_INIT_CACHE(__gmpfr_cache_const_log2, mpfr_const_log2);
+MPFR_DECL_INIT_CACHE(__gmpfr_cache_const_euler, mpfr_const_euler);
+
+void
+mpfr_init_cache (mpfr_cache_t cache, int (*func)(mpfr_ptr, mp_rnd_t))
+{
+ MPFR_PREC (cache->x) = 0; /* Invalid prec to detect that the cache is not
+ valid. Maybe add a flag? */
+ cache->func = func;
+}
+
+void
+mpfr_clear_cache (mpfr_cache_t cache)
+{
+ if (MPFR_PREC(cache->x) != 0)
+ mpfr_clear (cache->x);
+ MPFR_PREC(cache->x) = 0;
+}
+
+int
+mpfr_cache (mpfr_ptr dest, mpfr_cache_t cache, mp_rnd_t rnd)
+{
+ mp_prec_t prec = MPFR_PREC (dest);
+ mp_prec_t pold = MPFR_PREC (cache->x);
+ int inexact;
+
+ /* Check if the cache has been already filled */
+ if (MPFR_UNLIKELY(pold == 0))
+ mpfr_init2 (cache->x, MPFR_PREC_MIN);
+
+ /* Check if we can round with the previous result */
+ else if (prec <= pold)
+ goto round;
+
+ /* Update the cache */
+ pold = prec /*MPFR_PREC_MIN + prec + __gmpfr_ceil_exp2 (prec)*/;
+ MPFR_ASSERTD (pold >= prec);
+ mpfr_prec_round (cache->x, pold, GMP_RNDN);
+ cache->inexact = (*cache->func) (cache->x, GMP_RNDN);
+
+ round:
+ /* First check if the cache has the exact value (Unlikely)
+ Else the exact value is between (assuming x=cache->x > 0)
+ x and x+ulp(x) if cache->inexact < 0
+ x-ulp(x) and x if cache->inexact > 0
+ and abs(x-exact) <= ulp(x)/2 */
+ MPFR_ASSERTD (MPFR_IS_POS(cache->x)); /* TODO...*/
+
+ inexact = mpfr_set (dest, cache->x, rnd);
+ if (MPFR_LIKELY(cache->inexact != 0))
+ {
+ switch (rnd)
+ {
+ case GMP_RNDZ:
+ case GMP_RNDD:
+ if (MPFR_UNLIKELY(inexact == 0))
+ {
+ inexact = cache->inexact;
+ if (inexact > 0)
+ mpfr_sub_one_ulp (dest, rnd);
+ }
+ break;
+ case GMP_RNDU:
+ if (MPFR_UNLIKELY(inexact == 0))
+ {
+ inexact = cache->inexact;
+ if (inexact < 0)
+ mpfr_add_one_ulp (dest, rnd);
+ }
+ break;
+ default: /* GMP_RNDN */
+ if (MPFR_UNLIKELY(inexact == MPFR_EVEN_INEX ||
+ inexact == -MPFR_EVEN_INEX))
+ {
+ if (cache->inexact < 0)
+ mpfr_add_one_ulp (dest, rnd);
+ else
+ mpfr_sub_one_ulp (dest, rnd);
+ inexact = -inexact;
+ }
+ else if (MPFR_UNLIKELY(inexact == 0))
+ inexact = cache->inexact;
+ break;
+ }
+ }
+ return inexact;
+}