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;
35 if ( flags & DEST_RM )
54 taga = FPU_gettagi(rm);
71 tagb = FPU_gettagi(rm);
81 saved_sign = getsign(dest);
85 /* Both regs Valid, this should be the most common case. */
90 tag = FPU_u_div(&x, &y, dest, control_w, sign);
95 FPU_settagi(deststnr, tag);
99 if ( taga == TAG_Special )
100 taga = FPU_Special(a);
101 if ( tagb == TAG_Special )
102 tagb = FPU_Special(b);
104 if ( ((taga == TAG_Valid) && (tagb == TW_Denormal))
105 || ((taga == TW_Denormal) && (tagb == TAG_Valid))
106 || ((taga == TW_Denormal) && (tagb == TW_Denormal)) )
108 if ( denormal_operand() < 0 )
109 return FPU_Exception;
113 tag = FPU_u_div(&x, &y, dest, control_w, sign);
117 FPU_settagi(deststnr, tag);
120 else if ( (taga <= TW_Denormal) && (tagb <= TW_Denormal) )
122 if ( tagb != TAG_Zero )
124 /* Want to find Zero/Valid */
125 if ( tagb == TW_Denormal )
127 if ( denormal_operand() < 0 )
128 return FPU_Exception;
131 /* The result is zero. */
132 FPU_copy_to_regi(&CONST_Z, TAG_Zero, deststnr);
136 /* We have an exception condition, either 0/0 or Valid/Zero. */
137 if ( taga == TAG_Zero )
140 return arith_invalid(deststnr);
143 return FPU_divide_by_zero(deststnr, sign);
145 /* Must have infinities, NaNs, etc */
146 else if ( (taga == TW_NaN) || (tagb == TW_NaN) )
148 if ( flags & LOADED )
149 return real_2op_NaN((FPU_REG *)rm, flags & 0x0f, 0, st0_ptr);
151 if ( flags & DEST_RM )
155 if ( tag == TAG_Special )
156 tag = FPU_Special(st0_ptr);
157 return real_2op_NaN(st0_ptr, tag, rm, (flags & REV) ? st0_ptr : &st(rm));
162 tag = FPU_gettagi(rm);
163 if ( tag == TAG_Special )
164 tag = FPU_Special(&st(rm));
165 return real_2op_NaN(&st(rm), tag, 0, (flags & REV) ? st0_ptr : &st(rm));
168 else if (taga == TW_Infinity)
170 if (tagb == TW_Infinity)
172 /* infinity/infinity */
173 return arith_invalid(deststnr);
177 /* tagb must be Valid or Zero */
178 if ( (tagb == TW_Denormal) && (denormal_operand() < 0) )
179 return FPU_Exception;
181 /* Infinity divided by Zero or Valid does
182 not raise and exception, but returns Infinity */
183 FPU_copy_to_regi(a, TAG_Special, deststnr);
188 else if (tagb == TW_Infinity)
190 if ( (taga == TW_Denormal) && (denormal_operand() < 0) )
191 return FPU_Exception;
193 /* The result is zero. */
194 FPU_copy_to_regi(&CONST_Z, TAG_Zero, deststnr);
201 EXCEPTION(EX_INTERNAL|0x102);
202 return FPU_Exception;
204 #endif /* PARANOID */