parisc-mont.pl 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. #! /usr/bin/env perl
  2. # Copyright 2009-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. # ====================================================================
  9. # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
  10. # project. The module is, however, dual licensed under OpenSSL and
  11. # CRYPTOGAMS licenses depending on where you obtain it. For further
  12. # details see http://www.openssl.org/~appro/cryptogams/.
  13. # ====================================================================
  14. # On PA-7100LC this module performs ~90-50% better, less for longer
  15. # keys, than code generated by gcc 3.2 for PA-RISC 1.1. Latter means
  16. # that compiler utilized xmpyu instruction to perform 32x32=64-bit
  17. # multiplication, which in turn means that "baseline" performance was
  18. # optimal in respect to instruction set capabilities. Fair comparison
  19. # with vendor compiler is problematic, because OpenSSL doesn't define
  20. # BN_LLONG [presumably] for historical reasons, which drives compiler
  21. # toward 4 times 16x16=32-bit multiplicatons [plus complementary
  22. # shifts and additions] instead. This means that you should observe
  23. # several times improvement over code generated by vendor compiler
  24. # for PA-RISC 1.1, but the "baseline" is far from optimal. The actual
  25. # improvement coefficient was never collected on PA-7100LC, or any
  26. # other 1.1 CPU, because I don't have access to such machine with
  27. # vendor compiler. But to give you a taste, PA-RISC 1.1 code path
  28. # reportedly outperformed code generated by cc +DA1.1 +O3 by factor
  29. # of ~5x on PA-8600.
  30. #
  31. # On PA-RISC 2.0 it has to compete with pa-risc2[W].s, which is
  32. # reportedly ~2x faster than vendor compiler generated code [according
  33. # to comment in pa-risc2[W].s]. Here comes a catch. Execution core of
  34. # this implementation is actually 32-bit one, in the sense that it
  35. # operates on 32-bit values. But pa-risc2[W].s operates on arrays of
  36. # 64-bit BN_LONGs... How do they interoperate then? No problem. This
  37. # module picks halves of 64-bit values in reverse order and pretends
  38. # they were 32-bit BN_LONGs. But can 32-bit core compete with "pure"
  39. # 64-bit code such as pa-risc2[W].s then? Well, the thing is that
  40. # 32x32=64-bit multiplication is the best even PA-RISC 2.0 can do,
  41. # i.e. there is no "wider" multiplication like on most other 64-bit
  42. # platforms. This means that even being effectively 32-bit, this
  43. # implementation performs "64-bit" computational task in same amount
  44. # of arithmetic operations, most notably multiplications. It requires
  45. # more memory references, most notably to tp[num], but this doesn't
  46. # seem to exhaust memory port capacity. And indeed, dedicated PA-RISC
  47. # 2.0 code path provides virtually same performance as pa-risc2[W].s:
  48. # it's ~10% better for shortest key length and ~10% worse for longest
  49. # one.
  50. #
  51. # In case it wasn't clear. The module has two distinct code paths:
  52. # PA-RISC 1.1 and PA-RISC 2.0 ones. Latter features carry-free 64-bit
  53. # additions and 64-bit integer loads, not to mention specific
  54. # instruction scheduling. In 64-bit build naturally only 2.0 code path
  55. # is assembled. In 32-bit application context both code paths are
  56. # assembled, PA-RISC 2.0 CPU is detected at run-time and proper path
  57. # is taken automatically. Also, in 32-bit build the module imposes
  58. # couple of limitations: vector lengths has to be even and vector
  59. # addresses has to be 64-bit aligned. Normally neither is a problem:
  60. # most common key lengths are even and vectors are commonly malloc-ed,
  61. # which ensures alignment.
  62. #
  63. # Special thanks to polarhome.com for providing HP-UX account on
  64. # PA-RISC 1.1 machine, and to correspondent who chose to remain
  65. # anonymous for testing the code on PA-RISC 2.0 machine.
  66. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  67. $flavour = shift;
  68. $output = shift;
  69. open STDOUT,">$output";
  70. if ($flavour =~ /64/) {
  71. $LEVEL ="2.0W";
  72. $SIZE_T =8;
  73. $FRAME_MARKER =80;
  74. $SAVED_RP =16;
  75. $PUSH ="std";
  76. $PUSHMA ="std,ma";
  77. $POP ="ldd";
  78. $POPMB ="ldd,mb";
  79. $BN_SZ =$SIZE_T;
  80. } else {
  81. $LEVEL ="1.1"; #$LEVEL.="\n\t.ALLOW\t2.0";
  82. $SIZE_T =4;
  83. $FRAME_MARKER =48;
  84. $SAVED_RP =20;
  85. $PUSH ="stw";
  86. $PUSHMA ="stwm";
  87. $POP ="ldw";
  88. $POPMB ="ldwm";
  89. $BN_SZ =$SIZE_T;
  90. if (open CONF,"<${dir}../../opensslconf.h") {
  91. while(<CONF>) {
  92. if (m/#\s*define\s+SIXTY_FOUR_BIT/) {
  93. $BN_SZ=8;
  94. $LEVEL="2.0";
  95. last;
  96. }
  97. }
  98. close CONF;
  99. }
  100. }
  101. $FRAME=8*$SIZE_T+$FRAME_MARKER; # 8 saved regs + frame marker
  102. # [+ argument transfer]
  103. $LOCALS=$FRAME-$FRAME_MARKER;
  104. $FRAME+=32; # local variables
  105. $tp="%r31";
  106. $ti1="%r29";
  107. $ti0="%r28";
  108. $rp="%r26";
  109. $ap="%r25";
  110. $bp="%r24";
  111. $np="%r23";
  112. $n0="%r22"; # passed through stack in 32-bit
  113. $num="%r21"; # passed through stack in 32-bit
  114. $idx="%r20";
  115. $arrsz="%r19";
  116. $nm1="%r7";
  117. $nm0="%r6";
  118. $ab1="%r5";
  119. $ab0="%r4";
  120. $fp="%r3";
  121. $hi1="%r2";
  122. $hi0="%r1";
  123. $xfer=$n0; # accommodates [-16..15] offset in fld[dw]s
  124. $fm0="%fr4"; $fti=$fm0;
  125. $fbi="%fr5L";
  126. $fn0="%fr5R";
  127. $fai="%fr6"; $fab0="%fr7"; $fab1="%fr8";
  128. $fni="%fr9"; $fnm0="%fr10"; $fnm1="%fr11";
  129. $code=<<___;
  130. .LEVEL $LEVEL
  131. .SPACE \$TEXT\$
  132. .SUBSPA \$CODE\$,QUAD=0,ALIGN=8,ACCESS=0x2C,CODE_ONLY
  133. .EXPORT bn_mul_mont,ENTRY,ARGW0=GR,ARGW1=GR,ARGW2=GR,ARGW3=GR
  134. .ALIGN 64
  135. bn_mul_mont
  136. .PROC
  137. .CALLINFO FRAME=`$FRAME-8*$SIZE_T`,NO_CALLS,SAVE_RP,SAVE_SP,ENTRY_GR=6
  138. .ENTRY
  139. $PUSH %r2,-$SAVED_RP(%sp) ; standard prologue
  140. $PUSHMA %r3,$FRAME(%sp)
  141. $PUSH %r4,`-$FRAME+1*$SIZE_T`(%sp)
  142. $PUSH %r5,`-$FRAME+2*$SIZE_T`(%sp)
  143. $PUSH %r6,`-$FRAME+3*$SIZE_T`(%sp)
  144. $PUSH %r7,`-$FRAME+4*$SIZE_T`(%sp)
  145. $PUSH %r8,`-$FRAME+5*$SIZE_T`(%sp)
  146. $PUSH %r9,`-$FRAME+6*$SIZE_T`(%sp)
  147. $PUSH %r10,`-$FRAME+7*$SIZE_T`(%sp)
  148. ldo -$FRAME(%sp),$fp
  149. ___
  150. $code.=<<___ if ($SIZE_T==4);
  151. ldw `-$FRAME_MARKER-4`($fp),$n0
  152. ldw `-$FRAME_MARKER-8`($fp),$num
  153. nop
  154. nop ; alignment
  155. ___
  156. $code.=<<___ if ($BN_SZ==4);
  157. comiclr,<= 6,$num,%r0 ; are vectors long enough?
  158. b L\$abort
  159. ldi 0,%r28 ; signal "unhandled"
  160. add,ev %r0,$num,$num ; is $num even?
  161. b L\$abort
  162. nop
  163. or $ap,$np,$ti1
  164. extru,= $ti1,31,3,%r0 ; are ap and np 64-bit aligned?
  165. b L\$abort
  166. nop
  167. nop ; alignment
  168. nop
  169. fldws 0($n0),${fn0}
  170. fldws,ma 4($bp),${fbi} ; bp[0]
  171. ___
  172. $code.=<<___ if ($BN_SZ==8);
  173. comib,> 3,$num,L\$abort ; are vectors long enough?
  174. ldi 0,%r28 ; signal "unhandled"
  175. addl $num,$num,$num ; I operate on 32-bit values
  176. fldws 4($n0),${fn0} ; only low part of n0
  177. fldws 4($bp),${fbi} ; bp[0] in flipped word order
  178. ___
  179. $code.=<<___;
  180. fldds 0($ap),${fai} ; ap[0,1]
  181. fldds 0($np),${fni} ; np[0,1]
  182. sh2addl $num,%r0,$arrsz
  183. ldi 31,$hi0
  184. ldo 36($arrsz),$hi1 ; space for tp[num+1]
  185. andcm $hi1,$hi0,$hi1 ; align
  186. addl $hi1,%sp,%sp
  187. $PUSH $fp,-$SIZE_T(%sp)
  188. ldo `$LOCALS+16`($fp),$xfer
  189. ldo `$LOCALS+32+4`($fp),$tp
  190. xmpyu ${fai}L,${fbi},${fab0} ; ap[0]*bp[0]
  191. xmpyu ${fai}R,${fbi},${fab1} ; ap[1]*bp[0]
  192. xmpyu ${fn0},${fab0}R,${fm0}
  193. addl $arrsz,$ap,$ap ; point at the end
  194. addl $arrsz,$np,$np
  195. subi 0,$arrsz,$idx ; j=0
  196. ldo 8($idx),$idx ; j++++
  197. xmpyu ${fni}L,${fm0}R,${fnm0} ; np[0]*m
  198. xmpyu ${fni}R,${fm0}R,${fnm1} ; np[1]*m
  199. fstds ${fab0},-16($xfer)
  200. fstds ${fnm0},-8($xfer)
  201. fstds ${fab1},0($xfer)
  202. fstds ${fnm1},8($xfer)
  203. flddx $idx($ap),${fai} ; ap[2,3]
  204. flddx $idx($np),${fni} ; np[2,3]
  205. ___
  206. $code.=<<___ if ($BN_SZ==4);
  207. mtctl $hi0,%cr11 ; $hi0 still holds 31
  208. extrd,u,*= $hi0,%sar,1,$hi0 ; executes on PA-RISC 1.0
  209. b L\$parisc11
  210. nop
  211. ___
  212. $code.=<<___; # PA-RISC 2.0 code-path
  213. xmpyu ${fai}L,${fbi},${fab0} ; ap[j]*bp[0]
  214. xmpyu ${fni}L,${fm0}R,${fnm0} ; np[j]*m
  215. ldd -16($xfer),$ab0
  216. fstds ${fab0},-16($xfer)
  217. extrd,u $ab0,31,32,$hi0
  218. extrd,u $ab0,63,32,$ab0
  219. ldd -8($xfer),$nm0
  220. fstds ${fnm0},-8($xfer)
  221. ldo 8($idx),$idx ; j++++
  222. addl $ab0,$nm0,$nm0 ; low part is discarded
  223. extrd,u $nm0,31,32,$hi1
  224. L\$1st
  225. xmpyu ${fai}R,${fbi},${fab1} ; ap[j+1]*bp[0]
  226. xmpyu ${fni}R,${fm0}R,${fnm1} ; np[j+1]*m
  227. ldd 0($xfer),$ab1
  228. fstds ${fab1},0($xfer)
  229. addl $hi0,$ab1,$ab1
  230. extrd,u $ab1,31,32,$hi0
  231. ldd 8($xfer),$nm1
  232. fstds ${fnm1},8($xfer)
  233. extrd,u $ab1,63,32,$ab1
  234. addl $hi1,$nm1,$nm1
  235. flddx $idx($ap),${fai} ; ap[j,j+1]
  236. flddx $idx($np),${fni} ; np[j,j+1]
  237. addl $ab1,$nm1,$nm1
  238. extrd,u $nm1,31,32,$hi1
  239. xmpyu ${fai}L,${fbi},${fab0} ; ap[j]*bp[0]
  240. xmpyu ${fni}L,${fm0}R,${fnm0} ; np[j]*m
  241. ldd -16($xfer),$ab0
  242. fstds ${fab0},-16($xfer)
  243. addl $hi0,$ab0,$ab0
  244. extrd,u $ab0,31,32,$hi0
  245. ldd -8($xfer),$nm0
  246. fstds ${fnm0},-8($xfer)
  247. extrd,u $ab0,63,32,$ab0
  248. addl $hi1,$nm0,$nm0
  249. stw $nm1,-4($tp) ; tp[j-1]
  250. addl $ab0,$nm0,$nm0
  251. stw,ma $nm0,8($tp) ; tp[j-1]
  252. addib,<> 8,$idx,L\$1st ; j++++
  253. extrd,u $nm0,31,32,$hi1
  254. xmpyu ${fai}R,${fbi},${fab1} ; ap[j]*bp[0]
  255. xmpyu ${fni}R,${fm0}R,${fnm1} ; np[j]*m
  256. ldd 0($xfer),$ab1
  257. fstds ${fab1},0($xfer)
  258. addl $hi0,$ab1,$ab1
  259. extrd,u $ab1,31,32,$hi0
  260. ldd 8($xfer),$nm1
  261. fstds ${fnm1},8($xfer)
  262. extrd,u $ab1,63,32,$ab1
  263. addl $hi1,$nm1,$nm1
  264. ldd -16($xfer),$ab0
  265. addl $ab1,$nm1,$nm1
  266. ldd -8($xfer),$nm0
  267. extrd,u $nm1,31,32,$hi1
  268. addl $hi0,$ab0,$ab0
  269. extrd,u $ab0,31,32,$hi0
  270. stw $nm1,-4($tp) ; tp[j-1]
  271. extrd,u $ab0,63,32,$ab0
  272. addl $hi1,$nm0,$nm0
  273. ldd 0($xfer),$ab1
  274. addl $ab0,$nm0,$nm0
  275. ldd,mb 8($xfer),$nm1
  276. extrd,u $nm0,31,32,$hi1
  277. stw,ma $nm0,8($tp) ; tp[j-1]
  278. ldo -1($num),$num ; i--
  279. subi 0,$arrsz,$idx ; j=0
  280. ___
  281. $code.=<<___ if ($BN_SZ==4);
  282. fldws,ma 4($bp),${fbi} ; bp[1]
  283. ___
  284. $code.=<<___ if ($BN_SZ==8);
  285. fldws 0($bp),${fbi} ; bp[1] in flipped word order
  286. ___
  287. $code.=<<___;
  288. flddx $idx($ap),${fai} ; ap[0,1]
  289. flddx $idx($np),${fni} ; np[0,1]
  290. fldws 8($xfer),${fti}R ; tp[0]
  291. addl $hi0,$ab1,$ab1
  292. extrd,u $ab1,31,32,$hi0
  293. extrd,u $ab1,63,32,$ab1
  294. ldo 8($idx),$idx ; j++++
  295. xmpyu ${fai}L,${fbi},${fab0} ; ap[0]*bp[1]
  296. xmpyu ${fai}R,${fbi},${fab1} ; ap[1]*bp[1]
  297. addl $hi1,$nm1,$nm1
  298. addl $ab1,$nm1,$nm1
  299. extrd,u $nm1,31,32,$hi1
  300. fstws,mb ${fab0}L,-8($xfer) ; save high part
  301. stw $nm1,-4($tp) ; tp[j-1]
  302. fcpy,sgl %fr0,${fti}L ; zero high part
  303. fcpy,sgl %fr0,${fab0}L
  304. addl $hi1,$hi0,$hi0
  305. extrd,u $hi0,31,32,$hi1
  306. fcnvxf,dbl,dbl ${fti},${fti} ; 32-bit unsigned int -> double
  307. fcnvxf,dbl,dbl ${fab0},${fab0}
  308. stw $hi0,0($tp)
  309. stw $hi1,4($tp)
  310. fadd,dbl ${fti},${fab0},${fab0} ; add tp[0]
  311. fcnvfx,dbl,dbl ${fab0},${fab0} ; double -> 33-bit unsigned int
  312. xmpyu ${fn0},${fab0}R,${fm0}
  313. ldo `$LOCALS+32+4`($fp),$tp
  314. L\$outer
  315. xmpyu ${fni}L,${fm0}R,${fnm0} ; np[0]*m
  316. xmpyu ${fni}R,${fm0}R,${fnm1} ; np[1]*m
  317. fstds ${fab0},-16($xfer) ; 33-bit value
  318. fstds ${fnm0},-8($xfer)
  319. flddx $idx($ap),${fai} ; ap[2]
  320. flddx $idx($np),${fni} ; np[2]
  321. ldo 8($idx),$idx ; j++++
  322. ldd -16($xfer),$ab0 ; 33-bit value
  323. ldd -8($xfer),$nm0
  324. ldw 0($xfer),$hi0 ; high part
  325. xmpyu ${fai}L,${fbi},${fab0} ; ap[j]*bp[i]
  326. xmpyu ${fni}L,${fm0}R,${fnm0} ; np[j]*m
  327. extrd,u $ab0,31,32,$ti0 ; carry bit
  328. extrd,u $ab0,63,32,$ab0
  329. fstds ${fab1},0($xfer)
  330. addl $ti0,$hi0,$hi0 ; account carry bit
  331. fstds ${fnm1},8($xfer)
  332. addl $ab0,$nm0,$nm0 ; low part is discarded
  333. ldw 0($tp),$ti1 ; tp[1]
  334. extrd,u $nm0,31,32,$hi1
  335. fstds ${fab0},-16($xfer)
  336. fstds ${fnm0},-8($xfer)
  337. L\$inner
  338. xmpyu ${fai}R,${fbi},${fab1} ; ap[j+1]*bp[i]
  339. xmpyu ${fni}R,${fm0}R,${fnm1} ; np[j+1]*m
  340. ldd 0($xfer),$ab1
  341. fstds ${fab1},0($xfer)
  342. addl $hi0,$ti1,$ti1
  343. addl $ti1,$ab1,$ab1
  344. ldd 8($xfer),$nm1
  345. fstds ${fnm1},8($xfer)
  346. extrd,u $ab1,31,32,$hi0
  347. extrd,u $ab1,63,32,$ab1
  348. flddx $idx($ap),${fai} ; ap[j,j+1]
  349. flddx $idx($np),${fni} ; np[j,j+1]
  350. addl $hi1,$nm1,$nm1
  351. addl $ab1,$nm1,$nm1
  352. ldw 4($tp),$ti0 ; tp[j]
  353. stw $nm1,-4($tp) ; tp[j-1]
  354. xmpyu ${fai}L,${fbi},${fab0} ; ap[j]*bp[i]
  355. xmpyu ${fni}L,${fm0}R,${fnm0} ; np[j]*m
  356. ldd -16($xfer),$ab0
  357. fstds ${fab0},-16($xfer)
  358. addl $hi0,$ti0,$ti0
  359. addl $ti0,$ab0,$ab0
  360. ldd -8($xfer),$nm0
  361. fstds ${fnm0},-8($xfer)
  362. extrd,u $ab0,31,32,$hi0
  363. extrd,u $nm1,31,32,$hi1
  364. ldw 8($tp),$ti1 ; tp[j]
  365. extrd,u $ab0,63,32,$ab0
  366. addl $hi1,$nm0,$nm0
  367. addl $ab0,$nm0,$nm0
  368. stw,ma $nm0,8($tp) ; tp[j-1]
  369. addib,<> 8,$idx,L\$inner ; j++++
  370. extrd,u $nm0,31,32,$hi1
  371. xmpyu ${fai}R,${fbi},${fab1} ; ap[j]*bp[i]
  372. xmpyu ${fni}R,${fm0}R,${fnm1} ; np[j]*m
  373. ldd 0($xfer),$ab1
  374. fstds ${fab1},0($xfer)
  375. addl $hi0,$ti1,$ti1
  376. addl $ti1,$ab1,$ab1
  377. ldd 8($xfer),$nm1
  378. fstds ${fnm1},8($xfer)
  379. extrd,u $ab1,31,32,$hi0
  380. extrd,u $ab1,63,32,$ab1
  381. ldw 4($tp),$ti0 ; tp[j]
  382. addl $hi1,$nm1,$nm1
  383. addl $ab1,$nm1,$nm1
  384. ldd -16($xfer),$ab0
  385. ldd -8($xfer),$nm0
  386. extrd,u $nm1,31,32,$hi1
  387. addl $hi0,$ab0,$ab0
  388. addl $ti0,$ab0,$ab0
  389. stw $nm1,-4($tp) ; tp[j-1]
  390. extrd,u $ab0,31,32,$hi0
  391. ldw 8($tp),$ti1 ; tp[j]
  392. extrd,u $ab0,63,32,$ab0
  393. addl $hi1,$nm0,$nm0
  394. ldd 0($xfer),$ab1
  395. addl $ab0,$nm0,$nm0
  396. ldd,mb 8($xfer),$nm1
  397. extrd,u $nm0,31,32,$hi1
  398. stw,ma $nm0,8($tp) ; tp[j-1]
  399. addib,= -1,$num,L\$outerdone ; i--
  400. subi 0,$arrsz,$idx ; j=0
  401. ___
  402. $code.=<<___ if ($BN_SZ==4);
  403. fldws,ma 4($bp),${fbi} ; bp[i]
  404. ___
  405. $code.=<<___ if ($BN_SZ==8);
  406. ldi 12,$ti0 ; bp[i] in flipped word order
  407. addl,ev %r0,$num,$num
  408. ldi -4,$ti0
  409. addl $ti0,$bp,$bp
  410. fldws 0($bp),${fbi}
  411. ___
  412. $code.=<<___;
  413. flddx $idx($ap),${fai} ; ap[0]
  414. addl $hi0,$ab1,$ab1
  415. flddx $idx($np),${fni} ; np[0]
  416. fldws 8($xfer),${fti}R ; tp[0]
  417. addl $ti1,$ab1,$ab1
  418. extrd,u $ab1,31,32,$hi0
  419. extrd,u $ab1,63,32,$ab1
  420. ldo 8($idx),$idx ; j++++
  421. xmpyu ${fai}L,${fbi},${fab0} ; ap[0]*bp[i]
  422. xmpyu ${fai}R,${fbi},${fab1} ; ap[1]*bp[i]
  423. ldw 4($tp),$ti0 ; tp[j]
  424. addl $hi1,$nm1,$nm1
  425. fstws,mb ${fab0}L,-8($xfer) ; save high part
  426. addl $ab1,$nm1,$nm1
  427. extrd,u $nm1,31,32,$hi1
  428. fcpy,sgl %fr0,${fti}L ; zero high part
  429. fcpy,sgl %fr0,${fab0}L
  430. stw $nm1,-4($tp) ; tp[j-1]
  431. fcnvxf,dbl,dbl ${fti},${fti} ; 32-bit unsigned int -> double
  432. fcnvxf,dbl,dbl ${fab0},${fab0}
  433. addl $hi1,$hi0,$hi0
  434. fadd,dbl ${fti},${fab0},${fab0} ; add tp[0]
  435. addl $ti0,$hi0,$hi0
  436. extrd,u $hi0,31,32,$hi1
  437. fcnvfx,dbl,dbl ${fab0},${fab0} ; double -> 33-bit unsigned int
  438. stw $hi0,0($tp)
  439. stw $hi1,4($tp)
  440. xmpyu ${fn0},${fab0}R,${fm0}
  441. b L\$outer
  442. ldo `$LOCALS+32+4`($fp),$tp
  443. L\$outerdone
  444. addl $hi0,$ab1,$ab1
  445. addl $ti1,$ab1,$ab1
  446. extrd,u $ab1,31,32,$hi0
  447. extrd,u $ab1,63,32,$ab1
  448. ldw 4($tp),$ti0 ; tp[j]
  449. addl $hi1,$nm1,$nm1
  450. addl $ab1,$nm1,$nm1
  451. extrd,u $nm1,31,32,$hi1
  452. stw $nm1,-4($tp) ; tp[j-1]
  453. addl $hi1,$hi0,$hi0
  454. addl $ti0,$hi0,$hi0
  455. extrd,u $hi0,31,32,$hi1
  456. stw $hi0,0($tp)
  457. stw $hi1,4($tp)
  458. ldo `$LOCALS+32`($fp),$tp
  459. sub %r0,%r0,%r0 ; clear borrow
  460. ___
  461. $code.=<<___ if ($BN_SZ==4);
  462. ldws,ma 4($tp),$ti0
  463. extru,= $rp,31,3,%r0 ; is rp 64-bit aligned?
  464. b L\$sub_pa11
  465. addl $tp,$arrsz,$tp
  466. L\$sub
  467. ldwx $idx($np),$hi0
  468. subb $ti0,$hi0,$hi1
  469. ldwx $idx($tp),$ti0
  470. addib,<> 4,$idx,L\$sub
  471. stws,ma $hi1,4($rp)
  472. subb $ti0,%r0,$hi1
  473. ldo -4($tp),$tp
  474. ___
  475. $code.=<<___ if ($BN_SZ==8);
  476. ldd,ma 8($tp),$ti0
  477. L\$sub
  478. ldd $idx($np),$hi0
  479. shrpd $ti0,$ti0,32,$ti0 ; flip word order
  480. std $ti0,-8($tp) ; save flipped value
  481. sub,db $ti0,$hi0,$hi1
  482. ldd,ma 8($tp),$ti0
  483. addib,<> 8,$idx,L\$sub
  484. std,ma $hi1,8($rp)
  485. extrd,u $ti0,31,32,$ti0 ; carry in flipped word order
  486. sub,db $ti0,%r0,$hi1
  487. ldo -8($tp),$tp
  488. ___
  489. $code.=<<___;
  490. and $tp,$hi1,$ap
  491. andcm $rp,$hi1,$bp
  492. or $ap,$bp,$np
  493. sub $rp,$arrsz,$rp ; rewind rp
  494. subi 0,$arrsz,$idx
  495. ldo `$LOCALS+32`($fp),$tp
  496. L\$copy
  497. ldd $idx($np),$hi0
  498. std,ma %r0,8($tp)
  499. addib,<> 8,$idx,.-8 ; L\$copy
  500. std,ma $hi0,8($rp)
  501. ___
  502. if ($BN_SZ==4) { # PA-RISC 1.1 code-path
  503. $ablo=$ab0;
  504. $abhi=$ab1;
  505. $nmlo0=$nm0;
  506. $nmhi0=$nm1;
  507. $nmlo1="%r9";
  508. $nmhi1="%r8";
  509. $code.=<<___;
  510. b L\$done
  511. nop
  512. .ALIGN 8
  513. L\$parisc11
  514. xmpyu ${fai}L,${fbi},${fab0} ; ap[j]*bp[0]
  515. xmpyu ${fni}L,${fm0}R,${fnm0} ; np[j]*m
  516. ldw -12($xfer),$ablo
  517. ldw -16($xfer),$hi0
  518. ldw -4($xfer),$nmlo0
  519. ldw -8($xfer),$nmhi0
  520. fstds ${fab0},-16($xfer)
  521. fstds ${fnm0},-8($xfer)
  522. ldo 8($idx),$idx ; j++++
  523. add $ablo,$nmlo0,$nmlo0 ; discarded
  524. addc %r0,$nmhi0,$hi1
  525. ldw 4($xfer),$ablo
  526. ldw 0($xfer),$abhi
  527. nop
  528. L\$1st_pa11
  529. xmpyu ${fai}R,${fbi},${fab1} ; ap[j+1]*bp[0]
  530. flddx $idx($ap),${fai} ; ap[j,j+1]
  531. xmpyu ${fni}R,${fm0}R,${fnm1} ; np[j+1]*m
  532. flddx $idx($np),${fni} ; np[j,j+1]
  533. add $hi0,$ablo,$ablo
  534. ldw 12($xfer),$nmlo1
  535. addc %r0,$abhi,$hi0
  536. ldw 8($xfer),$nmhi1
  537. add $ablo,$nmlo1,$nmlo1
  538. fstds ${fab1},0($xfer)
  539. addc %r0,$nmhi1,$nmhi1
  540. fstds ${fnm1},8($xfer)
  541. add $hi1,$nmlo1,$nmlo1
  542. ldw -12($xfer),$ablo
  543. addc %r0,$nmhi1,$hi1
  544. ldw -16($xfer),$abhi
  545. xmpyu ${fai}L,${fbi},${fab0} ; ap[j]*bp[0]
  546. ldw -4($xfer),$nmlo0
  547. xmpyu ${fni}L,${fm0}R,${fnm0} ; np[j]*m
  548. ldw -8($xfer),$nmhi0
  549. add $hi0,$ablo,$ablo
  550. stw $nmlo1,-4($tp) ; tp[j-1]
  551. addc %r0,$abhi,$hi0
  552. fstds ${fab0},-16($xfer)
  553. add $ablo,$nmlo0,$nmlo0
  554. fstds ${fnm0},-8($xfer)
  555. addc %r0,$nmhi0,$nmhi0
  556. ldw 0($xfer),$abhi
  557. add $hi1,$nmlo0,$nmlo0
  558. ldw 4($xfer),$ablo
  559. stws,ma $nmlo0,8($tp) ; tp[j-1]
  560. addib,<> 8,$idx,L\$1st_pa11 ; j++++
  561. addc %r0,$nmhi0,$hi1
  562. ldw 8($xfer),$nmhi1
  563. ldw 12($xfer),$nmlo1
  564. xmpyu ${fai}R,${fbi},${fab1} ; ap[j]*bp[0]
  565. xmpyu ${fni}R,${fm0}R,${fnm1} ; np[j]*m
  566. add $hi0,$ablo,$ablo
  567. fstds ${fab1},0($xfer)
  568. addc %r0,$abhi,$hi0
  569. fstds ${fnm1},8($xfer)
  570. add $ablo,$nmlo1,$nmlo1
  571. ldw -16($xfer),$abhi
  572. addc %r0,$nmhi1,$nmhi1
  573. ldw -12($xfer),$ablo
  574. add $hi1,$nmlo1,$nmlo1
  575. ldw -8($xfer),$nmhi0
  576. addc %r0,$nmhi1,$hi1
  577. ldw -4($xfer),$nmlo0
  578. add $hi0,$ablo,$ablo
  579. stw $nmlo1,-4($tp) ; tp[j-1]
  580. addc %r0,$abhi,$hi0
  581. ldw 0($xfer),$abhi
  582. add $ablo,$nmlo0,$nmlo0
  583. ldw 4($xfer),$ablo
  584. addc %r0,$nmhi0,$nmhi0
  585. ldws,mb 8($xfer),$nmhi1
  586. add $hi1,$nmlo0,$nmlo0
  587. ldw 4($xfer),$nmlo1
  588. addc %r0,$nmhi0,$hi1
  589. stws,ma $nmlo0,8($tp) ; tp[j-1]
  590. ldo -1($num),$num ; i--
  591. subi 0,$arrsz,$idx ; j=0
  592. fldws,ma 4($bp),${fbi} ; bp[1]
  593. flddx $idx($ap),${fai} ; ap[0,1]
  594. flddx $idx($np),${fni} ; np[0,1]
  595. fldws 8($xfer),${fti}R ; tp[0]
  596. add $hi0,$ablo,$ablo
  597. addc %r0,$abhi,$hi0
  598. ldo 8($idx),$idx ; j++++
  599. xmpyu ${fai}L,${fbi},${fab0} ; ap[0]*bp[1]
  600. xmpyu ${fai}R,${fbi},${fab1} ; ap[1]*bp[1]
  601. add $hi1,$nmlo1,$nmlo1
  602. addc %r0,$nmhi1,$nmhi1
  603. add $ablo,$nmlo1,$nmlo1
  604. addc %r0,$nmhi1,$hi1
  605. fstws,mb ${fab0}L,-8($xfer) ; save high part
  606. stw $nmlo1,-4($tp) ; tp[j-1]
  607. fcpy,sgl %fr0,${fti}L ; zero high part
  608. fcpy,sgl %fr0,${fab0}L
  609. add $hi1,$hi0,$hi0
  610. addc %r0,%r0,$hi1
  611. fcnvxf,dbl,dbl ${fti},${fti} ; 32-bit unsigned int -> double
  612. fcnvxf,dbl,dbl ${fab0},${fab0}
  613. stw $hi0,0($tp)
  614. stw $hi1,4($tp)
  615. fadd,dbl ${fti},${fab0},${fab0} ; add tp[0]
  616. fcnvfx,dbl,dbl ${fab0},${fab0} ; double -> 33-bit unsigned int
  617. xmpyu ${fn0},${fab0}R,${fm0}
  618. ldo `$LOCALS+32+4`($fp),$tp
  619. L\$outer_pa11
  620. xmpyu ${fni}L,${fm0}R,${fnm0} ; np[0]*m
  621. xmpyu ${fni}R,${fm0}R,${fnm1} ; np[1]*m
  622. fstds ${fab0},-16($xfer) ; 33-bit value
  623. fstds ${fnm0},-8($xfer)
  624. flddx $idx($ap),${fai} ; ap[2,3]
  625. flddx $idx($np),${fni} ; np[2,3]
  626. ldw -16($xfer),$abhi ; carry bit actually
  627. ldo 8($idx),$idx ; j++++
  628. ldw -12($xfer),$ablo
  629. ldw -8($xfer),$nmhi0
  630. ldw -4($xfer),$nmlo0
  631. ldw 0($xfer),$hi0 ; high part
  632. xmpyu ${fai}L,${fbi},${fab0} ; ap[j]*bp[i]
  633. xmpyu ${fni}L,${fm0}R,${fnm0} ; np[j]*m
  634. fstds ${fab1},0($xfer)
  635. addl $abhi,$hi0,$hi0 ; account carry bit
  636. fstds ${fnm1},8($xfer)
  637. add $ablo,$nmlo0,$nmlo0 ; discarded
  638. ldw 0($tp),$ti1 ; tp[1]
  639. addc %r0,$nmhi0,$hi1
  640. fstds ${fab0},-16($xfer)
  641. fstds ${fnm0},-8($xfer)
  642. ldw 4($xfer),$ablo
  643. ldw 0($xfer),$abhi
  644. L\$inner_pa11
  645. xmpyu ${fai}R,${fbi},${fab1} ; ap[j+1]*bp[i]
  646. flddx $idx($ap),${fai} ; ap[j,j+1]
  647. xmpyu ${fni}R,${fm0}R,${fnm1} ; np[j+1]*m
  648. flddx $idx($np),${fni} ; np[j,j+1]
  649. add $hi0,$ablo,$ablo
  650. ldw 4($tp),$ti0 ; tp[j]
  651. addc %r0,$abhi,$abhi
  652. ldw 12($xfer),$nmlo1
  653. add $ti1,$ablo,$ablo
  654. ldw 8($xfer),$nmhi1
  655. addc %r0,$abhi,$hi0
  656. fstds ${fab1},0($xfer)
  657. add $ablo,$nmlo1,$nmlo1
  658. fstds ${fnm1},8($xfer)
  659. addc %r0,$nmhi1,$nmhi1
  660. ldw -12($xfer),$ablo
  661. add $hi1,$nmlo1,$nmlo1
  662. ldw -16($xfer),$abhi
  663. addc %r0,$nmhi1,$hi1
  664. xmpyu ${fai}L,${fbi},${fab0} ; ap[j]*bp[i]
  665. ldw 8($tp),$ti1 ; tp[j]
  666. xmpyu ${fni}L,${fm0}R,${fnm0} ; np[j]*m
  667. ldw -4($xfer),$nmlo0
  668. add $hi0,$ablo,$ablo
  669. ldw -8($xfer),$nmhi0
  670. addc %r0,$abhi,$abhi
  671. stw $nmlo1,-4($tp) ; tp[j-1]
  672. add $ti0,$ablo,$ablo
  673. fstds ${fab0},-16($xfer)
  674. addc %r0,$abhi,$hi0
  675. fstds ${fnm0},-8($xfer)
  676. add $ablo,$nmlo0,$nmlo0
  677. ldw 4($xfer),$ablo
  678. addc %r0,$nmhi0,$nmhi0
  679. ldw 0($xfer),$abhi
  680. add $hi1,$nmlo0,$nmlo0
  681. stws,ma $nmlo0,8($tp) ; tp[j-1]
  682. addib,<> 8,$idx,L\$inner_pa11 ; j++++
  683. addc %r0,$nmhi0,$hi1
  684. xmpyu ${fai}R,${fbi},${fab1} ; ap[j]*bp[i]
  685. ldw 12($xfer),$nmlo1
  686. xmpyu ${fni}R,${fm0}R,${fnm1} ; np[j]*m
  687. ldw 8($xfer),$nmhi1
  688. add $hi0,$ablo,$ablo
  689. ldw 4($tp),$ti0 ; tp[j]
  690. addc %r0,$abhi,$abhi
  691. fstds ${fab1},0($xfer)
  692. add $ti1,$ablo,$ablo
  693. fstds ${fnm1},8($xfer)
  694. addc %r0,$abhi,$hi0
  695. ldw -16($xfer),$abhi
  696. add $ablo,$nmlo1,$nmlo1
  697. ldw -12($xfer),$ablo
  698. addc %r0,$nmhi1,$nmhi1
  699. ldw -8($xfer),$nmhi0
  700. add $hi1,$nmlo1,$nmlo1
  701. ldw -4($xfer),$nmlo0
  702. addc %r0,$nmhi1,$hi1
  703. add $hi0,$ablo,$ablo
  704. stw $nmlo1,-4($tp) ; tp[j-1]
  705. addc %r0,$abhi,$abhi
  706. add $ti0,$ablo,$ablo
  707. ldw 8($tp),$ti1 ; tp[j]
  708. addc %r0,$abhi,$hi0
  709. ldw 0($xfer),$abhi
  710. add $ablo,$nmlo0,$nmlo0
  711. ldw 4($xfer),$ablo
  712. addc %r0,$nmhi0,$nmhi0
  713. ldws,mb 8($xfer),$nmhi1
  714. add $hi1,$nmlo0,$nmlo0
  715. ldw 4($xfer),$nmlo1
  716. addc %r0,$nmhi0,$hi1
  717. stws,ma $nmlo0,8($tp) ; tp[j-1]
  718. addib,= -1,$num,L\$outerdone_pa11; i--
  719. subi 0,$arrsz,$idx ; j=0
  720. fldws,ma 4($bp),${fbi} ; bp[i]
  721. flddx $idx($ap),${fai} ; ap[0]
  722. add $hi0,$ablo,$ablo
  723. addc %r0,$abhi,$abhi
  724. flddx $idx($np),${fni} ; np[0]
  725. fldws 8($xfer),${fti}R ; tp[0]
  726. add $ti1,$ablo,$ablo
  727. addc %r0,$abhi,$hi0
  728. ldo 8($idx),$idx ; j++++
  729. xmpyu ${fai}L,${fbi},${fab0} ; ap[0]*bp[i]
  730. xmpyu ${fai}R,${fbi},${fab1} ; ap[1]*bp[i]
  731. ldw 4($tp),$ti0 ; tp[j]
  732. add $hi1,$nmlo1,$nmlo1
  733. addc %r0,$nmhi1,$nmhi1
  734. fstws,mb ${fab0}L,-8($xfer) ; save high part
  735. add $ablo,$nmlo1,$nmlo1
  736. addc %r0,$nmhi1,$hi1
  737. fcpy,sgl %fr0,${fti}L ; zero high part
  738. fcpy,sgl %fr0,${fab0}L
  739. stw $nmlo1,-4($tp) ; tp[j-1]
  740. fcnvxf,dbl,dbl ${fti},${fti} ; 32-bit unsigned int -> double
  741. fcnvxf,dbl,dbl ${fab0},${fab0}
  742. add $hi1,$hi0,$hi0
  743. addc %r0,%r0,$hi1
  744. fadd,dbl ${fti},${fab0},${fab0} ; add tp[0]
  745. add $ti0,$hi0,$hi0
  746. addc %r0,$hi1,$hi1
  747. fcnvfx,dbl,dbl ${fab0},${fab0} ; double -> 33-bit unsigned int
  748. stw $hi0,0($tp)
  749. stw $hi1,4($tp)
  750. xmpyu ${fn0},${fab0}R,${fm0}
  751. b L\$outer_pa11
  752. ldo `$LOCALS+32+4`($fp),$tp
  753. L\$outerdone_pa11
  754. add $hi0,$ablo,$ablo
  755. addc %r0,$abhi,$abhi
  756. add $ti1,$ablo,$ablo
  757. addc %r0,$abhi,$hi0
  758. ldw 4($tp),$ti0 ; tp[j]
  759. add $hi1,$nmlo1,$nmlo1
  760. addc %r0,$nmhi1,$nmhi1
  761. add $ablo,$nmlo1,$nmlo1
  762. addc %r0,$nmhi1,$hi1
  763. stw $nmlo1,-4($tp) ; tp[j-1]
  764. add $hi1,$hi0,$hi0
  765. addc %r0,%r0,$hi1
  766. add $ti0,$hi0,$hi0
  767. addc %r0,$hi1,$hi1
  768. stw $hi0,0($tp)
  769. stw $hi1,4($tp)
  770. ldo `$LOCALS+32+4`($fp),$tp
  771. sub %r0,%r0,%r0 ; clear borrow
  772. ldw -4($tp),$ti0
  773. addl $tp,$arrsz,$tp
  774. L\$sub_pa11
  775. ldwx $idx($np),$hi0
  776. subb $ti0,$hi0,$hi1
  777. ldwx $idx($tp),$ti0
  778. addib,<> 4,$idx,L\$sub_pa11
  779. stws,ma $hi1,4($rp)
  780. subb $ti0,%r0,$hi1
  781. ldo -4($tp),$tp
  782. and $tp,$hi1,$ap
  783. andcm $rp,$hi1,$bp
  784. or $ap,$bp,$np
  785. sub $rp,$arrsz,$rp ; rewind rp
  786. subi 0,$arrsz,$idx
  787. ldo `$LOCALS+32`($fp),$tp
  788. L\$copy_pa11
  789. ldwx $idx($np),$hi0
  790. stws,ma %r0,4($tp)
  791. addib,<> 4,$idx,L\$copy_pa11
  792. stws,ma $hi0,4($rp)
  793. nop ; alignment
  794. L\$done
  795. ___
  796. }
  797. $code.=<<___;
  798. ldi 1,%r28 ; signal "handled"
  799. ldo $FRAME($fp),%sp ; destroy tp[num+1]
  800. $POP `-$FRAME-$SAVED_RP`(%sp),%r2 ; standard epilogue
  801. $POP `-$FRAME+1*$SIZE_T`(%sp),%r4
  802. $POP `-$FRAME+2*$SIZE_T`(%sp),%r5
  803. $POP `-$FRAME+3*$SIZE_T`(%sp),%r6
  804. $POP `-$FRAME+4*$SIZE_T`(%sp),%r7
  805. $POP `-$FRAME+5*$SIZE_T`(%sp),%r8
  806. $POP `-$FRAME+6*$SIZE_T`(%sp),%r9
  807. $POP `-$FRAME+7*$SIZE_T`(%sp),%r10
  808. L\$abort
  809. bv (%r2)
  810. .EXIT
  811. $POPMB -$FRAME(%sp),%r3
  812. .PROCEND
  813. .STRINGZ "Montgomery Multiplication for PA-RISC, CRYPTOGAMS by <appro\@openssl.org>"
  814. ___
  815. # Explicitly encode PA-RISC 2.0 instructions used in this module, so
  816. # that it can be compiled with .LEVEL 1.0. It should be noted that I
  817. # wouldn't have to do this, if GNU assembler understood .ALLOW 2.0
  818. # directive...
  819. my $ldd = sub {
  820. my ($mod,$args) = @_;
  821. my $orig = "ldd$mod\t$args";
  822. if ($args =~ /%r([0-9]+)\(%r([0-9]+)\),%r([0-9]+)/) # format 4
  823. { my $opcode=(0x03<<26)|($2<<21)|($1<<16)|(3<<6)|$3;
  824. sprintf "\t.WORD\t0x%08x\t; %s",$opcode,$orig;
  825. }
  826. elsif ($args =~ /(\-?[0-9]+)\(%r([0-9]+)\),%r([0-9]+)/) # format 5
  827. { my $opcode=(0x03<<26)|($2<<21)|(1<<12)|(3<<6)|$3;
  828. $opcode|=(($1&0xF)<<17)|(($1&0x10)<<12); # encode offset
  829. $opcode|=(1<<5) if ($mod =~ /^,m/);
  830. $opcode|=(1<<13) if ($mod =~ /^,mb/);
  831. sprintf "\t.WORD\t0x%08x\t; %s",$opcode,$orig;
  832. }
  833. else { "\t".$orig; }
  834. };
  835. my $std = sub {
  836. my ($mod,$args) = @_;
  837. my $orig = "std$mod\t$args";
  838. if ($args =~ /%r([0-9]+),(\-?[0-9]+)\(%r([0-9]+)\)/) # format 6
  839. { my $opcode=(0x03<<26)|($3<<21)|($1<<16)|(1<<12)|(0xB<<6);
  840. $opcode|=(($2&0xF)<<1)|(($2&0x10)>>4); # encode offset
  841. $opcode|=(1<<5) if ($mod =~ /^,m/);
  842. $opcode|=(1<<13) if ($mod =~ /^,mb/);
  843. sprintf "\t.WORD\t0x%08x\t; %s",$opcode,$orig;
  844. }
  845. else { "\t".$orig; }
  846. };
  847. my $extrd = sub {
  848. my ($mod,$args) = @_;
  849. my $orig = "extrd$mod\t$args";
  850. # I only have ",u" completer, it's implicitly encoded...
  851. if ($args =~ /%r([0-9]+),([0-9]+),([0-9]+),%r([0-9]+)/) # format 15
  852. { my $opcode=(0x36<<26)|($1<<21)|($4<<16);
  853. my $len=32-$3;
  854. $opcode |= (($2&0x20)<<6)|(($2&0x1f)<<5); # encode pos
  855. $opcode |= (($len&0x20)<<7)|($len&0x1f); # encode len
  856. sprintf "\t.WORD\t0x%08x\t; %s",$opcode,$orig;
  857. }
  858. elsif ($args =~ /%r([0-9]+),%sar,([0-9]+),%r([0-9]+)/) # format 12
  859. { my $opcode=(0x34<<26)|($1<<21)|($3<<16)|(2<<11)|(1<<9);
  860. my $len=32-$2;
  861. $opcode |= (($len&0x20)<<3)|($len&0x1f); # encode len
  862. $opcode |= (1<<13) if ($mod =~ /,\**=/);
  863. sprintf "\t.WORD\t0x%08x\t; %s",$opcode,$orig;
  864. }
  865. else { "\t".$orig; }
  866. };
  867. my $shrpd = sub {
  868. my ($mod,$args) = @_;
  869. my $orig = "shrpd$mod\t$args";
  870. if ($args =~ /%r([0-9]+),%r([0-9]+),([0-9]+),%r([0-9]+)/) # format 14
  871. { my $opcode=(0x34<<26)|($2<<21)|($1<<16)|(1<<10)|$4;
  872. my $cpos=63-$3;
  873. $opcode |= (($cpos&0x20)<<6)|(($cpos&0x1f)<<5); # encode sa
  874. sprintf "\t.WORD\t0x%08x\t; %s",$opcode,$orig;
  875. }
  876. else { "\t".$orig; }
  877. };
  878. my $sub = sub {
  879. my ($mod,$args) = @_;
  880. my $orig = "sub$mod\t$args";
  881. if ($mod eq ",db" && $args =~ /%r([0-9]+),%r([0-9]+),%r([0-9]+)/) {
  882. my $opcode=(0x02<<26)|($2<<21)|($1<<16)|$3;
  883. $opcode|=(1<<10); # e1
  884. $opcode|=(1<<8); # e2
  885. $opcode|=(1<<5); # d
  886. sprintf "\t.WORD\t0x%08x\t; %s",$opcode,$orig
  887. }
  888. else { "\t".$orig; }
  889. };
  890. sub assemble {
  891. my ($mnemonic,$mod,$args)=@_;
  892. my $opcode = eval("\$$mnemonic");
  893. ref($opcode) eq 'CODE' ? &$opcode($mod,$args) : "\t$mnemonic$mod\t$args";
  894. }
  895. foreach (split("\n",$code)) {
  896. s/\`([^\`]*)\`/eval $1/ge;
  897. # flip word order in 64-bit mode...
  898. s/(xmpyu\s+)($fai|$fni)([LR])/$1.$2.($3 eq "L"?"R":"L")/e if ($BN_SZ==8);
  899. # assemble 2.0 instructions in 32-bit mode...
  900. s/^\s+([a-z]+)([\S]*)\s+([\S]*)/&assemble($1,$2,$3)/e if ($BN_SZ==4);
  901. s/\bbv\b/bve/gm if ($SIZE_T==8);
  902. print $_,"\n";
  903. }
  904. close STDOUT;