summaryrefslogtreecommitdiff
path: root/src/isqrt.c
blob: 906673b1ce7457cf21d12a20ea9114cefafc0609 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* __gmpfr_isqrt && __gmpfr_cuberoot -- Integer square root and cube root

Copyright 2004-2014 Free Software Foundation, Inc.
Contributed by the AriC and Caramel projects, INRIA.

This file is part of the GNU MPFR Library.

The GNU MPFR Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

The GNU MPFR Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */

#include "mpfr-impl.h"

/* returns floor(sqrt(n)) */
unsigned long
__gmpfr_isqrt (unsigned long n)
{
  unsigned long i, s;

  /* First find an approximation to floor(sqrt(n)) of the form 2^k. */
  i = n;
  s = 1;
  while (i >= 2)
    {
      i >>= 2;
      s <<= 1;
    }

  do
    {
      s = (s + n / s) / 2;
    }
  while (!(s*s <= n && (s*s > s*(s+2) || n <= s*(s+2))));
  /* Short explanation: As mathematically s*(s+2) < 2*ULONG_MAX,
     the condition s*s > s*(s+2) is evaluated as true when s*(s+2)
     "overflows" but not s*s. This implies that mathematically, one
     has s*s <= n <= s*(s+2). If s*s "overflows", this means that n
     is "large" and the inequality n <= s*(s+2) cannot be satisfied. */
  return s;
}

/* returns floor(n^(1/3)) */
unsigned long
__gmpfr_cuberoot (unsigned long n)
{
  unsigned long i, s;

  /* First find an approximation to floor(cbrt(n)) of the form 2^k. */
  i = n;
  s = 1;
  while (i >= 4)
    {
      i >>= 3;
      s <<= 1;
    }

  /* Improve the approximation (this is necessary if n is large, so that
     mathematically (s+1)*(s+1)*(s+1) isn't much larger than ULONG_MAX). */
  if (n >= 256)
    {
      s = (2 * s + n / (s * s)) / 3;
      s = (2 * s + n / (s * s)) / 3;
      s = (2 * s + n / (s * s)) / 3;
    }

  do
    {
      s = (2 * s + n / (s * s)) / 3;
    }
  while (!(s*s*s <= n && (s*s*s > (s+1)*(s+1)*(s+1) ||
                          n < (s+1)*(s+1)*(s+1))));
  return s;
}