reg_mul.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. {
  36. /* Both regs Valid, this should be the most common case. */
  37. tag = FPU_u_mul(a, b, dest, control_w, sign, exponent(a) + exponent(b));
  38. if ( tag < 0 )
  39. {
  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. {
  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. {
  63. setsign(dest, saved_sign);
  64. return tag;
  65. }
  66. FPU_settagi(deststnr, tag);
  67. return tag;
  68. }
  69. else if ( (taga <= TW_Denormal) && (tagb <= TW_Denormal) )
  70. {
  71. if ( ((tagb == TW_Denormal) || (taga == TW_Denormal))
  72. && (denormal_operand() < 0) )
  73. return FPU_Exception;
  74. /* Must have either both arguments == zero, or
  75. one valid and the other zero.
  76. The result is therefore zero. */
  77. FPU_copy_to_regi(&CONST_Z, TAG_Zero, deststnr);
  78. /* The 80486 book says that the answer is +0, but a real
  79. 80486 behaves this way.
  80. IEEE-754 apparently says it should be this way. */
  81. setsign(dest, sign);
  82. return TAG_Zero;
  83. }
  84. /* Must have infinities, NaNs, etc */
  85. else if ( (taga == TW_NaN) || (tagb == TW_NaN) )
  86. {
  87. return real_2op_NaN(b, tagb, deststnr, &st(0));
  88. }
  89. else if ( ((taga == TW_Infinity) && (tagb == TAG_Zero))
  90. || ((tagb == TW_Infinity) && (taga == TAG_Zero)) )
  91. {
  92. return arith_invalid(deststnr); /* Zero*Infinity is invalid */
  93. }
  94. else if ( ((taga == TW_Denormal) || (tagb == TW_Denormal))
  95. && (denormal_operand() < 0) )
  96. {
  97. return FPU_Exception;
  98. }
  99. else if (taga == TW_Infinity)
  100. {
  101. FPU_copy_to_regi(a, TAG_Special, deststnr);
  102. setsign(dest, sign);
  103. return TAG_Special;
  104. }
  105. else if (tagb == TW_Infinity)
  106. {
  107. FPU_copy_to_regi(b, TAG_Special, deststnr);
  108. setsign(dest, sign);
  109. return TAG_Special;
  110. }
  111. #ifdef PARANOID
  112. else
  113. {
  114. EXCEPTION(EX_INTERNAL|0x102);
  115. return FPU_Exception;
  116. }
  117. #endif /* PARANOID */
  118. return 0;
  119. }