ia64.S 45 KB

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