_sparc.s
上传用户:gzelex
上传日期:2007-01-07
资源大小:707k
文件大小:7k
开发平台:

MultiPlatform

  1. !------------------------------------------------------------------------------
  2. ! assembler code for unsigned 32 bit multiplication
  3. ! taken from 
  4. !
  5. !            "The SPARC Architecture Manual"  (Appendix E.2)
  6. !
  7. !------------------------------------------------------------------------------
  8. !------------------------------------------------------------------------------
  9. ! extern "C" word Multiply_Words(word a, word b, word* high);
  10. ! (returns lower 32 bit of product, stores higher 32 bit in *high)
  11. !------------------------------------------------------------------------------
  12. .seg "text"
  13. .global _Multiply_Words
  14. _Multiply_Words:
  15. or      %o0, %o1, %o4   !logical or of multiplier and miltiplicand
  16. mov     %o0, %y         !multiplier to Y register 
  17. andncc  %o4, 0xfff, %o5 !mask out lower 12 bits
  18. be      mul_shortway    !can do it the short way
  19. andcc   %g0, %g0, %o4   !zero the partial product; clear N and V cond.
  20. ! long  multiply
  21. mulscc  %o4, %o1, %o4   !first of 33 iterations
  22. mulscc  %o4, %o1, %o4
  23. mulscc  %o4, %o1, %o4
  24. mulscc  %o4, %o1, %o4
  25. mulscc  %o4, %o1, %o4
  26. mulscc  %o4, %o1, %o4
  27. mulscc  %o4, %o1, %o4
  28. mulscc  %o4, %o1, %o4
  29. mulscc  %o4, %o1, %o4
  30. mulscc  %o4, %o1, %o4
  31. mulscc  %o4, %o1, %o4
  32. mulscc  %o4, %o1, %o4
  33. mulscc  %o4, %o1, %o4
  34. mulscc  %o4, %o1, %o4
  35. mulscc  %o4, %o1, %o4
  36. mulscc  %o4, %o1, %o4
  37. mulscc  %o4, %o1, %o4
  38. mulscc  %o4, %o1, %o4
  39. mulscc  %o4, %o1, %o4
  40. mulscc  %o4, %o1, %o4
  41. mulscc  %o4, %o1, %o4
  42. mulscc  %o4, %o1, %o4
  43. mulscc  %o4, %o1, %o4
  44. mulscc  %o4, %o1, %o4
  45. mulscc  %o4, %o1, %o4
  46. mulscc  %o4, %o1, %o4
  47. mulscc  %o4, %o1, %o4
  48. mulscc  %o4, %o1, %o4
  49. mulscc  %o4, %o1, %o4
  50. mulscc  %o4, %o1, %o4
  51. mulscc  %o4, %o1, %o4
  52. mulscc  %o4, %o1, %o4  ! 32nd iteration;
  53. mulscc  %o4, %g0, %o4  ! last iteration only shits
  54. tst     %o1
  55. bge     1f
  56. nop
  57. add     %o4, %o0, %o4
  58. 1:
  59. rd      %y, %o0 
  60. addcc   %o4, %g0, %o1  ! set zero bit appropriately
  61. st      %o1, [%o2]     ! return high bits
  62. retl                   ! leaf-routine return
  63. nop
  64. ! short multiply
  65. mul_shortway:
  66. mulscc  %o4, %o1, %o4 ! first iteration of 13
  67. mulscc  %o4, %o1, %o4
  68. mulscc  %o4, %o1, %o4
  69. mulscc  %o4, %o1, %o4
  70. mulscc  %o4, %o1, %o4
  71. mulscc  %o4, %o1, %o4
  72. mulscc  %o4, %o1, %o4
  73. mulscc  %o4, %o1, %o4
  74. mulscc  %o4, %o1, %o4
  75. mulscc  %o4, %o1, %o4
  76. mulscc  %o4, %o1, %o4
  77. mulscc  %o4, %o1, %o4  ! 12nd iteration
  78. mulscc  %o4, %g0, %o4  ! last iteration only shits
  79. rd      %y, %o5
  80. sll     %o4, 12, %o4   ! left shift partial product by 12 bits
  81. srl     %o5, 20, %o5   ! right shift product by 12 bits
  82. or      %o5, %o4, %o0  ! merge for true product
  83. addcc   %g0, %g0, %o1
  84. st      %o1, [%o2]     ! return high bits
  85. retl                   ! leaf routine return
  86. nop
  87. !------------------------------------------------------------------------------
  88. ! extern "C" word Mult_Inner_Loop(word *p, word *a, word* a_stop, word B)
  89. !------------------------------------------------------------------------------
  90. ! %o0 p
  91. ! %o1 a
  92. ! %o2 a_stop
  93. ! %o3 B
  94. !
  95. .global    _Mult_Inner_Loop
  96. _Mult_Inner_Loop:
  97. cmp        %o1,%o2       ! if a >= a_stop return 
  98. bge        IMULT_L1
  99. mov        0, %g5        ! carry = 0
  100. IMULT_L0:                 ! Loop: while (a < a_stop)
  101. mov        %o3, %y       ! load B to Y register
  102. andcc      %g0, %g0, %g3
  103. ld         [%o1], %g2    ! A = *a
  104. ld         [%o0], %g4    ! P = *p
  105. !
  106. ! compute 64 bit product A*B = (high,low)
  107. !
  108. mulscc     %g3, %g2, %g3 
  109. mulscc     %g3, %g2, %g3
  110. mulscc     %g3, %g2, %g3
  111. mulscc     %g3, %g2, %g3
  112. mulscc     %g3, %g2, %g3
  113. mulscc     %g3, %g2, %g3
  114. mulscc     %g3, %g2, %g3
  115. mulscc     %g3, %g2, %g3
  116. mulscc     %g3, %g2, %g3
  117. mulscc     %g3, %g2, %g3
  118. mulscc     %g3, %g2, %g3
  119. mulscc     %g3, %g2, %g3
  120. mulscc     %g3, %g2, %g3
  121. mulscc     %g3, %g2, %g3
  122. mulscc     %g3, %g2, %g3
  123. mulscc     %g3, %g2, %g3
  124. mulscc     %g3, %g2, %g3
  125. mulscc     %g3, %g2, %g3
  126. mulscc     %g3, %g2, %g3
  127. mulscc     %g3, %g2, %g3
  128. mulscc     %g3, %g2, %g3
  129. mulscc     %g3, %g2, %g3
  130. mulscc     %g3, %g2, %g3
  131. mulscc     %g3, %g2, %g3
  132. mulscc     %g3, %g2, %g3
  133. mulscc     %g3, %g2, %g3
  134. mulscc     %g3, %g2, %g3
  135. mulscc     %g3, %g2, %g3
  136. mulscc     %g3, %g2, %g3
  137. mulscc     %g3, %g2, %g3
  138. mulscc     %g3, %g2, %g3
  139. mulscc     %g3, %g2, %g3
  140. mulscc     %g3, %g0, %g3
  141. tst        %g2            ! sign correction
  142. bge        IMULT_L2
  143. rd         %y, %g2        ! low
  144. add        %g3, %o3, %g3  ! sign correction
  145. IMULT_L2:
  146. addcc      %g5, %g4, %g4  ! P += carry
  147. addx       %g0, %g0, %g5  ! new carry from addition
  148. addcc      %g2, %g4, %g2  ! low += P
  149. addx       %g3, %g5, %g5  ! carry += high
  150. st         %g2, [%o0]     ! *p = low
  151. inc        4,   %o1       ! a++
  152. cmp        %o1, %o2       ! if (a < a_stop) goto IMULT_L0
  153. bl         IMULT_L0       ! loop
  154. inc        4,   %o0       ! p++
  155. IMULT_L1:
  156. st         %g5, [%o0]     ! *p = carry
  157. retl
  158. mov        %g5, %o0       ! return carry
  159. !------------------------------------------------------------------------------
  160. ! extern "C" word Div_Inner_Loop(word *p, word *a, word* a_stop, word B)
  161. !------------------------------------------------------------------------------
  162. ! %o0 p
  163. ! %o1 a
  164. ! %o2 a_stop
  165. ! %o3 B
  166. !
  167. .global    _Div_Inner_Loop
  168. _Div_Inner_Loop:
  169. cmp        %o1,%o2       ! if a >= a_stop return 
  170. bge        IDIV_L1
  171. mov        0, %g5        ! carry = 0
  172. IDIV_L0:                 ! Loop: while (a < a_stop)
  173. mov        %o3, %y       ! load B to Y register
  174. andcc      %g0, %g0, %g3
  175. ld         [%o1], %g2    ! A = *a
  176. ld         [%o0], %g4    ! P = *p
  177. !
  178. ! compute 64 bit product A*B = (high,low)
  179. !
  180. mulscc     %g3, %g2, %g3 
  181. mulscc     %g3, %g2, %g3
  182. mulscc     %g3, %g2, %g3
  183. mulscc     %g3, %g2, %g3
  184. mulscc     %g3, %g2, %g3
  185. mulscc     %g3, %g2, %g3
  186. mulscc     %g3, %g2, %g3
  187. mulscc     %g3, %g2, %g3
  188. mulscc     %g3, %g2, %g3
  189. mulscc     %g3, %g2, %g3
  190. mulscc     %g3, %g2, %g3
  191. mulscc     %g3, %g2, %g3
  192. mulscc     %g3, %g2, %g3
  193. mulscc     %g3, %g2, %g3
  194. mulscc     %g3, %g2, %g3
  195. mulscc     %g3, %g2, %g3
  196. mulscc     %g3, %g2, %g3
  197. mulscc     %g3, %g2, %g3
  198. mulscc     %g3, %g2, %g3
  199. mulscc     %g3, %g2, %g3
  200. mulscc     %g3, %g2, %g3
  201. mulscc     %g3, %g2, %g3
  202. mulscc     %g3, %g2, %g3
  203. mulscc     %g3, %g2, %g3
  204. mulscc     %g3, %g2, %g3
  205. mulscc     %g3, %g2, %g3
  206. mulscc     %g3, %g2, %g3
  207. mulscc     %g3, %g2, %g3
  208. mulscc     %g3, %g2, %g3
  209. mulscc     %g3, %g2, %g3
  210. mulscc     %g3, %g2, %g3
  211. mulscc     %g3, %g2, %g3
  212. mulscc     %g3, %g0, %g3
  213. tst        %g2            ! sign correction
  214. bge        IDIV_L2
  215. rd         %y, %g2        ! low
  216. add        %g3, %o3, %g3  ! sign correction
  217. IDIV_L2:
  218. subcc      %g4, %g5, %g4  ! P -= carry
  219. addx       %g0, %g0, %g5  ! new carry from subtraction
  220. subcc      %g4, %g2, %g4  ! P -= low
  221. addx       %g3, %g5, %g5  ! carry += high
  222. st         %g4, [%o0]     ! *p = P
  223. inc        4,   %o1       ! a++
  224. cmp        %o1, %o2       ! if (a < a_stop) goto IDIV_L0
  225. bl         IDIV_L0        ! loop
  226. inc        4,   %o0       ! p++
  227. IDIV_L1:
  228. ld         [%o0], %g4     ! P = *p
  229. subcc      %g4, %g5, %g4  ! P -= carry
  230. addx       %g0, %g0, %g5  ! new carry 
  231. retl
  232. mov        %g5, %o0       ! return carry