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