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