mul_1.asm
上传用户:qaz666999
上传日期:2022-08-06
资源大小:2570k
文件大小:13k
源码类别:

数学计算

开发平台:

Unix_Linux

  1. dnl  Alpha ev6 mpn_mul_1 -- Multiply a limb vector with a limb and store the
  2. dnl  result in a second limb vector.
  3. dnl  Copyright 2000, 2001, 2005 Free Software Foundation, Inc.
  4. dnl  This file is part of the GNU MP Library.
  5. dnl  The GNU MP Library is free software; you can redistribute it and/or modify
  6. dnl  it under the terms of the GNU Lesser General Public License as published
  7. dnl  by the Free Software Foundation; either version 3 of the License, or (at
  8. dnl  your option) any later version.
  9. dnl  The GNU MP Library is distributed in the hope that it will be useful, but
  10. dnl  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. dnl  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  12. dnl  License for more details.
  13. dnl  You should have received a copy of the GNU Lesser General Public License
  14. dnl  along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.
  15. include(`../config.m4')
  16. C INPUT PARAMETERS
  17. C res_ptr r16
  18. C s1_ptr r17
  19. C size r18
  20. C s2_limb r19
  21. C This code runs at 2.25 cycles/limb on EV6.
  22. C This code was written in close cooperation with ev6 pipeline expert
  23. C Steve Root.  Any errors are tege's fault, though.
  24. C Code structure:
  25. C  code for n < 8
  26. C  code for n > 8 code for (n mod 8)
  27. C code for (n div 8) feed-in code
  28. C 8-way unrolled loop
  29. C wind-down code
  30. C Some notes about unrolled loop:
  31. C
  32. C   r1-r8     multiplies and workup
  33. C   r21-r28   multiplies and workup
  34. C   r9-r12    loads
  35. C   r0       -1
  36. C   r20,r29,r13-r15  scramble
  37. C
  38. C   We're doing 7 of the 8 carry propagations with a br fixup code and 1 with a
  39. C   put-the-carry-into-hi.  The idea is that these branches are very rarely
  40. C   taken, and since a non-taken branch consumes no resources, that is better
  41. C   than an addq.
  42. C
  43. C   Software pipeline: a load in cycle #09, feeds a mul in cycle #16, feeds an
  44. C   add NEXT cycle #09 which feeds a store in NEXT cycle #02
  45. C The code could use some further work:
  46. C   1. Speed up really small multiplies.  The default alpha/mul_1.asm code is
  47. C      faster than this for size < 3.
  48. C   2. Improve feed-in code, perhaps with the equivalent of switch(n%8) unless
  49. C      that is too costly.
  50. C   3. Consider using 4-way unrolling, even if that runs slower.
  51. C   4. Reduce register usage.  In particular, try to avoid using r29.
  52. ASM_START()
  53. PROLOGUE(mpn_mul_1)
  54. cmpult r18, 8, r1
  55. beq r1, $Large
  56. $Lsmall:
  57. ldq r2,0(r17) C r2 = s1_limb
  58. lda r18,-1(r18) C size--
  59. mulq r2,r19,r3 C r3 = prod_low
  60. bic r31,r31,r4 C clear cy_limb
  61. umulh r2,r19,r0 C r0 = prod_high
  62. beq r18,$Le1a C jump if size was == 1
  63. ldq r2,8(r17) C r2 = s1_limb
  64. lda r18,-1(r18) C size--
  65. stq r3,0(r16)
  66. beq r18,$Le2a C jump if size was == 2
  67. ALIGN(8)
  68. $Lopa: mulq r2,r19,r3 C r3 = prod_low
  69. addq r4,r0,r0 C cy_limb = cy_limb + 'cy'
  70. lda r18,-1(r18) C size--
  71. umulh r2,r19,r4 C r4 = cy_limb
  72. ldq r2,16(r17) C r2 = s1_limb
  73. lda r17,8(r17) C s1_ptr++
  74. addq r3,r0,r3 C r3 = cy_limb + prod_low
  75. stq r3,8(r16)
  76. cmpult r3,r0,r0 C r0 = carry from (cy_limb + prod_low)
  77. lda r16,8(r16) C res_ptr++
  78. bne r18,$Lopa
  79. $Le2a: mulq r2,r19,r3 C r3 = prod_low
  80. addq r4,r0,r0 C cy_limb = cy_limb + 'cy'
  81. umulh r2,r19,r4 C r4 = cy_limb
  82. addq r3,r0,r3 C r3 = cy_limb + prod_low
  83. cmpult r3,r0,r0 C r0 = carry from (cy_limb + prod_low)
  84. stq r3,8(r16)
  85. addq r4,r0,r0 C cy_limb = prod_high + cy
  86. ret r31,(r26),1
  87. $Le1a: stq r3,0(r16)
  88. ret r31,(r26),1
  89. $Large:
  90. lda r30, -224(r30)
  91. stq r26, 0(r30)
  92. stq r9, 8(r30)
  93. stq r10, 16(r30)
  94. stq r11, 24(r30)
  95. stq r12, 32(r30)
  96. stq r13, 40(r30)
  97. stq r14, 48(r30)
  98. stq r15, 56(r30)
  99. stq r29, 64(r30)
  100. and r18, 7, r20 C count for the first loop, 0-7
  101. srl r18, 3, r18 C count for unrolled loop
  102. bis r31, r31, r21
  103. beq r20, $L_8_or_more C skip first loop
  104. $L_9_or_more:
  105. ldq r2,0(r17) C r2 = s1_limb
  106. lda r17,8(r17) C s1_ptr++
  107. lda r20,-1(r20) C size--
  108. mulq r2,r19,r3 C r3 = prod_low
  109. umulh r2,r19,r21 C r21 = prod_high
  110. beq r20,$Le1b C jump if size was == 1
  111. bis r31, r31, r0 C FIXME: shouldn't need this
  112. ldq r2,0(r17) C r2 = s1_limb
  113. lda r17,8(r17) C s1_ptr++
  114. lda r20,-1(r20) C size--
  115. stq r3,0(r16)
  116. lda r16,8(r16) C res_ptr++
  117. beq r20,$Le2b C jump if size was == 2
  118. ALIGN(8)
  119. $Lopb: mulq r2,r19,r3 C r3 = prod_low
  120. addq r21,r0,r0 C cy_limb = cy_limb + 'cy'
  121. lda r20,-1(r20) C size--
  122. umulh r2,r19,r21 C r21 = prod_high
  123. ldq r2,0(r17) C r2 = s1_limb
  124. lda r17,8(r17) C s1_ptr++
  125. addq r3,r0,r3 C r3 = cy_limb + prod_low
  126. stq r3,0(r16)
  127. cmpult r3,r0,r0 C r0 = carry from (cy_limb + prod_low)
  128. lda r16,8(r16) C res_ptr++
  129. bne r20,$Lopb
  130. $Le2b: mulq r2,r19,r3 C r3 = prod_low
  131. addq r21,r0,r0 C cy_limb = cy_limb + 'cy'
  132. umulh r2,r19,r21 C r21 = prod_high
  133. addq r3,r0,r3 C r3 = cy_limb + prod_low
  134. cmpult r3,r0,r0 C r0 = carry from (cy_limb + prod_low)
  135. stq r3,0(r16)
  136. lda r16,8(r16) C res_ptr++
  137. addq r21,r0,r21 C cy_limb = prod_high + cy
  138. br r31, $L_8_or_more
  139. $Le1b: stq r3,0(r16)
  140. lda r16,8(r16) C res_ptr++
  141. $L_8_or_more:
  142. lda r0, -1(r31) C put -1 in r0, for tricky loop control
  143. lda r17, -32(r17) C L1 bookkeeping
  144. lda r18, -1(r18) C decrement count
  145. ldq r9, 32(r17) C L1
  146. ldq r10, 40(r17) C L1
  147. mulq r9, r19, r22 C U1 #07
  148. ldq r11, 48(r17) C L1
  149. umulh r9, r19, r23 C U1 #08
  150. ldq r12, 56(r17) C L1
  151. mulq r10, r19, r24 C U1 #09
  152. ldq r9, 64(r17) C L1
  153. lda r17, 64(r17) C L1 bookkeeping
  154. umulh r10, r19, r25 C U1 #11
  155. mulq r11, r19, r26 C U1 #12
  156. umulh r11, r19, r27 C U1 #13
  157. mulq r12, r19, r28 C U1 #14
  158. ldq r10, 8(r17) C L1
  159. umulh r12, r19, r1 C U1 #15
  160. ldq r11, 16(r17) C L1
  161. mulq r9, r19, r2 C U1 #16
  162. ldq r12, 24(r17) C L1
  163. umulh r9, r19, r3 C U1 #17
  164. addq r21, r22, r13 C L1 mov
  165. mulq r10, r19, r4 C U1 #18
  166. addq r23, r24, r22 C L0 sum 2 mul's
  167. cmpult r13, r21, r14 C L1 carry from sum
  168. bgt r18, $L_16_or_more
  169. cmpult r22, r24, r24 C U0 carry from sum
  170. umulh r10, r19, r5 C U1 #02
  171. addq r25, r26, r23 C U0 sum 2 mul's
  172. mulq r11, r19, r6 C U1 #03
  173. cmpult r23, r26, r25 C U0 carry from sum
  174. umulh r11, r19, r7 C U1 #04
  175. addq r27, r28, r28 C U0 sum 2 mul's
  176. mulq r12, r19, r8 C U1 #05
  177. cmpult r28, r27, r15 C L0 carry from sum
  178. lda r16, 32(r16) C L1 bookkeeping
  179. addq r13, r31, r13 C U0 start carry cascade
  180. umulh r12, r19, r21 C U1 #06
  181. br r31, $ret0c
  182. $L_16_or_more:
  183. C ---------------------------------------------------------------
  184. subq r18,1,r18
  185. cmpult r22, r24, r24 C U0 carry from sum
  186. ldq r9, 32(r17) C L1
  187. umulh r10, r19, r5 C U1 #02
  188. addq r25, r26, r23 C U0 sum 2 mul's
  189. mulq r11, r19, r6 C U1 #03
  190. cmpult r23, r26, r25 C U0 carry from sum
  191. umulh r11, r19, r7 C U1 #04
  192. addq r27, r28, r28 C U0 sum 2 mul's
  193. mulq r12, r19, r8 C U1 #05
  194. cmpult r28, r27, r15 C L0 carry from sum
  195. lda r16, 32(r16) C L1 bookkeeping
  196. addq r13, r31, r13 C U0 start carry cascade
  197. umulh r12, r19, r21 C U1 #06
  198. C beq r13, $fix0w C U0
  199. $ret0w: addq r22, r14, r26 C L0
  200. ldq r10, 40(r17) C L1
  201. mulq r9, r19, r22 C U1 #07
  202. beq r26, $fix1w C U0
  203. $ret1w: addq r23, r24, r27 C L0
  204. ldq r11, 48(r17) C L1
  205. umulh r9, r19, r23 C U1 #08
  206. beq r27, $fix2w C U0
  207. $ret2w: addq r28, r25, r28 C L0
  208. ldq r12, 56(r17) C L1
  209. mulq r10, r19, r24 C U1 #09
  210. beq r28, $fix3w C U0
  211. $ret3w: addq r1, r2, r20 C L0 sum 2 mul's
  212. ldq r9, 64(r17) C L1
  213. addq r3, r4, r2 C L0 #10 2 mul's
  214. lda r17, 64(r17) C L1 bookkeeping
  215. cmpult r20, r1, r29 C U0 carry from sum
  216. umulh r10, r19, r25 C U1 #11
  217. cmpult r2, r4, r4 C U0 carry from sum
  218. stq r13, -32(r16) C L0
  219. stq r26, -24(r16) C L1
  220. mulq r11, r19, r26 C U1 #12
  221. addq r5, r6, r14 C U0 sum 2 mul's
  222. stq r27, -16(r16) C L0
  223. stq r28, -8(r16) C L1
  224. umulh r11, r19, r27 C U1 #13
  225. cmpult r14, r6, r3 C U0 carry from sum
  226. C could do cross-jumping here:
  227. C bra $L_middle_of_unrolled_loop
  228. mulq r12, r19, r28 C U1 #14
  229. addq r7, r3, r5 C L0 eat carry
  230. addq r20, r15, r20 C U0 carry cascade
  231. ldq r10, 8(r17) C L1
  232. umulh r12, r19, r1 C U1 #15
  233. beq r20, $fix4 C U0
  234. $ret4w: addq r2, r29, r6 C L0
  235. ldq r11, 16(r17) C L1
  236. mulq r9, r19, r2 C U1 #16
  237. beq r6, $fix5 C U0
  238. $ret5w: addq r14, r4, r7 C L0
  239. ldq r12, 24(r17) C L1
  240. umulh r9, r19, r3 C U1 #17
  241. beq r7, $fix6 C U0
  242. $ret6w: addq r5, r8, r8 C L0 sum 2
  243. addq r21, r22, r13 C L1 sum 2 mul's
  244. mulq r10, r19, r4 C U1 #18
  245. addq r23, r24, r22 C L0 sum 2 mul's
  246. cmpult r13, r21, r14 C L1 carry from sum
  247. ble r18, $Lend C U0
  248. C ---------------------------------------------------------------
  249. ALIGN(16)
  250. $Loop:
  251. umulh r0, r18, r18 C U1 #01 decrement r18!
  252. cmpult r8, r5, r29 C L0 carry from last bunch
  253. cmpult r22, r24, r24 C U0 carry from sum
  254. ldq r9, 32(r17) C L1
  255. umulh r10, r19, r5 C U1 #02
  256. addq r25, r26, r23 C U0 sum 2 mul's
  257. stq r20, 0(r16) C L0
  258. stq r6, 8(r16) C L1
  259. mulq r11, r19, r6 C U1 #03
  260. cmpult r23, r26, r25 C U0 carry from sum
  261. stq r7, 16(r16) C L0
  262. stq r8, 24(r16) C L1
  263. umulh r11, r19, r7 C U1 #04
  264. bis r31, r31, r31 C L0 st slosh
  265. bis r31, r31, r31 C L1 st slosh
  266. addq r27, r28, r28 C U0 sum 2 mul's
  267. mulq r12, r19, r8 C U1 #05
  268. cmpult r28, r27, r15 C L0 carry from sum
  269. lda r16, 64(r16) C L1 bookkeeping
  270. addq r13, r29, r13 C U0 start carry cascade
  271. umulh r12, r19, r21 C U1 #06
  272. beq r13, $fix0 C U0
  273. $ret0: addq r22, r14, r26 C L0
  274. ldq r10, 40(r17) C L1
  275. mulq r9, r19, r22 C U1 #07
  276. beq r26, $fix1 C U0
  277. $ret1: addq r23, r24, r27 C L0
  278. ldq r11, 48(r17) C L1
  279. umulh r9, r19, r23 C U1 #08
  280. beq r27, $fix2 C U0
  281. $ret2: addq r28, r25, r28 C L0
  282. ldq r12, 56(r17) C L1
  283. mulq r10, r19, r24 C U1 #09
  284. beq r28, $fix3 C U0
  285. $ret3: addq r1, r2, r20 C L0 sum 2 mul's
  286. ldq r9, 64(r17) C L1
  287. addq r3, r4, r2 C L0 #10 2 mul's
  288. bis r31, r31, r31 C U1 mul hole
  289. lda r17, 64(r17) C L1 bookkeeping
  290. cmpult r20, r1, r29 C U0 carry from sum
  291. umulh r10, r19, r25 C U1 #11
  292. cmpult r2, r4, r4 C U0 carry from sum
  293. stq r13, -32(r16) C L0
  294. stq r26, -24(r16) C L1
  295. mulq r11, r19, r26 C U1 #12
  296. addq r5, r6, r14 C U0 sum 2 mul's
  297. stq r27, -16(r16) C L0
  298. stq r28, -8(r16) C L1
  299. umulh r11, r19, r27 C U1 #13
  300. bis r31, r31, r31 C L0 st slosh
  301. bis r31, r31, r31 C L1 st slosh
  302. cmpult r14, r6, r3 C U0 carry from sum
  303. $L_middle_of_unrolled_loop:
  304. mulq r12, r19, r28 C U1 #14
  305. addq r7, r3, r5 C L0 eat carry
  306. addq r20, r15, r20 C U0 carry cascade
  307. ldq r10, 8(r17) C L1
  308. umulh r12, r19, r1 C U1 #15
  309. beq r20, $fix4 C U0
  310. $ret4: addq r2, r29, r6 C L0
  311. ldq r11, 16(r17) C L1
  312. mulq r9, r19, r2 C U1 #16
  313. beq r6, $fix5 C U0
  314. $ret5: addq r14, r4, r7 C L0
  315. ldq r12, 24(r17) C L1
  316. umulh r9, r19, r3 C U1 #17
  317. beq r7, $fix6 C U0
  318. $ret6: addq r5, r8, r8 C L0 sum 2
  319. addq r21, r22, r13 C L1 sum 2 mul's
  320. mulq r10, r19, r4 C U1 #18
  321. addq r23, r24, r22 C L0 sum 2 mul's
  322. cmpult r13, r21, r14 C L1 carry from sum
  323. bgt r18, $Loop C U0
  324. C ---------------------------------------------------------------
  325. $Lend:
  326. cmpult r8, r5, r29 C L0 carry from last bunch
  327. cmpult r22, r24, r24 C U0 carry from sum
  328. umulh r10, r19, r5 C U1 #02
  329. addq r25, r26, r23 C U0 sum 2 mul's
  330. stq r20, 0(r16) C L0
  331. stq r6, 8(r16) C L1
  332. mulq r11, r19, r6 C U1 #03
  333. cmpult r23, r26, r25 C U0 carry from sum
  334. stq r7, 16(r16) C L0
  335. stq r8, 24(r16) C L1
  336. umulh r11, r19, r7 C U1 #04
  337. addq r27, r28, r28 C U0 sum 2 mul's
  338. mulq r12, r19, r8 C U1 #05
  339. cmpult r28, r27, r15 C L0 carry from sum
  340. lda r16, 64(r16) C L1 bookkeeping
  341. addq r13, r29, r13 C U0 start carry cascade
  342. umulh r12, r19, r21 C U1 #06
  343. beq r13, $fix0c C U0
  344. $ret0c: addq r22, r14, r26 C L0
  345. beq r26, $fix1c C U0
  346. $ret1c: addq r23, r24, r27 C L0
  347. beq r27, $fix2c C U0
  348. $ret2c: addq r28, r25, r28 C L0
  349. beq r28, $fix3c C U0
  350. $ret3c: addq r1, r2, r20 C L0 sum 2 mul's
  351. addq r3, r4, r2 C L0 #10 2 mul's
  352. lda r17, 64(r17) C L1 bookkeeping
  353. cmpult r20, r1, r29 C U0 carry from sum
  354. cmpult r2, r4, r4 C U0 carry from sum
  355. stq r13, -32(r16) C L0
  356. stq r26, -24(r16) C L1
  357. addq r5, r6, r14 C U0 sum 2 mul's
  358. stq r27, -16(r16) C L0
  359. stq r28, -8(r16) C L1
  360. cmpult r14, r6, r3 C U0 carry from sum
  361. addq r7, r3, r5 C L0 eat carry
  362. addq r20, r15, r20 C U0 carry cascade
  363. beq r20, $fix4c C U0
  364. $ret4c: addq r2, r29, r6 C L0
  365. beq r6, $fix5c C U0
  366. $ret5c: addq r14, r4, r7 C L0
  367. beq r7, $fix6c C U0
  368. $ret6c: addq r5, r8, r8 C L0 sum 2
  369. cmpult r8, r5, r29 C L0 carry from last bunch
  370. stq r20, 0(r16) C L0
  371. stq r6, 8(r16) C L1
  372. stq r7, 16(r16) C L0
  373. stq r8, 24(r16) C L1
  374. addq r29, r21, r0
  375. ldq r26, 0(r30)
  376. ldq r9, 8(r30)
  377. ldq r10, 16(r30)
  378. ldq r11, 24(r30)
  379. ldq r12, 32(r30)
  380. ldq r13, 40(r30)
  381. ldq r14, 48(r30)
  382. ldq r15, 56(r30)
  383. ldq r29, 64(r30)
  384. lda r30, 224(r30)
  385. ret r31, (r26), 1
  386. C $fix0w: bis r14, r29, r14 C join carries
  387. C br r31, $ret0w
  388. $fix1w: bis r24, r14, r24 C join carries
  389. br r31, $ret1w
  390. $fix2w: bis r25, r24, r25 C join carries
  391. br r31, $ret2w
  392. $fix3w: bis r15, r25, r15 C join carries
  393. br r31, $ret3w
  394. $fix0: bis r14, r29, r14 C join carries
  395. br r31, $ret0
  396. $fix1: bis r24, r14, r24 C join carries
  397. br r31, $ret1
  398. $fix2: bis r25, r24, r25 C join carries
  399. br r31, $ret2
  400. $fix3: bis r15, r25, r15 C join carries
  401. br r31, $ret3
  402. $fix4: bis r29, r15, r29 C join carries
  403. br r31, $ret4
  404. $fix5: bis r4, r29, r4 C join carries
  405. br r31, $ret5
  406. $fix6: addq r5, r4, r5 C can't carry twice!
  407. br r31, $ret6
  408. $fix0c: bis r14, r29, r14 C join carries
  409. br r31, $ret0c
  410. $fix1c: bis r24, r14, r24 C join carries
  411. br r31, $ret1c
  412. $fix2c: bis r25, r24, r25 C join carries
  413. br r31, $ret2c
  414. $fix3c: bis r15, r25, r15 C join carries
  415. br r31, $ret3c
  416. $fix4c: bis r29, r15, r29 C join carries
  417. br r31, $ret4c
  418. $fix5c: bis r4, r29, r4 C join carries
  419. br r31, $ret5c
  420. $fix6c: addq r5, r4, r5 C can't carry twice!
  421. br r31, $ret6c
  422. EPILOGUE(mpn_mul_1)
  423. ASM_END()