1 /* mpfr_sqrt -- square root of a floating-point number
3 Copyright 1999-2015 Free Software Foundation, Inc.
4 Contributed by the AriC and Caramel projects, INRIA.
6 This file is part of the GNU MPFR Library.
8 The GNU MPFR Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
13 The GNU MPFR Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16 License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with the GNU MPFR Library; see the file COPYING.LESSER. If not, see
20 http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
21 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include "mpfr-impl.h"
26 mpfr_sqrt (mpfr_ptr r
, mpfr_srcptr u
, mpfr_rnd_t rnd_mode
)
28 mp_size_t rsize
; /* number of limbs of r (plus 1 if exact limb multiple) */
30 mp_size_t usize
; /* number of limbs of u */
31 mp_size_t tsize
; /* number of limbs of the sqrtrem remainder */
34 mpfr_limb_ptr rp
, rp0
;
37 mp_limb_t sticky0
; /* truncated part of input */
38 mp_limb_t sticky1
; /* truncated part of rp[0] */
41 int sh
; /* number of extra bits in rp[0] */
42 int inexact
; /* return ternary flag */
44 MPFR_TMP_DECL(marker
);
47 (("x[%Pu]=%.*Rg rnd=%d", mpfr_get_prec (u
), mpfr_log_prec
, u
, rnd_mode
),
48 ("y[%Pu]=%.*Rg inexact=%d",
49 mpfr_get_prec (r
), mpfr_log_prec
, r
, inexact
));
51 if (MPFR_UNLIKELY(MPFR_IS_SINGULAR(u
)))
58 else if (MPFR_IS_ZERO(u
))
61 MPFR_SET_SAME_SIGN(r
, u
);
63 MPFR_RET(0); /* zero is exact */
67 MPFR_ASSERTD(MPFR_IS_INF(u
));
68 /* sqrt(-Inf) = NAN */
79 if (MPFR_UNLIKELY(MPFR_IS_NEG(u
)))
86 MPFR_TMP_MARK (marker
);
87 MPFR_UNSIGNED_MINUS_MODULO(sh
,MPFR_PREC(r
));
88 if (sh
== 0 && rnd_mode
== MPFR_RNDN
)
89 sh
= GMP_NUMB_BITS
; /* ugly case */
90 rsize
= MPFR_LIMB_SIZE(r
) + (sh
== GMP_NUMB_BITS
);
91 /* rsize is the number of limbs of r + 1 if exact limb multiple and rounding
92 to nearest, this is the number of wanted limbs for the square root */
93 rrsize
= rsize
+ rsize
;
94 usize
= MPFR_LIMB_SIZE(u
); /* number of limbs of u */
96 rp
= (sh
< GMP_NUMB_BITS
) ? rp0
: MPFR_TMP_LIMBS_ALLOC (rsize
);
98 sticky0
= MPFR_LIMB_ZERO
; /* truncated part of input */
99 sticky1
= MPFR_LIMB_ZERO
; /* truncated part of rp[0] */
100 odd_exp
= (unsigned int) MPFR_GET_EXP (u
) & 1;
101 inexact
= -1; /* return ternary flag */
103 sp
= MPFR_TMP_LIMBS_ALLOC (rrsize
);
105 /* copy the most significant limbs of u to {sp, rrsize} */
106 if (MPFR_LIKELY(usize
<= rrsize
)) /* in case r and u have the same precision,
107 we have indeed rrsize = 2 * usize */
115 sp
[k
- 1] = mpn_rshift (sp
+ k
, up
, usize
, 1);
117 sticky0
= mpn_rshift (sp
, up
, usize
, 1);
120 MPN_COPY (sp
+ rrsize
- usize
, up
, usize
);
122 else /* usize > rrsize: truncate the input */
126 sticky0
= mpn_rshift (sp
, up
+ k
, rrsize
, 1);
128 MPN_COPY (sp
, up
+ k
, rrsize
);
130 while (sticky0
== MPFR_LIMB_ZERO
&& l
!= 0)
134 /* sticky0 is non-zero iff the truncated part of the input is non-zero */
136 /* mpn_rootrem with NULL 2nd argument is faster than mpn_sqrtrem, thus use
137 it if available and if the user asked to use GMP internal functions */
138 #if defined(WANT_GMP_INTERNALS) && defined(HAVE___GMPN_ROOTREM)
139 tsize
= __gmpn_rootrem (rp
, NULL
, sp
, rrsize
, 2);
141 tsize
= mpn_sqrtrem (rp
, NULL
, sp
, rrsize
);
144 /* a return value of zero in mpn_sqrtrem indicates a perfect square */
145 sticky
= sticky0
|| tsize
!= 0;
147 /* truncate low bits of rp[0] */
148 sticky1
= rp
[0] & ((sh
< GMP_NUMB_BITS
) ? MPFR_LIMB_MASK(sh
)
152 sticky
= sticky
|| sticky1
;
154 expr
= (MPFR_GET_EXP(u
) + odd_exp
) / 2; /* exact */
156 if (rnd_mode
== MPFR_RNDZ
|| rnd_mode
== MPFR_RNDD
|| sticky
== MPFR_LIMB_ZERO
)
158 inexact
= (sticky
== MPFR_LIMB_ZERO
) ? 0 : -1;
161 else if (rnd_mode
== MPFR_RNDN
)
163 /* if sh < GMP_NUMB_BITS, the round bit is bit (sh-1) of sticky1
164 and the sticky bit is formed by the low sh-1 bits from
165 sticky1, together with the sqrtrem remainder and sticky0. */
166 if (sh
< GMP_NUMB_BITS
)
168 if (sticky1
& (MPFR_LIMB_ONE
<< (sh
- 1)))
169 { /* round bit is set */
170 if (sticky1
== (MPFR_LIMB_ONE
<< (sh
- 1)) && tsize
== 0
176 else /* round bit is zero */
177 goto truncate
; /* with the default inexact=-1 */
179 else /* sh = GMP_NUMB_BITS: the round bit is the most significant bit
180 of rp[0], and the remaining GMP_NUMB_BITS-1 bits contribute to
183 if (sticky1
& MPFR_LIMB_HIGHBIT
)
184 { /* round bit is set */
185 if (sticky1
== MPFR_LIMB_HIGHBIT
&& tsize
== 0 && sticky0
== 0)
190 else /* round bit is zero */
191 goto truncate
; /* with the default inexact=-1 */
194 else /* rnd_mode=GMP_RDNU, necessarily sticky <> 0, thus add 1 ulp */
197 even_rule
: /* has to set inexact */
198 if (sh
< GMP_NUMB_BITS
)
199 inexact
= (rp
[0] & (MPFR_LIMB_ONE
<< sh
)) ? 1 : -1;
201 inexact
= (rp
[1] & MPFR_LIMB_ONE
) ? 1 : -1;
204 /* else go through add_one_ulp */
207 inexact
= 1; /* always here */
208 if (sh
== GMP_NUMB_BITS
)
214 if (mpn_add_1 (rp0
, rp
, rsize
, MPFR_LIMB_ONE
<< sh
))
217 rp
[rsize
- 1] = MPFR_LIMB_HIGHBIT
;
221 truncate
: /* inexact = 0 or -1 */
222 if (sh
== GMP_NUMB_BITS
)
223 MPN_COPY (rp0
, rp
+ 1, rsize
- 1);
226 MPFR_ASSERTN (expr
>= MPFR_EMIN_MIN
&& expr
<= MPFR_EMAX_MAX
);
228 MPFR_TMP_FREE(marker
);
230 return mpfr_check_range (r
, inexact
, rnd_mode
);