Import mpfr-2.4.1
[dragonfly.git] / contrib / mpfr / frac.c
1 /* mpfr_frac -- Fractional part of a floating-point number.
2
3 Copyright 2002, 2003, 2004, 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
24 #define MPFR_NEED_LONGLONG_H
25 #include "mpfr-impl.h"
26
27 /* Optimization note: it is not a good idea to call mpfr_integer_p,
28    as some cases will take longer (the number may be parsed twice). */
29
30 int
31 mpfr_frac (mpfr_ptr r, mpfr_srcptr u, mp_rnd_t rnd_mode)
32 {
33   mp_exp_t re, ue;
34   mp_prec_t uq;
35   mp_size_t un, tn, t0;
36   mp_limb_t *up, *tp, k;
37   int sh;
38   mpfr_t tmp;
39   mpfr_ptr t;
40
41   /* Special cases */
42   if (MPFR_UNLIKELY(MPFR_IS_NAN(u)))
43     {
44       MPFR_SET_NAN(r);
45       MPFR_RET_NAN;
46     }
47   else if (MPFR_UNLIKELY(MPFR_IS_INF(u) || mpfr_integer_p (u)))
48     {
49       MPFR_CLEAR_FLAGS(r);
50       MPFR_SET_SAME_SIGN(r, u);
51       MPFR_SET_ZERO(r);
52       MPFR_RET(0);  /* zero is exact */
53     }
54
55   ue = MPFR_GET_EXP (u);
56   if (ue <= 0)  /* |u| < 1 */
57     return mpfr_set (r, u, rnd_mode);
58
59   uq = MPFR_PREC(u);
60   un = (uq - 1) / BITS_PER_MP_LIMB;  /* index of most significant limb */
61   un -= (mp_size_t) (ue / BITS_PER_MP_LIMB);
62   /* now the index of the MSL containing bits of the fractional part */
63
64   up = MPFR_MANT(u);
65   sh = ue % BITS_PER_MP_LIMB;
66   k = up[un] << sh;
67   /* the first bit of the fractional part is the MSB of k */
68
69   if (k != 0)
70     {
71       int cnt;
72
73       count_leading_zeros(cnt, k);
74       /* first bit 1 of the fractional part -> MSB of the number */
75       re = -cnt;
76       sh += cnt;
77       MPFR_ASSERTN (sh < BITS_PER_MP_LIMB);
78       k <<= cnt;
79     }
80   else
81     {
82       re = sh - BITS_PER_MP_LIMB;
83       /* searching for the first bit 1 (exists since u isn't an integer) */
84       while (up[--un] == 0)
85         re -= BITS_PER_MP_LIMB;
86       MPFR_ASSERTN(un >= 0);
87       k = up[un];
88       count_leading_zeros(sh, k);
89       re -= sh;
90       k <<= sh;
91     }
92   /* The exponent of r will be re */
93   /* un: index of the limb of u that contains the first bit 1 of the FP */
94
95   t = (mp_size_t) (MPFR_PREC(r) - 1) / BITS_PER_MP_LIMB < un ?
96     (mpfr_init2 (tmp, (un + 1) * BITS_PER_MP_LIMB), tmp) : r;
97   /* t has enough precision to contain the fractional part of u */
98   /* If we use a temporary variable, we take the non-significant bits
99      of u into account, because of the mpn_lshift below. */
100   MPFR_CLEAR_FLAGS(t);
101   MPFR_SET_SAME_SIGN(t, u);
102   MPFR_SET_EXP (t, re);
103
104   /* Put the fractional part of u into t */
105   tn = (MPFR_PREC(t) - 1) / BITS_PER_MP_LIMB;
106   MPFR_ASSERTN(tn >= un);
107   t0 = tn - un;
108   tp = MPFR_MANT(t);
109   if (sh == 0)
110     MPN_COPY_DECR(tp + t0, up, un + 1);
111   else /* warning: un may be 0 here */
112     tp[tn] = k | ((un) ? mpn_lshift (tp + t0, up, un, sh) : (mp_limb_t) 0);
113   if (t0 > 0)
114     MPN_ZERO(tp, t0);
115
116   if (t != r)
117     { /* t is tmp */
118       int inex;
119
120       inex = mpfr_set (r, t, rnd_mode);
121       mpfr_clear (t);
122       return inex;
123     }
124   else
125     MPFR_RET(0);
126 }