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