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/sfdiv.c $Revision: 1.1 $
28 * Single Precision Floating-point Divide
30 * External Interfaces:
31 * sgl_fdiv(srcptr1,srcptr2,dstptr,status)
33 * Internal Interfaces:
36 * <<please update with a overview of the operation of this file>>
43 #include "sgl_float.h"
46 * Single Precision Floating-point Divide
50 sgl_fdiv (sgl_floating_point
* srcptr1
, sgl_floating_point
* srcptr2
,
51 sgl_floating_point
* dstptr
, unsigned int *status
)
53 register unsigned int opnd1
, opnd2
, opnd3
, result
;
54 register int dest_exponent
, count
;
55 register boolean inexact
= FALSE
, guardbit
= FALSE
, stickybit
= FALSE
;
61 * set sign bit of result
63 if (Sgl_sign(opnd1
) ^ Sgl_sign(opnd2
)) Sgl_setnegativezero(result
);
64 else Sgl_setzero(result
);
66 * check first operand for NaN's or infinity
68 if (Sgl_isinfinity_exponent(opnd1
)) {
69 if (Sgl_iszero_mantissa(opnd1
)) {
70 if (Sgl_isnotnan(opnd2
)) {
71 if (Sgl_isinfinity(opnd2
)) {
73 * invalid since both operands
76 if (Is_invalidtrap_enabled())
77 return(INVALIDEXCEPTION
);
79 Sgl_makequietnan(result
);
86 Sgl_setinfinity_exponentmantissa(result
);
93 * is NaN; signaling or quiet?
95 if (Sgl_isone_signaling(opnd1
)) {
96 /* trap if INVALIDTRAP enabled */
97 if (Is_invalidtrap_enabled())
98 return(INVALIDEXCEPTION
);
101 Sgl_set_quiet(opnd1
);
104 * is second operand a signaling NaN?
106 else if (Sgl_is_signalingnan(opnd2
)) {
107 /* trap if INVALIDTRAP enabled */
108 if (Is_invalidtrap_enabled())
109 return(INVALIDEXCEPTION
);
112 Sgl_set_quiet(opnd2
);
124 * check second operand for NaN's or infinity
126 if (Sgl_isinfinity_exponent(opnd2
)) {
127 if (Sgl_iszero_mantissa(opnd2
)) {
131 Sgl_setzero_exponentmantissa(result
);
136 * is NaN; signaling or quiet?
138 if (Sgl_isone_signaling(opnd2
)) {
139 /* trap if INVALIDTRAP enabled */
140 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
143 Sgl_set_quiet(opnd2
);
152 * check for division by zero
154 if (Sgl_iszero_exponentmantissa(opnd2
)) {
155 if (Sgl_iszero_exponentmantissa(opnd1
)) {
156 /* invalid since both operands are zero */
157 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
159 Sgl_makequietnan(result
);
163 if (Is_divisionbyzerotrap_enabled())
164 return(DIVISIONBYZEROEXCEPTION
);
165 Set_divisionbyzeroflag();
166 Sgl_setinfinity_exponentmantissa(result
);
173 dest_exponent
= Sgl_exponent(opnd1
) - Sgl_exponent(opnd2
) + SGL_BIAS
;
178 if (Sgl_isnotzero_exponent(opnd1
)) {
180 Sgl_clear_signexponent_set_hidden(opnd1
);
184 if (Sgl_iszero_mantissa(opnd1
)) {
185 Sgl_setzero_exponentmantissa(result
);
189 /* is denormalized; want to normalize */
190 Sgl_clear_signexponent(opnd1
);
191 Sgl_leftshiftby1(opnd1
);
192 Sgl_normalize(opnd1
,dest_exponent
);
194 /* opnd2 needs to have hidden bit set with msb in hidden bit */
195 if (Sgl_isnotzero_exponent(opnd2
)) {
196 Sgl_clear_signexponent_set_hidden(opnd2
);
199 /* is denormalized; want to normalize */
200 Sgl_clear_signexponent(opnd2
);
201 Sgl_leftshiftby1(opnd2
);
202 while(Sgl_iszero_hiddenhigh7mantissa(opnd2
)) {
203 Sgl_leftshiftby8(opnd2
);
206 if(Sgl_iszero_hiddenhigh3mantissa(opnd2
)) {
207 Sgl_leftshiftby4(opnd2
);
210 while(Sgl_iszero_hidden(opnd2
)) {
211 Sgl_leftshiftby1(opnd2
);
216 /* Divide the source mantissas */
219 * A non_restoring divide algorithm is used.
221 Sgl_subtract(opnd1
,opnd2
,opnd1
);
223 for (count
=1;count
<=SGL_P
&& Sgl_all(opnd1
);count
++) {
224 Sgl_leftshiftby1(opnd1
);
225 Sgl_leftshiftby1(opnd3
);
226 if (Sgl_iszero_sign(opnd1
)) {
227 Sgl_setone_lowmantissa(opnd3
);
228 Sgl_subtract(opnd1
,opnd2
,opnd1
);
230 else Sgl_addition(opnd1
,opnd2
,opnd1
);
232 if (count
<= SGL_P
) {
233 Sgl_leftshiftby1(opnd3
);
234 Sgl_setone_lowmantissa(opnd3
);
235 Sgl_leftshift(opnd3
,SGL_P
-count
);
236 if (Sgl_iszero_hidden(opnd3
)) {
237 Sgl_leftshiftby1(opnd3
);
242 if (Sgl_iszero_hidden(opnd3
)) {
243 /* need to get one more bit of result */
244 Sgl_leftshiftby1(opnd1
);
245 Sgl_leftshiftby1(opnd3
);
246 if (Sgl_iszero_sign(opnd1
)) {
247 Sgl_setone_lowmantissa(opnd3
);
248 Sgl_subtract(opnd1
,opnd2
,opnd1
);
250 else Sgl_addition(opnd1
,opnd2
,opnd1
);
253 if (Sgl_iszero_sign(opnd1
)) guardbit
= TRUE
;
254 stickybit
= Sgl_all(opnd1
);
256 inexact
= guardbit
| stickybit
;
261 if (inexact
&& (dest_exponent
> 0 || Is_underflowtrap_enabled())) {
262 Sgl_clear_signexponent(opnd3
);
263 switch (Rounding_mode()) {
265 if (Sgl_iszero_sign(result
))
266 Sgl_increment_mantissa(opnd3
);
269 if (Sgl_isone_sign(result
))
270 Sgl_increment_mantissa(opnd3
);
274 if (stickybit
|| Sgl_isone_lowmantissa(opnd3
))
275 Sgl_increment_mantissa(opnd3
);
278 if (Sgl_isone_hidden(opnd3
)) dest_exponent
++;
280 Sgl_set_mantissa(result
,opnd3
);
285 if (dest_exponent
>= SGL_INFINITY_EXPONENT
) {
286 /* trap if OVERFLOWTRAP enabled */
287 if (Is_overflowtrap_enabled()) {
289 * Adjust bias of result
291 Sgl_setwrapped_exponent(result
,dest_exponent
,ovfl
);
294 if (Is_inexacttrap_enabled())
295 return(OVERFLOWEXCEPTION
| INEXACTEXCEPTION
);
296 else Set_inexactflag();
297 return(OVERFLOWEXCEPTION
);
300 /* set result to infinity or largest number */
301 Sgl_setoverflow(result
);
307 else if (dest_exponent
<= 0) {
308 /* trap if UNDERFLOWTRAP enabled */
309 if (Is_underflowtrap_enabled()) {
311 * Adjust bias of result
313 Sgl_setwrapped_exponent(result
,dest_exponent
,unfl
);
316 if (Is_inexacttrap_enabled())
317 return(UNDERFLOWEXCEPTION
| INEXACTEXCEPTION
);
318 else Set_inexactflag();
319 return(UNDERFLOWEXCEPTION
);
322 /* Determine if should set underflow flag */
324 if (dest_exponent
== 0 && inexact
) {
325 switch (Rounding_mode()) {
327 if (Sgl_iszero_sign(result
)) {
328 Sgl_increment(opnd3
);
329 if (Sgl_isone_hiddenoverflow(opnd3
))
331 Sgl_decrement(opnd3
);
335 if (Sgl_isone_sign(result
)) {
336 Sgl_increment(opnd3
);
337 if (Sgl_isone_hiddenoverflow(opnd3
))
339 Sgl_decrement(opnd3
);
343 if (guardbit
&& (stickybit
||
344 Sgl_isone_lowmantissa(opnd3
))) {
345 Sgl_increment(opnd3
);
346 if (Sgl_isone_hiddenoverflow(opnd3
))
348 Sgl_decrement(opnd3
);
355 * denormalize result or set to signed zero
358 Sgl_denormalize(opnd3
,dest_exponent
,guardbit
,stickybit
,inexact
);
360 /* return rounded number */
362 switch (Rounding_mode()) {
364 if (Sgl_iszero_sign(result
)) {
365 Sgl_increment(opnd3
);
369 if (Sgl_isone_sign(result
)) {
370 Sgl_increment(opnd3
);
374 if (guardbit
&& (stickybit
||
375 Sgl_isone_lowmantissa(opnd3
))) {
376 Sgl_increment(opnd3
);
380 if (is_tiny
) Set_underflowflag();
382 Sgl_set_exponentmantissa(result
,opnd3
);
384 else Sgl_set_exponent(result
,dest_exponent
);
386 /* check for inexact */
388 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
389 else Set_inexactflag();