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