Merge branch 'vendor/BINUTILS220' into bu220
[dragonfly.git] / contrib / mpfr / cache.c
1 /* mpfr_cache -- cache interface for multi-precision const in MPFR.
2
3 Copyright 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Contributed by the Arenaire and Cacao projects, INRIA.
5
6 This file is part of the GNU MPFR Library.
7
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.
12
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.
17
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. */
22
23 #include "mpfr-impl.h"
24
25 void
26 mpfr_init_cache (mpfr_cache_t cache, int (*func)(mpfr_ptr, mp_rnd_t))
27 {
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;
31 }
32
33 void
34 mpfr_clear_cache (mpfr_cache_t cache)
35 {
36   if (MPFR_PREC (cache->x) != 0)
37     mpfr_clear (cache->x);
38   MPFR_PREC (cache->x) = 0;
39 }
40
41 int
42 mpfr_cache (mpfr_ptr dest, mpfr_cache_t cache, mp_rnd_t rnd)
43 {
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);
48
49   MPFR_SAVE_EXPO_MARK (expo);
50
51   if (MPFR_UNLIKELY (prec > pold))
52     {
53       /* No previous result in the cache or the precision of the
54          previous result is not sufficient. */
55
56       if (MPFR_UNLIKELY (pold == 0))  /* No previous result. */
57         mpfr_init2 (cache->x, prec);
58
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);
63     }
64
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))
77                      {
78                        if ((_sp[0] & _ulp) == 0)
79                          {
80                            inexact = -sign;
81                            goto trunc_doit;
82                          }
83                        else
84                          goto addoneulp;
85                      }
86                    else if (cache->inexact < 0)
87                      goto addoneulp;
88                    else
89                      {
90                        inexact = -sign;
91                        goto trunc_doit;
92                      },
93                    if (MPFR_UNLIKELY (++MPFR_EXP (dest) > __gmpfr_emax))
94                      mpfr_overflow (dest, rnd, sign);
95                   );
96   if (MPFR_LIKELY (cache->inexact != 0))
97     {
98       switch (rnd)
99         {
100         case GMP_RNDZ:
101         case GMP_RNDD:
102           if (MPFR_UNLIKELY (inexact == 0))
103             {
104               inexact = cache->inexact;
105               if (inexact > 0)
106                 mpfr_nextbelow (dest);
107             }
108           break;
109         case GMP_RNDU:
110           if (MPFR_UNLIKELY (inexact == 0))
111             {
112               inexact = cache->inexact;
113               if (inexact < 0)
114                 mpfr_nextabove (dest);
115             }
116           break;
117         default: /* GMP_RNDN */
118           if (MPFR_UNLIKELY(inexact == 0))
119             inexact = cache->inexact;
120           break;
121         }
122     }
123
124   MPFR_SAVE_EXPO_FREE (expo);
125   return mpfr_check_range (dest, inexact, rnd);
126 }