reg_mul.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*---------------------------------------------------------------------------+
  2. | reg_mul.c |
  3. | |
  4. | Multiply one FPU_REG by another, put the result in a destination FPU_REG. |
  5. | |
  6. | Copyright (C) 1992,1993,1997 |
  7. | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, Australia |
  8. | E-mail billm@suburbia.net |
  9. | |
  10. | Returns the tag of the result if no exceptions or errors occurred. |
  11. | |
  12. +---------------------------------------------------------------------------*/
  13. /*---------------------------------------------------------------------------+
  14. | The destination may be any FPU_REG, including one of the source FPU_REGs. |
  15. +---------------------------------------------------------------------------*/
  16. #include "fpu_emu.h"
  17. #include "exception.h"
  18. #include "reg_constant.h"
  19. #include "fpu_system.h"
  20. /*
  21. Multiply two registers to give a register result.
  22. The sources are st(deststnr) and (b,tagb,signb).
  23. The destination is st(deststnr).
  24. */
  25. /* This routine must be called with non-empty source registers */
  26. int FPU_mul(FPU_REG const *b, u_char tagb, int deststnr, int control_w)
  27. {
  28. FPU_REG *a = &st(deststnr);
  29. FPU_REG *dest = a;
  30. u_char taga = FPU_gettagi(deststnr);
  31. u_char saved_sign = getsign(dest);
  32. u_char sign = (getsign(a) ^ getsign(b));
  33. int tag;
  34. if (!(taga | tagb)) {
  35. /* Both regs Valid, this should be the most common case. */
  36. tag =
  37. FPU_u_mul(a, b, dest, control_w, sign,
  38. exponent(a) + exponent(b));
  39. if (tag < 0) {
  40. setsign(dest, saved_sign);
  41. return tag;
  42. }
  43. FPU_settagi(deststnr, tag);
  44. return tag;
  45. }
  46. if (taga == TAG_Special)
  47. taga = FPU_Special(a);
  48. if (tagb == TAG_Special)
  49. tagb = FPU_Special(b);
  50. if (((taga == TAG_Valid) && (tagb == TW_Denormal))
  51. || ((taga == TW_Denormal) && (tagb == TAG_Valid))
  52. || ((taga == TW_Denormal) && (tagb == TW_Denormal))) {
  53. FPU_REG x, y;
  54. if (denormal_operand() < 0)
  55. return FPU_Exception;
  56. FPU_to_exp16(a, &x);
  57. FPU_to_exp16(b, &y);
  58. tag = FPU_u_mul(&x, &y, dest, control_w, sign,
  59. exponent16(&x) + exponent16(&y));
  60. if (tag < 0) {
  61. setsign(dest, saved_sign);
  62. return tag;
  63. }
  64. FPU_settagi(deststnr, tag);
  65. return tag;
  66. } else if ((taga <= TW_Denormal) && (tagb <= TW_Denormal)) {
  67. if (((tagb == TW_Denormal) || (taga == TW_Denormal))
  68. && (denormal_operand() < 0))
  69. return FPU_Exception;
  70. /* Must have either both arguments == zero, or
  71. one valid and the other zero.
  72. The result is therefore zero. */
  73. FPU_copy_to_regi(&CONST_Z, TAG_Zero, deststnr);
  74. /* The 80486 book says that the answer is +0, but a real
  75. 80486 behaves this way.
  76. IEEE-754 apparently says it should be this way. */
  77. setsign(dest, sign);
  78. return TAG_Zero;
  79. }
  80. /* Must have infinities, NaNs, etc */
  81. else if ((taga == TW_NaN) || (tagb == TW_NaN)) {
  82. return real_2op_NaN(b, tagb, deststnr, &st(0));
  83. } else if (((taga == TW_Infinity) && (tagb == TAG_Zero))
  84. || ((tagb == TW_Infinity) && (taga == TAG_Zero))) {
  85. return arith_invalid(deststnr); /* Zero*Infinity is invalid */
  86. } else if (((taga == TW_Denormal) || (tagb == TW_Denormal))
  87. && (denormal_operand() < 0)) {
  88. return FPU_Exception;
  89. } else if (taga == TW_Infinity) {
  90. FPU_copy_to_regi(a, TAG_Special, deststnr);
  91. setsign(dest, sign);
  92. return TAG_Special;
  93. } else if (tagb == TW_Infinity) {
  94. FPU_copy_to_regi(b, TAG_Special, deststnr);
  95. setsign(dest, sign);
  96. return TAG_Special;
  97. }
  98. #ifdef PARANOID
  99. else {
  100. EXCEPTION(EX_INTERNAL | 0x102);
  101. return FPU_Exception;
  102. }
  103. #endif /* PARANOID */
  104. return 0;
  105. }