armv4-mont.pl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. #! /usr/bin/env perl
  2. # Copyright 2007-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@openssl.org> 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. # January 2007.
  15. # Montgomery multiplication for ARMv4.
  16. #
  17. # Performance improvement naturally varies among CPU implementations
  18. # and compilers. The code was observed to provide +65-35% improvement
  19. # [depending on key length, less for longer keys] on ARM920T, and
  20. # +115-80% on Intel IXP425. This is compared to pre-bn_mul_mont code
  21. # base and compiler generated code with in-lined umull and even umlal
  22. # instructions. The latter means that this code didn't really have an
  23. # "advantage" of utilizing some "secret" instruction.
  24. #
  25. # The code is interoperable with Thumb ISA and is rather compact, less
  26. # than 1/2KB. Windows CE port would be trivial, as it's exclusively
  27. # about decorations, ABI and instruction syntax are identical.
  28. # November 2013
  29. #
  30. # Add NEON code path, which handles lengths divisible by 8. RSA/DSA
  31. # performance improvement on Cortex-A8 is ~45-100% depending on key
  32. # length, more for longer keys. On Cortex-A15 the span is ~10-105%.
  33. # On Snapdragon S4 improvement was measured to vary from ~70% to
  34. # incredible ~380%, yes, 4.8x faster, for RSA4096 sign. But this is
  35. # rather because original integer-only code seems to perform
  36. # suboptimally on S4. Situation on Cortex-A9 is unfortunately
  37. # different. It's being looked into, but the trouble is that
  38. # performance for vectors longer than 256 bits is actually couple
  39. # of percent worse than for integer-only code. The code is chosen
  40. # for execution on all NEON-capable processors, because gain on
  41. # others outweighs the marginal loss on Cortex-A9.
  42. # September 2015
  43. #
  44. # Align Cortex-A9 performance with November 2013 improvements, i.e.
  45. # NEON code is now ~20-105% faster than integer-only one on this
  46. # processor. But this optimization further improved performance even
  47. # on other processors: NEON code path is ~45-180% faster than original
  48. # integer-only on Cortex-A8, ~10-210% on Cortex-A15, ~70-450% on
  49. # Snapdragon S4.
  50. $flavour = shift;
  51. if ($flavour=~/\w[\w\-]*\.\w+$/) { $output=$flavour; undef $flavour; }
  52. else { while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {} }
  53. if ($flavour && $flavour ne "void") {
  54. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  55. ( $xlate="${dir}arm-xlate.pl" and -f $xlate ) or
  56. ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
  57. die "can't locate arm-xlate.pl";
  58. open STDOUT,"| \"$^X\" $xlate $flavour $output";
  59. } else {
  60. open STDOUT,">$output";
  61. }
  62. $num="r0"; # starts as num argument, but holds &tp[num-1]
  63. $ap="r1";
  64. $bp="r2"; $bi="r2"; $rp="r2";
  65. $np="r3";
  66. $tp="r4";
  67. $aj="r5";
  68. $nj="r6";
  69. $tj="r7";
  70. $n0="r8";
  71. ########### # r9 is reserved by ELF as platform specific, e.g. TLS pointer
  72. $alo="r10"; # sl, gcc uses it to keep @GOT
  73. $ahi="r11"; # fp
  74. $nlo="r12"; # ip
  75. ########### # r13 is stack pointer
  76. $nhi="r14"; # lr
  77. ########### # r15 is program counter
  78. #### argument block layout relative to &tp[num-1], a.k.a. $num
  79. $_rp="$num,#12*4";
  80. # ap permanently resides in r1
  81. $_bp="$num,#13*4";
  82. # np permanently resides in r3
  83. $_n0="$num,#14*4";
  84. $_num="$num,#15*4"; $_bpend=$_num;
  85. $code=<<___;
  86. #include "arm_arch.h"
  87. .text
  88. #if defined(__thumb2__)
  89. .syntax unified
  90. .thumb
  91. #else
  92. .code 32
  93. #endif
  94. #if __ARM_MAX_ARCH__>=7
  95. .align 5
  96. .LOPENSSL_armcap:
  97. .word OPENSSL_armcap_P-.Lbn_mul_mont
  98. #endif
  99. .global bn_mul_mont
  100. .type bn_mul_mont,%function
  101. .align 5
  102. bn_mul_mont:
  103. .Lbn_mul_mont:
  104. ldr ip,[sp,#4] @ load num
  105. stmdb sp!,{r0,r2} @ sp points at argument block
  106. #if __ARM_MAX_ARCH__>=7
  107. tst ip,#7
  108. bne .Lialu
  109. adr r0,.Lbn_mul_mont
  110. ldr r2,.LOPENSSL_armcap
  111. ldr r0,[r0,r2]
  112. #ifdef __APPLE__
  113. ldr r0,[r0]
  114. #endif
  115. tst r0,#ARMV7_NEON @ NEON available?
  116. ldmia sp, {r0,r2}
  117. beq .Lialu
  118. add sp,sp,#8
  119. b bn_mul8x_mont_neon
  120. .align 4
  121. .Lialu:
  122. #endif
  123. cmp ip,#2
  124. mov $num,ip @ load num
  125. #ifdef __thumb2__
  126. ittt lt
  127. #endif
  128. movlt r0,#0
  129. addlt sp,sp,#2*4
  130. blt .Labrt
  131. stmdb sp!,{r4-r12,lr} @ save 10 registers
  132. mov $num,$num,lsl#2 @ rescale $num for byte count
  133. sub sp,sp,$num @ alloca(4*num)
  134. sub sp,sp,#4 @ +extra dword
  135. sub $num,$num,#4 @ "num=num-1"
  136. add $tp,$bp,$num @ &bp[num-1]
  137. add $num,sp,$num @ $num to point at &tp[num-1]
  138. ldr $n0,[$_n0] @ &n0
  139. ldr $bi,[$bp] @ bp[0]
  140. ldr $aj,[$ap],#4 @ ap[0],ap++
  141. ldr $nj,[$np],#4 @ np[0],np++
  142. ldr $n0,[$n0] @ *n0
  143. str $tp,[$_bpend] @ save &bp[num]
  144. umull $alo,$ahi,$aj,$bi @ ap[0]*bp[0]
  145. str $n0,[$_n0] @ save n0 value
  146. mul $n0,$alo,$n0 @ "tp[0]"*n0
  147. mov $nlo,#0
  148. umlal $alo,$nlo,$nj,$n0 @ np[0]*n0+"t[0]"
  149. mov $tp,sp
  150. .L1st:
  151. ldr $aj,[$ap],#4 @ ap[j],ap++
  152. mov $alo,$ahi
  153. ldr $nj,[$np],#4 @ np[j],np++
  154. mov $ahi,#0
  155. umlal $alo,$ahi,$aj,$bi @ ap[j]*bp[0]
  156. mov $nhi,#0
  157. umlal $nlo,$nhi,$nj,$n0 @ np[j]*n0
  158. adds $nlo,$nlo,$alo
  159. str $nlo,[$tp],#4 @ tp[j-1]=,tp++
  160. adc $nlo,$nhi,#0
  161. cmp $tp,$num
  162. bne .L1st
  163. adds $nlo,$nlo,$ahi
  164. ldr $tp,[$_bp] @ restore bp
  165. mov $nhi,#0
  166. ldr $n0,[$_n0] @ restore n0
  167. adc $nhi,$nhi,#0
  168. str $nlo,[$num] @ tp[num-1]=
  169. mov $tj,sp
  170. str $nhi,[$num,#4] @ tp[num]=
  171. .Louter:
  172. sub $tj,$num,$tj @ "original" $num-1 value
  173. sub $ap,$ap,$tj @ "rewind" ap to &ap[1]
  174. ldr $bi,[$tp,#4]! @ *(++bp)
  175. sub $np,$np,$tj @ "rewind" np to &np[1]
  176. ldr $aj,[$ap,#-4] @ ap[0]
  177. ldr $alo,[sp] @ tp[0]
  178. ldr $nj,[$np,#-4] @ np[0]
  179. ldr $tj,[sp,#4] @ tp[1]
  180. mov $ahi,#0
  181. umlal $alo,$ahi,$aj,$bi @ ap[0]*bp[i]+tp[0]
  182. str $tp,[$_bp] @ save bp
  183. mul $n0,$alo,$n0
  184. mov $nlo,#0
  185. umlal $alo,$nlo,$nj,$n0 @ np[0]*n0+"tp[0]"
  186. mov $tp,sp
  187. .Linner:
  188. ldr $aj,[$ap],#4 @ ap[j],ap++
  189. adds $alo,$ahi,$tj @ +=tp[j]
  190. ldr $nj,[$np],#4 @ np[j],np++
  191. mov $ahi,#0
  192. umlal $alo,$ahi,$aj,$bi @ ap[j]*bp[i]
  193. mov $nhi,#0
  194. umlal $nlo,$nhi,$nj,$n0 @ np[j]*n0
  195. adc $ahi,$ahi,#0
  196. ldr $tj,[$tp,#8] @ tp[j+1]
  197. adds $nlo,$nlo,$alo
  198. str $nlo,[$tp],#4 @ tp[j-1]=,tp++
  199. adc $nlo,$nhi,#0
  200. cmp $tp,$num
  201. bne .Linner
  202. adds $nlo,$nlo,$ahi
  203. mov $nhi,#0
  204. ldr $tp,[$_bp] @ restore bp
  205. adc $nhi,$nhi,#0
  206. ldr $n0,[$_n0] @ restore n0
  207. adds $nlo,$nlo,$tj
  208. ldr $tj,[$_bpend] @ restore &bp[num]
  209. adc $nhi,$nhi,#0
  210. str $nlo,[$num] @ tp[num-1]=
  211. str $nhi,[$num,#4] @ tp[num]=
  212. cmp $tp,$tj
  213. #ifdef __thumb2__
  214. itt ne
  215. #endif
  216. movne $tj,sp
  217. bne .Louter
  218. ldr $rp,[$_rp] @ pull rp
  219. mov $aj,sp
  220. add $num,$num,#4 @ $num to point at &tp[num]
  221. sub $aj,$num,$aj @ "original" num value
  222. mov $tp,sp @ "rewind" $tp
  223. mov $ap,$tp @ "borrow" $ap
  224. sub $np,$np,$aj @ "rewind" $np to &np[0]
  225. subs $tj,$tj,$tj @ "clear" carry flag
  226. .Lsub: ldr $tj,[$tp],#4
  227. ldr $nj,[$np],#4
  228. sbcs $tj,$tj,$nj @ tp[j]-np[j]
  229. str $tj,[$rp],#4 @ rp[j]=
  230. teq $tp,$num @ preserve carry
  231. bne .Lsub
  232. sbcs $nhi,$nhi,#0 @ upmost carry
  233. mov $tp,sp @ "rewind" $tp
  234. sub $rp,$rp,$aj @ "rewind" $rp
  235. and $ap,$tp,$nhi
  236. bic $np,$rp,$nhi
  237. orr $ap,$ap,$np @ ap=borrow?tp:rp
  238. .Lcopy: ldr $tj,[$ap],#4 @ copy or in-place refresh
  239. str sp,[$tp],#4 @ zap tp
  240. str $tj,[$rp],#4
  241. cmp $tp,$num
  242. bne .Lcopy
  243. mov sp,$num
  244. add sp,sp,#4 @ skip over tp[num+1]
  245. ldmia sp!,{r4-r12,lr} @ restore registers
  246. add sp,sp,#2*4 @ skip over {r0,r2}
  247. mov r0,#1
  248. .Labrt:
  249. #if __ARM_ARCH__>=5
  250. ret @ bx lr
  251. #else
  252. tst lr,#1
  253. moveq pc,lr @ be binary compatible with V4, yet
  254. bx lr @ interoperable with Thumb ISA:-)
  255. #endif
  256. .size bn_mul_mont,.-bn_mul_mont
  257. ___
  258. {
  259. my ($A0,$A1,$A2,$A3)=map("d$_",(0..3));
  260. my ($N0,$N1,$N2,$N3)=map("d$_",(4..7));
  261. my ($Z,$Temp)=("q4","q5");
  262. my @ACC=map("q$_",(6..13));
  263. my ($Bi,$Ni,$M0)=map("d$_",(28..31));
  264. my $zero="$Z#lo";
  265. my $temp="$Temp#lo";
  266. my ($rptr,$aptr,$bptr,$nptr,$n0,$num)=map("r$_",(0..5));
  267. my ($tinptr,$toutptr,$inner,$outer,$bnptr)=map("r$_",(6..11));
  268. $code.=<<___;
  269. #if __ARM_MAX_ARCH__>=7
  270. .arch armv7-a
  271. .fpu neon
  272. .type bn_mul8x_mont_neon,%function
  273. .align 5
  274. bn_mul8x_mont_neon:
  275. mov ip,sp
  276. stmdb sp!,{r4-r11}
  277. vstmdb sp!,{d8-d15} @ ABI specification says so
  278. ldmia ip,{r4-r5} @ load rest of parameter block
  279. mov ip,sp
  280. cmp $num,#8
  281. bhi .LNEON_8n
  282. @ special case for $num==8, everything is in register bank...
  283. vld1.32 {${Bi}[0]}, [$bptr,:32]!
  284. veor $zero,$zero,$zero
  285. sub $toutptr,sp,$num,lsl#4
  286. vld1.32 {$A0-$A3}, [$aptr]! @ can't specify :32 :-(
  287. and $toutptr,$toutptr,#-64
  288. vld1.32 {${M0}[0]}, [$n0,:32]
  289. mov sp,$toutptr @ alloca
  290. vzip.16 $Bi,$zero
  291. vmull.u32 @ACC[0],$Bi,${A0}[0]
  292. vmull.u32 @ACC[1],$Bi,${A0}[1]
  293. vmull.u32 @ACC[2],$Bi,${A1}[0]
  294. vshl.i64 $Ni,@ACC[0]#hi,#16
  295. vmull.u32 @ACC[3],$Bi,${A1}[1]
  296. vadd.u64 $Ni,$Ni,@ACC[0]#lo
  297. veor $zero,$zero,$zero
  298. vmul.u32 $Ni,$Ni,$M0
  299. vmull.u32 @ACC[4],$Bi,${A2}[0]
  300. vld1.32 {$N0-$N3}, [$nptr]!
  301. vmull.u32 @ACC[5],$Bi,${A2}[1]
  302. vmull.u32 @ACC[6],$Bi,${A3}[0]
  303. vzip.16 $Ni,$zero
  304. vmull.u32 @ACC[7],$Bi,${A3}[1]
  305. vmlal.u32 @ACC[0],$Ni,${N0}[0]
  306. sub $outer,$num,#1
  307. vmlal.u32 @ACC[1],$Ni,${N0}[1]
  308. vmlal.u32 @ACC[2],$Ni,${N1}[0]
  309. vmlal.u32 @ACC[3],$Ni,${N1}[1]
  310. vmlal.u32 @ACC[4],$Ni,${N2}[0]
  311. vmov $Temp,@ACC[0]
  312. vmlal.u32 @ACC[5],$Ni,${N2}[1]
  313. vmov @ACC[0],@ACC[1]
  314. vmlal.u32 @ACC[6],$Ni,${N3}[0]
  315. vmov @ACC[1],@ACC[2]
  316. vmlal.u32 @ACC[7],$Ni,${N3}[1]
  317. vmov @ACC[2],@ACC[3]
  318. vmov @ACC[3],@ACC[4]
  319. vshr.u64 $temp,$temp,#16
  320. vmov @ACC[4],@ACC[5]
  321. vmov @ACC[5],@ACC[6]
  322. vadd.u64 $temp,$temp,$Temp#hi
  323. vmov @ACC[6],@ACC[7]
  324. veor @ACC[7],@ACC[7]
  325. vshr.u64 $temp,$temp,#16
  326. b .LNEON_outer8
  327. .align 4
  328. .LNEON_outer8:
  329. vld1.32 {${Bi}[0]}, [$bptr,:32]!
  330. veor $zero,$zero,$zero
  331. vzip.16 $Bi,$zero
  332. vadd.u64 @ACC[0]#lo,@ACC[0]#lo,$temp
  333. vmlal.u32 @ACC[0],$Bi,${A0}[0]
  334. vmlal.u32 @ACC[1],$Bi,${A0}[1]
  335. vmlal.u32 @ACC[2],$Bi,${A1}[0]
  336. vshl.i64 $Ni,@ACC[0]#hi,#16
  337. vmlal.u32 @ACC[3],$Bi,${A1}[1]
  338. vadd.u64 $Ni,$Ni,@ACC[0]#lo
  339. veor $zero,$zero,$zero
  340. subs $outer,$outer,#1
  341. vmul.u32 $Ni,$Ni,$M0
  342. vmlal.u32 @ACC[4],$Bi,${A2}[0]
  343. vmlal.u32 @ACC[5],$Bi,${A2}[1]
  344. vmlal.u32 @ACC[6],$Bi,${A3}[0]
  345. vzip.16 $Ni,$zero
  346. vmlal.u32 @ACC[7],$Bi,${A3}[1]
  347. vmlal.u32 @ACC[0],$Ni,${N0}[0]
  348. vmlal.u32 @ACC[1],$Ni,${N0}[1]
  349. vmlal.u32 @ACC[2],$Ni,${N1}[0]
  350. vmlal.u32 @ACC[3],$Ni,${N1}[1]
  351. vmlal.u32 @ACC[4],$Ni,${N2}[0]
  352. vmov $Temp,@ACC[0]
  353. vmlal.u32 @ACC[5],$Ni,${N2}[1]
  354. vmov @ACC[0],@ACC[1]
  355. vmlal.u32 @ACC[6],$Ni,${N3}[0]
  356. vmov @ACC[1],@ACC[2]
  357. vmlal.u32 @ACC[7],$Ni,${N3}[1]
  358. vmov @ACC[2],@ACC[3]
  359. vmov @ACC[3],@ACC[4]
  360. vshr.u64 $temp,$temp,#16
  361. vmov @ACC[4],@ACC[5]
  362. vmov @ACC[5],@ACC[6]
  363. vadd.u64 $temp,$temp,$Temp#hi
  364. vmov @ACC[6],@ACC[7]
  365. veor @ACC[7],@ACC[7]
  366. vshr.u64 $temp,$temp,#16
  367. bne .LNEON_outer8
  368. vadd.u64 @ACC[0]#lo,@ACC[0]#lo,$temp
  369. mov $toutptr,sp
  370. vshr.u64 $temp,@ACC[0]#lo,#16
  371. mov $inner,$num
  372. vadd.u64 @ACC[0]#hi,@ACC[0]#hi,$temp
  373. add $tinptr,sp,#96
  374. vshr.u64 $temp,@ACC[0]#hi,#16
  375. vzip.16 @ACC[0]#lo,@ACC[0]#hi
  376. b .LNEON_tail_entry
  377. .align 4
  378. .LNEON_8n:
  379. veor @ACC[0],@ACC[0],@ACC[0]
  380. sub $toutptr,sp,#128
  381. veor @ACC[1],@ACC[1],@ACC[1]
  382. sub $toutptr,$toutptr,$num,lsl#4
  383. veor @ACC[2],@ACC[2],@ACC[2]
  384. and $toutptr,$toutptr,#-64
  385. veor @ACC[3],@ACC[3],@ACC[3]
  386. mov sp,$toutptr @ alloca
  387. veor @ACC[4],@ACC[4],@ACC[4]
  388. add $toutptr,$toutptr,#256
  389. veor @ACC[5],@ACC[5],@ACC[5]
  390. sub $inner,$num,#8
  391. veor @ACC[6],@ACC[6],@ACC[6]
  392. veor @ACC[7],@ACC[7],@ACC[7]
  393. .LNEON_8n_init:
  394. vst1.64 {@ACC[0]-@ACC[1]},[$toutptr,:256]!
  395. subs $inner,$inner,#8
  396. vst1.64 {@ACC[2]-@ACC[3]},[$toutptr,:256]!
  397. vst1.64 {@ACC[4]-@ACC[5]},[$toutptr,:256]!
  398. vst1.64 {@ACC[6]-@ACC[7]},[$toutptr,:256]!
  399. bne .LNEON_8n_init
  400. add $tinptr,sp,#256
  401. vld1.32 {$A0-$A3},[$aptr]!
  402. add $bnptr,sp,#8
  403. vld1.32 {${M0}[0]},[$n0,:32]
  404. mov $outer,$num
  405. b .LNEON_8n_outer
  406. .align 4
  407. .LNEON_8n_outer:
  408. vld1.32 {${Bi}[0]},[$bptr,:32]! @ *b++
  409. veor $zero,$zero,$zero
  410. vzip.16 $Bi,$zero
  411. add $toutptr,sp,#128
  412. vld1.32 {$N0-$N3},[$nptr]!
  413. vmlal.u32 @ACC[0],$Bi,${A0}[0]
  414. vmlal.u32 @ACC[1],$Bi,${A0}[1]
  415. veor $zero,$zero,$zero
  416. vmlal.u32 @ACC[2],$Bi,${A1}[0]
  417. vshl.i64 $Ni,@ACC[0]#hi,#16
  418. vmlal.u32 @ACC[3],$Bi,${A1}[1]
  419. vadd.u64 $Ni,$Ni,@ACC[0]#lo
  420. vmlal.u32 @ACC[4],$Bi,${A2}[0]
  421. vmul.u32 $Ni,$Ni,$M0
  422. vmlal.u32 @ACC[5],$Bi,${A2}[1]
  423. vst1.32 {$Bi},[sp,:64] @ put aside smashed b[8*i+0]
  424. vmlal.u32 @ACC[6],$Bi,${A3}[0]
  425. vzip.16 $Ni,$zero
  426. vmlal.u32 @ACC[7],$Bi,${A3}[1]
  427. ___
  428. for ($i=0; $i<7;) {
  429. $code.=<<___;
  430. vld1.32 {${Bi}[0]},[$bptr,:32]! @ *b++
  431. vmlal.u32 @ACC[0],$Ni,${N0}[0]
  432. veor $temp,$temp,$temp
  433. vmlal.u32 @ACC[1],$Ni,${N0}[1]
  434. vzip.16 $Bi,$temp
  435. vmlal.u32 @ACC[2],$Ni,${N1}[0]
  436. vshr.u64 @ACC[0]#lo,@ACC[0]#lo,#16
  437. vmlal.u32 @ACC[3],$Ni,${N1}[1]
  438. vmlal.u32 @ACC[4],$Ni,${N2}[0]
  439. vadd.u64 @ACC[0]#lo,@ACC[0]#lo,@ACC[0]#hi
  440. vmlal.u32 @ACC[5],$Ni,${N2}[1]
  441. vshr.u64 @ACC[0]#lo,@ACC[0]#lo,#16
  442. vmlal.u32 @ACC[6],$Ni,${N3}[0]
  443. vmlal.u32 @ACC[7],$Ni,${N3}[1]
  444. vadd.u64 @ACC[1]#lo,@ACC[1]#lo,@ACC[0]#lo
  445. vst1.32 {$Ni},[$bnptr,:64]! @ put aside smashed m[8*i+$i]
  446. ___
  447. push(@ACC,shift(@ACC)); $i++;
  448. $code.=<<___;
  449. vmlal.u32 @ACC[0],$Bi,${A0}[0]
  450. vld1.64 {@ACC[7]},[$tinptr,:128]!
  451. vmlal.u32 @ACC[1],$Bi,${A0}[1]
  452. veor $zero,$zero,$zero
  453. vmlal.u32 @ACC[2],$Bi,${A1}[0]
  454. vshl.i64 $Ni,@ACC[0]#hi,#16
  455. vmlal.u32 @ACC[3],$Bi,${A1}[1]
  456. vadd.u64 $Ni,$Ni,@ACC[0]#lo
  457. vmlal.u32 @ACC[4],$Bi,${A2}[0]
  458. vmul.u32 $Ni,$Ni,$M0
  459. vmlal.u32 @ACC[5],$Bi,${A2}[1]
  460. vst1.32 {$Bi},[$bnptr,:64]! @ put aside smashed b[8*i+$i]
  461. vmlal.u32 @ACC[6],$Bi,${A3}[0]
  462. vzip.16 $Ni,$zero
  463. vmlal.u32 @ACC[7],$Bi,${A3}[1]
  464. ___
  465. }
  466. $code.=<<___;
  467. vld1.32 {$Bi},[sp,:64] @ pull smashed b[8*i+0]
  468. vmlal.u32 @ACC[0],$Ni,${N0}[0]
  469. vld1.32 {$A0-$A3},[$aptr]!
  470. vmlal.u32 @ACC[1],$Ni,${N0}[1]
  471. vmlal.u32 @ACC[2],$Ni,${N1}[0]
  472. vshr.u64 @ACC[0]#lo,@ACC[0]#lo,#16
  473. vmlal.u32 @ACC[3],$Ni,${N1}[1]
  474. vmlal.u32 @ACC[4],$Ni,${N2}[0]
  475. vadd.u64 @ACC[0]#lo,@ACC[0]#lo,@ACC[0]#hi
  476. vmlal.u32 @ACC[5],$Ni,${N2}[1]
  477. vshr.u64 @ACC[0]#lo,@ACC[0]#lo,#16
  478. vmlal.u32 @ACC[6],$Ni,${N3}[0]
  479. vmlal.u32 @ACC[7],$Ni,${N3}[1]
  480. vadd.u64 @ACC[1]#lo,@ACC[1]#lo,@ACC[0]#lo
  481. vst1.32 {$Ni},[$bnptr,:64] @ put aside smashed m[8*i+$i]
  482. add $bnptr,sp,#8 @ rewind
  483. ___
  484. push(@ACC,shift(@ACC));
  485. $code.=<<___;
  486. sub $inner,$num,#8
  487. b .LNEON_8n_inner
  488. .align 4
  489. .LNEON_8n_inner:
  490. subs $inner,$inner,#8
  491. vmlal.u32 @ACC[0],$Bi,${A0}[0]
  492. vld1.64 {@ACC[7]},[$tinptr,:128]
  493. vmlal.u32 @ACC[1],$Bi,${A0}[1]
  494. vld1.32 {$Ni},[$bnptr,:64]! @ pull smashed m[8*i+0]
  495. vmlal.u32 @ACC[2],$Bi,${A1}[0]
  496. vld1.32 {$N0-$N3},[$nptr]!
  497. vmlal.u32 @ACC[3],$Bi,${A1}[1]
  498. it ne
  499. addne $tinptr,$tinptr,#16 @ don't advance in last iteration
  500. vmlal.u32 @ACC[4],$Bi,${A2}[0]
  501. vmlal.u32 @ACC[5],$Bi,${A2}[1]
  502. vmlal.u32 @ACC[6],$Bi,${A3}[0]
  503. vmlal.u32 @ACC[7],$Bi,${A3}[1]
  504. ___
  505. for ($i=1; $i<8; $i++) {
  506. $code.=<<___;
  507. vld1.32 {$Bi},[$bnptr,:64]! @ pull smashed b[8*i+$i]
  508. vmlal.u32 @ACC[0],$Ni,${N0}[0]
  509. vmlal.u32 @ACC[1],$Ni,${N0}[1]
  510. vmlal.u32 @ACC[2],$Ni,${N1}[0]
  511. vmlal.u32 @ACC[3],$Ni,${N1}[1]
  512. vmlal.u32 @ACC[4],$Ni,${N2}[0]
  513. vmlal.u32 @ACC[5],$Ni,${N2}[1]
  514. vmlal.u32 @ACC[6],$Ni,${N3}[0]
  515. vmlal.u32 @ACC[7],$Ni,${N3}[1]
  516. vst1.64 {@ACC[0]},[$toutptr,:128]!
  517. ___
  518. push(@ACC,shift(@ACC));
  519. $code.=<<___;
  520. vmlal.u32 @ACC[0],$Bi,${A0}[0]
  521. vld1.64 {@ACC[7]},[$tinptr,:128]
  522. vmlal.u32 @ACC[1],$Bi,${A0}[1]
  523. vld1.32 {$Ni},[$bnptr,:64]! @ pull smashed m[8*i+$i]
  524. vmlal.u32 @ACC[2],$Bi,${A1}[0]
  525. it ne
  526. addne $tinptr,$tinptr,#16 @ don't advance in last iteration
  527. vmlal.u32 @ACC[3],$Bi,${A1}[1]
  528. vmlal.u32 @ACC[4],$Bi,${A2}[0]
  529. vmlal.u32 @ACC[5],$Bi,${A2}[1]
  530. vmlal.u32 @ACC[6],$Bi,${A3}[0]
  531. vmlal.u32 @ACC[7],$Bi,${A3}[1]
  532. ___
  533. }
  534. $code.=<<___;
  535. it eq
  536. subeq $aptr,$aptr,$num,lsl#2 @ rewind
  537. vmlal.u32 @ACC[0],$Ni,${N0}[0]
  538. vld1.32 {$Bi},[sp,:64] @ pull smashed b[8*i+0]
  539. vmlal.u32 @ACC[1],$Ni,${N0}[1]
  540. vld1.32 {$A0-$A3},[$aptr]!
  541. vmlal.u32 @ACC[2],$Ni,${N1}[0]
  542. add $bnptr,sp,#8 @ rewind
  543. vmlal.u32 @ACC[3],$Ni,${N1}[1]
  544. vmlal.u32 @ACC[4],$Ni,${N2}[0]
  545. vmlal.u32 @ACC[5],$Ni,${N2}[1]
  546. vmlal.u32 @ACC[6],$Ni,${N3}[0]
  547. vst1.64 {@ACC[0]},[$toutptr,:128]!
  548. vmlal.u32 @ACC[7],$Ni,${N3}[1]
  549. bne .LNEON_8n_inner
  550. ___
  551. push(@ACC,shift(@ACC));
  552. $code.=<<___;
  553. add $tinptr,sp,#128
  554. vst1.64 {@ACC[0]-@ACC[1]},[$toutptr,:256]!
  555. veor q2,q2,q2 @ $N0-$N1
  556. vst1.64 {@ACC[2]-@ACC[3]},[$toutptr,:256]!
  557. veor q3,q3,q3 @ $N2-$N3
  558. vst1.64 {@ACC[4]-@ACC[5]},[$toutptr,:256]!
  559. vst1.64 {@ACC[6]},[$toutptr,:128]
  560. subs $outer,$outer,#8
  561. vld1.64 {@ACC[0]-@ACC[1]},[$tinptr,:256]!
  562. vld1.64 {@ACC[2]-@ACC[3]},[$tinptr,:256]!
  563. vld1.64 {@ACC[4]-@ACC[5]},[$tinptr,:256]!
  564. vld1.64 {@ACC[6]-@ACC[7]},[$tinptr,:256]!
  565. itt ne
  566. subne $nptr,$nptr,$num,lsl#2 @ rewind
  567. bne .LNEON_8n_outer
  568. add $toutptr,sp,#128
  569. vst1.64 {q2-q3}, [sp,:256]! @ start wiping stack frame
  570. vshr.u64 $temp,@ACC[0]#lo,#16
  571. vst1.64 {q2-q3},[sp,:256]!
  572. vadd.u64 @ACC[0]#hi,@ACC[0]#hi,$temp
  573. vst1.64 {q2-q3}, [sp,:256]!
  574. vshr.u64 $temp,@ACC[0]#hi,#16
  575. vst1.64 {q2-q3}, [sp,:256]!
  576. vzip.16 @ACC[0]#lo,@ACC[0]#hi
  577. mov $inner,$num
  578. b .LNEON_tail_entry
  579. .align 4
  580. .LNEON_tail:
  581. vadd.u64 @ACC[0]#lo,@ACC[0]#lo,$temp
  582. vshr.u64 $temp,@ACC[0]#lo,#16
  583. vld1.64 {@ACC[2]-@ACC[3]}, [$tinptr, :256]!
  584. vadd.u64 @ACC[0]#hi,@ACC[0]#hi,$temp
  585. vld1.64 {@ACC[4]-@ACC[5]}, [$tinptr, :256]!
  586. vshr.u64 $temp,@ACC[0]#hi,#16
  587. vld1.64 {@ACC[6]-@ACC[7]}, [$tinptr, :256]!
  588. vzip.16 @ACC[0]#lo,@ACC[0]#hi
  589. .LNEON_tail_entry:
  590. ___
  591. for ($i=1; $i<8; $i++) {
  592. $code.=<<___;
  593. vadd.u64 @ACC[1]#lo,@ACC[1]#lo,$temp
  594. vst1.32 {@ACC[0]#lo[0]}, [$toutptr, :32]!
  595. vshr.u64 $temp,@ACC[1]#lo,#16
  596. vadd.u64 @ACC[1]#hi,@ACC[1]#hi,$temp
  597. vshr.u64 $temp,@ACC[1]#hi,#16
  598. vzip.16 @ACC[1]#lo,@ACC[1]#hi
  599. ___
  600. push(@ACC,shift(@ACC));
  601. }
  602. push(@ACC,shift(@ACC));
  603. $code.=<<___;
  604. vld1.64 {@ACC[0]-@ACC[1]}, [$tinptr, :256]!
  605. subs $inner,$inner,#8
  606. vst1.32 {@ACC[7]#lo[0]}, [$toutptr, :32]!
  607. bne .LNEON_tail
  608. vst1.32 {${temp}[0]}, [$toutptr, :32] @ top-most bit
  609. sub $nptr,$nptr,$num,lsl#2 @ rewind $nptr
  610. subs $aptr,sp,#0 @ clear carry flag
  611. add $bptr,sp,$num,lsl#2
  612. .LNEON_sub:
  613. ldmia $aptr!, {r4-r7}
  614. ldmia $nptr!, {r8-r11}
  615. sbcs r8, r4,r8
  616. sbcs r9, r5,r9
  617. sbcs r10,r6,r10
  618. sbcs r11,r7,r11
  619. teq $aptr,$bptr @ preserves carry
  620. stmia $rptr!, {r8-r11}
  621. bne .LNEON_sub
  622. ldr r10, [$aptr] @ load top-most bit
  623. mov r11,sp
  624. veor q0,q0,q0
  625. sub r11,$bptr,r11 @ this is num*4
  626. veor q1,q1,q1
  627. mov $aptr,sp
  628. sub $rptr,$rptr,r11 @ rewind $rptr
  629. mov $nptr,$bptr @ second 3/4th of frame
  630. sbcs r10,r10,#0 @ result is carry flag
  631. .LNEON_copy_n_zap:
  632. ldmia $aptr!, {r4-r7}
  633. ldmia $rptr, {r8-r11}
  634. it cc
  635. movcc r8, r4
  636. vst1.64 {q0-q1}, [$nptr,:256]! @ wipe
  637. itt cc
  638. movcc r9, r5
  639. movcc r10,r6
  640. vst1.64 {q0-q1}, [$nptr,:256]! @ wipe
  641. it cc
  642. movcc r11,r7
  643. ldmia $aptr, {r4-r7}
  644. stmia $rptr!, {r8-r11}
  645. sub $aptr,$aptr,#16
  646. ldmia $rptr, {r8-r11}
  647. it cc
  648. movcc r8, r4
  649. vst1.64 {q0-q1}, [$aptr,:256]! @ wipe
  650. itt cc
  651. movcc r9, r5
  652. movcc r10,r6
  653. vst1.64 {q0-q1}, [$nptr,:256]! @ wipe
  654. it cc
  655. movcc r11,r7
  656. teq $aptr,$bptr @ preserves carry
  657. stmia $rptr!, {r8-r11}
  658. bne .LNEON_copy_n_zap
  659. mov sp,ip
  660. vldmia sp!,{d8-d15}
  661. ldmia sp!,{r4-r11}
  662. ret @ bx lr
  663. .size bn_mul8x_mont_neon,.-bn_mul8x_mont_neon
  664. #endif
  665. ___
  666. }
  667. $code.=<<___;
  668. .asciz "Montgomery multiplication for ARMv4/NEON, CRYPTOGAMS by <appro\@openssl.org>"
  669. .align 2
  670. #if __ARM_MAX_ARCH__>=7
  671. .comm OPENSSL_armcap_P,4,4
  672. #endif
  673. ___
  674. foreach (split("\n",$code)) {
  675. s/\`([^\`]*)\`/eval $1/ge;
  676. s/\bq([0-9]+)#(lo|hi)/sprintf "d%d",2*$1+($2 eq "hi")/ge or
  677. s/\bret\b/bx lr/g or
  678. s/\bbx\s+lr\b/.word\t0xe12fff1e/g; # make it possible to compile with -march=armv4
  679. print $_,"\n";
  680. }
  681. close STDOUT;