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