2 * Linux/PA-RISC Project (http://www.parisc-linux.org/)
4 * Floating-point emulation code
5 * Copyright (C) 2001 Hewlett-Packard (Paul Bame) <bame@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * @(#) pa/spmath/sfsqrt.c $Revision: 1.1 $
28 * Single Floating-point Square Root
30 * External Interfaces:
31 * sgl_fsqrt(srcptr,nullptr,dstptr,status)
33 * Internal Interfaces:
36 * <<please update with a overview of the operation of this file>>
43 #include "sgl_float.h"
46 * Single Floating-point Square Root
52 sgl_floating_point
*srcptr
,
53 unsigned int *nullptr,
54 sgl_floating_point
*dstptr
,
57 register unsigned int src
, result
;
58 register int src_exponent
;
59 register unsigned int newbit
, sum
;
60 register boolean guardbit
= FALSE
, even_exponent
;
64 * check source operand for NaN or infinity
66 if ((src_exponent
= Sgl_exponent(src
)) == SGL_INFINITY_EXPONENT
) {
70 if (Sgl_isone_signaling(src
)) {
71 /* trap if INVALIDTRAP enabled */
72 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
78 * Return quiet NaN or positive infinity.
79 * Fall thru to negative test if negative infinity.
81 if (Sgl_iszero_sign(src
) || Sgl_isnotzero_mantissa(src
)) {
88 * check for zero source operand
90 if (Sgl_iszero_exponentmantissa(src
)) {
96 * check for negative source operand
98 if (Sgl_isone_sign(src
)) {
99 /* trap if INVALIDTRAP enabled */
100 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
103 Sgl_makequietnan(src
);
111 if (src_exponent
> 0) {
112 even_exponent
= Sgl_hidden(src
);
113 Sgl_clear_signexponent_set_hidden(src
);
116 /* normalize operand */
117 Sgl_clear_signexponent(src
);
119 Sgl_normalize(src
,src_exponent
);
120 even_exponent
= src_exponent
& 1;
123 /* exponent is even */
124 /* Add comment here. Explain why odd exponent needs correction */
125 Sgl_leftshiftby1(src
);
128 * Add comment here. Explain following algorithm.
130 * Trust me, it works.
135 while (newbit
&& Sgl_isnotzero(src
)) {
136 Sgl_addition(result
,newbit
,sum
);
137 if(sum
<= Sgl_all(src
)) {
139 Sgl_addition(result
,(newbit
<<1),result
);
140 Sgl_subtract(src
,sum
,src
);
142 Sgl_rightshiftby1(newbit
);
143 Sgl_leftshiftby1(src
);
145 /* correct exponent for pre-shift */
147 Sgl_rightshiftby1(result
);
150 /* check for inexact */
151 if (Sgl_isnotzero(src
)) {
152 if (!even_exponent
&& Sgl_islessthan(result
,src
))
153 Sgl_increment(result
);
154 guardbit
= Sgl_lowmantissa(result
);
155 Sgl_rightshiftby1(result
);
157 /* now round result */
158 switch (Rounding_mode()) {
160 Sgl_increment(result
);
163 /* stickybit is always true, so guardbit
164 * is enough to determine rounding */
166 Sgl_increment(result
);
170 /* increment result exponent by 1 if mantissa overflowed */
171 if (Sgl_isone_hiddenoverflow(result
)) src_exponent
+=2;
173 if (Is_inexacttrap_enabled()) {
174 Sgl_set_exponent(result
,
175 ((src_exponent
-SGL_BIAS
)>>1)+SGL_BIAS
);
177 return(INEXACTEXCEPTION
);
179 else Set_inexactflag();
182 Sgl_rightshiftby1(result
);
184 Sgl_set_exponent(result
,((src_exponent
-SGL_BIAS
)>>1)+SGL_BIAS
);