ia64.S 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. .explicit
  2. .text
  3. .ident "ia64.S, Version 1.1"
  4. .ident "IA-64 ISA artwork by Andy Polyakov <appro@fy.chalmers.se>"
  5. //
  6. // ====================================================================
  7. // Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
  8. // project.
  9. //
  10. // Rights for redistribution and usage in source and binary forms are
  11. // granted according to the OpenSSL license. Warranty of any kind is
  12. // disclaimed.
  13. // ====================================================================
  14. //
  15. // Q. How much faster does it get?
  16. // A. Here is the output from 'openssl speed rsa dsa' for vanilla
  17. // 0.9.6a compiled with gcc version 2.96 20000731 (Red Hat
  18. // Linux 7.1 2.96-81):
  19. //
  20. // sign verify sign/s verify/s
  21. // rsa 512 bits 0.0036s 0.0003s 275.3 2999.2
  22. // rsa 1024 bits 0.0203s 0.0011s 49.3 894.1
  23. // rsa 2048 bits 0.1331s 0.0040s 7.5 250.9
  24. // rsa 4096 bits 0.9270s 0.0147s 1.1 68.1
  25. // sign verify sign/s verify/s
  26. // dsa 512 bits 0.0035s 0.0043s 288.3 234.8
  27. // dsa 1024 bits 0.0111s 0.0135s 90.0 74.2
  28. //
  29. // And here is similar output but for this assembler
  30. // implementation:-)
  31. //
  32. // sign verify sign/s verify/s
  33. // rsa 512 bits 0.0021s 0.0001s 549.4 9638.5
  34. // rsa 1024 bits 0.0055s 0.0002s 183.8 4481.1
  35. // rsa 2048 bits 0.0244s 0.0006s 41.4 1726.3
  36. // rsa 4096 bits 0.1295s 0.0018s 7.7 561.5
  37. // sign verify sign/s verify/s
  38. // dsa 512 bits 0.0012s 0.0013s 891.9 756.6
  39. // dsa 1024 bits 0.0023s 0.0028s 440.4 376.2
  40. //
  41. // Yes, you may argue that it's not fair comparison as it's
  42. // possible to craft the C implementation with BN_UMULT_HIGH
  43. // inline assembler macro. But of course! Here is the output
  44. // with the macro:
  45. //
  46. // sign verify sign/s verify/s
  47. // rsa 512 bits 0.0020s 0.0002s 495.0 6561.0
  48. // rsa 1024 bits 0.0086s 0.0004s 116.2 2235.7
  49. // rsa 2048 bits 0.0519s 0.0015s 19.3 667.3
  50. // rsa 4096 bits 0.3464s 0.0053s 2.9 187.7
  51. // sign verify sign/s verify/s
  52. // dsa 512 bits 0.0016s 0.0020s 613.1 510.5
  53. // dsa 1024 bits 0.0045s 0.0054s 221.0 183.9
  54. //
  55. // My code is still way faster, huh:-) And I believe that even
  56. // higher performance can be achieved. Note that as keys get
  57. // longer, performance gain is larger. Why? According to the
  58. // profiler there is another player in the field, namely
  59. // BN_from_montgomery consuming larger and larger portion of CPU
  60. // time as keysize decreases. I therefore consider putting effort
  61. // to assembler implementation of the following routine:
  62. //
  63. // void bn_mul_add_mont (BN_ULONG *rp,BN_ULONG *np,int nl,BN_ULONG n0)
  64. // {
  65. // int i,j;
  66. // BN_ULONG v;
  67. //
  68. // for (i=0; i<nl; i++)
  69. // {
  70. // v=bn_mul_add_words(rp,np,nl,(rp[0]*n0)&BN_MASK2);
  71. // nrp++;
  72. // rp++;
  73. // if (((nrp[-1]+=v)&BN_MASK2) < v)
  74. // for (j=0; ((++nrp[j])&BN_MASK2) == 0; j++) ;
  75. // }
  76. // }
  77. //
  78. // It might as well be beneficial to implement even combaX
  79. // variants, as it appears as it can literally unleash the
  80. // performance (see comment section to bn_mul_comba8 below).
  81. //
  82. // And finally for your reference the output for 0.9.6a compiled
  83. // with SGIcc version 0.01.0-12 (keep in mind that for the moment
  84. // of this writing it's not possible to convince SGIcc to use
  85. // BN_UMULT_HIGH inline assembler macro, yet the code is fast,
  86. // i.e. for a compiler generated one:-):
  87. //
  88. // sign verify sign/s verify/s
  89. // rsa 512 bits 0.0022s 0.0002s 452.7 5894.3
  90. // rsa 1024 bits 0.0097s 0.0005s 102.7 2002.9
  91. // rsa 2048 bits 0.0578s 0.0017s 17.3 600.2
  92. // rsa 4096 bits 0.3838s 0.0061s 2.6 164.5
  93. // sign verify sign/s verify/s
  94. // dsa 512 bits 0.0018s 0.0022s 547.3 459.6
  95. // dsa 1024 bits 0.0051s 0.0062s 196.6 161.3
  96. //
  97. // Oh! Benchmarks were performed on 733MHz Lion-class Itanium
  98. // system running Redhat Linux 7.1 (very special thanks to Ray
  99. // McCaffity of Williams Communications for providing an account).
  100. //
  101. // Q. What's the heck with 'rum 1<<5' at the end of every function?
  102. // A. Well, by clearing the "upper FP registers written" bit of the
  103. // User Mask I want to excuse the kernel from preserving upper
  104. // (f32-f128) FP register bank over process context switch, thus
  105. // minimizing bus bandwidth consumption during the switch (i.e.
  106. // after PKI opration completes and the program is off doing
  107. // something else like bulk symmetric encryption). Having said
  108. // this, I also want to point out that it might be good idea
  109. // to compile the whole toolkit (as well as majority of the
  110. // programs for that matter) with -mfixed-range=f32-f127 command
  111. // line option. No, it doesn't prevent the compiler from writing
  112. // to upper bank, but at least discourages to do so. If you don't
  113. // like the idea you have the option to compile the module with
  114. // -Drum=nop.m in command line.
  115. //
  116. #if 1
  117. //
  118. // bn_[add|sub]_words routines.
  119. //
  120. // Loops are spinning in 2*(n+5) ticks on Itanuim (provided that the
  121. // data reside in L1 cache, i.e. 2 ticks away). It's possible to
  122. // compress the epilogue and get down to 2*n+6, but at the cost of
  123. // scalability (the neat feature of this implementation is that it
  124. // shall automagically spin in n+5 on "wider" IA-64 implementations:-)
  125. // I consider that the epilogue is short enough as it is to trade tiny
  126. // performance loss on Itanium for scalability.
  127. //
  128. // BN_ULONG bn_add_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,int num)
  129. //
  130. .global bn_add_words#
  131. .proc bn_add_words#
  132. .align 64
  133. .skip 32 // makes the loop body aligned at 64-byte boundary
  134. bn_add_words:
  135. .prologue
  136. .fframe 0
  137. .save ar.pfs,r2
  138. { .mii; alloc r2=ar.pfs,4,12,0,16
  139. cmp4.le p6,p0=r35,r0 };;
  140. { .mfb; mov r8=r0 // return value
  141. (p6) br.ret.spnt.many b0 };;
  142. .save ar.lc,r3
  143. { .mib; sub r10=r35,r0,1
  144. mov r3=ar.lc
  145. brp.loop.imp .L_bn_add_words_ctop,.L_bn_add_words_cend-16
  146. }
  147. .body
  148. { .mib; mov r14=r32 // rp
  149. mov r9=pr };;
  150. { .mii; mov r15=r33 // ap
  151. mov ar.lc=r10
  152. mov ar.ec=6 }
  153. { .mib; mov r16=r34 // bp
  154. mov pr.rot=1<<16 };;
  155. .L_bn_add_words_ctop:
  156. { .mii; (p16) ld8 r32=[r16],8 // b=*(bp++)
  157. (p18) add r39=r37,r34
  158. (p19) cmp.ltu.unc p56,p0=r40,r38 }
  159. { .mfb; (p0) nop.m 0x0
  160. (p0) nop.f 0x0
  161. (p0) nop.b 0x0 }
  162. { .mii; (p16) ld8 r35=[r15],8 // a=*(ap++)
  163. (p58) cmp.eq.or p57,p0=-1,r41 // (p20)
  164. (p58) add r41=1,r41 } // (p20)
  165. { .mfb; (p21) st8 [r14]=r42,8 // *(rp++)=r
  166. (p0) nop.f 0x0
  167. br.ctop.sptk .L_bn_add_words_ctop };;
  168. .L_bn_add_words_cend:
  169. { .mii;
  170. (p59) add r8=1,r8 // return value
  171. mov pr=r9,-1
  172. mov ar.lc=r3 }
  173. { .mbb; nop.b 0x0
  174. br.ret.sptk.many b0 };;
  175. .endp bn_add_words#
  176. //
  177. // BN_ULONG bn_sub_words(BN_ULONG *rp, BN_ULONG *ap, BN_ULONG *bp,int num)
  178. //
  179. .global bn_sub_words#
  180. .proc bn_sub_words#
  181. .align 64
  182. .skip 32 // makes the loop body aligned at 64-byte boundary
  183. bn_sub_words:
  184. .prologue
  185. .fframe 0
  186. .save ar.pfs,r2
  187. { .mii; alloc r2=ar.pfs,4,12,0,16
  188. cmp4.le p6,p0=r35,r0 };;
  189. { .mfb; mov r8=r0 // return value
  190. (p6) br.ret.spnt.many b0 };;
  191. .save ar.lc,r3
  192. { .mib; sub r10=r35,r0,1
  193. mov r3=ar.lc
  194. brp.loop.imp .L_bn_sub_words_ctop,.L_bn_sub_words_cend-16
  195. }
  196. .body
  197. { .mib; mov r14=r32 // rp
  198. mov r9=pr };;
  199. { .mii; mov r15=r33 // ap
  200. mov ar.lc=r10
  201. mov ar.ec=6 }
  202. { .mib; mov r16=r34 // bp
  203. mov pr.rot=1<<16 };;
  204. .L_bn_sub_words_ctop:
  205. { .mii; (p16) ld8 r32=[r16],8 // b=*(bp++)
  206. (p18) sub r39=r37,r34
  207. (p19) cmp.gtu.unc p56,p0=r40,r38 }
  208. { .mfb; (p0) nop.m 0x0
  209. (p0) nop.f 0x0
  210. (p0) nop.b 0x0 }
  211. { .mii; (p16) ld8 r35=[r15],8 // a=*(ap++)
  212. (p58) cmp.eq.or p57,p0=0,r41 // (p20)
  213. (p58) add r41=-1,r41 } // (p20)
  214. { .mbb; (p21) st8 [r14]=r42,8 // *(rp++)=r
  215. (p0) nop.b 0x0
  216. br.ctop.sptk .L_bn_sub_words_ctop };;
  217. .L_bn_sub_words_cend:
  218. { .mii;
  219. (p59) add r8=1,r8 // return value
  220. mov pr=r9,-1
  221. mov ar.lc=r3 }
  222. { .mbb; nop.b 0x0
  223. br.ret.sptk.many b0 };;
  224. .endp bn_sub_words#
  225. #endif
  226. #if 0
  227. #define XMA_TEMPTATION
  228. #endif
  229. #if 1
  230. //
  231. // BN_ULONG bn_mul_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w)
  232. //
  233. .global bn_mul_words#
  234. .proc bn_mul_words#
  235. .align 64
  236. .skip 32 // makes the loop body aligned at 64-byte boundary
  237. bn_mul_words:
  238. .prologue
  239. .fframe 0
  240. .save ar.pfs,r2
  241. #ifdef XMA_TEMPTATION
  242. { .mfi; alloc r2=ar.pfs,4,0,0,0 };;
  243. #else
  244. { .mfi; alloc r2=ar.pfs,4,4,0,8 };;
  245. #endif
  246. { .mib; mov r8=r0 // return value
  247. cmp4.le p6,p0=r34,r0
  248. (p6) br.ret.spnt.many b0 };;
  249. .save ar.lc,r3
  250. { .mii; sub r10=r34,r0,1
  251. mov r3=ar.lc
  252. mov r9=pr };;
  253. .body
  254. { .mib; setf.sig f8=r35 // w
  255. mov pr.rot=0x400001<<16
  256. // ------^----- serves as (p48) at first (p26)
  257. brp.loop.imp .L_bn_mul_words_ctop,.L_bn_mul_words_cend-16
  258. }
  259. #ifndef XMA_TEMPTATION
  260. { .mii; mov r14=r32 // rp
  261. mov r15=r33 // ap
  262. mov ar.lc=r10 }
  263. { .mii; mov r39=0 // serves as r33 at first (p26)
  264. mov ar.ec=12 };;
  265. // This loop spins in 2*(n+11) ticks. It's scheduled for data in L2
  266. // cache (i.e. 9 ticks away) as floating point load/store instructions
  267. // bypass L1 cache and L2 latency is actually best-case scenario for
  268. // ldf8. The loop is not scalable and shall run in 2*(n+11) even on
  269. // "wider" IA-64 implementations. It's a trade-off here. n+22 loop
  270. // would give us ~5% in *overall* performance improvement on "wider"
  271. // IA-64, but would hurt Itanium for about same because of longer
  272. // epilogue. As it's a matter of few percents in either case I've
  273. // chosen to trade the scalability for development time (you can see
  274. // this very instruction sequence in bn_mul_add_words loop which in
  275. // turn is scalable).
  276. .L_bn_mul_words_ctop:
  277. { .mfi; (p25) getf.sig r36=f49 // low
  278. (p21) xmpy.lu f45=f37,f8
  279. (p27) cmp.ltu p52,p48=r39,r38 }
  280. { .mfi; (p16) ldf8 f32=[r15],8
  281. (p21) xmpy.hu f38=f37,f8
  282. (p0) nop.i 0x0 };;
  283. { .mii; (p26) getf.sig r32=f43 // high
  284. .pred.rel "mutex",p48,p52
  285. (p48) add r38=r37,r33 // (p26)
  286. (p52) add r38=r37,r33,1 } // (p26)
  287. { .mfb; (p27) st8 [r14]=r39,8
  288. (p0) nop.f 0x0
  289. br.ctop.sptk .L_bn_mul_words_ctop };;
  290. .L_bn_mul_words_cend:
  291. { .mii; nop.m 0x0
  292. .pred.rel "mutex",p49,p53
  293. (p49) add r8=r34,r0
  294. (p53) add r8=r34,r0,1 }
  295. { .mfb; nop.m 0x0
  296. nop.f 0x0
  297. nop.b 0x0 }
  298. #else // XMA_TEMPTATION
  299. setf.sig f37=r0 // serves as carry at (p18) tick
  300. mov ar.lc=r10
  301. mov ar.ec=5;;
  302. // Most of you examining this code very likely wonder why in the name
  303. // of Intel the following loop is commented out? Indeed, it looks so
  304. // neat that you find it hard to believe that it's something wrong
  305. // with it, right? The catch is that every iteration depends on the
  306. // result from previous one and the latter isn't available instantly.
  307. // The loop therefore spins at the latency of xma minus 1, or in other
  308. // words at 6*(n+4) ticks:-( Compare to the "production" loop above
  309. // that runs in 2*(n+11) where the low latency problem is worked around
  310. // by moving the dependency to one-tick latent interger ALU. Note that
  311. // "distance" between ldf8 and xma is not latency of ldf8, but the
  312. // *difference* between xma and ldf8 latencies.
  313. .L_bn_mul_words_ctop:
  314. { .mfi; (p16) ldf8 f32=[r33],8
  315. (p18) xma.hu f38=f34,f8,f39 }
  316. { .mfb; (p20) stf8 [r32]=f37,8
  317. (p18) xma.lu f35=f34,f8,f39
  318. br.ctop.sptk .L_bn_mul_words_ctop };;
  319. .L_bn_mul_words_cend:
  320. getf.sig r8=f41 // the return value
  321. #endif // XMA_TEMPTATION
  322. { .mii; nop.m 0x0
  323. mov pr=r9,-1
  324. mov ar.lc=r3 }
  325. { .mfb; rum 1<<5 // clear um.mfh
  326. nop.f 0x0
  327. br.ret.sptk.many b0 };;
  328. .endp bn_mul_words#
  329. #endif
  330. #if 1
  331. //
  332. // BN_ULONG bn_mul_add_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w)
  333. //
  334. .global bn_mul_add_words#
  335. .proc bn_mul_add_words#
  336. .align 64
  337. //.skip 0 // makes the loop split at 64-byte boundary
  338. bn_mul_add_words:
  339. .prologue
  340. .fframe 0
  341. .save ar.pfs,r2
  342. { .mii; alloc r2=ar.pfs,4,12,0,16
  343. cmp4.le p6,p0=r34,r0 };;
  344. { .mfb; mov r8=r0 // return value
  345. (p6) br.ret.spnt.many b0 };;
  346. .save ar.lc,r3
  347. { .mii; sub r10=r34,r0,1
  348. mov r3=ar.lc
  349. mov r9=pr };;
  350. .body
  351. { .mib; setf.sig f8=r35 // w
  352. mov pr.rot=0x400001<<16
  353. // ------^----- serves as (p48) at first (p26)
  354. brp.loop.imp .L_bn_mul_add_words_ctop,.L_bn_mul_add_words_cend-16
  355. }
  356. { .mii; mov r14=r32 // rp
  357. mov r15=r33 // ap
  358. mov ar.lc=r10 }
  359. { .mii; mov r39=0 // serves as r33 at first (p26)
  360. mov r18=r32 // rp copy
  361. mov ar.ec=14 };;
  362. // This loop spins in 3*(n+13) ticks on Itanium and should spin in
  363. // 2*(n+13) on "wider" IA-64 implementations (to be verified with new
  364. // µ-architecture manuals as they become available). As usual it's
  365. // possible to compress the epilogue, down to 10 in this case, at the
  366. // cost of scalability. Compressed (and therefore non-scalable) loop
  367. // running at 3*(n+10) would buy you ~10% on Itanium but take ~35%
  368. // from "wider" IA-64 so let it be scalable! Special attention was
  369. // paid for having the loop body split at 64-byte boundary. ld8 is
  370. // scheduled for L1 cache as the data is more than likely there.
  371. // Indeed, bn_mul_words has put it there a moment ago:-)
  372. .L_bn_mul_add_words_ctop:
  373. { .mfi; (p25) getf.sig r36=f49 // low
  374. (p21) xmpy.lu f45=f37,f8
  375. (p27) cmp.ltu p52,p48=r39,r38 }
  376. { .mfi; (p16) ldf8 f32=[r15],8
  377. (p21) xmpy.hu f38=f37,f8
  378. (p27) add r43=r43,r39 };;
  379. { .mii; (p26) getf.sig r32=f43 // high
  380. .pred.rel "mutex",p48,p52
  381. (p48) add r38=r37,r33 // (p26)
  382. (p52) add r38=r37,r33,1 } // (p26)
  383. { .mfb; (p27) cmp.ltu.unc p56,p0=r43,r39
  384. (p0) nop.f 0x0
  385. (p0) nop.b 0x0 }
  386. { .mii; (p26) ld8 r42=[r18],8
  387. (p58) cmp.eq.or p57,p0=-1,r44
  388. (p58) add r44=1,r44 }
  389. { .mfb; (p29) st8 [r14]=r45,8
  390. (p0) nop.f 0x0
  391. br.ctop.sptk .L_bn_mul_add_words_ctop};;
  392. .L_bn_mul_add_words_cend:
  393. { .mii; nop.m 0x0
  394. .pred.rel "mutex",p51,p55
  395. (p51) add r8=r36,r0
  396. (p55) add r8=r36,r0,1 }
  397. { .mfb; nop.m 0x0
  398. nop.f 0x0
  399. nop.b 0x0 };;
  400. { .mii;
  401. (p59) add r8=1,r8
  402. mov pr=r9,-1
  403. mov ar.lc=r3 }
  404. { .mfb; rum 1<<5 // clear um.mfh
  405. nop.f 0x0
  406. br.ret.sptk.many b0 };;
  407. .endp bn_mul_add_words#
  408. #endif
  409. #if 1
  410. //
  411. // void bn_sqr_words(BN_ULONG *rp, BN_ULONG *ap, int num)
  412. //
  413. .global bn_sqr_words#
  414. .proc bn_sqr_words#
  415. .align 64
  416. .skip 32 // makes the loop body aligned at 64-byte boundary
  417. bn_sqr_words:
  418. .prologue
  419. .fframe 0
  420. .save ar.pfs,r2
  421. { .mii; alloc r2=ar.pfs,3,0,0,0
  422. sxt4 r34=r34 };;
  423. { .mii; cmp.le p6,p0=r34,r0
  424. mov r8=r0 } // return value
  425. { .mfb; nop.f 0x0
  426. (p6) br.ret.spnt.many b0 };;
  427. .save ar.lc,r3
  428. { .mii; sub r10=r34,r0,1
  429. mov r3=ar.lc
  430. mov r9=pr };;
  431. .body
  432. { .mib;
  433. mov pr.rot=1<<16
  434. brp.loop.imp .L_bn_sqr_words_ctop,.L_bn_sqr_words_cend-16
  435. }
  436. { .mii; add r34=8,r32
  437. mov ar.lc=r10
  438. mov ar.ec=18 };;
  439. // 2*(n+17) on Itanium, (n+17) on "wider" IA-64 implementations. It's
  440. // possible to compress the epilogue (I'm getting tired to write this
  441. // comment over and over) and get down to 2*n+16 at the cost of
  442. // scalability. The decision will very likely be reconsidered after the
  443. // benchmark program is profiled. I.e. if perfomance gain on Itanium
  444. // will appear larger than loss on "wider" IA-64, then the loop should
  445. // be explicitely split and the epilogue compressed.
  446. .L_bn_sqr_words_ctop:
  447. { .mfi; (p16) ldf8 f32=[r33],8
  448. (p25) xmpy.lu f42=f41,f41
  449. (p0) nop.i 0x0 }
  450. { .mib; (p33) stf8 [r32]=f50,16
  451. (p0) nop.i 0x0
  452. (p0) nop.b 0x0 }
  453. { .mfi; (p0) nop.m 0x0
  454. (p25) xmpy.hu f52=f41,f41
  455. (p0) nop.i 0x0 }
  456. { .mib; (p33) stf8 [r34]=f60,16
  457. (p0) nop.i 0x0
  458. br.ctop.sptk .L_bn_sqr_words_ctop };;
  459. .L_bn_sqr_words_cend:
  460. { .mii; nop.m 0x0
  461. mov pr=r9,-1
  462. mov ar.lc=r3 }
  463. { .mfb; rum 1<<5 // clear um.mfh
  464. nop.f 0x0
  465. br.ret.sptk.many b0 };;
  466. .endp bn_sqr_words#
  467. #endif
  468. #if 1
  469. // Apparently we win nothing by implementing special bn_sqr_comba8.
  470. // Yes, it is possible to reduce the number of multiplications by
  471. // almost factor of two, but then the amount of additions would
  472. // increase by factor of two (as we would have to perform those
  473. // otherwise performed by xma ourselves). Normally we would trade
  474. // anyway as multiplications are way more expensive, but not this
  475. // time... Multiplication kernel is fully pipelined and as we drain
  476. // one 128-bit multiplication result per clock cycle multiplications
  477. // are effectively as inexpensive as additions. Special implementation
  478. // might become of interest for "wider" IA-64 implementation as you'll
  479. // be able to get through the multiplication phase faster (there won't
  480. // be any stall issues as discussed in the commentary section below and
  481. // you therefore will be able to employ all 4 FP units)... But these
  482. // Itanium days it's simply too hard to justify the effort so I just
  483. // drop down to bn_mul_comba8 code:-)
  484. //
  485. // void bn_sqr_comba8(BN_ULONG *r, BN_ULONG *a)
  486. //
  487. .global bn_sqr_comba8#
  488. .proc bn_sqr_comba8#
  489. .align 64
  490. bn_sqr_comba8:
  491. .prologue
  492. .fframe 0
  493. .save ar.pfs,r2
  494. { .mii; alloc r2=ar.pfs,2,1,0,0
  495. mov r34=r33
  496. add r14=8,r33 };;
  497. .body
  498. { .mii; add r17=8,r34
  499. add r15=16,r33
  500. add r18=16,r34 }
  501. { .mfb; add r16=24,r33
  502. br .L_cheat_entry_point8 };;
  503. .endp bn_sqr_comba8#
  504. #endif
  505. #if 1
  506. // I've estimated this routine to run in ~120 ticks, but in reality
  507. // (i.e. according to ar.itc) it takes ~160 ticks. Are those extra
  508. // cycles consumed for instructions fetch? Or did I misinterpret some
  509. // clause in Itanium µ-architecture manual? Comments are welcomed and
  510. // highly appreciated.
  511. //
  512. // However! It should be noted that even 160 ticks is darn good result
  513. // as it's over 10 (yes, ten, spelled as t-e-n) times faster than the
  514. // C version (compiled with gcc with inline assembler). I really
  515. // kicked compiler's butt here, didn't I? Yeah! This brings us to the
  516. // following statement. It's damn shame that this routine isn't called
  517. // very often nowadays! According to the profiler most CPU time is
  518. // consumed by bn_mul_add_words called from BN_from_montgomery. In
  519. // order to estimate what we're missing, I've compared the performance
  520. // of this routine against "traditional" implementation, i.e. against
  521. // following routine:
  522. //
  523. // void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  524. // { r[ 8]=bn_mul_words( &(r[0]),a,8,b[0]);
  525. // r[ 9]=bn_mul_add_words(&(r[1]),a,8,b[1]);
  526. // r[10]=bn_mul_add_words(&(r[2]),a,8,b[2]);
  527. // r[11]=bn_mul_add_words(&(r[3]),a,8,b[3]);
  528. // r[12]=bn_mul_add_words(&(r[4]),a,8,b[4]);
  529. // r[13]=bn_mul_add_words(&(r[5]),a,8,b[5]);
  530. // r[14]=bn_mul_add_words(&(r[6]),a,8,b[6]);
  531. // r[15]=bn_mul_add_words(&(r[7]),a,8,b[7]);
  532. // }
  533. //
  534. // The one below is over 8 times faster than the one above:-( Even
  535. // more reasons to "combafy" bn_mul_add_mont...
  536. //
  537. // And yes, this routine really made me wish there were an optimizing
  538. // assembler! It also feels like it deserves a dedication.
  539. //
  540. // To my wife for being there and to my kids...
  541. //
  542. // void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  543. //
  544. #define carry1 r14
  545. #define carry2 r15
  546. #define carry3 r34
  547. .global bn_mul_comba8#
  548. .proc bn_mul_comba8#
  549. .align 64
  550. bn_mul_comba8:
  551. .prologue
  552. .fframe 0
  553. .save ar.pfs,r2
  554. { .mii; alloc r2=ar.pfs,3,0,0,0
  555. add r14=8,r33
  556. add r17=8,r34 }
  557. .body
  558. { .mii; add r15=16,r33
  559. add r18=16,r34
  560. add r16=24,r33 }
  561. .L_cheat_entry_point8:
  562. { .mmi; add r19=24,r34
  563. ldf8 f32=[r33],32 };;
  564. { .mmi; ldf8 f120=[r34],32
  565. ldf8 f121=[r17],32 }
  566. { .mmi; ldf8 f122=[r18],32
  567. ldf8 f123=[r19],32 };;
  568. { .mmi; ldf8 f124=[r34]
  569. ldf8 f125=[r17] }
  570. { .mmi; ldf8 f126=[r18]
  571. ldf8 f127=[r19] }
  572. { .mmi; ldf8 f33=[r14],32
  573. ldf8 f34=[r15],32 }
  574. { .mmi; ldf8 f35=[r16],32;;
  575. ldf8 f36=[r33] }
  576. { .mmi; ldf8 f37=[r14]
  577. ldf8 f38=[r15] }
  578. { .mfi; ldf8 f39=[r16]
  579. // -------\ Entering multiplier's heaven /-------
  580. // ------------\ /------------
  581. // -----------------\ /-----------------
  582. // ----------------------\/----------------------
  583. xma.hu f41=f32,f120,f0 }
  584. { .mfi; xma.lu f40=f32,f120,f0 };; // (*)
  585. { .mfi; xma.hu f51=f32,f121,f0 }
  586. { .mfi; xma.lu f50=f32,f121,f0 };;
  587. { .mfi; xma.hu f61=f32,f122,f0 }
  588. { .mfi; xma.lu f60=f32,f122,f0 };;
  589. { .mfi; xma.hu f71=f32,f123,f0 }
  590. { .mfi; xma.lu f70=f32,f123,f0 };;
  591. { .mfi; xma.hu f81=f32,f124,f0 }
  592. { .mfi; xma.lu f80=f32,f124,f0 };;
  593. { .mfi; xma.hu f91=f32,f125,f0 }
  594. { .mfi; xma.lu f90=f32,f125,f0 };;
  595. { .mfi; xma.hu f101=f32,f126,f0 }
  596. { .mfi; xma.lu f100=f32,f126,f0 };;
  597. { .mfi; xma.hu f111=f32,f127,f0 }
  598. { .mfi; xma.lu f110=f32,f127,f0 };;//
  599. // (*) You can argue that splitting at every second bundle would
  600. // prevent "wider" IA-64 implementations from achieving the peak
  601. // performance. Well, not really... The catch is that if you
  602. // intend to keep 4 FP units busy by splitting at every fourth
  603. // bundle and thus perform these 16 multiplications in 4 ticks,
  604. // the first bundle *below* would stall because the result from
  605. // the first xma bundle *above* won't be available for another 3
  606. // ticks (if not more, being an optimist, I assume that "wider"
  607. // implementation will have same latency:-). This stall will hold
  608. // you back and the performance would be as if every second bundle
  609. // were split *anyway*...
  610. { .mfi; getf.sig r16=f40
  611. xma.hu f42=f33,f120,f41
  612. add r33=8,r32 }
  613. { .mfi; xma.lu f41=f33,f120,f41 };;
  614. { .mfi; getf.sig r24=f50
  615. xma.hu f52=f33,f121,f51 }
  616. { .mfi; xma.lu f51=f33,f121,f51 };;
  617. { .mfi; st8 [r32]=r16,16
  618. xma.hu f62=f33,f122,f61 }
  619. { .mfi; xma.lu f61=f33,f122,f61 };;
  620. { .mfi; xma.hu f72=f33,f123,f71 }
  621. { .mfi; xma.lu f71=f33,f123,f71 };;
  622. { .mfi; xma.hu f82=f33,f124,f81 }
  623. { .mfi; xma.lu f81=f33,f124,f81 };;
  624. { .mfi; xma.hu f92=f33,f125,f91 }
  625. { .mfi; xma.lu f91=f33,f125,f91 };;
  626. { .mfi; xma.hu f102=f33,f126,f101 }
  627. { .mfi; xma.lu f101=f33,f126,f101 };;
  628. { .mfi; xma.hu f112=f33,f127,f111 }
  629. { .mfi; xma.lu f111=f33,f127,f111 };;//
  630. //-------------------------------------------------//
  631. { .mfi; getf.sig r25=f41
  632. xma.hu f43=f34,f120,f42 }
  633. { .mfi; xma.lu f42=f34,f120,f42 };;
  634. { .mfi; getf.sig r16=f60
  635. xma.hu f53=f34,f121,f52 }
  636. { .mfi; xma.lu f52=f34,f121,f52 };;
  637. { .mfi; getf.sig r17=f51
  638. xma.hu f63=f34,f122,f62
  639. add r25=r25,r24 }
  640. { .mfi; xma.lu f62=f34,f122,f62
  641. mov carry1=0 };;
  642. { .mfi; cmp.ltu p6,p0=r25,r24
  643. xma.hu f73=f34,f123,f72 }
  644. { .mfi; xma.lu f72=f34,f123,f72 };;
  645. { .mfi; st8 [r33]=r25,16
  646. xma.hu f83=f34,f124,f82
  647. (p6) add carry1=1,carry1 }
  648. { .mfi; xma.lu f82=f34,f124,f82 };;
  649. { .mfi; xma.hu f93=f34,f125,f92 }
  650. { .mfi; xma.lu f92=f34,f125,f92 };;
  651. { .mfi; xma.hu f103=f34,f126,f102 }
  652. { .mfi; xma.lu f102=f34,f126,f102 };;
  653. { .mfi; xma.hu f113=f34,f127,f112 }
  654. { .mfi; xma.lu f112=f34,f127,f112 };;//
  655. //-------------------------------------------------//
  656. { .mfi; getf.sig r18=f42
  657. xma.hu f44=f35,f120,f43
  658. add r17=r17,r16 }
  659. { .mfi; xma.lu f43=f35,f120,f43 };;
  660. { .mfi; getf.sig r24=f70
  661. xma.hu f54=f35,f121,f53 }
  662. { .mfi; mov carry2=0
  663. xma.lu f53=f35,f121,f53 };;
  664. { .mfi; getf.sig r25=f61
  665. xma.hu f64=f35,f122,f63
  666. cmp.ltu p7,p0=r17,r16 }
  667. { .mfi; add r18=r18,r17
  668. xma.lu f63=f35,f122,f63 };;
  669. { .mfi; getf.sig r26=f52
  670. xma.hu f74=f35,f123,f73
  671. (p7) add carry2=1,carry2 }
  672. { .mfi; cmp.ltu p7,p0=r18,r17
  673. xma.lu f73=f35,f123,f73
  674. add r18=r18,carry1 };;
  675. { .mfi;
  676. xma.hu f84=f35,f124,f83
  677. (p7) add carry2=1,carry2 }
  678. { .mfi; cmp.ltu p7,p0=r18,carry1
  679. xma.lu f83=f35,f124,f83 };;
  680. { .mfi; st8 [r32]=r18,16
  681. xma.hu f94=f35,f125,f93
  682. (p7) add carry2=1,carry2 }
  683. { .mfi; xma.lu f93=f35,f125,f93 };;
  684. { .mfi; xma.hu f104=f35,f126,f103 }
  685. { .mfi; xma.lu f103=f35,f126,f103 };;
  686. { .mfi; xma.hu f114=f35,f127,f113 }
  687. { .mfi; mov carry1=0
  688. xma.lu f113=f35,f127,f113
  689. add r25=r25,r24 };;//
  690. //-------------------------------------------------//
  691. { .mfi; getf.sig r27=f43
  692. xma.hu f45=f36,f120,f44
  693. cmp.ltu p6,p0=r25,r24 }
  694. { .mfi; xma.lu f44=f36,f120,f44
  695. add r26=r26,r25 };;
  696. { .mfi; getf.sig r16=f80
  697. xma.hu f55=f36,f121,f54
  698. (p6) add carry1=1,carry1 }
  699. { .mfi; xma.lu f54=f36,f121,f54 };;
  700. { .mfi; getf.sig r17=f71
  701. xma.hu f65=f36,f122,f64
  702. cmp.ltu p6,p0=r26,r25 }
  703. { .mfi; xma.lu f64=f36,f122,f64
  704. add r27=r27,r26 };;
  705. { .mfi; getf.sig r18=f62
  706. xma.hu f75=f36,f123,f74
  707. (p6) add carry1=1,carry1 }
  708. { .mfi; cmp.ltu p6,p0=r27,r26
  709. xma.lu f74=f36,f123,f74
  710. add r27=r27,carry2 };;
  711. { .mfi; getf.sig r19=f53
  712. xma.hu f85=f36,f124,f84
  713. (p6) add carry1=1,carry1 }
  714. { .mfi; xma.lu f84=f36,f124,f84
  715. cmp.ltu p6,p0=r27,carry2 };;
  716. { .mfi; st8 [r33]=r27,16
  717. xma.hu f95=f36,f125,f94
  718. (p6) add carry1=1,carry1 }
  719. { .mfi; xma.lu f94=f36,f125,f94 };;
  720. { .mfi; xma.hu f105=f36,f126,f104 }
  721. { .mfi; mov carry2=0
  722. xma.lu f104=f36,f126,f104
  723. add r17=r17,r16 };;
  724. { .mfi; xma.hu f115=f36,f127,f114
  725. cmp.ltu p7,p0=r17,r16 }
  726. { .mfi; xma.lu f114=f36,f127,f114
  727. add r18=r18,r17 };;//
  728. //-------------------------------------------------//
  729. { .mfi; getf.sig r20=f44
  730. xma.hu f46=f37,f120,f45
  731. (p7) add carry2=1,carry2 }
  732. { .mfi; cmp.ltu p7,p0=r18,r17
  733. xma.lu f45=f37,f120,f45
  734. add r19=r19,r18 };;
  735. { .mfi; getf.sig r24=f90
  736. xma.hu f56=f37,f121,f55 }
  737. { .mfi; xma.lu f55=f37,f121,f55 };;
  738. { .mfi; getf.sig r25=f81
  739. xma.hu f66=f37,f122,f65
  740. (p7) add carry2=1,carry2 }
  741. { .mfi; cmp.ltu p7,p0=r19,r18
  742. xma.lu f65=f37,f122,f65
  743. add r20=r20,r19 };;
  744. { .mfi; getf.sig r26=f72
  745. xma.hu f76=f37,f123,f75
  746. (p7) add carry2=1,carry2 }
  747. { .mfi; cmp.ltu p7,p0=r20,r19
  748. xma.lu f75=f37,f123,f75
  749. add r20=r20,carry1 };;
  750. { .mfi; getf.sig r27=f63
  751. xma.hu f86=f37,f124,f85
  752. (p7) add carry2=1,carry2 }
  753. { .mfi; xma.lu f85=f37,f124,f85
  754. cmp.ltu p7,p0=r20,carry1 };;
  755. { .mfi; getf.sig r28=f54
  756. xma.hu f96=f37,f125,f95
  757. (p7) add carry2=1,carry2 }
  758. { .mfi; st8 [r32]=r20,16
  759. xma.lu f95=f37,f125,f95 };;
  760. { .mfi; xma.hu f106=f37,f126,f105 }
  761. { .mfi; mov carry1=0
  762. xma.lu f105=f37,f126,f105
  763. add r25=r25,r24 };;
  764. { .mfi; xma.hu f116=f37,f127,f115
  765. cmp.ltu p6,p0=r25,r24 }
  766. { .mfi; xma.lu f115=f37,f127,f115
  767. add r26=r26,r25 };;//
  768. //-------------------------------------------------//
  769. { .mfi; getf.sig r29=f45
  770. xma.hu f47=f38,f120,f46
  771. (p6) add carry1=1,carry1 }
  772. { .mfi; cmp.ltu p6,p0=r26,r25
  773. xma.lu f46=f38,f120,f46
  774. add r27=r27,r26 };;
  775. { .mfi; getf.sig r16=f100
  776. xma.hu f57=f38,f121,f56
  777. (p6) add carry1=1,carry1 }
  778. { .mfi; cmp.ltu p6,p0=r27,r26
  779. xma.lu f56=f38,f121,f56
  780. add r28=r28,r27 };;
  781. { .mfi; getf.sig r17=f91
  782. xma.hu f67=f38,f122,f66
  783. (p6) add carry1=1,carry1 }
  784. { .mfi; cmp.ltu p6,p0=r28,r27
  785. xma.lu f66=f38,f122,f66
  786. add r29=r29,r28 };;
  787. { .mfi; getf.sig r18=f82
  788. xma.hu f77=f38,f123,f76
  789. (p6) add carry1=1,carry1 }
  790. { .mfi; cmp.ltu p6,p0=r29,r28
  791. xma.lu f76=f38,f123,f76
  792. add r29=r29,carry2 };;
  793. { .mfi; getf.sig r19=f73
  794. xma.hu f87=f38,f124,f86
  795. (p6) add carry1=1,carry1 }
  796. { .mfi; xma.lu f86=f38,f124,f86
  797. cmp.ltu p6,p0=r29,carry2 };;
  798. { .mfi; getf.sig r20=f64
  799. xma.hu f97=f38,f125,f96
  800. (p6) add carry1=1,carry1 }
  801. { .mfi; st8 [r33]=r29,16
  802. xma.lu f96=f38,f125,f96 };;
  803. { .mfi; getf.sig r21=f55
  804. xma.hu f107=f38,f126,f106 }
  805. { .mfi; mov carry2=0
  806. xma.lu f106=f38,f126,f106
  807. add r17=r17,r16 };;
  808. { .mfi; xma.hu f117=f38,f127,f116
  809. cmp.ltu p7,p0=r17,r16 }
  810. { .mfi; xma.lu f116=f38,f127,f116
  811. add r18=r18,r17 };;//
  812. //-------------------------------------------------//
  813. { .mfi; getf.sig r22=f46
  814. xma.hu f48=f39,f120,f47
  815. (p7) add carry2=1,carry2 }
  816. { .mfi; cmp.ltu p7,p0=r18,r17
  817. xma.lu f47=f39,f120,f47
  818. add r19=r19,r18 };;
  819. { .mfi; getf.sig r24=f110
  820. xma.hu f58=f39,f121,f57
  821. (p7) add carry2=1,carry2 }
  822. { .mfi; cmp.ltu p7,p0=r19,r18
  823. xma.lu f57=f39,f121,f57
  824. add r20=r20,r19 };;
  825. { .mfi; getf.sig r25=f101
  826. xma.hu f68=f39,f122,f67
  827. (p7) add carry2=1,carry2 }
  828. { .mfi; cmp.ltu p7,p0=r20,r19
  829. xma.lu f67=f39,f122,f67
  830. add r21=r21,r20 };;
  831. { .mfi; getf.sig r26=f92
  832. xma.hu f78=f39,f123,f77
  833. (p7) add carry2=1,carry2 }
  834. { .mfi; cmp.ltu p7,p0=r21,r20
  835. xma.lu f77=f39,f123,f77
  836. add r22=r22,r21 };;
  837. { .mfi; getf.sig r27=f83
  838. xma.hu f88=f39,f124,f87
  839. (p7) add carry2=1,carry2 }
  840. { .mfi; cmp.ltu p7,p0=r22,r21
  841. xma.lu f87=f39,f124,f87
  842. add r22=r22,carry1 };;
  843. { .mfi; getf.sig r28=f74
  844. xma.hu f98=f39,f125,f97
  845. (p7) add carry2=1,carry2 }
  846. { .mfi; xma.lu f97=f39,f125,f97
  847. cmp.ltu p7,p0=r22,carry1 };;
  848. { .mfi; getf.sig r29=f65
  849. xma.hu f108=f39,f126,f107
  850. (p7) add carry2=1,carry2 }
  851. { .mfi; st8 [r32]=r22,16
  852. xma.lu f107=f39,f126,f107 };;
  853. { .mfi; getf.sig r30=f56
  854. xma.hu f118=f39,f127,f117 }
  855. { .mfi; xma.lu f117=f39,f127,f117 };;//
  856. //-------------------------------------------------//
  857. // Leaving muliplier's heaven... Quite a ride, huh?
  858. { .mii; getf.sig r31=f47
  859. add r25=r25,r24
  860. mov carry1=0 };;
  861. { .mii; getf.sig r16=f111
  862. cmp.ltu p6,p0=r25,r24
  863. add r26=r26,r25 };;
  864. { .mfb; getf.sig r17=f102 }
  865. { .mii;
  866. (p6) add carry1=1,carry1
  867. cmp.ltu p6,p0=r26,r25
  868. add r27=r27,r26 };;
  869. { .mfb; nop.m 0x0 }
  870. { .mii;
  871. (p6) add carry1=1,carry1
  872. cmp.ltu p6,p0=r27,r26
  873. add r28=r28,r27 };;
  874. { .mii; getf.sig r18=f93
  875. add r17=r17,r16
  876. mov carry3=0 }
  877. { .mii;
  878. (p6) add carry1=1,carry1
  879. cmp.ltu p6,p0=r28,r27
  880. add r29=r29,r28 };;
  881. { .mii; getf.sig r19=f84
  882. cmp.ltu p7,p0=r17,r16 }
  883. { .mii;
  884. (p6) add carry1=1,carry1
  885. cmp.ltu p6,p0=r29,r28
  886. add r30=r30,r29 };;
  887. { .mii; getf.sig r20=f75
  888. add r18=r18,r17 }
  889. { .mii;
  890. (p6) add carry1=1,carry1
  891. cmp.ltu p6,p0=r30,r29
  892. add r31=r31,r30 };;
  893. { .mfb; getf.sig r21=f66 }
  894. { .mii; (p7) add carry3=1,carry3
  895. cmp.ltu p7,p0=r18,r17
  896. add r19=r19,r18 }
  897. { .mfb; nop.m 0x0 }
  898. { .mii;
  899. (p6) add carry1=1,carry1
  900. cmp.ltu p6,p0=r31,r30
  901. add r31=r31,carry2 };;
  902. { .mfb; getf.sig r22=f57 }
  903. { .mii; (p7) add carry3=1,carry3
  904. cmp.ltu p7,p0=r19,r18
  905. add r20=r20,r19 }
  906. { .mfb; nop.m 0x0 }
  907. { .mii;
  908. (p6) add carry1=1,carry1
  909. cmp.ltu p6,p0=r31,carry2 };;
  910. { .mfb; getf.sig r23=f48 }
  911. { .mii; (p7) add carry3=1,carry3
  912. cmp.ltu p7,p0=r20,r19
  913. add r21=r21,r20 }
  914. { .mii;
  915. (p6) add carry1=1,carry1 }
  916. { .mfb; st8 [r33]=r31,16 };;
  917. { .mfb; getf.sig r24=f112 }
  918. { .mii; (p7) add carry3=1,carry3
  919. cmp.ltu p7,p0=r21,r20
  920. add r22=r22,r21 };;
  921. { .mfb; getf.sig r25=f103 }
  922. { .mii; (p7) add carry3=1,carry3
  923. cmp.ltu p7,p0=r22,r21
  924. add r23=r23,r22 };;
  925. { .mfb; getf.sig r26=f94 }
  926. { .mii; (p7) add carry3=1,carry3
  927. cmp.ltu p7,p0=r23,r22
  928. add r23=r23,carry1 };;
  929. { .mfb; getf.sig r27=f85 }
  930. { .mii; (p7) add carry3=1,carry3
  931. cmp.ltu p7,p8=r23,carry1};;
  932. { .mii; getf.sig r28=f76
  933. add r25=r25,r24
  934. mov carry1=0 }
  935. { .mii; st8 [r32]=r23,16
  936. (p7) add carry2=1,carry3
  937. (p8) add carry2=0,carry3 };;
  938. { .mfb; nop.m 0x0 }
  939. { .mii; getf.sig r29=f67
  940. cmp.ltu p6,p0=r25,r24
  941. add r26=r26,r25 };;
  942. { .mfb; getf.sig r30=f58 }
  943. { .mii;
  944. (p6) add carry1=1,carry1
  945. cmp.ltu p6,p0=r26,r25
  946. add r27=r27,r26 };;
  947. { .mfb; getf.sig r16=f113 }
  948. { .mii;
  949. (p6) add carry1=1,carry1
  950. cmp.ltu p6,p0=r27,r26
  951. add r28=r28,r27 };;
  952. { .mfb; getf.sig r17=f104 }
  953. { .mii;
  954. (p6) add carry1=1,carry1
  955. cmp.ltu p6,p0=r28,r27
  956. add r29=r29,r28 };;
  957. { .mfb; getf.sig r18=f95 }
  958. { .mii;
  959. (p6) add carry1=1,carry1
  960. cmp.ltu p6,p0=r29,r28
  961. add r30=r30,r29 };;
  962. { .mii; getf.sig r19=f86
  963. add r17=r17,r16
  964. mov carry3=0 }
  965. { .mii;
  966. (p6) add carry1=1,carry1
  967. cmp.ltu p6,p0=r30,r29
  968. add r30=r30,carry2 };;
  969. { .mii; getf.sig r20=f77
  970. cmp.ltu p7,p0=r17,r16
  971. add r18=r18,r17 }
  972. { .mii;
  973. (p6) add carry1=1,carry1
  974. cmp.ltu p6,p0=r30,carry2 };;
  975. { .mfb; getf.sig r21=f68 }
  976. { .mii; st8 [r33]=r30,16
  977. (p6) add carry1=1,carry1 };;
  978. { .mfb; getf.sig r24=f114 }
  979. { .mii; (p7) add carry3=1,carry3
  980. cmp.ltu p7,p0=r18,r17
  981. add r19=r19,r18 };;
  982. { .mfb; getf.sig r25=f105 }
  983. { .mii; (p7) add carry3=1,carry3
  984. cmp.ltu p7,p0=r19,r18
  985. add r20=r20,r19 };;
  986. { .mfb; getf.sig r26=f96 }
  987. { .mii; (p7) add carry3=1,carry3
  988. cmp.ltu p7,p0=r20,r19
  989. add r21=r21,r20 };;
  990. { .mfb; getf.sig r27=f87 }
  991. { .mii; (p7) add carry3=1,carry3
  992. cmp.ltu p7,p0=r21,r20
  993. add r21=r21,carry1 };;
  994. { .mib; getf.sig r28=f78
  995. add r25=r25,r24 }
  996. { .mib; (p7) add carry3=1,carry3
  997. cmp.ltu p7,p8=r21,carry1};;
  998. { .mii; st8 [r32]=r21,16
  999. (p7) add carry2=1,carry3
  1000. (p8) add carry2=0,carry3 }
  1001. { .mii; mov carry1=0
  1002. cmp.ltu p6,p0=r25,r24
  1003. add r26=r26,r25 };;
  1004. { .mfb; getf.sig r16=f115 }
  1005. { .mii;
  1006. (p6) add carry1=1,carry1
  1007. cmp.ltu p6,p0=r26,r25
  1008. add r27=r27,r26 };;
  1009. { .mfb; getf.sig r17=f106 }
  1010. { .mii;
  1011. (p6) add carry1=1,carry1
  1012. cmp.ltu p6,p0=r27,r26
  1013. add r28=r28,r27 };;
  1014. { .mfb; getf.sig r18=f97 }
  1015. { .mii;
  1016. (p6) add carry1=1,carry1
  1017. cmp.ltu p6,p0=r28,r27
  1018. add r28=r28,carry2 };;
  1019. { .mib; getf.sig r19=f88
  1020. add r17=r17,r16 }
  1021. { .mib;
  1022. (p6) add carry1=1,carry1
  1023. cmp.ltu p6,p0=r28,carry2 };;
  1024. { .mii; st8 [r33]=r28,16
  1025. (p6) add carry1=1,carry1 }
  1026. { .mii; mov carry2=0
  1027. cmp.ltu p7,p0=r17,r16
  1028. add r18=r18,r17 };;
  1029. { .mfb; getf.sig r24=f116 }
  1030. { .mii; (p7) add carry2=1,carry2
  1031. cmp.ltu p7,p0=r18,r17
  1032. add r19=r19,r18 };;
  1033. { .mfb; getf.sig r25=f107 }
  1034. { .mii; (p7) add carry2=1,carry2
  1035. cmp.ltu p7,p0=r19,r18
  1036. add r19=r19,carry1 };;
  1037. { .mfb; getf.sig r26=f98 }
  1038. { .mii; (p7) add carry2=1,carry2
  1039. cmp.ltu p7,p0=r19,carry1};;
  1040. { .mii; st8 [r32]=r19,16
  1041. (p7) add carry2=1,carry2 }
  1042. { .mfb; add r25=r25,r24 };;
  1043. { .mfb; getf.sig r16=f117 }
  1044. { .mii; mov carry1=0
  1045. cmp.ltu p6,p0=r25,r24
  1046. add r26=r26,r25 };;
  1047. { .mfb; getf.sig r17=f108 }
  1048. { .mii;
  1049. (p6) add carry1=1,carry1
  1050. cmp.ltu p6,p0=r26,r25
  1051. add r26=r26,carry2 };;
  1052. { .mfb; nop.m 0x0 }
  1053. { .mii;
  1054. (p6) add carry1=1,carry1
  1055. cmp.ltu p6,p0=r26,carry2 };;
  1056. { .mii; st8 [r33]=r26,16
  1057. (p6) add carry1=1,carry1 }
  1058. { .mfb; add r17=r17,r16 };;
  1059. { .mfb; getf.sig r24=f118 }
  1060. { .mii; mov carry2=0
  1061. cmp.ltu p7,p0=r17,r16
  1062. add r17=r17,carry1 };;
  1063. { .mii; (p7) add carry2=1,carry2
  1064. cmp.ltu p7,p0=r17,carry1};;
  1065. { .mii; st8 [r32]=r17
  1066. (p7) add carry2=1,carry2 };;
  1067. { .mfb; add r24=r24,carry2 };;
  1068. { .mib; st8 [r33]=r24 }
  1069. { .mib; rum 1<<5 // clear um.mfh
  1070. br.ret.sptk.many b0 };;
  1071. .endp bn_mul_comba8#
  1072. #undef carry3
  1073. #undef carry2
  1074. #undef carry1
  1075. #endif
  1076. #if 1
  1077. // It's possible to make it faster (see comment to bn_sqr_comba8), but
  1078. // I reckon it doesn't worth the effort. Basically because the routine
  1079. // (actually both of them) practically never called... So I just play
  1080. // same trick as with bn_sqr_comba8.
  1081. //
  1082. // void bn_sqr_comba4(BN_ULONG *r, BN_ULONG *a)
  1083. //
  1084. .global bn_sqr_comba4#
  1085. .proc bn_sqr_comba4#
  1086. .align 64
  1087. bn_sqr_comba4:
  1088. .prologue
  1089. .fframe 0
  1090. .save ar.pfs,r2
  1091. { .mii; alloc r2=ar.pfs,2,1,0,0
  1092. mov r34=r33
  1093. add r14=8,r33 };;
  1094. .body
  1095. { .mii; add r17=8,r34
  1096. add r15=16,r33
  1097. add r18=16,r34 }
  1098. { .mfb; add r16=24,r33
  1099. br .L_cheat_entry_point4 };;
  1100. .endp bn_sqr_comba4#
  1101. #endif
  1102. #if 1
  1103. // Runs in ~115 cycles and ~4.5 times faster than C. Well, whatever...
  1104. //
  1105. // void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
  1106. //
  1107. #define carry1 r14
  1108. #define carry2 r15
  1109. .global bn_mul_comba4#
  1110. .proc bn_mul_comba4#
  1111. .align 64
  1112. bn_mul_comba4:
  1113. .prologue
  1114. .fframe 0
  1115. .save ar.pfs,r2
  1116. { .mii; alloc r2=ar.pfs,3,0,0,0
  1117. add r14=8,r33
  1118. add r17=8,r34 }
  1119. .body
  1120. { .mii; add r15=16,r33
  1121. add r18=16,r34
  1122. add r16=24,r33 };;
  1123. .L_cheat_entry_point4:
  1124. { .mmi; add r19=24,r34
  1125. ldf8 f32=[r33] }
  1126. { .mmi; ldf8 f120=[r34]
  1127. ldf8 f121=[r17] };;
  1128. { .mmi; ldf8 f122=[r18]
  1129. ldf8 f123=[r19] }
  1130. { .mmi; ldf8 f33=[r14]
  1131. ldf8 f34=[r15] }
  1132. { .mfi; ldf8 f35=[r16]
  1133. xma.hu f41=f32,f120,f0 }
  1134. { .mfi; xma.lu f40=f32,f120,f0 };;
  1135. { .mfi; xma.hu f51=f32,f121,f0 }
  1136. { .mfi; xma.lu f50=f32,f121,f0 };;
  1137. { .mfi; xma.hu f61=f32,f122,f0 }
  1138. { .mfi; xma.lu f60=f32,f122,f0 };;
  1139. { .mfi; xma.hu f71=f32,f123,f0 }
  1140. { .mfi; xma.lu f70=f32,f123,f0 };;//
  1141. // Major stall takes place here, and 3 more places below. Result from
  1142. // first xma is not available for another 3 ticks.
  1143. { .mfi; getf.sig r16=f40
  1144. xma.hu f42=f33,f120,f41
  1145. add r33=8,r32 }
  1146. { .mfi; xma.lu f41=f33,f120,f41 };;
  1147. { .mfi; getf.sig r24=f50
  1148. xma.hu f52=f33,f121,f51 }
  1149. { .mfi; xma.lu f51=f33,f121,f51 };;
  1150. { .mfi; st8 [r32]=r16,16
  1151. xma.hu f62=f33,f122,f61 }
  1152. { .mfi; xma.lu f61=f33,f122,f61 };;
  1153. { .mfi; xma.hu f72=f33,f123,f71 }
  1154. { .mfi; xma.lu f71=f33,f123,f71 };;//
  1155. //-------------------------------------------------//
  1156. { .mfi; getf.sig r25=f41
  1157. xma.hu f43=f34,f120,f42 }
  1158. { .mfi; xma.lu f42=f34,f120,f42 };;
  1159. { .mfi; getf.sig r16=f60
  1160. xma.hu f53=f34,f121,f52 }
  1161. { .mfi; xma.lu f52=f34,f121,f52 };;
  1162. { .mfi; getf.sig r17=f51
  1163. xma.hu f63=f34,f122,f62
  1164. add r25=r25,r24 }
  1165. { .mfi; mov carry1=0
  1166. xma.lu f62=f34,f122,f62 };;
  1167. { .mfi; st8 [r33]=r25,16
  1168. xma.hu f73=f34,f123,f72
  1169. cmp.ltu p6,p0=r25,r24 }
  1170. { .mfi; xma.lu f72=f34,f123,f72 };;//
  1171. //-------------------------------------------------//
  1172. { .mfi; getf.sig r18=f42
  1173. xma.hu f44=f35,f120,f43
  1174. (p6) add carry1=1,carry1 }
  1175. { .mfi; add r17=r17,r16
  1176. xma.lu f43=f35,f120,f43
  1177. mov carry2=0 };;
  1178. { .mfi; getf.sig r24=f70
  1179. xma.hu f54=f35,f121,f53
  1180. cmp.ltu p7,p0=r17,r16 }
  1181. { .mfi; xma.lu f53=f35,f121,f53 };;
  1182. { .mfi; getf.sig r25=f61
  1183. xma.hu f64=f35,f122,f63
  1184. add r18=r18,r17 }
  1185. { .mfi; xma.lu f63=f35,f122,f63
  1186. (p7) add carry2=1,carry2 };;
  1187. { .mfi; getf.sig r26=f52
  1188. xma.hu f74=f35,f123,f73
  1189. cmp.ltu p7,p0=r18,r17 }
  1190. { .mfi; xma.lu f73=f35,f123,f73
  1191. add r18=r18,carry1 };;
  1192. //-------------------------------------------------//
  1193. { .mii; st8 [r32]=r18,16
  1194. (p7) add carry2=1,carry2
  1195. cmp.ltu p7,p0=r18,carry1 };;
  1196. { .mfi; getf.sig r27=f43 // last major stall
  1197. (p7) add carry2=1,carry2 };;
  1198. { .mii; getf.sig r16=f71
  1199. add r25=r25,r24
  1200. mov carry1=0 };;
  1201. { .mii; getf.sig r17=f62
  1202. cmp.ltu p6,p0=r25,r24
  1203. add r26=r26,r25 };;
  1204. { .mii;
  1205. (p6) add carry1=1,carry1
  1206. cmp.ltu p6,p0=r26,r25
  1207. add r27=r27,r26 };;
  1208. { .mii;
  1209. (p6) add carry1=1,carry1
  1210. cmp.ltu p6,p0=r27,r26
  1211. add r27=r27,carry2 };;
  1212. { .mii; getf.sig r18=f53
  1213. (p6) add carry1=1,carry1
  1214. cmp.ltu p6,p0=r27,carry2 };;
  1215. { .mfi; st8 [r33]=r27,16
  1216. (p6) add carry1=1,carry1 }
  1217. { .mii; getf.sig r19=f44
  1218. add r17=r17,r16
  1219. mov carry2=0 };;
  1220. { .mii; getf.sig r24=f72
  1221. cmp.ltu p7,p0=r17,r16
  1222. add r18=r18,r17 };;
  1223. { .mii; (p7) add carry2=1,carry2
  1224. cmp.ltu p7,p0=r18,r17
  1225. add r19=r19,r18 };;
  1226. { .mii; (p7) add carry2=1,carry2
  1227. cmp.ltu p7,p0=r19,r18
  1228. add r19=r19,carry1 };;
  1229. { .mii; getf.sig r25=f63
  1230. (p7) add carry2=1,carry2
  1231. cmp.ltu p7,p0=r19,carry1};;
  1232. { .mii; st8 [r32]=r19,16
  1233. (p7) add carry2=1,carry2 }
  1234. { .mii; getf.sig r26=f54
  1235. add r25=r25,r24
  1236. mov carry1=0 };;
  1237. { .mii; getf.sig r16=f73
  1238. cmp.ltu p6,p0=r25,r24
  1239. add r26=r26,r25 };;
  1240. { .mii;
  1241. (p6) add carry1=1,carry1
  1242. cmp.ltu p6,p0=r26,r25
  1243. add r26=r26,carry2 };;
  1244. { .mii; getf.sig r17=f64
  1245. (p6) add carry1=1,carry1
  1246. cmp.ltu p6,p0=r26,carry2 };;
  1247. { .mii; st8 [r33]=r26,16
  1248. (p6) add carry1=1,carry1 }
  1249. { .mii; getf.sig r24=f74
  1250. add r17=r17,r16
  1251. mov carry2=0 };;
  1252. { .mii; cmp.ltu p7,p0=r17,r16
  1253. add r17=r17,carry1 };;
  1254. { .mii; (p7) add carry2=1,carry2
  1255. cmp.ltu p7,p0=r17,carry1};;
  1256. { .mii; st8 [r32]=r17,16
  1257. (p7) add carry2=1,carry2 };;
  1258. { .mii; add r24=r24,carry2 };;
  1259. { .mii; st8 [r33]=r24 }
  1260. { .mib; rum 1<<5 // clear um.mfh
  1261. br.ret.sptk.many b0 };;
  1262. .endp bn_mul_comba4#
  1263. #undef carry2
  1264. #undef carry1
  1265. #endif
  1266. #if 1
  1267. //
  1268. // BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d)
  1269. //
  1270. // In the nutshell it's a port of my MIPS III/IV implementation.
  1271. //
  1272. #define AT r14
  1273. #define H r16
  1274. #define HH r20
  1275. #define L r17
  1276. #define D r18
  1277. #define DH r22
  1278. #define I r21
  1279. #if 0
  1280. // Some preprocessors (most notably HP-UX) apper to be allergic to
  1281. // macros enclosed to parenthesis as these three will be.
  1282. #define cont p16
  1283. #define break p0 // p20
  1284. #define equ p24
  1285. #else
  1286. cont=p16
  1287. break=p0
  1288. equ=p24
  1289. #endif
  1290. .global abort#
  1291. .global bn_div_words#
  1292. .proc bn_div_words#
  1293. .align 64
  1294. bn_div_words:
  1295. .prologue
  1296. .fframe 0
  1297. .save ar.pfs,r2
  1298. .save b0,r3
  1299. { .mii; alloc r2=ar.pfs,3,5,0,8
  1300. mov r3=b0
  1301. mov r10=pr };;
  1302. { .mmb; cmp.eq p6,p0=r34,r0
  1303. mov r8=-1
  1304. (p6) br.ret.spnt.many b0 };;
  1305. .body
  1306. { .mii; mov H=r32 // save h
  1307. mov ar.ec=0 // don't rotate at exit
  1308. mov pr.rot=0 }
  1309. { .mii; mov L=r33 // save l
  1310. mov r36=r0 };;
  1311. .L_divw_shift: // -vv- note signed comparison
  1312. { .mfi; (p0) cmp.lt p16,p0=r0,r34 // d
  1313. (p0) shladd r33=r34,1,r0 }
  1314. { .mfb; (p0) add r35=1,r36
  1315. (p0) nop.f 0x0
  1316. (p16) br.wtop.dpnt .L_divw_shift };;
  1317. { .mii; mov D=r34
  1318. shr.u DH=r34,32
  1319. sub r35=64,r36 };;
  1320. { .mii; setf.sig f7=DH
  1321. shr.u AT=H,r35
  1322. mov I=r36 };;
  1323. { .mib; cmp.ne p6,p0=r0,AT
  1324. shl H=H,r36
  1325. (p6) br.call.spnt.clr b0=abort };; // overflow, die...
  1326. { .mfi; fcvt.xuf.s1 f7=f7
  1327. shr.u AT=L,r35 };;
  1328. { .mii; shl L=L,r36
  1329. or H=H,AT };;
  1330. { .mii; nop.m 0x0
  1331. cmp.leu p6,p0=D,H;;
  1332. (p6) sub H=H,D }
  1333. { .mlx; setf.sig f14=D
  1334. movl AT=0xffffffff };;
  1335. ///////////////////////////////////////////////////////////
  1336. { .mii; setf.sig f6=H
  1337. shr.u HH=H,32;;
  1338. cmp.eq p6,p7=HH,DH };;
  1339. { .mfb;
  1340. (p6) setf.sig f8=AT
  1341. (p7) fcvt.xuf.s1 f6=f6
  1342. (p7) br.call.sptk b6=.L_udiv64_32_b6 };;
  1343. { .mfi; getf.sig r33=f8 // q
  1344. xmpy.lu f9=f8,f14 }
  1345. { .mfi; xmpy.hu f10=f8,f14
  1346. shrp H=H,L,32 };;
  1347. { .mmi; getf.sig r35=f9 // tl
  1348. getf.sig r31=f10 };; // th
  1349. .L_divw_1st_iter:
  1350. { .mii; (p0) add r32=-1,r33
  1351. (p0) cmp.eq equ,cont=HH,r31 };;
  1352. { .mii; (p0) cmp.ltu p8,p0=r35,D
  1353. (p0) sub r34=r35,D
  1354. (equ) cmp.leu break,cont=r35,H };;
  1355. { .mib; (cont) cmp.leu cont,break=HH,r31
  1356. (p8) add r31=-1,r31
  1357. (cont) br.wtop.spnt .L_divw_1st_iter };;
  1358. ///////////////////////////////////////////////////////////
  1359. { .mii; sub H=H,r35
  1360. shl r8=r33,32
  1361. shl L=L,32 };;
  1362. ///////////////////////////////////////////////////////////
  1363. { .mii; setf.sig f6=H
  1364. shr.u HH=H,32;;
  1365. cmp.eq p6,p7=HH,DH };;
  1366. { .mfb;
  1367. (p6) setf.sig f8=AT
  1368. (p7) fcvt.xuf.s1 f6=f6
  1369. (p7) br.call.sptk b6=.L_udiv64_32_b6 };;
  1370. { .mfi; getf.sig r33=f8 // q
  1371. xmpy.lu f9=f8,f14 }
  1372. { .mfi; xmpy.hu f10=f8,f14
  1373. shrp H=H,L,32 };;
  1374. { .mmi; getf.sig r35=f9 // tl
  1375. getf.sig r31=f10 };; // th
  1376. .L_divw_2nd_iter:
  1377. { .mii; (p0) add r32=-1,r33
  1378. (p0) cmp.eq equ,cont=HH,r31 };;
  1379. { .mii; (p0) cmp.ltu p8,p0=r35,D
  1380. (p0) sub r34=r35,D
  1381. (equ) cmp.leu break,cont=r35,H };;
  1382. { .mib; (cont) cmp.leu cont,break=HH,r31
  1383. (p8) add r31=-1,r31
  1384. (cont) br.wtop.spnt .L_divw_2nd_iter };;
  1385. ///////////////////////////////////////////////////////////
  1386. { .mii; sub H=H,r35
  1387. or r8=r8,r33
  1388. mov ar.pfs=r2 };;
  1389. { .mii; shr.u r9=H,I // remainder if anybody wants it
  1390. mov pr=r10,-1 }
  1391. { .mfb; br.ret.sptk.many b0 };;
  1392. // Unsigned 64 by 32 (well, by 64 for the moment) bit integer division
  1393. // procedure.
  1394. //
  1395. // inputs: f6 = (double)a, f7 = (double)b
  1396. // output: f8 = (int)(a/b)
  1397. // clobbered: f8,f9,f10,f11,pred
  1398. pred=p15
  1399. // This procedure is essentially Intel code and therefore is
  1400. // copyrighted to Intel Corporation (I suppose...). It's sligtly
  1401. // modified for specific needs.
  1402. .align 32
  1403. .skip 16
  1404. .L_udiv64_32_b6:
  1405. frcpa.s1 f8,pred=f6,f7;; // [0] y0 = 1 / b
  1406. (pred) fnma.s1 f9=f7,f8,f1 // [5] e0 = 1 - b * y0
  1407. (pred) fmpy.s1 f10=f6,f8;; // [5] q0 = a * y0
  1408. (pred) fmpy.s1 f11=f9,f9 // [10] e1 = e0 * e0
  1409. (pred) fma.s1 f10=f9,f10,f10;; // [10] q1 = q0 + e0 * q0
  1410. (pred) fma.s1 f8=f9,f8,f8 //;; // [15] y1 = y0 + e0 * y0
  1411. (pred) fma.s1 f9=f11,f10,f10;; // [15] q2 = q1 + e1 * q1
  1412. (pred) fma.s1 f8=f11,f8,f8 //;; // [20] y2 = y1 + e1 * y1
  1413. (pred) fnma.s1 f10=f7,f9,f6;; // [20] r2 = a - b * q2
  1414. (pred) fma.s1 f8=f10,f8,f9;; // [25] q3 = q2 + r2 * y2
  1415. fcvt.fxu.trunc.s1 f8=f8 // [30] q = trunc(q3)
  1416. br.ret.sptk.many b6;;
  1417. .endp bn_div_words#
  1418. #endif