beta-0.89.2
[luatex.git] / source / libs / mpfr / mpfr-3.1.3 / src / cache.c
blob1ddddf3b86f84442e871ed9ffdf115949f9bb0b3
1 /* mpfr_cache -- cache interface for multiple-precision constants in MPFR.
3 Copyright 2004-2015 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
6 This file is part of the GNU MPFR Library.
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include "mpfr-impl.h"
25 #if 0 /* this function is not used/documented/tested so far, it could be
26 useful if some user wants to add a new constant to mpfr, and
27 implement a cache mechanism for that constant */
28 void
29 mpfr_init_cache (mpfr_cache_t cache, int (*func)(mpfr_ptr, mpfr_rnd_t))
31 MPFR_PREC (cache->x) = 0; /* Invalid prec to detect that the cache is not
32 valid. Maybe add a flag? */
33 cache->func = func;
35 #endif
37 void
38 mpfr_clear_cache (mpfr_cache_t cache)
40 if (MPFR_PREC (cache->x) != 0)
41 mpfr_clear (cache->x);
42 MPFR_PREC (cache->x) = 0;
45 int
46 mpfr_cache (mpfr_ptr dest, mpfr_cache_t cache, mpfr_rnd_t rnd)
48 mpfr_prec_t prec = MPFR_PREC (dest);
49 mpfr_prec_t pold = MPFR_PREC (cache->x);
50 int inexact, sign;
51 MPFR_SAVE_EXPO_DECL (expo);
53 MPFR_SAVE_EXPO_MARK (expo);
55 if (MPFR_UNLIKELY (prec > pold))
57 /* No previous result in the cache or the precision of the
58 previous result is not sufficient. */
60 if (MPFR_UNLIKELY (pold == 0)) /* No previous result. */
61 mpfr_init2 (cache->x, prec);
63 /* Update the cache. */
64 pold = prec;
65 /* no need to keep the previous value */
66 mpfr_set_prec (cache->x, pold);
67 cache->inexact = (*cache->func) (cache->x, MPFR_RNDN);
70 /* now pold >= prec is the precision of cache->x */
72 /* First, check if the cache has the exact value (unlikely).
73 Else the exact value is between (assuming x=cache->x > 0):
74 x and x+ulp(x) if cache->inexact < 0,
75 x-ulp(x) and x if cache->inexact > 0,
76 and abs(x-exact) <= ulp(x)/2. */
78 /* we assume all cached constants are positive */
79 MPFR_ASSERTN (MPFR_IS_POS (cache->x)); /* TODO... */
80 sign = MPFR_SIGN (cache->x);
81 MPFR_SET_EXP (dest, MPFR_GET_EXP (cache->x));
82 MPFR_SET_SIGN (dest, sign);
84 /* round cache->x from precision pold down to precision prec */
85 MPFR_RNDRAW_GEN (inexact, dest,
86 MPFR_MANT (cache->x), pold, rnd, sign,
87 if (MPFR_UNLIKELY (cache->inexact == 0))
89 if ((_sp[0] & _ulp) == 0)
91 inexact = -sign;
92 goto trunc_doit;
94 else
95 goto addoneulp;
97 else if (cache->inexact < 0)
98 goto addoneulp;
99 else /* cache->inexact > 0 */
101 inexact = -sign;
102 goto trunc_doit;
104 if (MPFR_UNLIKELY (++MPFR_EXP (dest) > __gmpfr_emax))
105 mpfr_overflow (dest, rnd, sign);
108 if (MPFR_LIKELY (cache->inexact != 0))
110 switch (rnd)
112 case MPFR_RNDZ:
113 case MPFR_RNDD:
114 if (MPFR_UNLIKELY (inexact == 0))
116 inexact = cache->inexact;
117 if (inexact > 0)
119 mpfr_nextbelow (dest);
120 inexact = -inexact;
123 break;
124 case MPFR_RNDU:
125 case MPFR_RNDA:
126 if (MPFR_UNLIKELY (inexact == 0))
128 inexact = cache->inexact;
129 if (inexact < 0)
131 mpfr_nextabove (dest);
132 inexact = -inexact;
135 break;
136 default: /* MPFR_RNDN */
137 if (MPFR_UNLIKELY(inexact == 0))
138 inexact = cache->inexact;
139 break;
143 MPFR_SAVE_EXPO_FREE (expo);
144 return mpfr_check_range (dest, inexact, rnd);