fpu_sqrt.c revision 1.3 1 1.3 lukem /* $NetBSD: fpu_sqrt.c,v 1.3 2003/07/15 02:43:11 lukem Exp $ */
2 1.1 briggs
3 1.1 briggs /*
4 1.1 briggs * Copyright (c) 1992, 1993
5 1.1 briggs * The Regents of the University of California. All rights reserved.
6 1.1 briggs *
7 1.1 briggs * This software was developed by the Computer Systems Engineering group
8 1.1 briggs * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 briggs * contributed to Berkeley.
10 1.1 briggs *
11 1.1 briggs * All advertising materials mentioning features or use of this software
12 1.1 briggs * must display the following acknowledgement:
13 1.1 briggs * This product includes software developed by the University of
14 1.1 briggs * California, Lawrence Berkeley Laboratory.
15 1.1 briggs *
16 1.1 briggs * Redistribution and use in source and binary forms, with or without
17 1.1 briggs * modification, are permitted provided that the following conditions
18 1.1 briggs * are met:
19 1.1 briggs * 1. Redistributions of source code must retain the above copyright
20 1.1 briggs * notice, this list of conditions and the following disclaimer.
21 1.1 briggs * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 briggs * notice, this list of conditions and the following disclaimer in the
23 1.1 briggs * documentation and/or other materials provided with the distribution.
24 1.1 briggs * 3. All advertising materials mentioning features or use of this software
25 1.1 briggs * must display the following acknowledgement:
26 1.1 briggs * This product includes software developed by the University of
27 1.1 briggs * California, Berkeley and its contributors.
28 1.1 briggs * 4. Neither the name of the University nor the names of its contributors
29 1.1 briggs * may be used to endorse or promote products derived from this software
30 1.1 briggs * without specific prior written permission.
31 1.1 briggs *
32 1.1 briggs * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 1.1 briggs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 1.1 briggs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 1.1 briggs * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 1.1 briggs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 1.1 briggs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 1.1 briggs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 1.1 briggs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 1.1 briggs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 1.1 briggs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 1.1 briggs * SUCH DAMAGE.
43 1.1 briggs *
44 1.1 briggs * @(#)fpu_sqrt.c 8.1 (Berkeley) 6/11/93
45 1.1 briggs */
46 1.1 briggs
47 1.1 briggs /*
48 1.1 briggs * Perform an FPU square root (return sqrt(x)).
49 1.1 briggs */
50 1.3 lukem
51 1.3 lukem #include <sys/cdefs.h>
52 1.3 lukem __KERNEL_RCSID(0, "$NetBSD: fpu_sqrt.c,v 1.3 2003/07/15 02:43:11 lukem Exp $");
53 1.1 briggs
54 1.1 briggs #include <sys/types.h>
55 1.1 briggs
56 1.1 briggs #include <machine/reg.h>
57 1.1 briggs
58 1.1 briggs #include "fpu_arith.h"
59 1.1 briggs #include "fpu_emulate.h"
60 1.1 briggs
61 1.1 briggs /*
62 1.1 briggs * Our task is to calculate the square root of a floating point number x0.
63 1.1 briggs * This number x normally has the form:
64 1.1 briggs *
65 1.1 briggs * exp
66 1.1 briggs * x = mant * 2 (where 1 <= mant < 2 and exp is an integer)
67 1.1 briggs *
68 1.1 briggs * This can be left as it stands, or the mantissa can be doubled and the
69 1.1 briggs * exponent decremented:
70 1.1 briggs *
71 1.1 briggs * exp-1
72 1.1 briggs * x = (2 * mant) * 2 (where 2 <= 2 * mant < 4)
73 1.1 briggs *
74 1.1 briggs * If the exponent `exp' is even, the square root of the number is best
75 1.1 briggs * handled using the first form, and is by definition equal to:
76 1.1 briggs *
77 1.1 briggs * exp/2
78 1.1 briggs * sqrt(x) = sqrt(mant) * 2
79 1.1 briggs *
80 1.1 briggs * If exp is odd, on the other hand, it is convenient to use the second
81 1.1 briggs * form, giving:
82 1.1 briggs *
83 1.1 briggs * (exp-1)/2
84 1.1 briggs * sqrt(x) = sqrt(2 * mant) * 2
85 1.1 briggs *
86 1.1 briggs * In the first case, we have
87 1.1 briggs *
88 1.1 briggs * 1 <= mant < 2
89 1.1 briggs *
90 1.1 briggs * and therefore
91 1.1 briggs *
92 1.1 briggs * sqrt(1) <= sqrt(mant) < sqrt(2)
93 1.1 briggs *
94 1.1 briggs * while in the second case we have
95 1.1 briggs *
96 1.1 briggs * 2 <= 2*mant < 4
97 1.1 briggs *
98 1.1 briggs * and therefore
99 1.1 briggs *
100 1.1 briggs * sqrt(2) <= sqrt(2*mant) < sqrt(4)
101 1.1 briggs *
102 1.1 briggs * so that in any case, we are sure that
103 1.1 briggs *
104 1.1 briggs * sqrt(1) <= sqrt(n * mant) < sqrt(4), n = 1 or 2
105 1.1 briggs *
106 1.1 briggs * or
107 1.1 briggs *
108 1.1 briggs * 1 <= sqrt(n * mant) < 2, n = 1 or 2.
109 1.1 briggs *
110 1.1 briggs * This root is therefore a properly formed mantissa for a floating
111 1.1 briggs * point number. The exponent of sqrt(x) is either exp/2 or (exp-1)/2
112 1.1 briggs * as above. This leaves us with the problem of finding the square root
113 1.1 briggs * of a fixed-point number in the range [1..4).
114 1.1 briggs *
115 1.1 briggs * Though it may not be instantly obvious, the following square root
116 1.1 briggs * algorithm works for any integer x of an even number of bits, provided
117 1.1 briggs * that no overflows occur:
118 1.1 briggs *
119 1.1 briggs * let q = 0
120 1.1 briggs * for k = NBITS-1 to 0 step -1 do -- for each digit in the answer...
121 1.1 briggs * x *= 2 -- multiply by radix, for next digit
122 1.1 briggs * if x >= 2q + 2^k then -- if adding 2^k does not
123 1.1 briggs * x -= 2q + 2^k -- exceed the correct root,
124 1.1 briggs * q += 2^k -- add 2^k and adjust x
125 1.1 briggs * fi
126 1.1 briggs * done
127 1.1 briggs * sqrt = q / 2^(NBITS/2) -- (and any remainder is in x)
128 1.1 briggs *
129 1.1 briggs * If NBITS is odd (so that k is initially even), we can just add another
130 1.1 briggs * zero bit at the top of x. Doing so means that q is not going to acquire
131 1.1 briggs * a 1 bit in the first trip around the loop (since x0 < 2^NBITS). If the
132 1.1 briggs * final value in x is not needed, or can be off by a factor of 2, this is
133 1.1 briggs * equivalant to moving the `x *= 2' step to the bottom of the loop:
134 1.1 briggs *
135 1.1 briggs * for k = NBITS-1 to 0 step -1 do if ... fi; x *= 2; done
136 1.1 briggs *
137 1.1 briggs * and the result q will then be sqrt(x0) * 2^floor(NBITS / 2).
138 1.1 briggs * (Since the algorithm is destructive on x, we will call x's initial
139 1.1 briggs * value, for which q is some power of two times its square root, x0.)
140 1.1 briggs *
141 1.1 briggs * If we insert a loop invariant y = 2q, we can then rewrite this using
142 1.1 briggs * C notation as:
143 1.1 briggs *
144 1.1 briggs * q = y = 0; x = x0;
145 1.1 briggs * for (k = NBITS; --k >= 0;) {
146 1.1 briggs * #if (NBITS is even)
147 1.1 briggs * x *= 2;
148 1.1 briggs * #endif
149 1.1 briggs * t = y + (1 << k);
150 1.1 briggs * if (x >= t) {
151 1.1 briggs * x -= t;
152 1.1 briggs * q += 1 << k;
153 1.1 briggs * y += 1 << (k + 1);
154 1.1 briggs * }
155 1.1 briggs * #if (NBITS is odd)
156 1.1 briggs * x *= 2;
157 1.1 briggs * #endif
158 1.1 briggs * }
159 1.1 briggs *
160 1.1 briggs * If x0 is fixed point, rather than an integer, we can simply alter the
161 1.1 briggs * scale factor between q and sqrt(x0). As it happens, we can easily arrange
162 1.1 briggs * for the scale factor to be 2**0 or 1, so that sqrt(x0) == q.
163 1.1 briggs *
164 1.1 briggs * In our case, however, x0 (and therefore x, y, q, and t) are multiword
165 1.1 briggs * integers, which adds some complication. But note that q is built one
166 1.1 briggs * bit at a time, from the top down, and is not used itself in the loop
167 1.1 briggs * (we use 2q as held in y instead). This means we can build our answer
168 1.1 briggs * in an integer, one word at a time, which saves a bit of work. Also,
169 1.1 briggs * since 1 << k is always a `new' bit in q, 1 << k and 1 << (k+1) are
170 1.1 briggs * `new' bits in y and we can set them with an `or' operation rather than
171 1.1 briggs * a full-blown multiword add.
172 1.1 briggs *
173 1.1 briggs * We are almost done, except for one snag. We must prove that none of our
174 1.1 briggs * intermediate calculations can overflow. We know that x0 is in [1..4)
175 1.1 briggs * and therefore the square root in q will be in [1..2), but what about x,
176 1.1 briggs * y, and t?
177 1.1 briggs *
178 1.1 briggs * We know that y = 2q at the beginning of each loop. (The relation only
179 1.1 briggs * fails temporarily while y and q are being updated.) Since q < 2, y < 4.
180 1.1 briggs * The sum in t can, in our case, be as much as y+(1<<1) = y+2 < 6, and.
181 1.1 briggs * Furthermore, we can prove with a bit of work that x never exceeds y by
182 1.1 briggs * more than 2, so that even after doubling, 0 <= x < 8. (This is left as
183 1.1 briggs * an exercise to the reader, mostly because I have become tired of working
184 1.1 briggs * on this comment.)
185 1.1 briggs *
186 1.1 briggs * If our floating point mantissas (which are of the form 1.frac) occupy
187 1.1 briggs * B+1 bits, our largest intermediary needs at most B+3 bits, or two extra.
188 1.1 briggs * In fact, we want even one more bit (for a carry, to avoid compares), or
189 1.1 briggs * three extra. There is a comment in fpu_emu.h reminding maintainers of
190 1.1 briggs * this, so we have some justification in assuming it.
191 1.1 briggs */
192 1.1 briggs struct fpn *
193 1.1 briggs fpu_sqrt(fe)
194 1.1 briggs struct fpemu *fe;
195 1.1 briggs {
196 1.1 briggs register struct fpn *x = &fe->fe_f2;
197 1.1 briggs register u_int bit, q, tt;
198 1.2 briggs register u_int x0, x1, x2;
199 1.2 briggs register u_int y0, y1, y2;
200 1.2 briggs register u_int d0, d1, d2;
201 1.1 briggs register int e;
202 1.1 briggs FPU_DECL_CARRY
203 1.1 briggs
204 1.1 briggs /*
205 1.1 briggs * Take care of special cases first. In order:
206 1.1 briggs *
207 1.1 briggs * sqrt(NaN) = NaN
208 1.1 briggs * sqrt(+0) = +0
209 1.1 briggs * sqrt(-0) = -0
210 1.1 briggs * sqrt(x < 0) = NaN (including sqrt(-Inf))
211 1.1 briggs * sqrt(+Inf) = +Inf
212 1.1 briggs *
213 1.1 briggs * Then all that remains are numbers with mantissas in [1..2).
214 1.1 briggs */
215 1.1 briggs if (ISNAN(x) || ISZERO(x))
216 1.1 briggs return (x);
217 1.1 briggs if (x->fp_sign)
218 1.1 briggs return (fpu_newnan(fe));
219 1.1 briggs if (ISINF(x))
220 1.1 briggs return (x);
221 1.1 briggs
222 1.1 briggs /*
223 1.1 briggs * Calculate result exponent. As noted above, this may involve
224 1.1 briggs * doubling the mantissa. We will also need to double x each
225 1.1 briggs * time around the loop, so we define a macro for this here, and
226 1.1 briggs * we break out the multiword mantissa.
227 1.1 briggs */
228 1.1 briggs #ifdef FPU_SHL1_BY_ADD
229 1.1 briggs #define DOUBLE_X { \
230 1.2 briggs FPU_ADDS(x2, x2, x2); \
231 1.1 briggs FPU_ADDCS(x1, x1, x1); FPU_ADDC(x0, x0, x0); \
232 1.1 briggs }
233 1.1 briggs #else
234 1.1 briggs #define DOUBLE_X { \
235 1.1 briggs x0 = (x0 << 1) | (x1 >> 31); x1 = (x1 << 1) | (x2 >> 31); \
236 1.2 briggs x2 <<= 1; \
237 1.1 briggs }
238 1.1 briggs #endif
239 1.1 briggs #if (FP_NMANT & 1) != 0
240 1.1 briggs # define ODD_DOUBLE DOUBLE_X
241 1.1 briggs # define EVEN_DOUBLE /* nothing */
242 1.1 briggs #else
243 1.1 briggs # define ODD_DOUBLE /* nothing */
244 1.1 briggs # define EVEN_DOUBLE DOUBLE_X
245 1.1 briggs #endif
246 1.1 briggs x0 = x->fp_mant[0];
247 1.1 briggs x1 = x->fp_mant[1];
248 1.1 briggs x2 = x->fp_mant[2];
249 1.1 briggs e = x->fp_exp;
250 1.1 briggs if (e & 1) /* exponent is odd; use sqrt(2mant) */
251 1.1 briggs DOUBLE_X;
252 1.1 briggs /* THE FOLLOWING ASSUMES THAT RIGHT SHIFT DOES SIGN EXTENSION */
253 1.1 briggs x->fp_exp = e >> 1; /* calculates (e&1 ? (e-1)/2 : e/2 */
254 1.1 briggs
255 1.1 briggs /*
256 1.1 briggs * Now calculate the mantissa root. Since x is now in [1..4),
257 1.1 briggs * we know that the first trip around the loop will definitely
258 1.1 briggs * set the top bit in q, so we can do that manually and start
259 1.1 briggs * the loop at the next bit down instead. We must be sure to
260 1.1 briggs * double x correctly while doing the `known q=1.0'.
261 1.1 briggs *
262 1.1 briggs * We do this one mantissa-word at a time, as noted above, to
263 1.1 briggs * save work. To avoid `(1 << 31) << 1', we also do the top bit
264 1.1 briggs * outside of each per-word loop.
265 1.1 briggs *
266 1.1 briggs * The calculation `t = y + bit' breaks down into `t0 = y0, ...,
267 1.2 briggs * t2 = y2, t? |= bit' for the appropriate word. Since the bit
268 1.1 briggs * is always a `new' one, this means that three of the `t?'s are
269 1.1 briggs * just the corresponding `y?'; we use `#define's here for this.
270 1.1 briggs * The variable `tt' holds the actual `t?' variable.
271 1.1 briggs */
272 1.1 briggs
273 1.1 briggs /* calculate q0 */
274 1.1 briggs #define t0 tt
275 1.1 briggs bit = FP_1;
276 1.1 briggs EVEN_DOUBLE;
277 1.1 briggs /* if (x >= (t0 = y0 | bit)) { */ /* always true */
278 1.1 briggs q = bit;
279 1.1 briggs x0 -= bit;
280 1.1 briggs y0 = bit << 1;
281 1.1 briggs /* } */
282 1.1 briggs ODD_DOUBLE;
283 1.1 briggs while ((bit >>= 1) != 0) { /* for remaining bits in q0 */
284 1.1 briggs EVEN_DOUBLE;
285 1.1 briggs t0 = y0 | bit; /* t = y + bit */
286 1.1 briggs if (x0 >= t0) { /* if x >= t then */
287 1.1 briggs x0 -= t0; /* x -= t */
288 1.1 briggs q |= bit; /* q += bit */
289 1.1 briggs y0 |= bit << 1; /* y += bit << 1 */
290 1.1 briggs }
291 1.1 briggs ODD_DOUBLE;
292 1.1 briggs }
293 1.1 briggs x->fp_mant[0] = q;
294 1.1 briggs #undef t0
295 1.1 briggs
296 1.1 briggs /* calculate q1. note (y0&1)==0. */
297 1.1 briggs #define t0 y0
298 1.1 briggs #define t1 tt
299 1.1 briggs q = 0;
300 1.1 briggs y1 = 0;
301 1.1 briggs bit = 1 << 31;
302 1.1 briggs EVEN_DOUBLE;
303 1.1 briggs t1 = bit;
304 1.1 briggs FPU_SUBS(d1, x1, t1);
305 1.1 briggs FPU_SUBC(d0, x0, t0); /* d = x - t */
306 1.1 briggs if ((int)d0 >= 0) { /* if d >= 0 (i.e., x >= t) then */
307 1.1 briggs x0 = d0, x1 = d1; /* x -= t */
308 1.1 briggs q = bit; /* q += bit */
309 1.1 briggs y0 |= 1; /* y += bit << 1 */
310 1.1 briggs }
311 1.1 briggs ODD_DOUBLE;
312 1.1 briggs while ((bit >>= 1) != 0) { /* for remaining bits in q1 */
313 1.1 briggs EVEN_DOUBLE; /* as before */
314 1.1 briggs t1 = y1 | bit;
315 1.1 briggs FPU_SUBS(d1, x1, t1);
316 1.1 briggs FPU_SUBC(d0, x0, t0);
317 1.1 briggs if ((int)d0 >= 0) {
318 1.1 briggs x0 = d0, x1 = d1;
319 1.1 briggs q |= bit;
320 1.1 briggs y1 |= bit << 1;
321 1.1 briggs }
322 1.1 briggs ODD_DOUBLE;
323 1.1 briggs }
324 1.1 briggs x->fp_mant[1] = q;
325 1.1 briggs #undef t1
326 1.1 briggs
327 1.1 briggs /* calculate q2. note (y1&1)==0; y0 (aka t0) is fixed. */
328 1.1 briggs #define t1 y1
329 1.1 briggs #define t2 tt
330 1.1 briggs q = 0;
331 1.1 briggs y2 = 0;
332 1.1 briggs bit = 1 << 31;
333 1.1 briggs EVEN_DOUBLE;
334 1.1 briggs t2 = bit;
335 1.1 briggs FPU_SUBS(d2, x2, t2);
336 1.1 briggs FPU_SUBCS(d1, x1, t1);
337 1.1 briggs FPU_SUBC(d0, x0, t0);
338 1.1 briggs if ((int)d0 >= 0) {
339 1.1 briggs x0 = d0, x1 = d1, x2 = d2;
340 1.1 briggs q |= bit;
341 1.1 briggs y1 |= 1; /* now t1, y1 are set in concrete */
342 1.1 briggs }
343 1.1 briggs ODD_DOUBLE;
344 1.1 briggs while ((bit >>= 1) != 0) {
345 1.1 briggs EVEN_DOUBLE;
346 1.1 briggs t2 = y2 | bit;
347 1.1 briggs FPU_SUBS(d2, x2, t2);
348 1.1 briggs FPU_SUBCS(d1, x1, t1);
349 1.1 briggs FPU_SUBC(d0, x0, t0);
350 1.1 briggs if ((int)d0 >= 0) {
351 1.1 briggs x0 = d0, x1 = d1, x2 = d2;
352 1.1 briggs q |= bit;
353 1.1 briggs y2 |= bit << 1;
354 1.1 briggs }
355 1.1 briggs ODD_DOUBLE;
356 1.1 briggs }
357 1.1 briggs x->fp_mant[2] = q;
358 1.1 briggs #undef t2
359 1.1 briggs
360 1.1 briggs /*
361 1.1 briggs * The result, which includes guard and round bits, is exact iff
362 1.1 briggs * x is now zero; any nonzero bits in x represent sticky bits.
363 1.1 briggs */
364 1.2 briggs x->fp_sticky = x0 | x1 | x2;
365 1.1 briggs return (x);
366 1.1 briggs }
367