1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux/PA-RISC Project (http://www.parisc-linux.org/)
5 * Floating-point emulation code
12 * @(#) pa/spmath/sfsqrt.c $Revision: 1.1 $
15 * Single Floating-point Square Root
17 * External Interfaces:
18 * sgl_fsqrt(srcptr,nullptr,dstptr,status)
20 * Internal Interfaces:
23 * <<please update with a overview of the operation of this file>>
30 #include "sgl_float.h"
33 * Single Floating-point Square Root
39 sgl_floating_point *srcptr,
40 unsigned int *nullptr,
41 sgl_floating_point *dstptr,
44 register unsigned int src, result;
45 register int src_exponent;
46 register unsigned int newbit, sum;
47 register boolean guardbit = FALSE, even_exponent;
51 * check source operand for NaN or infinity
53 if ((src_exponent = Sgl_exponent(src)) == SGL_INFINITY_EXPONENT) {
57 if (Sgl_isone_signaling(src)) {
58 /* trap if INVALIDTRAP enabled */
59 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
65 * Return quiet NaN or positive infinity.
66 * Fall through to negative test if negative infinity.
68 if (Sgl_iszero_sign(src) || Sgl_isnotzero_mantissa(src)) {
75 * check for zero source operand
77 if (Sgl_iszero_exponentmantissa(src)) {
83 * check for negative source operand
85 if (Sgl_isone_sign(src)) {
86 /* trap if INVALIDTRAP enabled */
87 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
90 Sgl_makequietnan(src);
98 if (src_exponent > 0) {
99 even_exponent = Sgl_hidden(src);
100 Sgl_clear_signexponent_set_hidden(src);
103 /* normalize operand */
104 Sgl_clear_signexponent(src);
106 Sgl_normalize(src,src_exponent);
107 even_exponent = src_exponent & 1;
110 /* exponent is even */
111 /* Add comment here. Explain why odd exponent needs correction */
112 Sgl_leftshiftby1(src);
115 * Add comment here. Explain following algorithm.
117 * Trust me, it works.
122 while (newbit && Sgl_isnotzero(src)) {
123 Sgl_addition(result,newbit,sum);
124 if(sum <= Sgl_all(src)) {
126 Sgl_addition(result,(newbit<<1),result);
127 Sgl_subtract(src,sum,src);
129 Sgl_rightshiftby1(newbit);
130 Sgl_leftshiftby1(src);
132 /* correct exponent for pre-shift */
134 Sgl_rightshiftby1(result);
137 /* check for inexact */
138 if (Sgl_isnotzero(src)) {
139 if (!even_exponent && Sgl_islessthan(result,src))
140 Sgl_increment(result);
141 guardbit = Sgl_lowmantissa(result);
142 Sgl_rightshiftby1(result);
144 /* now round result */
145 switch (Rounding_mode()) {
147 Sgl_increment(result);
150 /* stickybit is always true, so guardbit
151 * is enough to determine rounding */
153 Sgl_increment(result);
157 /* increment result exponent by 1 if mantissa overflowed */
158 if (Sgl_isone_hiddenoverflow(result)) src_exponent+=2;
160 if (Is_inexacttrap_enabled()) {
161 Sgl_set_exponent(result,
162 ((src_exponent-SGL_BIAS)>>1)+SGL_BIAS);
164 return(INEXACTEXCEPTION);
166 else Set_inexactflag();
169 Sgl_rightshiftby1(result);
171 Sgl_set_exponent(result,((src_exponent-SGL_BIAS)>>1)+SGL_BIAS);