65397be7 |
1 | /* -*-c-*- |
2 | * |
4b536f42 |
3 | * $Id: mp-sqrt.c,v 1.2 2000/10/08 12:02:21 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 $ |
4b536f42 |
33 | * Revision 1.2 2000/10/08 12:02:21 mdw |
34 | * Use @MP_EQ@ instead of @MP_CMP@. |
35 | * |
65397be7 |
36 | * Revision 1.1 2000/06/22 19:01:44 mdw |
37 | * Compute (approximations to) integer square roots. |
38 | * |
39 | */ |
40 | |
41 | /*----- Header files ------------------------------------------------------*/ |
42 | |
43 | #include "mp.h" |
44 | |
45 | /*----- Main code ---------------------------------------------------------*/ |
46 | |
47 | /* --- @mp_sqrt@ --- * |
48 | * |
49 | * Arguments: @mp *d@ = pointer to destination integer |
50 | * @mp *a@ = (nonnegative) integer to take square root of |
51 | * |
52 | * Returns: The largest integer %$x$% such that %$x^2 \le a$%. |
53 | * |
54 | * Use: Computes integer square roots. |
55 | * |
56 | * The current implementation isn't very good: it uses the |
57 | * Newton-Raphson method to find an approximation to %$a$%. If |
58 | * there's any demand for a better version, I'll write one. |
59 | */ |
60 | |
61 | mp *mp_sqrt(mp *d, mp *a) |
62 | { |
63 | unsigned long z; |
64 | mp *q = MP_NEW, *r = MP_NEW; |
65 | |
66 | /* --- Sanity preservation --- */ |
67 | |
68 | assert(((void)"imaginary root in mp_sqrt", !(a->f & MP_NEG))); |
69 | |
70 | /* --- Deal with trivial cases --- */ |
71 | |
72 | MP_SHRINK(a); |
73 | if (a->v == a->vl) { |
74 | if (d) |
75 | mp_drop(d); |
76 | return (MP_ZERO); |
77 | } |
78 | |
79 | /* --- Find an initial guess of about the right size --- */ |
80 | |
81 | z = mp_bits(a); |
82 | z >>= 1; |
83 | mp_copy(a); |
84 | d = mp_lsr(d, a, z); |
85 | mp_drop(a); |
86 | |
87 | /* --- Main approximation --- * |
88 | * |
89 | * We use the Newton-Raphson recurrence relation |
90 | * |
91 | * %$x_{i+1} = x_i - \frac{x_i^2 - a}{2 x_i}$% |
92 | * |
93 | * We inspect the term %$q = x^2 - a$% to see when to stop. Increasing |
94 | * %$x$% is pointless when %$-q < 2 x + 1$%. |
95 | */ |
96 | |
97 | for (;;) { |
98 | q = mp_sqr(q, d); |
99 | q = mp_sub(q, q, a); |
100 | if (q->v == q->vl) |
101 | break; |
102 | if (q->f & MP_NEG) { |
103 | r = mp_lsl(r, d, 1); |
104 | r->f |= MP_NEG; |
105 | if (MP_CMP(q, <=, r)) |
106 | break; |
107 | } |
108 | mp_div(&r, &q, q, d); |
109 | r = mp_lsr(r, r, 1); |
110 | if (r->v == r->vl) |
111 | d = mp_sub(d, d, MP_ONE); |
112 | else |
113 | d = mp_sub(d, d, r); |
114 | } |
115 | |
116 | /* --- Finished, at last --- */ |
117 | |
118 | mp_drop(q); |
119 | if (r) |
120 | mp_drop(r); |
121 | return (d); |
122 | } |
123 | |
124 | /*----- Test rig ----------------------------------------------------------*/ |
125 | |
126 | #ifdef TEST_RIG |
127 | |
128 | #include <mLib/testrig.h> |
129 | |
130 | static int verify(dstr *v) |
131 | { |
132 | mp *a = *(mp **)v[0].buf; |
133 | mp *qq = *(mp **)v[1].buf; |
134 | mp *q = mp_sqrt(MP_NEW, a); |
135 | int ok = 1; |
136 | |
4b536f42 |
137 | if (!MP_EQ(q, qq)) { |
65397be7 |
138 | ok = 0; |
139 | fputs("\n*** sqrt failed", stderr); |
140 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10); |
141 | fputs("\n*** result = ", stderr); mp_writefile(q, stderr, 10); |
142 | fputs("\n*** expect = ", stderr); mp_writefile(qq, stderr, 10); |
143 | fputc('\n', stderr); |
144 | } |
145 | |
146 | mp_drop(a); |
147 | mp_drop(q); |
148 | mp_drop(qq); |
149 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
150 | |
151 | return (ok); |
152 | } |
153 | |
154 | static test_chunk tests[] = { |
155 | { "sqrt", verify, { &type_mp, &type_mp, 0 } }, |
156 | { 0, 0, { 0 } }, |
157 | }; |
158 | |
159 | int main(int argc, char *argv[]) |
160 | { |
161 | sub_init(); |
162 | test_run(argc, argv, tests, SRCDIR "/tests/mp"); |
163 | return (0); |
164 | } |
165 | |
166 | #endif |
167 | |
168 | /*----- That's all, folks -------------------------------------------------*/ |