1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2da957e11SThomas Gleixner /*---------------------------------------------------------------------------+
3da957e11SThomas Gleixner | reg_divide.c |
4da957e11SThomas Gleixner | |
5da957e11SThomas Gleixner | Divide one FPU_REG by another and put the result in a destination FPU_REG.|
6da957e11SThomas Gleixner | |
7da957e11SThomas Gleixner | Copyright (C) 1996 |
8da957e11SThomas Gleixner | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
9da957e11SThomas Gleixner | E-mail billm@jacobi.maths.monash.edu.au |
10da957e11SThomas Gleixner | |
11da957e11SThomas Gleixner | Return value is the tag of the answer, or-ed with FPU_Exception if |
12da957e11SThomas Gleixner | one was raised, or -1 on internal error. |
13da957e11SThomas Gleixner | |
14da957e11SThomas Gleixner +---------------------------------------------------------------------------*/
15da957e11SThomas Gleixner
16da957e11SThomas Gleixner /*---------------------------------------------------------------------------+
17da957e11SThomas Gleixner | The destination may be any FPU_REG, including one of the source FPU_REGs. |
18da957e11SThomas Gleixner +---------------------------------------------------------------------------*/
19da957e11SThomas Gleixner
20da957e11SThomas Gleixner #include "exception.h"
21da957e11SThomas Gleixner #include "reg_constant.h"
22da957e11SThomas Gleixner #include "fpu_emu.h"
23da957e11SThomas Gleixner #include "fpu_system.h"
24da957e11SThomas Gleixner
25da957e11SThomas Gleixner /*
26da957e11SThomas Gleixner Divide one register by another and put the result into a third register.
27da957e11SThomas Gleixner */
FPU_div(int flags,int rm,int control_w)28da957e11SThomas Gleixner int FPU_div(int flags, int rm, int control_w)
29da957e11SThomas Gleixner {
30da957e11SThomas Gleixner FPU_REG x, y;
31da957e11SThomas Gleixner FPU_REG const *a, *b, *st0_ptr, *st_ptr;
32da957e11SThomas Gleixner FPU_REG *dest;
33da957e11SThomas Gleixner u_char taga, tagb, signa, signb, sign, saved_sign;
34da957e11SThomas Gleixner int tag, deststnr;
35da957e11SThomas Gleixner
36da957e11SThomas Gleixner if (flags & DEST_RM)
37da957e11SThomas Gleixner deststnr = rm;
38da957e11SThomas Gleixner else
39da957e11SThomas Gleixner deststnr = 0;
40da957e11SThomas Gleixner
413d0d14f9SIngo Molnar if (flags & REV) {
42da957e11SThomas Gleixner b = &st(0);
43da957e11SThomas Gleixner st0_ptr = b;
44da957e11SThomas Gleixner tagb = FPU_gettag0();
453d0d14f9SIngo Molnar if (flags & LOADED) {
46da957e11SThomas Gleixner a = (FPU_REG *) rm;
47da957e11SThomas Gleixner taga = flags & 0x0f;
483d0d14f9SIngo Molnar } else {
49da957e11SThomas Gleixner a = &st(rm);
50da957e11SThomas Gleixner st_ptr = a;
51da957e11SThomas Gleixner taga = FPU_gettagi(rm);
52da957e11SThomas Gleixner }
533d0d14f9SIngo Molnar } else {
54da957e11SThomas Gleixner a = &st(0);
55da957e11SThomas Gleixner st0_ptr = a;
56da957e11SThomas Gleixner taga = FPU_gettag0();
573d0d14f9SIngo Molnar if (flags & LOADED) {
58da957e11SThomas Gleixner b = (FPU_REG *) rm;
59da957e11SThomas Gleixner tagb = flags & 0x0f;
603d0d14f9SIngo Molnar } else {
61da957e11SThomas Gleixner b = &st(rm);
62da957e11SThomas Gleixner st_ptr = b;
63da957e11SThomas Gleixner tagb = FPU_gettagi(rm);
64da957e11SThomas Gleixner }
65da957e11SThomas Gleixner }
66da957e11SThomas Gleixner
67da957e11SThomas Gleixner signa = getsign(a);
68da957e11SThomas Gleixner signb = getsign(b);
69da957e11SThomas Gleixner
70da957e11SThomas Gleixner sign = signa ^ signb;
71da957e11SThomas Gleixner
72da957e11SThomas Gleixner dest = &st(deststnr);
73da957e11SThomas Gleixner saved_sign = getsign(dest);
74da957e11SThomas Gleixner
753d0d14f9SIngo Molnar if (!(taga | tagb)) {
76da957e11SThomas Gleixner /* Both regs Valid, this should be the most common case. */
77da957e11SThomas Gleixner reg_copy(a, &x);
78da957e11SThomas Gleixner reg_copy(b, &y);
79da957e11SThomas Gleixner setpositive(&x);
80da957e11SThomas Gleixner setpositive(&y);
81da957e11SThomas Gleixner tag = FPU_u_div(&x, &y, dest, control_w, sign);
82da957e11SThomas Gleixner
83da957e11SThomas Gleixner if (tag < 0)
84da957e11SThomas Gleixner return tag;
85da957e11SThomas Gleixner
86da957e11SThomas Gleixner FPU_settagi(deststnr, tag);
87da957e11SThomas Gleixner return tag;
88da957e11SThomas Gleixner }
89da957e11SThomas Gleixner
90da957e11SThomas Gleixner if (taga == TAG_Special)
91da957e11SThomas Gleixner taga = FPU_Special(a);
92da957e11SThomas Gleixner if (tagb == TAG_Special)
93da957e11SThomas Gleixner tagb = FPU_Special(b);
94da957e11SThomas Gleixner
95da957e11SThomas Gleixner if (((taga == TAG_Valid) && (tagb == TW_Denormal))
96da957e11SThomas Gleixner || ((taga == TW_Denormal) && (tagb == TAG_Valid))
973d0d14f9SIngo Molnar || ((taga == TW_Denormal) && (tagb == TW_Denormal))) {
98da957e11SThomas Gleixner if (denormal_operand() < 0)
99da957e11SThomas Gleixner return FPU_Exception;
100da957e11SThomas Gleixner
101da957e11SThomas Gleixner FPU_to_exp16(a, &x);
102da957e11SThomas Gleixner FPU_to_exp16(b, &y);
103da957e11SThomas Gleixner tag = FPU_u_div(&x, &y, dest, control_w, sign);
104da957e11SThomas Gleixner if (tag < 0)
105da957e11SThomas Gleixner return tag;
106da957e11SThomas Gleixner
107da957e11SThomas Gleixner FPU_settagi(deststnr, tag);
108da957e11SThomas Gleixner return tag;
1093d0d14f9SIngo Molnar } else if ((taga <= TW_Denormal) && (tagb <= TW_Denormal)) {
1103d0d14f9SIngo Molnar if (tagb != TAG_Zero) {
111da957e11SThomas Gleixner /* Want to find Zero/Valid */
1123d0d14f9SIngo Molnar if (tagb == TW_Denormal) {
113da957e11SThomas Gleixner if (denormal_operand() < 0)
114da957e11SThomas Gleixner return FPU_Exception;
115da957e11SThomas Gleixner }
116da957e11SThomas Gleixner
117da957e11SThomas Gleixner /* The result is zero. */
118da957e11SThomas Gleixner FPU_copy_to_regi(&CONST_Z, TAG_Zero, deststnr);
119da957e11SThomas Gleixner setsign(dest, sign);
120da957e11SThomas Gleixner return TAG_Zero;
121da957e11SThomas Gleixner }
122da957e11SThomas Gleixner /* We have an exception condition, either 0/0 or Valid/Zero. */
1233d0d14f9SIngo Molnar if (taga == TAG_Zero) {
124da957e11SThomas Gleixner /* 0/0 */
125da957e11SThomas Gleixner return arith_invalid(deststnr);
126da957e11SThomas Gleixner }
127da957e11SThomas Gleixner /* Valid/Zero */
128da957e11SThomas Gleixner return FPU_divide_by_zero(deststnr, sign);
129da957e11SThomas Gleixner }
130da957e11SThomas Gleixner /* Must have infinities, NaNs, etc */
1313d0d14f9SIngo Molnar else if ((taga == TW_NaN) || (tagb == TW_NaN)) {
132da957e11SThomas Gleixner if (flags & LOADED)
1333d0d14f9SIngo Molnar return real_2op_NaN((FPU_REG *) rm, flags & 0x0f, 0,
1343d0d14f9SIngo Molnar st0_ptr);
135da957e11SThomas Gleixner
1363d0d14f9SIngo Molnar if (flags & DEST_RM) {
137da957e11SThomas Gleixner int tag;
138da957e11SThomas Gleixner tag = FPU_gettag0();
139da957e11SThomas Gleixner if (tag == TAG_Special)
140da957e11SThomas Gleixner tag = FPU_Special(st0_ptr);
1413d0d14f9SIngo Molnar return real_2op_NaN(st0_ptr, tag, rm,
1423d0d14f9SIngo Molnar (flags & REV) ? st0_ptr : &st(rm));
1433d0d14f9SIngo Molnar } else {
144da957e11SThomas Gleixner int tag;
145da957e11SThomas Gleixner tag = FPU_gettagi(rm);
146da957e11SThomas Gleixner if (tag == TAG_Special)
147da957e11SThomas Gleixner tag = FPU_Special(&st(rm));
1483d0d14f9SIngo Molnar return real_2op_NaN(&st(rm), tag, 0,
1493d0d14f9SIngo Molnar (flags & REV) ? st0_ptr : &st(rm));
150da957e11SThomas Gleixner }
1513d0d14f9SIngo Molnar } else if (taga == TW_Infinity) {
1523d0d14f9SIngo Molnar if (tagb == TW_Infinity) {
153da957e11SThomas Gleixner /* infinity/infinity */
154da957e11SThomas Gleixner return arith_invalid(deststnr);
1553d0d14f9SIngo Molnar } else {
156da957e11SThomas Gleixner /* tagb must be Valid or Zero */
157da957e11SThomas Gleixner if ((tagb == TW_Denormal) && (denormal_operand() < 0))
158da957e11SThomas Gleixner return FPU_Exception;
159da957e11SThomas Gleixner
160da957e11SThomas Gleixner /* Infinity divided by Zero or Valid does
161da957e11SThomas Gleixner not raise and exception, but returns Infinity */
162da957e11SThomas Gleixner FPU_copy_to_regi(a, TAG_Special, deststnr);
163da957e11SThomas Gleixner setsign(dest, sign);
164da957e11SThomas Gleixner return taga;
165da957e11SThomas Gleixner }
1663d0d14f9SIngo Molnar } else if (tagb == TW_Infinity) {
167da957e11SThomas Gleixner if ((taga == TW_Denormal) && (denormal_operand() < 0))
168da957e11SThomas Gleixner return FPU_Exception;
169da957e11SThomas Gleixner
170da957e11SThomas Gleixner /* The result is zero. */
171da957e11SThomas Gleixner FPU_copy_to_regi(&CONST_Z, TAG_Zero, deststnr);
172da957e11SThomas Gleixner setsign(dest, sign);
173da957e11SThomas Gleixner return TAG_Zero;
174da957e11SThomas Gleixner }
175da957e11SThomas Gleixner #ifdef PARANOID
1763d0d14f9SIngo Molnar else {
177da957e11SThomas Gleixner EXCEPTION(EX_INTERNAL | 0x102);
178da957e11SThomas Gleixner return FPU_Exception;
179da957e11SThomas Gleixner }
180da957e11SThomas Gleixner #endif /* PARANOID */
181da957e11SThomas Gleixner
182da957e11SThomas Gleixner return 0;
183da957e11SThomas Gleixner }
184