ia64.S 45 KB

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