1 /*---------------------------------------------------------------------------+
4 | Divide one FPU_REG by another and put the result in a destination FPU_REG.|
7 | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
8 | E-mail billm@jacobi.maths.monash.edu.au |
10 | Return value is the tag of the answer, or-ed with FPU_Exception if |
11 | one was raised, or -1 on internal error. |
13 +---------------------------------------------------------------------------*/
15 /*---------------------------------------------------------------------------+
16 | The destination may be any FPU_REG, including one of the source FPU_REGs. |
17 +---------------------------------------------------------------------------*/
19 #include "exception.h"
20 #include "reg_constant.h"
22 #include "fpu_system.h"
25 Divide one register by another and put the result into a third register.
27 int FPU_div(int flags
, int rm
, int control_w
)
30 FPU_REG
const *a
, *b
, *st0_ptr
, *st_ptr
;
32 u_char taga
, tagb
, signa
, signb
, sign
, saved_sign
;
50 taga
= FPU_gettagi(rm
);
62 tagb
= FPU_gettagi(rm
);
72 saved_sign
= getsign(dest
);
75 /* Both regs Valid, this should be the most common case. */
80 tag
= FPU_u_div(&x
, &y
, dest
, control_w
, sign
);
85 FPU_settagi(deststnr
, tag
);
89 if (taga
== TAG_Special
)
90 taga
= FPU_Special(a
);
91 if (tagb
== TAG_Special
)
92 tagb
= FPU_Special(b
);
94 if (((taga
== TAG_Valid
) && (tagb
== TW_Denormal
))
95 || ((taga
== TW_Denormal
) && (tagb
== TAG_Valid
))
96 || ((taga
== TW_Denormal
) && (tagb
== TW_Denormal
))) {
97 if (denormal_operand() < 0)
102 tag
= FPU_u_div(&x
, &y
, dest
, control_w
, sign
);
106 FPU_settagi(deststnr
, tag
);
108 } else if ((taga
<= TW_Denormal
) && (tagb
<= TW_Denormal
)) {
109 if (tagb
!= TAG_Zero
) {
110 /* Want to find Zero/Valid */
111 if (tagb
== TW_Denormal
) {
112 if (denormal_operand() < 0)
113 return FPU_Exception
;
116 /* The result is zero. */
117 FPU_copy_to_regi(&CONST_Z
, TAG_Zero
, deststnr
);
121 /* We have an exception condition, either 0/0 or Valid/Zero. */
122 if (taga
== TAG_Zero
) {
124 return arith_invalid(deststnr
);
127 return FPU_divide_by_zero(deststnr
, sign
);
129 /* Must have infinities, NaNs, etc */
130 else if ((taga
== TW_NaN
) || (tagb
== TW_NaN
)) {
132 return real_2op_NaN((FPU_REG
*) rm
, flags
& 0x0f, 0,
135 if (flags
& DEST_RM
) {
138 if (tag
== TAG_Special
)
139 tag
= FPU_Special(st0_ptr
);
140 return real_2op_NaN(st0_ptr
, tag
, rm
,
141 (flags
& REV
) ? st0_ptr
: &st(rm
));
144 tag
= FPU_gettagi(rm
);
145 if (tag
== TAG_Special
)
146 tag
= FPU_Special(&st(rm
));
147 return real_2op_NaN(&st(rm
), tag
, 0,
148 (flags
& REV
) ? st0_ptr
: &st(rm
));
150 } else if (taga
== TW_Infinity
) {
151 if (tagb
== TW_Infinity
) {
152 /* infinity/infinity */
153 return arith_invalid(deststnr
);
155 /* tagb must be Valid or Zero */
156 if ((tagb
== TW_Denormal
) && (denormal_operand() < 0))
157 return FPU_Exception
;
159 /* Infinity divided by Zero or Valid does
160 not raise and exception, but returns Infinity */
161 FPU_copy_to_regi(a
, TAG_Special
, deststnr
);
165 } else if (tagb
== TW_Infinity
) {
166 if ((taga
== TW_Denormal
) && (denormal_operand() < 0))
167 return FPU_Exception
;
169 /* The result is zero. */
170 FPU_copy_to_regi(&CONST_Z
, TAG_Zero
, deststnr
);
176 EXCEPTION(EX_INTERNAL
| 0x102);
177 return FPU_Exception
;
179 #endif /* PARANOID */