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