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