reg_mul.c 3.9 KB

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