/* $NetBSD: fpu_sqrt.c,v 1.4 2005/12/11 12:18:42 christos Exp $ */12/*-3* SPDX-License-Identifier: BSD-3-Clause4*5* Copyright (c) 1992, 19936* The Regents of the University of California. All rights reserved.7*8* This software was developed by the Computer Systems Engineering group9* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and10* contributed to Berkeley.11*12* All advertising materials mentioning features or use of this software13* must display the following acknowledgement:14* This product includes software developed by the University of15* California, Lawrence Berkeley Laboratory.16*17* Redistribution and use in source and binary forms, with or without18* modification, are permitted provided that the following conditions19* are met:20* 1. Redistributions of source code must retain the above copyright21* notice, this list of conditions and the following disclaimer.22* 2. Redistributions in binary form must reproduce the above copyright23* notice, this list of conditions and the following disclaimer in the24* documentation and/or other materials provided with the distribution.25* 3. Neither the name of the University nor the names of its contributors26* may be used to endorse or promote products derived from this software27* without specific prior written permission.28*29* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND30* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE31* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE32* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE33* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL34* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS35* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)36* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT37* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY38* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF39* SUCH DAMAGE.40*/4142/*43* Perform an FPU square root (return sqrt(x)).44*/4546#include <sys/types.h>47#include <sys/systm.h>4849#include <machine/fpu.h>5051#include <powerpc/fpu/fpu_arith.h>52#include <powerpc/fpu/fpu_emu.h>5354/*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* exp59* 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 the62* exponent decremented:63*64* exp-165* x = (2 * mant) * 2 (where 2 <= 2 * mant < 4)66*67* If the exponent `exp' is even, the square root of the number is best68* handled using the first form, and is by definition equal to:69*70* exp/271* sqrt(x) = sqrt(mant) * 272*73* If exp is odd, on the other hand, it is convenient to use the second74* form, giving:75*76* (exp-1)/277* sqrt(x) = sqrt(2 * mant) * 278*79* In the first case, we have80*81* 1 <= mant < 282*83* and therefore84*85* sqrt(1) <= sqrt(mant) < sqrt(2)86*87* while in the second case we have88*89* 2 <= 2*mant < 490*91* and therefore92*93* sqrt(2) <= sqrt(2*mant) < sqrt(4)94*95* so that in any case, we are sure that96*97* sqrt(1) <= sqrt(n * mant) < sqrt(4), n = 1 or 298*99* or100*101* 1 <= sqrt(n * mant) < 2, n = 1 or 2.102*103* This root is therefore a properly formed mantissa for a floating104* point number. The exponent of sqrt(x) is either exp/2 or (exp-1)/2105* as above. This leaves us with the problem of finding the square root106* of a fixed-point number in the range [1..4).107*108* Though it may not be instantly obvious, the following square root109* algorithm works for any integer x of an even number of bits, provided110* that no overflows occur:111*112* let q = 0113* for k = NBITS-1 to 0 step -1 do -- for each digit in the answer...114* x *= 2 -- multiply by radix, for next digit115* if x >= 2q + 2^k then -- if adding 2^k does not116* x -= 2q + 2^k -- exceed the correct root,117* q += 2^k -- add 2^k and adjust x118* fi119* done120* 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 another123* zero bit at the top of x. Doing so means that q is not going to acquire124* a 1 bit in the first trip around the loop (since x0 < 2^NBITS). If the125* final value in x is not needed, or can be off by a factor of 2, this is126* 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; done129*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 initial132* 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 using135* 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* #endif142* 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* #endif151* }152*153* If x0 is fixed point, rather than an integer, we can simply alter the154* scale factor between q and sqrt(x0). As it happens, we can easily arrange155* 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 multiword158* integers, which adds some complication. But note that q is built one159* bit at a time, from the top down, and is not used itself in the loop160* (we use 2q as held in y instead). This means we can build our answer161* 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) are163* `new' bits in y and we can set them with an `or' operation rather than164* a full-blown multiword add.165*166* We are almost done, except for one snag. We must prove that none of our167* 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 only172* 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 by175* more than 2, so that even after doubling, 0 <= x < 8. (This is left as176* an exercise to the reader, mostly because I have become tired of working177* on this comment.)178*179* If our floating point mantissas (which are of the form 1.frac) occupy180* 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), or182* three extra. There is a comment in fpu_emu.h reminding maintainers of183* this, so we have some justification in assuming it.184*/185struct fpn *186fpu_sqrt(struct fpemu *fe)187{188struct fpn *x = &fe->fe_f1;189u_int bit, q, tt;190u_int x0, x1, x2, x3;191u_int y0, y1, y2, y3;192u_int d0, d1, d2, d3;193int e;194FPU_DECL_CARRY;195196/*197* Take care of special cases first. In order:198*199* sqrt(NaN) = NaN200* sqrt(+0) = +0201* sqrt(-0) = -0202* sqrt(x < 0) = NaN (including sqrt(-Inf))203* sqrt(+Inf) = +Inf204*205* Then all that remains are numbers with mantissas in [1..2).206*/207DPRINTF(FPE_REG, ("fpu_sqer:\n"));208DUMPFPN(FPE_REG, x);209DPRINTF(FPE_REG, ("=>\n"));210if (ISNAN(x)) {211fe->fe_cx |= FPSCR_VXSNAN;212DUMPFPN(FPE_REG, x);213return (x);214}215if (ISZERO(x)) {216fe->fe_cx |= FPSCR_ZX;217x->fp_class = FPC_INF;218DUMPFPN(FPE_REG, x);219return (x);220}221if (x->fp_sign) {222fe->fe_cx |= FPSCR_VXSQRT;223return (fpu_newnan(fe));224}225if (ISINF(x)) {226DUMPFPN(FPE_REG, x);227return (x);228}229230/*231* Calculate result exponent. As noted above, this may involve232* doubling the mantissa. We will also need to double x each233* time around the loop, so we define a macro for this here, and234* we break out the multiword mantissa.235*/236#ifdef FPU_SHL1_BY_ADD237#define DOUBLE_X { \238FPU_ADDS(x3, x3, x3); FPU_ADDCS(x2, x2, x2); \239FPU_ADDCS(x1, x1, x1); FPU_ADDC(x0, x0, x0); \240}241#else242#define DOUBLE_X { \243x0 = (x0 << 1) | (x1 >> 31); x1 = (x1 << 1) | (x2 >> 31); \244x2 = (x2 << 1) | (x3 >> 31); x3 <<= 1; \245}246#endif247#if (FP_NMANT & 1) != 0248# define ODD_DOUBLE DOUBLE_X249# define EVEN_DOUBLE /* nothing */250#else251# define ODD_DOUBLE /* nothing */252# define EVEN_DOUBLE DOUBLE_X253#endif254x0 = x->fp_mant[0];255x1 = x->fp_mant[1];256x2 = x->fp_mant[2];257x3 = x->fp_mant[3];258e = x->fp_exp;259if (e & 1) /* exponent is odd; use sqrt(2mant) */260DOUBLE_X;261/* THE FOLLOWING ASSUMES THAT RIGHT SHIFT DOES SIGN EXTENSION */262x->fp_exp = e >> 1; /* calculates (e&1 ? (e-1)/2 : e/2 */263264/*265* Now calculate the mantissa root. Since x is now in [1..4),266* we know that the first trip around the loop will definitely267* set the top bit in q, so we can do that manually and start268* the loop at the next bit down instead. We must be sure to269* double x correctly while doing the `known q=1.0'.270*271* We do this one mantissa-word at a time, as noted above, to272* save work. To avoid `(1U << 31) << 1', we also do the top bit273* outside of each per-word loop.274*275* The calculation `t = y + bit' breaks down into `t0 = y0, ...,276* t3 = y3, t? |= bit' for the appropriate word. Since the bit277* is always a `new' one, this means that three of the `t?'s are278* just the corresponding `y?'; we use `#define's here for this.279* The variable `tt' holds the actual `t?' variable.280*/281282/* calculate q0 */283#define t0 tt284bit = FP_1;285EVEN_DOUBLE;286/* if (x >= (t0 = y0 | bit)) { */ /* always true */287q = bit;288x0 -= bit;289y0 = bit << 1;290/* } */291ODD_DOUBLE;292while ((bit >>= 1) != 0) { /* for remaining bits in q0 */293EVEN_DOUBLE;294t0 = y0 | bit; /* t = y + bit */295if (x0 >= t0) { /* if x >= t then */296x0 -= t0; /* x -= t */297q |= bit; /* q += bit */298y0 |= bit << 1; /* y += bit << 1 */299}300ODD_DOUBLE;301}302x->fp_mant[0] = q;303#undef t0304305/* calculate q1. note (y0&1)==0. */306#define t0 y0307#define t1 tt308q = 0;309y1 = 0;310bit = 1 << 31;311EVEN_DOUBLE;312t1 = bit;313FPU_SUBS(d1, x1, t1);314FPU_SUBC(d0, x0, t0); /* d = x - t */315if ((int)d0 >= 0) { /* if d >= 0 (i.e., x >= t) then */316x0 = d0, x1 = d1; /* x -= t */317q = bit; /* q += bit */318y0 |= 1; /* y += bit << 1 */319}320ODD_DOUBLE;321while ((bit >>= 1) != 0) { /* for remaining bits in q1 */322EVEN_DOUBLE; /* as before */323t1 = y1 | bit;324FPU_SUBS(d1, x1, t1);325FPU_SUBC(d0, x0, t0);326if ((int)d0 >= 0) {327x0 = d0, x1 = d1;328q |= bit;329y1 |= bit << 1;330}331ODD_DOUBLE;332}333x->fp_mant[1] = q;334#undef t1335336/* calculate q2. note (y1&1)==0; y0 (aka t0) is fixed. */337#define t1 y1338#define t2 tt339q = 0;340y2 = 0;341bit = 1 << 31;342EVEN_DOUBLE;343t2 = bit;344FPU_SUBS(d2, x2, t2);345FPU_SUBCS(d1, x1, t1);346FPU_SUBC(d0, x0, t0);347if ((int)d0 >= 0) {348x0 = d0, x1 = d1, x2 = d2;349q = bit;350y1 |= 1; /* now t1, y1 are set in concrete */351}352ODD_DOUBLE;353while ((bit >>= 1) != 0) {354EVEN_DOUBLE;355t2 = y2 | bit;356FPU_SUBS(d2, x2, t2);357FPU_SUBCS(d1, x1, t1);358FPU_SUBC(d0, x0, t0);359if ((int)d0 >= 0) {360x0 = d0, x1 = d1, x2 = d2;361q |= bit;362y2 |= bit << 1;363}364ODD_DOUBLE;365}366x->fp_mant[2] = q;367#undef t2368369/* calculate q3. y0, t0, y1, t1 all fixed; y2, t2, almost done. */370#define t2 y2371#define t3 tt372q = 0;373y3 = 0;374bit = 1 << 31;375EVEN_DOUBLE;376t3 = bit;377FPU_SUBS(d3, x3, t3);378FPU_SUBCS(d2, x2, t2);379FPU_SUBCS(d1, x1, t1);380FPU_SUBC(d0, x0, t0);381if ((int)d0 >= 0) {382x0 = d0, x1 = d1, x2 = d2; x3 = d3;383q = bit;384y2 |= 1;385}386ODD_DOUBLE;387while ((bit >>= 1) != 0) {388EVEN_DOUBLE;389t3 = y3 | bit;390FPU_SUBS(d3, x3, t3);391FPU_SUBCS(d2, x2, t2);392FPU_SUBCS(d1, x1, t1);393FPU_SUBC(d0, x0, t0);394if ((int)d0 >= 0) {395x0 = d0, x1 = d1, x2 = d2; x3 = d3;396q |= bit;397y3 |= bit << 1;398}399ODD_DOUBLE;400}401x->fp_mant[3] = q;402403/*404* The result, which includes guard and round bits, is exact iff405* x is now zero; any nonzero bits in x represent sticky bits.406*/407x->fp_sticky = x0 | x1 | x2 | x3;408DUMPFPN(FPE_REG, x);409return (x);410}411412413