Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / i386 / math-emu / reg_divide.c
blob5cee7ff920d93b8d8630134b6c5904c8e9cc1718
1 /*---------------------------------------------------------------------------+
2 | reg_divide.c |
3 | |
4 | Divide one FPU_REG by another and put the result in a destination FPU_REG.|
5 | |
6 | Copyright (C) 1996 |
7 | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
8 | E-mail billm@jacobi.maths.monash.edu.au |
9 | |
10 | Return value is the tag of the answer, or-ed with FPU_Exception if |
11 | one was raised, or -1 on internal error. |
12 | |
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"
21 #include "fpu_emu.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)
29 FPU_REG x, y;
30 FPU_REG const *a, *b, *st0_ptr, *st_ptr;
31 FPU_REG *dest;
32 u_char taga, tagb, signa, signb, sign, saved_sign;
33 int tag, deststnr;
35 if ( flags & DEST_RM )
36 deststnr = rm;
37 else
38 deststnr = 0;
40 if ( flags & REV )
42 b = &st(0);
43 st0_ptr = b;
44 tagb = FPU_gettag0();
45 if ( flags & LOADED )
47 a = (FPU_REG *)rm;
48 taga = flags & 0x0f;
50 else
52 a = &st(rm);
53 st_ptr = a;
54 taga = FPU_gettagi(rm);
57 else
59 a = &st(0);
60 st0_ptr = a;
61 taga = FPU_gettag0();
62 if ( flags & LOADED )
64 b = (FPU_REG *)rm;
65 tagb = flags & 0x0f;
67 else
69 b = &st(rm);
70 st_ptr = b;
71 tagb = FPU_gettagi(rm);
75 signa = getsign(a);
76 signb = getsign(b);
78 sign = signa ^ signb;
80 dest = &st(deststnr);
81 saved_sign = getsign(dest);
83 if ( !(taga | tagb) )
85 /* Both regs Valid, this should be the most common case. */
86 reg_copy(a, &x);
87 reg_copy(b, &y);
88 setpositive(&x);
89 setpositive(&y);
90 tag = FPU_u_div(&x, &y, dest, control_w, sign);
92 if ( tag < 0 )
93 return tag;
95 FPU_settagi(deststnr, tag);
96 return 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;
111 FPU_to_exp16(a, &x);
112 FPU_to_exp16(b, &y);
113 tag = FPU_u_div(&x, &y, dest, control_w, sign);
114 if ( tag < 0 )
115 return tag;
117 FPU_settagi(deststnr, tag);
118 return 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);
133 setsign(dest, sign);
134 return TAG_Zero;
136 /* We have an exception condition, either 0/0 or Valid/Zero. */
137 if ( taga == TAG_Zero )
139 /* 0/0 */
140 return arith_invalid(deststnr);
142 /* Valid/Zero */
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 )
153 int tag;
154 tag = FPU_gettag0();
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));
159 else
161 int tag;
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);
175 else
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);
184 setsign(dest, sign);
185 return taga;
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);
195 setsign(dest, sign);
196 return TAG_Zero;
198 #ifdef PARANOID
199 else
201 EXCEPTION(EX_INTERNAL|0x102);
202 return FPU_Exception;
204 #endif /* PARANOID */
206 return 0;