Merge branch 'vendor/GCC44'
[dragonfly.git] / contrib / mpfr / 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 Free Software Foundation, Inc.
5 Contributed by the Arenaire and Cacao 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 2.1 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.LIB.  If not, write to
21 the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
22 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, mp_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, BITS_PER_MP_LIMB);
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, BITS_PER_MP_LIMB - 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
84 int mpfr_si_div (mpfr_ptr y, long int u, mpfr_srcptr x,mp_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 }