beta-0.89.2
[luatex.git] / source / libs / mpfr / mpfr-3.1.3 / src / ui_div.c
blobac1efb3b9846df88876cfddbe374d2de4f3f8ad9
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
4 Copyright 2000-2015 Free Software Foundation, Inc.
5 Contributed by the AriC and Caramel projects, INRIA.
7 This file is part of the GNU MPFR Library.
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.
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.
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. */
25 #define MPFR_NEED_LONGLONG_H
26 #include "mpfr-impl.h"
28 int
29 mpfr_ui_div (mpfr_ptr y, unsigned long int u, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
31 MPFR_LOG_FUNC
32 (("u=%lu x[%Pu]=%.*Rg rnd=%d",
33 u, mpfr_get_prec(x), mpfr_log_prec, x, rnd_mode),
34 ("y[%Pu]=%.*Rg", mpfr_get_prec(y), mpfr_log_prec, y));
36 if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(x)))
38 if (MPFR_IS_NAN(x))
40 MPFR_SET_NAN(y);
41 MPFR_RET_NAN;
43 else if (MPFR_IS_INF(x)) /* u/Inf = 0 */
45 MPFR_SET_ZERO(y);
46 MPFR_SET_SAME_SIGN(y,x);
47 MPFR_RET(0);
49 else /* u / 0 */
51 MPFR_ASSERTD(MPFR_IS_ZERO(x));
52 if (u)
54 /* u > 0, so y = sign(x) * Inf */
55 MPFR_SET_SAME_SIGN(y, x);
56 MPFR_SET_INF(y);
57 mpfr_set_divby0 ();
58 MPFR_RET(0);
60 else
62 /* 0 / 0 */
63 MPFR_SET_NAN(y);
64 MPFR_RET_NAN;
68 else if (MPFR_LIKELY(u != 0))
70 mpfr_t uu;
71 mp_limb_t up[1];
72 int cnt;
73 int inex;
75 MPFR_SAVE_EXPO_DECL (expo);
77 MPFR_TMP_INIT1(up, uu, GMP_NUMB_BITS);
78 MPFR_ASSERTN(u == (mp_limb_t) u);
79 count_leading_zeros(cnt, (mp_limb_t) u);
80 up[0] = (mp_limb_t) u << cnt;
82 /* Optimization note: Exponent save/restore operations may be
83 removed if mpfr_div works even when uu is out-of-range. */
84 MPFR_SAVE_EXPO_MARK (expo);
85 MPFR_SET_EXP (uu, GMP_NUMB_BITS - cnt);
86 inex = mpfr_div (y, uu, x, rnd_mode);
87 MPFR_SAVE_EXPO_UPDATE_FLAGS (expo, __gmpfr_flags);
88 MPFR_SAVE_EXPO_FREE (expo);
89 return mpfr_check_range (y, inex, rnd_mode);
91 else /* u = 0, and x != 0 */
93 MPFR_SET_ZERO(y); /* if u=0, then set y to 0 */
94 MPFR_SET_SAME_SIGN(y, x); /* u considered as +0: sign(+0/x) = sign(x) */
95 MPFR_RET(0);
101 mpfr_si_div (mpfr_ptr y, long int u, mpfr_srcptr x, mpfr_rnd_t rnd_mode)
103 int res;
105 if (u >= 0)
106 res = mpfr_ui_div (y, u, x, rnd_mode);
107 else
109 res = -mpfr_ui_div (y, -u, x, MPFR_INVERT_RND(rnd_mode));
110 MPFR_CHANGE_SIGN (y);
112 return res;