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