1/* mpf_sqrt_ui -- Compute the square root of an unsigned integer.
2
3Copyright 1993, 1994, 1996, 2000, 2001, 2004, 2005 Free Software Foundation,
4Inc.
5
6This file is part of the GNU MP Library.
7
8The GNU MP Library is free software; you can redistribute it and/or modify
9it under the terms of the GNU Lesser General Public License as published by
10the Free Software Foundation; either version 3 of the License, or (at your
11option) any later version.
12
13The GNU MP Library is distributed in the hope that it will be useful, but
14WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
16License for more details.
17
18You should have received a copy of the GNU Lesser General Public License
19along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
20
21#include <stdio.h> /* for NULL */
22#include "gmp.h"
23#include "gmp-impl.h"
24
25
26/* As usual the aim is to produce PREC(r) limbs of result with the high limb
27   non-zero.  That high limb will end up floor(sqrt(u)), and limbs below are
28   produced by padding the input with zeros, two for each desired result
29   limb, being 2*(prec-1) for a total 2*prec-1 limbs passed to mpn_sqrtrem.
30   The way mpn_sqrtrem calculates floor(sqrt(x)) ensures the root is correct
31   to the intended accuracy, ie. truncated to prec limbs.
32
33   With nails, u might be two limbs, in which case a total 2*prec limbs is
34   passed to mpn_sqrtrem (still giving a prec limb result).  If uhigh is
35   zero we adjust back to 2*prec-1, since mpn_sqrtrem requires the high
36   non-zero.  2*prec limbs are always allocated, even when uhigh is zero, so
37   the store of uhigh can be done without a conditional.
38
39   u==0 is a special case so the rest of the code can assume the result is
40   non-zero (ie. will have a non-zero high limb on the result).
41
42   Not done:
43
44   No attempt is made to identify perfect squares.  It's considered this can
45   be left to an application if it might occur with any frequency.  As it
46   stands, mpn_sqrtrem does its normal amount of work on a perfect square
47   followed by zero limbs, though of course only an mpn_sqrtrem1 would be
48   actually needed.  We also end up leaving our mpf result with lots of low
49   trailing zeros, slowing down subsequent operations.
50
51   We're not aware of any optimizations that can be made using the fact the
52   input has lots of trailing zeros (apart from the perfect square
53   case).  */
54
55
56/* 1 if we (might) need two limbs for u */
57#define U2   (GMP_NUMB_BITS < BITS_PER_ULONG)
58
59void
60mpf_sqrt_ui (mpf_ptr r, unsigned long int u)
61{
62  mp_size_t rsize, zeros;
63  mp_ptr tp;
64  mp_size_t prec;
65  TMP_DECL;
66
67  if (UNLIKELY (u == 0))
68    {
69      r->_mp_size = 0;
70      r->_mp_exp = 0;
71      return;
72    }
73
74  TMP_MARK;
75
76  prec = r->_mp_prec;
77  zeros = 2 * prec - 2;
78  rsize = zeros + 1 + U2;
79
80  tp = TMP_ALLOC_LIMBS (rsize);
81
82  MPN_ZERO (tp, zeros);
83  tp[zeros] = u & GMP_NUMB_MASK;
84
85#if U2
86  {
87    mp_limb_t uhigh = u >> GMP_NUMB_BITS;
88    tp[zeros + 1] = uhigh;
89    rsize -= (uhigh == 0);
90  }
91#endif
92
93  mpn_sqrtrem (r->_mp_d, NULL, tp, rsize);
94
95  r->_mp_size = prec;
96  r->_mp_exp = 1;
97  TMP_FREE;
98}
99