xref: /dflybsd-src/contrib/gmp/mpf/sqrt.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* mpf_sqrt -- Compute the square root of a float.
286d7f5d3SJohn Marino 
386d7f5d3SJohn Marino Copyright 1993, 1994, 1996, 2000, 2001, 2004, 2005 Free Software Foundation,
486d7f5d3SJohn Marino Inc.
586d7f5d3SJohn Marino 
686d7f5d3SJohn Marino This file is part of the GNU MP Library.
786d7f5d3SJohn Marino 
886d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
986d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
1086d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
1186d7f5d3SJohn Marino option) any later version.
1286d7f5d3SJohn Marino 
1386d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
1486d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1586d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
1686d7f5d3SJohn Marino License for more details.
1786d7f5d3SJohn Marino 
1886d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
1986d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
2086d7f5d3SJohn Marino 
2186d7f5d3SJohn Marino #include <stdio.h> /* for NULL */
2286d7f5d3SJohn Marino #include "gmp.h"
2386d7f5d3SJohn Marino #include "gmp-impl.h"
2486d7f5d3SJohn Marino 
2586d7f5d3SJohn Marino 
2686d7f5d3SJohn Marino /* As usual, the aim is to produce PREC(r) limbs of result, with the high
2786d7f5d3SJohn Marino    limb non-zero.  This is accomplished by applying mpn_sqrtrem to either
2886d7f5d3SJohn Marino    2*prec or 2*prec-1 limbs, both such sizes resulting in prec limbs.
2986d7f5d3SJohn Marino 
3086d7f5d3SJohn Marino    The choice between 2*prec or 2*prec-1 limbs is based on the input
3186d7f5d3SJohn Marino    exponent.  With b=2^GMP_NUMB_BITS the limb base then we can think of
3286d7f5d3SJohn Marino    effectively taking out a factor b^(2k), for suitable k, to get to an
3386d7f5d3SJohn Marino    integer input of the desired size ready for mpn_sqrtrem.  It must be an
3486d7f5d3SJohn Marino    even power taken out, ie. an even number of limbs, so the square root
3586d7f5d3SJohn Marino    gives factor b^k and the radix point is still on a limb boundary.  So if
3686d7f5d3SJohn Marino    EXP(r) is even we'll get an even number of input limbs 2*prec, or if
3786d7f5d3SJohn Marino    EXP(r) is odd we get an odd number 2*prec-1.
3886d7f5d3SJohn Marino 
3986d7f5d3SJohn Marino    Further limbs below the 2*prec or 2*prec-1 used don't affect the result
4086d7f5d3SJohn Marino    and are simply truncated.  This can be seen by considering an integer x,
4186d7f5d3SJohn Marino    with s=floor(sqrt(x)).  s is the unique integer satisfying s^2 <= x <
4286d7f5d3SJohn Marino    (s+1)^2.  Notice that adding a fraction part to x (ie. some further bits)
4386d7f5d3SJohn Marino    doesn't change the inequality, s remains the unique solution.  Working
4486d7f5d3SJohn Marino    suitable factors of 2 into this argument lets it apply to an intended
4586d7f5d3SJohn Marino    precision at any position for any x, not just the integer binary point.
4686d7f5d3SJohn Marino 
4786d7f5d3SJohn Marino    If the input is smaller than 2*prec or 2*prec-1, then we just pad with
4886d7f5d3SJohn Marino    zeros, that of course being our usual interpretation of short inputs.
4986d7f5d3SJohn Marino    The effect is to extend the root beyond the size of the input (for
5086d7f5d3SJohn Marino    instance into fractional limbs if u is an integer).  */
5186d7f5d3SJohn Marino 
5286d7f5d3SJohn Marino void
mpf_sqrt(mpf_ptr r,mpf_srcptr u)5386d7f5d3SJohn Marino mpf_sqrt (mpf_ptr r, mpf_srcptr u)
5486d7f5d3SJohn Marino {
5586d7f5d3SJohn Marino   mp_size_t usize;
5686d7f5d3SJohn Marino   mp_ptr up, tp;
5786d7f5d3SJohn Marino   mp_size_t prec, tsize;
5886d7f5d3SJohn Marino   mp_exp_t uexp, expodd;
5986d7f5d3SJohn Marino   TMP_DECL;
6086d7f5d3SJohn Marino 
6186d7f5d3SJohn Marino   usize = u->_mp_size;
6286d7f5d3SJohn Marino   if (usize <= 0)
6386d7f5d3SJohn Marino     {
6486d7f5d3SJohn Marino       if (usize < 0)
6586d7f5d3SJohn Marino         SQRT_OF_NEGATIVE;
6686d7f5d3SJohn Marino       r->_mp_size = 0;
6786d7f5d3SJohn Marino       r->_mp_exp = 0;
6886d7f5d3SJohn Marino       return;
6986d7f5d3SJohn Marino     }
7086d7f5d3SJohn Marino 
7186d7f5d3SJohn Marino   TMP_MARK;
7286d7f5d3SJohn Marino 
7386d7f5d3SJohn Marino   uexp = u->_mp_exp;
7486d7f5d3SJohn Marino   prec = r->_mp_prec;
7586d7f5d3SJohn Marino   up = u->_mp_d;
7686d7f5d3SJohn Marino 
7786d7f5d3SJohn Marino   expodd = (uexp & 1);
7886d7f5d3SJohn Marino   tsize = 2 * prec - expodd;
7986d7f5d3SJohn Marino   r->_mp_size = prec;
8086d7f5d3SJohn Marino   r->_mp_exp = (uexp + expodd) / 2;    /* ceil(uexp/2) */
8186d7f5d3SJohn Marino 
8286d7f5d3SJohn Marino   /* root size is ceil(tsize/2), this will be our desired "prec" limbs */
8386d7f5d3SJohn Marino   ASSERT ((tsize + 1) / 2 == prec);
8486d7f5d3SJohn Marino 
8586d7f5d3SJohn Marino   tp = TMP_ALLOC_LIMBS (tsize);
8686d7f5d3SJohn Marino 
8786d7f5d3SJohn Marino   if (usize > tsize)
8886d7f5d3SJohn Marino     {
8986d7f5d3SJohn Marino       up += usize - tsize;
9086d7f5d3SJohn Marino       usize = tsize;
9186d7f5d3SJohn Marino       MPN_COPY (tp, up, tsize);
9286d7f5d3SJohn Marino     }
9386d7f5d3SJohn Marino   else
9486d7f5d3SJohn Marino     {
9586d7f5d3SJohn Marino       MPN_ZERO (tp, tsize - usize);
9686d7f5d3SJohn Marino       MPN_COPY (tp + (tsize - usize), up, usize);
9786d7f5d3SJohn Marino     }
9886d7f5d3SJohn Marino 
9986d7f5d3SJohn Marino   mpn_sqrtrem (r->_mp_d, NULL, tp, tsize);
10086d7f5d3SJohn Marino 
10186d7f5d3SJohn Marino   TMP_FREE;
10286d7f5d3SJohn Marino }
103