1 /* mpfr_mul_ui -- multiply a floating-point number by a machine integer
2 mpfr_mul_si -- multiply a floating-point number by a machine integer
4 Copyright 1999-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. */
24 #define MPFR_NEED_LONGLONG_H
25 #include "mpfr-impl.h"
28 mpfr_mul_ui (mpfr_ptr y
, mpfr_srcptr x
, unsigned long int u
, mpfr_rnd_t rnd_mode
)
33 MPFR_TMP_DECL (marker
);
35 if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x
)))
42 else if (MPFR_IS_INF (x
))
47 MPFR_SET_SAME_SIGN (y
, x
);
48 MPFR_RET (0); /* infinity is exact */
50 else /* 0 * infinity */
58 MPFR_ASSERTD (MPFR_IS_ZERO (x
));
60 MPFR_SET_SAME_SIGN (y
, x
);
61 MPFR_RET (0); /* zero is exact */
64 else if (MPFR_UNLIKELY (u
<= 1))
69 MPFR_SET_SAME_SIGN (y
, x
);
70 MPFR_RET (0); /* zero is exact */
73 return mpfr_set (y
, x
, rnd_mode
);
75 else if (MPFR_UNLIKELY (IS_POW2 (u
)))
76 return mpfr_mul_2si (y
, x
, MPFR_INT_CEIL_LOG2 (u
), rnd_mode
);
79 xn
= MPFR_LIMB_SIZE (x
);
81 MPFR_ASSERTD (xn
< MP_SIZE_T_MAX
);
82 MPFR_TMP_MARK(marker
);
83 yp
= MPFR_TMP_LIMBS_ALLOC (xn
+ 1);
85 MPFR_ASSERTN (u
== (mp_limb_t
) u
);
86 yp
[xn
] = mpn_mul_1 (yp
, MPFR_MANT (x
), xn
, u
);
88 /* x * u is stored in yp[xn], ..., yp[0] */
90 /* since the case u=1 was treated above, we have u >= 2, thus
91 yp[xn] >= 1 since x was msb-normalized */
92 MPFR_ASSERTD (yp
[xn
] != 0);
93 if (MPFR_LIKELY (MPFR_LIMB_MSB (yp
[xn
]) == 0))
95 count_leading_zeros (cnt
, yp
[xn
]);
96 mpn_lshift (yp
, yp
, xn
+ 1, cnt
);
103 /* now yp[xn], ..., yp[0] is msb-normalized too, and has at most
104 PREC(x) + (GMP_NUMB_BITS - cnt) non-zero bits */
105 MPFR_RNDRAW (inexact
, y
, yp
, (mpfr_prec_t
) (xn
+ 1) * GMP_NUMB_BITS
,
106 rnd_mode
, MPFR_SIGN (x
), cnt
-- );
108 MPFR_TMP_FREE (marker
);
110 cnt
= GMP_NUMB_BITS
- cnt
;
111 if (MPFR_UNLIKELY (__gmpfr_emax
< MPFR_EMAX_MIN
+ cnt
112 || MPFR_GET_EXP (x
) > __gmpfr_emax
- cnt
))
113 return mpfr_overflow (y
, rnd_mode
, MPFR_SIGN(x
));
115 MPFR_SET_EXP (y
, MPFR_GET_EXP (x
) + cnt
);
116 MPFR_SET_SAME_SIGN (y
, x
);
121 int mpfr_mul_si (mpfr_ptr y
, mpfr_srcptr x
, long int u
, mpfr_rnd_t rnd_mode
)
126 res
= mpfr_mul_ui (y
, x
, u
, rnd_mode
);
129 res
= -mpfr_mul_ui (y
, x
, -u
, MPFR_INVERT_RND (rnd_mode
));
130 MPFR_CHANGE_SIGN (y
);