xref: /dragonfly/contrib/gmp/mpf/sqrt.c (revision 86d7f5d3)
1*86d7f5d3SJohn Marino /* mpf_sqrt -- Compute the square root of a float.
2*86d7f5d3SJohn Marino 
3*86d7f5d3SJohn Marino Copyright 1993, 1994, 1996, 2000, 2001, 2004, 2005 Free Software Foundation,
4*86d7f5d3SJohn Marino Inc.
5*86d7f5d3SJohn Marino 
6*86d7f5d3SJohn Marino This file is part of the GNU MP Library.
7*86d7f5d3SJohn Marino 
8*86d7f5d3SJohn Marino The GNU MP Library is free software; you can redistribute it and/or modify
9*86d7f5d3SJohn Marino it under the terms of the GNU Lesser General Public License as published by
10*86d7f5d3SJohn Marino the Free Software Foundation; either version 3 of the License, or (at your
11*86d7f5d3SJohn Marino option) any later version.
12*86d7f5d3SJohn Marino 
13*86d7f5d3SJohn Marino The GNU MP Library is distributed in the hope that it will be useful, but
14*86d7f5d3SJohn Marino WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15*86d7f5d3SJohn Marino or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16*86d7f5d3SJohn Marino License for more details.
17*86d7f5d3SJohn Marino 
18*86d7f5d3SJohn Marino You should have received a copy of the GNU Lesser General Public License
19*86d7f5d3SJohn Marino along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
20*86d7f5d3SJohn Marino 
21*86d7f5d3SJohn Marino #include <stdio.h> /* for NULL */
22*86d7f5d3SJohn Marino #include "gmp.h"
23*86d7f5d3SJohn Marino #include "gmp-impl.h"
24*86d7f5d3SJohn Marino 
25*86d7f5d3SJohn Marino 
26*86d7f5d3SJohn Marino /* As usual, the aim is to produce PREC(r) limbs of result, with the high
27*86d7f5d3SJohn Marino    limb non-zero.  This is accomplished by applying mpn_sqrtrem to either
28*86d7f5d3SJohn Marino    2*prec or 2*prec-1 limbs, both such sizes resulting in prec limbs.
29*86d7f5d3SJohn Marino 
30*86d7f5d3SJohn Marino    The choice between 2*prec or 2*prec-1 limbs is based on the input
31*86d7f5d3SJohn Marino    exponent.  With b=2^GMP_NUMB_BITS the limb base then we can think of
32*86d7f5d3SJohn Marino    effectively taking out a factor b^(2k), for suitable k, to get to an
33*86d7f5d3SJohn Marino    integer input of the desired size ready for mpn_sqrtrem.  It must be an
34*86d7f5d3SJohn Marino    even power taken out, ie. an even number of limbs, so the square root
35*86d7f5d3SJohn Marino    gives factor b^k and the radix point is still on a limb boundary.  So if
36*86d7f5d3SJohn Marino    EXP(r) is even we'll get an even number of input limbs 2*prec, or if
37*86d7f5d3SJohn Marino    EXP(r) is odd we get an odd number 2*prec-1.
38*86d7f5d3SJohn Marino 
39*86d7f5d3SJohn Marino    Further limbs below the 2*prec or 2*prec-1 used don't affect the result
40*86d7f5d3SJohn Marino    and are simply truncated.  This can be seen by considering an integer x,
41*86d7f5d3SJohn Marino    with s=floor(sqrt(x)).  s is the unique integer satisfying s^2 <= x <
42*86d7f5d3SJohn Marino    (s+1)^2.  Notice that adding a fraction part to x (ie. some further bits)
43*86d7f5d3SJohn Marino    doesn't change the inequality, s remains the unique solution.  Working
44*86d7f5d3SJohn Marino    suitable factors of 2 into this argument lets it apply to an intended
45*86d7f5d3SJohn Marino    precision at any position for any x, not just the integer binary point.
46*86d7f5d3SJohn Marino 
47*86d7f5d3SJohn Marino    If the input is smaller than 2*prec or 2*prec-1, then we just pad with
48*86d7f5d3SJohn Marino    zeros, that of course being our usual interpretation of short inputs.
49*86d7f5d3SJohn Marino    The effect is to extend the root beyond the size of the input (for
50*86d7f5d3SJohn Marino    instance into fractional limbs if u is an integer).  */
51*86d7f5d3SJohn Marino 
52*86d7f5d3SJohn Marino void
mpf_sqrt(mpf_ptr r,mpf_srcptr u)53*86d7f5d3SJohn Marino mpf_sqrt (mpf_ptr r, mpf_srcptr u)
54*86d7f5d3SJohn Marino {
55*86d7f5d3SJohn Marino   mp_size_t usize;
56*86d7f5d3SJohn Marino   mp_ptr up, tp;
57*86d7f5d3SJohn Marino   mp_size_t prec, tsize;
58*86d7f5d3SJohn Marino   mp_exp_t uexp, expodd;
59*86d7f5d3SJohn Marino   TMP_DECL;
60*86d7f5d3SJohn Marino 
61*86d7f5d3SJohn Marino   usize = u->_mp_size;
62*86d7f5d3SJohn Marino   if (usize <= 0)
63*86d7f5d3SJohn Marino     {
64*86d7f5d3SJohn Marino       if (usize < 0)
65*86d7f5d3SJohn Marino         SQRT_OF_NEGATIVE;
66*86d7f5d3SJohn Marino       r->_mp_size = 0;
67*86d7f5d3SJohn Marino       r->_mp_exp = 0;
68*86d7f5d3SJohn Marino       return;
69*86d7f5d3SJohn Marino     }
70*86d7f5d3SJohn Marino 
71*86d7f5d3SJohn Marino   TMP_MARK;
72*86d7f5d3SJohn Marino 
73*86d7f5d3SJohn Marino   uexp = u->_mp_exp;
74*86d7f5d3SJohn Marino   prec = r->_mp_prec;
75*86d7f5d3SJohn Marino   up = u->_mp_d;
76*86d7f5d3SJohn Marino 
77*86d7f5d3SJohn Marino   expodd = (uexp & 1);
78*86d7f5d3SJohn Marino   tsize = 2 * prec - expodd;
79*86d7f5d3SJohn Marino   r->_mp_size = prec;
80*86d7f5d3SJohn Marino   r->_mp_exp = (uexp + expodd) / 2;    /* ceil(uexp/2) */
81*86d7f5d3SJohn Marino 
82*86d7f5d3SJohn Marino   /* root size is ceil(tsize/2), this will be our desired "prec" limbs */
83*86d7f5d3SJohn Marino   ASSERT ((tsize + 1) / 2 == prec);
84*86d7f5d3SJohn Marino 
85*86d7f5d3SJohn Marino   tp = TMP_ALLOC_LIMBS (tsize);
86*86d7f5d3SJohn Marino 
87*86d7f5d3SJohn Marino   if (usize > tsize)
88*86d7f5d3SJohn Marino     {
89*86d7f5d3SJohn Marino       up += usize - tsize;
90*86d7f5d3SJohn Marino       usize = tsize;
91*86d7f5d3SJohn Marino       MPN_COPY (tp, up, tsize);
92*86d7f5d3SJohn Marino     }
93*86d7f5d3SJohn Marino   else
94*86d7f5d3SJohn Marino     {
95*86d7f5d3SJohn Marino       MPN_ZERO (tp, tsize - usize);
96*86d7f5d3SJohn Marino       MPN_COPY (tp + (tsize - usize), up, usize);
97*86d7f5d3SJohn Marino     }
98*86d7f5d3SJohn Marino 
99*86d7f5d3SJohn Marino   mpn_sqrtrem (r->_mp_d, NULL, tp, tsize);
100*86d7f5d3SJohn Marino 
101*86d7f5d3SJohn Marino   TMP_FREE;
102*86d7f5d3SJohn Marino }
103