reg_divide.c 4.9 KB

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