1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42#include "float.h"
43#include "sgl_float.h"
44
45
46
47
48
49
50unsigned int
51sgl_fsqrt(
52 sgl_floating_point *srcptr,
53 unsigned int *nullptr,
54 sgl_floating_point *dstptr,
55 unsigned int *status)
56{
57 register unsigned int src, result;
58 register int src_exponent;
59 register unsigned int newbit, sum;
60 register boolean guardbit = FALSE, even_exponent;
61
62 src = *srcptr;
63
64
65
66 if ((src_exponent = Sgl_exponent(src)) == SGL_INFINITY_EXPONENT) {
67
68
69
70 if (Sgl_isone_signaling(src)) {
71
72 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
73
74 Set_invalidflag();
75 Sgl_set_quiet(src);
76 }
77
78
79
80
81 if (Sgl_iszero_sign(src) || Sgl_isnotzero_mantissa(src)) {
82 *dstptr = src;
83 return(NOEXCEPTION);
84 }
85 }
86
87
88
89
90 if (Sgl_iszero_exponentmantissa(src)) {
91 *dstptr = src;
92 return(NOEXCEPTION);
93 }
94
95
96
97
98 if (Sgl_isone_sign(src)) {
99
100 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
101
102 Set_invalidflag();
103 Sgl_makequietnan(src);
104 *dstptr = src;
105 return(NOEXCEPTION);
106 }
107
108
109
110
111 if (src_exponent > 0) {
112 even_exponent = Sgl_hidden(src);
113 Sgl_clear_signexponent_set_hidden(src);
114 }
115 else {
116
117 Sgl_clear_signexponent(src);
118 src_exponent++;
119 Sgl_normalize(src,src_exponent);
120 even_exponent = src_exponent & 1;
121 }
122 if (even_exponent) {
123
124
125 Sgl_leftshiftby1(src);
126 }
127
128
129
130
131
132
133 Sgl_setzero(result);
134 newbit = 1 << SGL_P;
135 while (newbit && Sgl_isnotzero(src)) {
136 Sgl_addition(result,newbit,sum);
137 if(sum <= Sgl_all(src)) {
138
139 Sgl_addition(result,(newbit<<1),result);
140 Sgl_subtract(src,sum,src);
141 }
142 Sgl_rightshiftby1(newbit);
143 Sgl_leftshiftby1(src);
144 }
145
146 if (even_exponent) {
147 Sgl_rightshiftby1(result);
148 }
149
150
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);
156
157
158 switch (Rounding_mode()) {
159 case ROUNDPLUS:
160 Sgl_increment(result);
161 break;
162 case ROUNDNEAREST:
163
164
165 if (guardbit) {
166 Sgl_increment(result);
167 }
168 break;
169 }
170
171 if (Sgl_isone_hiddenoverflow(result)) src_exponent+=2;
172
173 if (Is_inexacttrap_enabled()) {
174 Sgl_set_exponent(result,
175 ((src_exponent-SGL_BIAS)>>1)+SGL_BIAS);
176 *dstptr = result;
177 return(INEXACTEXCEPTION);
178 }
179 else Set_inexactflag();
180 }
181 else {
182 Sgl_rightshiftby1(result);
183 }
184 Sgl_set_exponent(result,((src_exponent-SGL_BIAS)>>1)+SGL_BIAS);
185 *dstptr = result;
186 return(NOEXCEPTION);
187}
188