mul.S 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* $Id: mul.S,v 1.4 1996/09/30 02:22:32 davem Exp $
  2. * mul.S: This routine was taken from glibc-1.09 and is covered
  3. * by the GNU Library General Public License Version 2.
  4. */
  5. /*
  6. * Signed multiply, from Appendix E of the Sparc Version 8
  7. * Architecture Manual.
  8. */
  9. /*
  10. * Returns %o0 * %o1 in %o1%o0 (i.e., %o1 holds the upper 32 bits of
  11. * the 64-bit product).
  12. *
  13. * This code optimizes short (less than 13-bit) multiplies.
  14. */
  15. .globl .mul
  16. .mul:
  17. mov %o0, %y ! multiplier -> Y
  18. andncc %o0, 0xfff, %g0 ! test bits 12..31
  19. be Lmul_shortway ! if zero, can do it the short way
  20. andcc %g0, %g0, %o4 ! zero the partial product and clear N and V
  21. /*
  22. * Long multiply. 32 steps, followed by a final shift step.
  23. */
  24. mulscc %o4, %o1, %o4 ! 1
  25. mulscc %o4, %o1, %o4 ! 2
  26. mulscc %o4, %o1, %o4 ! 3
  27. mulscc %o4, %o1, %o4 ! 4
  28. mulscc %o4, %o1, %o4 ! 5
  29. mulscc %o4, %o1, %o4 ! 6
  30. mulscc %o4, %o1, %o4 ! 7
  31. mulscc %o4, %o1, %o4 ! 8
  32. mulscc %o4, %o1, %o4 ! 9
  33. mulscc %o4, %o1, %o4 ! 10
  34. mulscc %o4, %o1, %o4 ! 11
  35. mulscc %o4, %o1, %o4 ! 12
  36. mulscc %o4, %o1, %o4 ! 13
  37. mulscc %o4, %o1, %o4 ! 14
  38. mulscc %o4, %o1, %o4 ! 15
  39. mulscc %o4, %o1, %o4 ! 16
  40. mulscc %o4, %o1, %o4 ! 17
  41. mulscc %o4, %o1, %o4 ! 18
  42. mulscc %o4, %o1, %o4 ! 19
  43. mulscc %o4, %o1, %o4 ! 20
  44. mulscc %o4, %o1, %o4 ! 21
  45. mulscc %o4, %o1, %o4 ! 22
  46. mulscc %o4, %o1, %o4 ! 23
  47. mulscc %o4, %o1, %o4 ! 24
  48. mulscc %o4, %o1, %o4 ! 25
  49. mulscc %o4, %o1, %o4 ! 26
  50. mulscc %o4, %o1, %o4 ! 27
  51. mulscc %o4, %o1, %o4 ! 28
  52. mulscc %o4, %o1, %o4 ! 29
  53. mulscc %o4, %o1, %o4 ! 30
  54. mulscc %o4, %o1, %o4 ! 31
  55. mulscc %o4, %o1, %o4 ! 32
  56. mulscc %o4, %g0, %o4 ! final shift
  57. ! If %o0 was negative, the result is
  58. ! (%o0 * %o1) + (%o1 << 32))
  59. ! We fix that here.
  60. #if 0
  61. tst %o0
  62. bge 1f
  63. rd %y, %o0
  64. ! %o0 was indeed negative; fix upper 32 bits of result by subtracting
  65. ! %o1 (i.e., return %o4 - %o1 in %o1).
  66. retl
  67. sub %o4, %o1, %o1
  68. 1:
  69. retl
  70. mov %o4, %o1
  71. #else
  72. /* Faster code adapted from tege@sics.se's code for umul.S. */
  73. sra %o0, 31, %o2 ! make mask from sign bit
  74. and %o1, %o2, %o2 ! %o2 = 0 or %o1, depending on sign of %o0
  75. rd %y, %o0 ! get lower half of product
  76. retl
  77. sub %o4, %o2, %o1 ! subtract compensation
  78. ! and put upper half in place
  79. #endif
  80. Lmul_shortway:
  81. /*
  82. * Short multiply. 12 steps, followed by a final shift step.
  83. * The resulting bits are off by 12 and (32-12) = 20 bit positions,
  84. * but there is no problem with %o0 being negative (unlike above).
  85. */
  86. mulscc %o4, %o1, %o4 ! 1
  87. mulscc %o4, %o1, %o4 ! 2
  88. mulscc %o4, %o1, %o4 ! 3
  89. mulscc %o4, %o1, %o4 ! 4
  90. mulscc %o4, %o1, %o4 ! 5
  91. mulscc %o4, %o1, %o4 ! 6
  92. mulscc %o4, %o1, %o4 ! 7
  93. mulscc %o4, %o1, %o4 ! 8
  94. mulscc %o4, %o1, %o4 ! 9
  95. mulscc %o4, %o1, %o4 ! 10
  96. mulscc %o4, %o1, %o4 ! 11
  97. mulscc %o4, %o1, %o4 ! 12
  98. mulscc %o4, %g0, %o4 ! final shift
  99. /*
  100. * %o4 has 20 of the bits that should be in the low part of the
  101. * result; %y has the bottom 12 (as %y's top 12). That is:
  102. *
  103. * %o4 %y
  104. * +----------------+----------------+
  105. * | -12- | -20- | -12- | -20- |
  106. * +------(---------+------)---------+
  107. * --hi-- ----low-part----
  108. *
  109. * The upper 12 bits of %o4 should be sign-extended to form the
  110. * high part of the product (i.e., highpart = %o4 >> 20).
  111. */
  112. rd %y, %o5
  113. sll %o4, 12, %o0 ! shift middle bits left 12
  114. srl %o5, 20, %o5 ! shift low bits right 20, zero fill at left
  115. or %o5, %o0, %o0 ! construct low part of result
  116. retl
  117. sra %o4, 20, %o1 ! ... and extract high part of result
  118. .globl .mul_patch
  119. .mul_patch:
  120. smul %o0, %o1, %o0
  121. retl
  122. rd %y, %o1
  123. nop