Actually hook powernow.4 into the build.
[dragonfly.git] / contrib / mpfr / cache.c
blob4c6015fb7a92cd372661efc2a508f1a223d3852c
1 /* mpfr_cache -- cache interface for multi-precision const in MPFR.
3 Copyright 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao 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 2.1 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.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #include "mpfr-impl.h"
25 void
26 mpfr_init_cache (mpfr_cache_t cache, int (*func)(mpfr_ptr, mp_rnd_t))
28 MPFR_PREC (cache->x) = 0; /* Invalid prec to detect that the cache is not
29 valid. Maybe add a flag? */
30 cache->func = func;
33 void
34 mpfr_clear_cache (mpfr_cache_t cache)
36 if (MPFR_PREC (cache->x) != 0)
37 mpfr_clear (cache->x);
38 MPFR_PREC (cache->x) = 0;
41 int
42 mpfr_cache (mpfr_ptr dest, mpfr_cache_t cache, mp_rnd_t rnd)
44 mp_prec_t prec = MPFR_PREC (dest);
45 mp_prec_t pold = MPFR_PREC (cache->x);
46 int inexact, sign;
47 MPFR_SAVE_EXPO_DECL (expo);
49 MPFR_SAVE_EXPO_MARK (expo);
51 if (MPFR_UNLIKELY (prec > pold))
53 /* No previous result in the cache or the precision of the
54 previous result is not sufficient. */
56 if (MPFR_UNLIKELY (pold == 0)) /* No previous result. */
57 mpfr_init2 (cache->x, prec);
59 /* Update the cache. */
60 pold = prec;
61 mpfr_prec_round (cache->x, pold, GMP_RNDN);
62 cache->inexact = (*cache->func) (cache->x, GMP_RNDN);
65 /* First, check if the cache has the exact value (unlikely).
66 Else the exact value is between (assuming x=cache->x > 0):
67 x and x+ulp(x) if cache->inexact < 0,
68 x-ulp(x) and x if cache->inexact > 0,
69 and abs(x-exact) <= ulp(x)/2. */
70 MPFR_ASSERTN (MPFR_IS_POS (cache->x)); /* TODO... */
71 sign = MPFR_SIGN (cache->x);
72 MPFR_SET_EXP (dest, MPFR_GET_EXP (cache->x));
73 MPFR_SET_SIGN (dest, sign);
74 MPFR_RNDRAW_GEN (inexact, dest,
75 MPFR_MANT (cache->x), MPFR_PREC (cache->x), rnd, sign,
76 if (MPFR_UNLIKELY (cache->inexact == 0))
78 if ((_sp[0] & _ulp) == 0)
80 inexact = -sign;
81 goto trunc_doit;
83 else
84 goto addoneulp;
86 else if (cache->inexact < 0)
87 goto addoneulp;
88 else
90 inexact = -sign;
91 goto trunc_doit;
93 if (MPFR_UNLIKELY (++MPFR_EXP (dest) > __gmpfr_emax))
94 mpfr_overflow (dest, rnd, sign);
96 if (MPFR_LIKELY (cache->inexact != 0))
98 switch (rnd)
100 case GMP_RNDZ:
101 case GMP_RNDD:
102 if (MPFR_UNLIKELY (inexact == 0))
104 inexact = cache->inexact;
105 if (inexact > 0)
106 mpfr_nextbelow (dest);
108 break;
109 case GMP_RNDU:
110 if (MPFR_UNLIKELY (inexact == 0))
112 inexact = cache->inexact;
113 if (inexact < 0)
114 mpfr_nextabove (dest);
116 break;
117 default: /* GMP_RNDN */
118 if (MPFR_UNLIKELY(inexact == 0))
119 inexact = cache->inexact;
120 break;
124 MPFR_SAVE_EXPO_FREE (expo);
125 return mpfr_check_range (dest, inexact, rnd);