1 /* $NetBSD: dfdiv.c,v 1.3 2005/12/11 12:17:40 christos Exp $ */
3 /* $OpenBSD: dfdiv.c,v 1.4 2001/03/29 03:58:17 mickey Exp $ */
6 * Copyright 1996 1995 by Open Software Foundation, Inc.
9 * Permission to use, copy, modify, and distribute this software and
10 * its documentation for any purpose and without fee is hereby granted,
11 * provided that the above copyright notice appears in all copies and
12 * that both the copyright notice and this permission notice appear in
13 * supporting documentation.
15 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
17 * FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
21 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
22 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
23 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 * (c) Copyright 1986 HEWLETT-PACKARD COMPANY
32 * To anyone who acknowledges that this file is provided "AS IS"
33 * without any express or implied warranty:
34 * permission to use, copy, modify, and distribute this file
35 * for any purpose is hereby granted without fee, provided that
36 * the above copyright notice and this notice appears in all
37 * copies, and that the name of Hewlett-Packard Company not be
38 * used in advertising or publicity pertaining to distribution
39 * of the software without specific, written prior permission.
40 * Hewlett-Packard Company makes no representations about the
41 * suitability of this software for any purpose.
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: dfdiv.c,v 1.3 2005/12/11 12:17:40 christos Exp $");
47 #include "../spmath/float.h"
48 #include "../spmath/dbl_float.h"
51 * Double Precision Floating-point Divide
55 dbl_fdiv(srcptr1
,srcptr2
,dstptr
,status
)
57 dbl_floating_point
*srcptr1
, *srcptr2
, *dstptr
;
60 register unsigned int opnd1p1
, opnd1p2
, opnd2p1
, opnd2p2
;
61 register unsigned int opnd3p1
, opnd3p2
, resultp1
, resultp2
;
62 register int dest_exponent
, count
;
63 register int inexact
= false, guardbit
= false, stickybit
= false;
66 Dbl_copyfromptr(srcptr1
,opnd1p1
,opnd1p2
);
67 Dbl_copyfromptr(srcptr2
,opnd2p1
,opnd2p2
);
69 * set sign bit of result
71 if (Dbl_sign(opnd1p1
) ^ Dbl_sign(opnd2p1
))
72 Dbl_setnegativezerop1(resultp1
);
73 else Dbl_setzerop1(resultp1
);
75 * check first operand for NaN's or infinity
77 if (Dbl_isinfinity_exponent(opnd1p1
)) {
78 if (Dbl_iszero_mantissa(opnd1p1
,opnd1p2
)) {
79 if (Dbl_isnotnan(opnd2p1
,opnd2p2
)) {
80 if (Dbl_isinfinity(opnd2p1
,opnd2p2
)) {
82 * invalid since both operands
85 if (Is_invalidtrap_enabled())
86 return(INVALIDEXCEPTION
);
88 Dbl_makequietnan(resultp1
,resultp2
);
89 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
95 Dbl_setinfinity_exponentmantissa(resultp1
,resultp2
);
96 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
102 * is NaN; signaling or quiet?
104 if (Dbl_isone_signaling(opnd1p1
)) {
105 /* trap if INVALIDTRAP enabled */
106 if (Is_invalidtrap_enabled())
107 return(INVALIDEXCEPTION
);
110 Dbl_set_quiet(opnd1p1
);
113 * is second operand a signaling NaN?
115 else if (Dbl_is_signalingnan(opnd2p1
)) {
116 /* trap if INVALIDTRAP enabled */
117 if (Is_invalidtrap_enabled())
118 return(INVALIDEXCEPTION
);
121 Dbl_set_quiet(opnd2p1
);
122 Dbl_copytoptr(opnd2p1
,opnd2p2
,dstptr
);
128 Dbl_copytoptr(opnd1p1
,opnd1p2
,dstptr
);
133 * check second operand for NaN's or infinity
135 if (Dbl_isinfinity_exponent(opnd2p1
)) {
136 if (Dbl_iszero_mantissa(opnd2p1
,opnd2p2
)) {
140 Dbl_setzero_exponentmantissa(resultp1
,resultp2
);
141 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
145 * is NaN; signaling or quiet?
147 if (Dbl_isone_signaling(opnd2p1
)) {
148 /* trap if INVALIDTRAP enabled */
149 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
152 Dbl_set_quiet(opnd2p1
);
157 Dbl_copytoptr(opnd2p1
,opnd2p2
,dstptr
);
161 * check for division by zero
163 if (Dbl_iszero_exponentmantissa(opnd2p1
,opnd2p2
)) {
164 if (Dbl_iszero_exponentmantissa(opnd1p1
,opnd1p2
)) {
165 /* invalid since both operands are zero */
166 if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION
);
168 Dbl_makequietnan(resultp1
,resultp2
);
169 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
172 if (Is_divisionbyzerotrap_enabled())
173 return(DIVISIONBYZEROEXCEPTION
);
174 Set_divisionbyzeroflag();
175 Dbl_setinfinity_exponentmantissa(resultp1
,resultp2
);
176 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
182 dest_exponent
= Dbl_exponent(opnd1p1
) - Dbl_exponent(opnd2p1
) + DBL_BIAS
;
187 if (Dbl_isnotzero_exponent(opnd1p1
)) {
189 Dbl_clear_signexponent_set_hidden(opnd1p1
);
193 if (Dbl_iszero_mantissa(opnd1p1
,opnd1p2
)) {
194 Dbl_setzero_exponentmantissa(resultp1
,resultp2
);
195 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
198 /* is denormalized, want to normalize */
199 Dbl_clear_signexponent(opnd1p1
);
200 Dbl_leftshiftby1(opnd1p1
,opnd1p2
);
201 Dbl_normalize(opnd1p1
,opnd1p2
,dest_exponent
);
203 /* opnd2 needs to have hidden bit set with msb in hidden bit */
204 if (Dbl_isnotzero_exponent(opnd2p1
)) {
205 Dbl_clear_signexponent_set_hidden(opnd2p1
);
208 /* is denormalized; want to normalize */
209 Dbl_clear_signexponent(opnd2p1
);
210 Dbl_leftshiftby1(opnd2p1
,opnd2p2
);
211 while (Dbl_iszero_hiddenhigh7mantissa(opnd2p1
)) {
213 Dbl_leftshiftby8(opnd2p1
,opnd2p2
);
215 if (Dbl_iszero_hiddenhigh3mantissa(opnd2p1
)) {
217 Dbl_leftshiftby4(opnd2p1
,opnd2p2
);
219 while (Dbl_iszero_hidden(opnd2p1
)) {
221 Dbl_leftshiftby1(opnd2p1
,opnd2p2
);
225 /* Divide the source mantissas */
228 * A non-restoring divide algorithm is used.
230 Twoword_subtract(opnd1p1
,opnd1p2
,opnd2p1
,opnd2p2
);
231 Dbl_setzero(opnd3p1
,opnd3p2
);
232 for (count
=1; count
<= DBL_P
&& (opnd1p1
|| opnd1p2
); count
++) {
233 Dbl_leftshiftby1(opnd1p1
,opnd1p2
);
234 Dbl_leftshiftby1(opnd3p1
,opnd3p2
);
235 if (Dbl_iszero_sign(opnd1p1
)) {
236 Dbl_setone_lowmantissap2(opnd3p2
);
237 Twoword_subtract(opnd1p1
,opnd1p2
,opnd2p1
,opnd2p2
);
240 Twoword_add(opnd1p1
, opnd1p2
, opnd2p1
, opnd2p2
);
243 if (count
<= DBL_P
) {
244 Dbl_leftshiftby1(opnd3p1
,opnd3p2
);
245 Dbl_setone_lowmantissap2(opnd3p2
);
246 Dbl_leftshift(opnd3p1
,opnd3p2
,(DBL_P
-count
));
247 if (Dbl_iszero_hidden(opnd3p1
)) {
248 Dbl_leftshiftby1(opnd3p1
,opnd3p2
);
253 if (Dbl_iszero_hidden(opnd3p1
)) {
254 /* need to get one more bit of result */
255 Dbl_leftshiftby1(opnd1p1
,opnd1p2
);
256 Dbl_leftshiftby1(opnd3p1
,opnd3p2
);
257 if (Dbl_iszero_sign(opnd1p1
)) {
258 Dbl_setone_lowmantissap2(opnd3p2
);
259 Twoword_subtract(opnd1p1
,opnd1p2
,opnd2p1
,opnd2p2
);
262 Twoword_add(opnd1p1
,opnd1p2
,opnd2p1
,opnd2p2
);
266 if (Dbl_iszero_sign(opnd1p1
)) guardbit
= true;
267 stickybit
= Dbl_allp1(opnd1p1
) || Dbl_allp2(opnd1p2
);
269 inexact
= guardbit
| stickybit
;
274 if (inexact
&& (dest_exponent
> 0 || Is_underflowtrap_enabled())) {
275 Dbl_clear_signexponent(opnd3p1
);
276 switch (Rounding_mode()) {
278 if (Dbl_iszero_sign(resultp1
))
279 Dbl_increment(opnd3p1
,opnd3p2
);
282 if (Dbl_isone_sign(resultp1
))
283 Dbl_increment(opnd3p1
,opnd3p2
);
286 if (guardbit
&& (stickybit
||
287 Dbl_isone_lowmantissap2(opnd3p2
))) {
288 Dbl_increment(opnd3p1
,opnd3p2
);
291 if (Dbl_isone_hidden(opnd3p1
)) dest_exponent
++;
293 Dbl_set_mantissa(resultp1
,resultp2
,opnd3p1
,opnd3p2
);
298 if (dest_exponent
>= DBL_INFINITY_EXPONENT
) {
299 /* trap if OVERFLOWTRAP enabled */
300 if (Is_overflowtrap_enabled()) {
302 * Adjust bias of result
304 Dbl_setwrapped_exponent(resultp1
,dest_exponent
,ovfl
);
305 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
307 if (Is_inexacttrap_enabled())
308 return(OVERFLOWEXCEPTION
| INEXACTEXCEPTION
);
312 return(OVERFLOWEXCEPTION
);
315 /* set result to infinity or largest number */
316 Dbl_setoverflow(resultp1
,resultp2
);
322 else if (dest_exponent
<= 0) {
323 /* trap if UNDERFLOWTRAP enabled */
324 if (Is_underflowtrap_enabled()) {
326 * Adjust bias of result
328 Dbl_setwrapped_exponent(resultp1
,dest_exponent
,unfl
);
329 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
331 if (Is_inexacttrap_enabled())
332 return(UNDERFLOWEXCEPTION
| INEXACTEXCEPTION
);
336 return(UNDERFLOWEXCEPTION
);
339 /* Determine if should set underflow flag */
341 if (dest_exponent
== 0 && inexact
) {
342 switch (Rounding_mode()) {
344 if (Dbl_iszero_sign(resultp1
)) {
345 Dbl_increment(opnd3p1
,opnd3p2
);
346 if (Dbl_isone_hiddenoverflow(opnd3p1
))
348 Dbl_decrement(opnd3p1
,opnd3p2
);
352 if (Dbl_isone_sign(resultp1
)) {
353 Dbl_increment(opnd3p1
,opnd3p2
);
354 if (Dbl_isone_hiddenoverflow(opnd3p1
))
356 Dbl_decrement(opnd3p1
,opnd3p2
);
360 if (guardbit
&& (stickybit
||
361 Dbl_isone_lowmantissap2(opnd3p2
))) {
362 Dbl_increment(opnd3p1
,opnd3p2
);
363 if (Dbl_isone_hiddenoverflow(opnd3p1
))
365 Dbl_decrement(opnd3p1
,opnd3p2
);
372 * denormalize result or set to signed zero
375 Dbl_denormalize(opnd3p1
,opnd3p2
,dest_exponent
,guardbit
,
378 /* return rounded number */
380 switch (Rounding_mode()) {
382 if (Dbl_iszero_sign(resultp1
)) {
383 Dbl_increment(opnd3p1
,opnd3p2
);
387 if (Dbl_isone_sign(resultp1
)) {
388 Dbl_increment(opnd3p1
,opnd3p2
);
392 if (guardbit
&& (stickybit
||
393 Dbl_isone_lowmantissap2(opnd3p2
))) {
394 Dbl_increment(opnd3p1
,opnd3p2
);
401 Dbl_set_exponentmantissa(resultp1
,resultp2
,opnd3p1
,opnd3p2
);
403 else Dbl_set_exponent(resultp1
,dest_exponent
);
404 Dbl_copytoptr(resultp1
,resultp2
,dstptr
);
406 /* check for inexact */
408 if (Is_inexacttrap_enabled()) return(INEXACTEXCEPTION
);
409 else Set_inexactflag();