xref: /openbsd-src/sys/arch/hppa/spmath/sfsqrt.c (revision 5b133f3f277e80f096764111e64f3a1284acb179)
1 /*	$OpenBSD: sfsqrt.c,v 1.8 2023/03/08 04:43:07 guenther Exp $	*/
2 /*
3   (c) Copyright 1986 HEWLETT-PACKARD COMPANY
4   To anyone who acknowledges that this file is provided "AS IS"
5   without any express or implied warranty:
6       permission to use, copy, modify, and distribute this file
7   for any purpose is hereby granted without fee, provided that
8   the above copyright notice and this notice appears in all
9   copies, and that the name of Hewlett-Packard Company not be
10   used in advertising or publicity pertaining to distribution
11   of the software without specific, written prior permission.
12   Hewlett-Packard Company makes no representations about the
13   suitability of this software for any purpose.
14 */
15 /* @(#)sfsqrt.c: Revision: 1.9.88.1 Date: 93/12/07 15:07:13 */
16 
17 #include "float.h"
18 #include "sgl_float.h"
19 
20 /*
21  *  Single Floating-point Square Root
22  */
23 
24 int
sgl_fsqrt(srcptr,null,dstptr,status)25 sgl_fsqrt(srcptr, null, dstptr, status)
26 	sgl_floating_point *srcptr, *null, *dstptr;
27 	unsigned int *status;
28 {
29 	register unsigned int src, result;
30 	register int src_exponent, newbit, sum;
31 	register int guardbit = FALSE, even_exponent;
32 
33 	src = *srcptr;
34 	/*
35 	 * check source operand for NaN or infinity
36 	 */
37 	if ((src_exponent = Sgl_exponent(src)) == SGL_INFINITY_EXPONENT) {
38 		/*
39 		 * is signaling NaN?
40 		 */
41 		if (Sgl_isone_signaling(src)) {
42 			/* trap if INVALIDTRAP enabled */
43 			if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
44 			/* make NaN quiet */
45 			Set_invalidflag();
46 			Sgl_set_quiet(src);
47 		}
48 		/*
49 		 * Return quiet NaN or positive infinity.
50 		 *  Fall thru to negative test if negative infinity.
51 		 */
52 		if (Sgl_iszero_sign(src) || Sgl_isnotzero_mantissa(src)) {
53 			*dstptr = src;
54 			return(NOEXCEPTION);
55 		}
56 	}
57 
58 	/*
59 	 * check for zero source operand
60 	 */
61 	if (Sgl_iszero_exponentmantissa(src)) {
62 		*dstptr = src;
63 		return(NOEXCEPTION);
64 	}
65 
66 	/*
67 	 * check for negative source operand
68 	 */
69 	if (Sgl_isone_sign(src)) {
70 		/* trap if INVALIDTRAP enabled */
71 		if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
72 		/* make NaN quiet */
73 		Set_invalidflag();
74 		Sgl_makequietnan(src);
75 		*dstptr = src;
76 		return(NOEXCEPTION);
77 	}
78 
79 	/*
80 	 * Generate result
81 	 */
82 	if (src_exponent > 0) {
83 		even_exponent = Sgl_hidden(src);
84 		Sgl_clear_signexponent_set_hidden(src);
85 	}
86 	else {
87 		/* normalize operand */
88 		Sgl_clear_signexponent(src);
89 		src_exponent++;
90 		Sgl_normalize(src,src_exponent);
91 		even_exponent = src_exponent & 1;
92 	}
93 	if (even_exponent) {
94 		/* exponent is even */
95 		/* Add comment here.  Explain why odd exponent needs correction */
96 		Sgl_leftshiftby1(src);
97 	}
98 	/*
99 	 * Add comment here.  Explain following algorithm.
100 	 *
101 	 * Trust me, it works.
102 	 *
103 	 */
104 	Sgl_setzero(result);
105 	newbit = 1 << SGL_P;
106 	while (newbit && Sgl_isnotzero(src)) {
107 		Sgl_addition(result,newbit,sum);
108 		if(sum <= Sgl_all(src)) {
109 			/* update result */
110 			Sgl_addition(result,(newbit<<1),result);
111 			Sgl_subtract(src,sum,src);
112 		}
113 		Sgl_rightshiftby1(newbit);
114 		Sgl_leftshiftby1(src);
115 	}
116 	/* correct exponent for pre-shift */
117 	if (even_exponent) {
118 		Sgl_rightshiftby1(result);
119 	}
120 
121 	/* check for inexact */
122 	if (Sgl_isnotzero(src)) {
123 		if (!even_exponent & Sgl_islessthan(result,src))
124 			Sgl_increment(result);
125 		guardbit = Sgl_lowmantissa(result);
126 		Sgl_rightshiftby1(result);
127 
128 		/*  now round result  */
129 		switch (Rounding_mode()) {
130 		case ROUNDPLUS:
131 		     Sgl_increment(result);
132 		     break;
133 		case ROUNDNEAREST:
134 		     /* stickybit is always true, so guardbit
135 		      * is enough to determine rounding */
136 		     if (guardbit) {
137 			Sgl_increment(result);
138 		     }
139 		     break;
140 		}
141 		/* increment result exponent by 1 if mantissa overflowed */
142 		if (Sgl_isone_hiddenoverflow(result)) src_exponent+=2;
143 
144 		if (Is_inexacttrap_enabled()) {
145 			Sgl_set_exponent(result,
146 			 ((src_exponent-SGL_BIAS)>>1)+SGL_BIAS);
147 			*dstptr = result;
148 			return(INEXACTEXCEPTION);
149 		}
150 		else Set_inexactflag();
151 	}
152 	else {
153 		Sgl_rightshiftby1(result);
154 	}
155 	Sgl_set_exponent(result,((src_exponent-SGL_BIAS)>>1)+SGL_BIAS);
156 	*dstptr = result;
157 	return(NOEXCEPTION);
158 }
159