Upgrade MPFR from 2.4.2-p3 to 3.1.0 on the vendor branch
[dragonfly.git] / contrib / mpfr / src / ui_div.c
1 /* mpfr_ui_div -- divide a machine integer by a floating-point number
2    mpfr_si_div -- divide a machine number by a floating-point number
3
4 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 Contributed by the Arenaire and Caramel projects, INRIA.
6
7 This file is part of the GNU MPFR Library.
8
9 The GNU MPFR Library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13
14 The GNU MPFR Library is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
17 License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
21 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
22 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23
24
25 #define MPFR_NEED_LONGLONG_H
26 #include "mpfr-impl.h"
27
28 int
29 mpfr_ui_div (mpfr_ptr y, unsigned long int u, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
30 {
31   mpfr_t uu;
32   mp_limb_t up[1];
33   unsigned long cnt;
34
35   MPFR_LOG_FUNC
36     (("u=%lu x[%Pu]=%.*Rg rnd=%d",
37       u, mpfr_get_prec(x), mpfr_log_prec, x, rnd_mode),
38      ("y[%Pu]=%.*Rg", mpfr_get_prec(y), mpfr_log_prec, y));
39
40   if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(x)))
41     {
42       if (MPFR_IS_NAN(x))
43         {
44           MPFR_SET_NAN(y);
45           MPFR_RET_NAN;
46         }
47       else if (MPFR_IS_INF(x)) /* u/Inf = 0 */
48         {
49           MPFR_SET_ZERO(y);
50           MPFR_SET_SAME_SIGN(y,x);
51           MPFR_RET(0);
52         }
53       else /* u / 0 */
54         {
55           MPFR_ASSERTD(MPFR_IS_ZERO(x));
56           if (u)
57             {
58               /* u > 0, so y = sign(x) * Inf */
59               MPFR_SET_SAME_SIGN(y, x);
60               MPFR_SET_INF(y);
61               mpfr_set_divby0 ();
62               MPFR_RET(0);
63             }
64           else
65             {
66               /* 0 / 0 */
67               MPFR_SET_NAN(y);
68               MPFR_RET_NAN;
69             }
70         }
71     }
72   else if (MPFR_LIKELY(u != 0))
73     {
74       MPFR_TMP_INIT1(up, uu, GMP_NUMB_BITS);
75       MPFR_ASSERTN(u == (mp_limb_t) u);
76       count_leading_zeros(cnt, (mp_limb_t) u);
77       up[0] = (mp_limb_t) u << cnt;
78       MPFR_SET_EXP (uu, GMP_NUMB_BITS - cnt);
79       return mpfr_div (y, uu, x, rnd_mode);
80     }
81   else /* u = 0, and x != 0 */
82     {
83       MPFR_SET_ZERO(y);         /* if u=0, then set y to 0 */
84       MPFR_SET_SAME_SIGN(y, x); /* u considered as +0: sign(+0/x) = sign(x) */
85       MPFR_RET(0);
86     }
87 }
88
89
90 int
91 mpfr_si_div (mpfr_ptr y, long int u, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
92 {
93   int res;
94
95   if (u >= 0)
96     res = mpfr_ui_div (y, u, x, rnd_mode);
97   else
98     {
99       res = -mpfr_ui_div (y, -u, x, MPFR_INVERT_RND(rnd_mode));
100       MPFR_CHANGE_SIGN (y);
101     }
102   return res;
103 }