math/mpreduce.h: Missing include files.
[u/mdw/catacomb] / math / mp-sqrt.c
1 /* -*-c-*-
2 *
3 * Compute integer square roots
4 *
5 * (c) 2000 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "mp.h"
31
32 /*----- Main code ---------------------------------------------------------*/
33
34 /* --- @mp_sqrt@ --- *
35 *
36 * Arguments: @mp *d@ = pointer to destination integer
37 * @mp *a@ = (nonnegative) integer to take square root of
38 *
39 * Returns: The largest integer %$x$% such that %$x^2 \le a$%.
40 *
41 * Use: Computes integer square roots.
42 *
43 * The current implementation isn't very good: it uses the
44 * Newton-Raphson method to find an approximation to %$a$%. If
45 * there's any demand for a better version, I'll write one.
46 */
47
48 mp *mp_sqrt(mp *d, mp *a)
49 {
50 unsigned long z;
51 mp *q = MP_NEW, *r = MP_NEW;
52
53 /* --- Sanity preservation --- */
54
55 assert(!MP_NEGP(a));
56
57 /* --- Deal with trivial cases --- */
58
59 MP_SHRINK(a);
60 if (MP_ZEROP(a)) {
61 mp_drop(d);
62 return (MP_ZERO);
63 }
64
65 /* --- Find an initial guess of about the right size --- */
66
67 z = mp_bits(a);
68 z >>= 1;
69 mp_copy(a);
70 d = mp_lsr(d, a, z);
71
72 /* --- Main approximation --- *
73 *
74 * We use the Newton-Raphson recurrence relation
75 *
76 * %$x_{i+1} = x_i - \frac{x_i^2 - a}{2 x_i}$%
77 *
78 * We inspect the term %$q = x^2 - a$% to see when to stop. Increasing
79 * %$x$% is pointless when %$-q < 2 x + 1$%.
80 */
81
82 for (;;) {
83 q = mp_sqr(q, d);
84 q = mp_sub(q, q, a);
85 if (MP_ZEROP(q))
86 break;
87 if (MP_NEGP(q)) {
88 r = mp_lsl(r, d, 1);
89 r->f |= MP_NEG;
90 if (MP_CMP(q, >=, r))
91 break;
92 }
93 mp_div(&r, &q, q, d);
94 r = mp_lsr(r, r, 1);
95 if (r->v == r->vl)
96 d = mp_sub(d, d, MP_ONE);
97 else
98 d = mp_sub(d, d, r);
99 }
100
101 /* --- Finished, at last --- */
102
103 mp_drop(a);
104 mp_drop(q);
105 mp_drop(r);
106 return (d);
107 }
108
109 /*----- Test rig ----------------------------------------------------------*/
110
111 #ifdef TEST_RIG
112
113 #include <mLib/testrig.h>
114
115 static int verify(dstr *v)
116 {
117 mp *a = *(mp **)v[0].buf;
118 mp *qq = *(mp **)v[1].buf;
119 mp *q = mp_sqrt(MP_NEW, a);
120 int ok = 1;
121
122 if (!MP_EQ(q, qq)) {
123 ok = 0;
124 fputs("\n*** sqrt failed", stderr);
125 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10);
126 fputs("\n*** result = ", stderr); mp_writefile(q, stderr, 10);
127 fputs("\n*** expect = ", stderr); mp_writefile(qq, stderr, 10);
128 fputc('\n', stderr);
129 }
130
131 mp_drop(a);
132 mp_drop(q);
133 mp_drop(qq);
134 assert(mparena_count(MPARENA_GLOBAL) == 0);
135
136 return (ok);
137 }
138
139 static test_chunk tests[] = {
140 { "sqrt", verify, { &type_mp, &type_mp, 0 } },
141 { 0, 0, { 0 } },
142 };
143
144 int main(int argc, char *argv[])
145 {
146 sub_init();
147 test_run(argc, argv, tests, SRCDIR "/t/mp");
148 return (0);
149 }
150
151 #endif
152
153 /*----- That's all, folks -------------------------------------------------*/