mul.S 3.5 KB

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