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