2
0

armv4-mont.pl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. #! /usr/bin/env perl
  2. # Copyright 2007-2020 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. .Lcopy: ldr $tj,[$tp] @ conditional copy
  236. ldr $aj,[$rp]
  237. str sp,[$tp],#4 @ zap tp
  238. #ifdef __thumb2__
  239. it cc
  240. #endif
  241. movcc $aj,$tj
  242. str $aj,[$rp],#4
  243. teq $tp,$num @ preserve carry
  244. bne .Lcopy
  245. mov sp,$num
  246. add sp,sp,#4 @ skip over tp[num+1]
  247. ldmia sp!,{r4-r12,lr} @ restore registers
  248. add sp,sp,#2*4 @ skip over {r0,r2}
  249. mov r0,#1
  250. .Labrt:
  251. #if __ARM_ARCH__>=5
  252. ret @ bx lr
  253. #else
  254. tst lr,#1
  255. moveq pc,lr @ be binary compatible with V4, yet
  256. bx lr @ interoperable with Thumb ISA:-)
  257. #endif
  258. .size bn_mul_mont,.-bn_mul_mont
  259. ___
  260. {
  261. my ($A0,$A1,$A2,$A3)=map("d$_",(0..3));
  262. my ($N0,$N1,$N2,$N3)=map("d$_",(4..7));
  263. my ($Z,$Temp)=("q4","q5");
  264. my @ACC=map("q$_",(6..13));
  265. my ($Bi,$Ni,$M0)=map("d$_",(28..31));
  266. my $zero="$Z#lo";
  267. my $temp="$Temp#lo";
  268. my ($rptr,$aptr,$bptr,$nptr,$n0,$num)=map("r$_",(0..5));
  269. my ($tinptr,$toutptr,$inner,$outer,$bnptr)=map("r$_",(6..11));
  270. $code.=<<___;
  271. #if __ARM_MAX_ARCH__>=7
  272. .arch armv7-a
  273. .fpu neon
  274. .type bn_mul8x_mont_neon,%function
  275. .align 5
  276. bn_mul8x_mont_neon:
  277. mov ip,sp
  278. stmdb sp!,{r4-r11}
  279. vstmdb sp!,{d8-d15} @ ABI specification says so
  280. ldmia ip,{r4-r5} @ load rest of parameter block
  281. mov ip,sp
  282. cmp $num,#8
  283. bhi .LNEON_8n
  284. @ special case for $num==8, everything is in register bank...
  285. vld1.32 {${Bi}[0]}, [$bptr,:32]!
  286. veor $zero,$zero,$zero
  287. sub $toutptr,sp,$num,lsl#4
  288. vld1.32 {$A0-$A3}, [$aptr]! @ can't specify :32 :-(
  289. and $toutptr,$toutptr,#-64
  290. vld1.32 {${M0}[0]}, [$n0,:32]
  291. mov sp,$toutptr @ alloca
  292. vzip.16 $Bi,$zero
  293. vmull.u32 @ACC[0],$Bi,${A0}[0]
  294. vmull.u32 @ACC[1],$Bi,${A0}[1]
  295. vmull.u32 @ACC[2],$Bi,${A1}[0]
  296. vshl.i64 $Ni,@ACC[0]#hi,#16
  297. vmull.u32 @ACC[3],$Bi,${A1}[1]
  298. vadd.u64 $Ni,$Ni,@ACC[0]#lo
  299. veor $zero,$zero,$zero
  300. vmul.u32 $Ni,$Ni,$M0
  301. vmull.u32 @ACC[4],$Bi,${A2}[0]
  302. vld1.32 {$N0-$N3}, [$nptr]!
  303. vmull.u32 @ACC[5],$Bi,${A2}[1]
  304. vmull.u32 @ACC[6],$Bi,${A3}[0]
  305. vzip.16 $Ni,$zero
  306. vmull.u32 @ACC[7],$Bi,${A3}[1]
  307. vmlal.u32 @ACC[0],$Ni,${N0}[0]
  308. sub $outer,$num,#1
  309. vmlal.u32 @ACC[1],$Ni,${N0}[1]
  310. vmlal.u32 @ACC[2],$Ni,${N1}[0]
  311. vmlal.u32 @ACC[3],$Ni,${N1}[1]
  312. vmlal.u32 @ACC[4],$Ni,${N2}[0]
  313. vmov $Temp,@ACC[0]
  314. vmlal.u32 @ACC[5],$Ni,${N2}[1]
  315. vmov @ACC[0],@ACC[1]
  316. vmlal.u32 @ACC[6],$Ni,${N3}[0]
  317. vmov @ACC[1],@ACC[2]
  318. vmlal.u32 @ACC[7],$Ni,${N3}[1]
  319. vmov @ACC[2],@ACC[3]
  320. vmov @ACC[3],@ACC[4]
  321. vshr.u64 $temp,$temp,#16
  322. vmov @ACC[4],@ACC[5]
  323. vmov @ACC[5],@ACC[6]
  324. vadd.u64 $temp,$temp,$Temp#hi
  325. vmov @ACC[6],@ACC[7]
  326. veor @ACC[7],@ACC[7]
  327. vshr.u64 $temp,$temp,#16
  328. b .LNEON_outer8
  329. .align 4
  330. .LNEON_outer8:
  331. vld1.32 {${Bi}[0]}, [$bptr,:32]!
  332. veor $zero,$zero,$zero
  333. vzip.16 $Bi,$zero
  334. vadd.u64 @ACC[0]#lo,@ACC[0]#lo,$temp
  335. vmlal.u32 @ACC[0],$Bi,${A0}[0]
  336. vmlal.u32 @ACC[1],$Bi,${A0}[1]
  337. vmlal.u32 @ACC[2],$Bi,${A1}[0]
  338. vshl.i64 $Ni,@ACC[0]#hi,#16
  339. vmlal.u32 @ACC[3],$Bi,${A1}[1]
  340. vadd.u64 $Ni,$Ni,@ACC[0]#lo
  341. veor $zero,$zero,$zero
  342. subs $outer,$outer,#1
  343. vmul.u32 $Ni,$Ni,$M0
  344. vmlal.u32 @ACC[4],$Bi,${A2}[0]
  345. vmlal.u32 @ACC[5],$Bi,${A2}[1]
  346. vmlal.u32 @ACC[6],$Bi,${A3}[0]
  347. vzip.16 $Ni,$zero
  348. vmlal.u32 @ACC[7],$Bi,${A3}[1]
  349. vmlal.u32 @ACC[0],$Ni,${N0}[0]
  350. vmlal.u32 @ACC[1],$Ni,${N0}[1]
  351. vmlal.u32 @ACC[2],$Ni,${N1}[0]
  352. vmlal.u32 @ACC[3],$Ni,${N1}[1]
  353. vmlal.u32 @ACC[4],$Ni,${N2}[0]
  354. vmov $Temp,@ACC[0]
  355. vmlal.u32 @ACC[5],$Ni,${N2}[1]
  356. vmov @ACC[0],@ACC[1]
  357. vmlal.u32 @ACC[6],$Ni,${N3}[0]
  358. vmov @ACC[1],@ACC[2]
  359. vmlal.u32 @ACC[7],$Ni,${N3}[1]
  360. vmov @ACC[2],@ACC[3]
  361. vmov @ACC[3],@ACC[4]
  362. vshr.u64 $temp,$temp,#16
  363. vmov @ACC[4],@ACC[5]
  364. vmov @ACC[5],@ACC[6]
  365. vadd.u64 $temp,$temp,$Temp#hi
  366. vmov @ACC[6],@ACC[7]
  367. veor @ACC[7],@ACC[7]
  368. vshr.u64 $temp,$temp,#16
  369. bne .LNEON_outer8
  370. vadd.u64 @ACC[0]#lo,@ACC[0]#lo,$temp
  371. mov $toutptr,sp
  372. vshr.u64 $temp,@ACC[0]#lo,#16
  373. mov $inner,$num
  374. vadd.u64 @ACC[0]#hi,@ACC[0]#hi,$temp
  375. add $tinptr,sp,#96
  376. vshr.u64 $temp,@ACC[0]#hi,#16
  377. vzip.16 @ACC[0]#lo,@ACC[0]#hi
  378. b .LNEON_tail_entry
  379. .align 4
  380. .LNEON_8n:
  381. veor @ACC[0],@ACC[0],@ACC[0]
  382. sub $toutptr,sp,#128
  383. veor @ACC[1],@ACC[1],@ACC[1]
  384. sub $toutptr,$toutptr,$num,lsl#4
  385. veor @ACC[2],@ACC[2],@ACC[2]
  386. and $toutptr,$toutptr,#-64
  387. veor @ACC[3],@ACC[3],@ACC[3]
  388. mov sp,$toutptr @ alloca
  389. veor @ACC[4],@ACC[4],@ACC[4]
  390. add $toutptr,$toutptr,#256
  391. veor @ACC[5],@ACC[5],@ACC[5]
  392. sub $inner,$num,#8
  393. veor @ACC[6],@ACC[6],@ACC[6]
  394. veor @ACC[7],@ACC[7],@ACC[7]
  395. .LNEON_8n_init:
  396. vst1.64 {@ACC[0]-@ACC[1]},[$toutptr,:256]!
  397. subs $inner,$inner,#8
  398. vst1.64 {@ACC[2]-@ACC[3]},[$toutptr,:256]!
  399. vst1.64 {@ACC[4]-@ACC[5]},[$toutptr,:256]!
  400. vst1.64 {@ACC[6]-@ACC[7]},[$toutptr,:256]!
  401. bne .LNEON_8n_init
  402. add $tinptr,sp,#256
  403. vld1.32 {$A0-$A3},[$aptr]!
  404. add $bnptr,sp,#8
  405. vld1.32 {${M0}[0]},[$n0,:32]
  406. mov $outer,$num
  407. b .LNEON_8n_outer
  408. .align 4
  409. .LNEON_8n_outer:
  410. vld1.32 {${Bi}[0]},[$bptr,:32]! @ *b++
  411. veor $zero,$zero,$zero
  412. vzip.16 $Bi,$zero
  413. add $toutptr,sp,#128
  414. vld1.32 {$N0-$N3},[$nptr]!
  415. vmlal.u32 @ACC[0],$Bi,${A0}[0]
  416. vmlal.u32 @ACC[1],$Bi,${A0}[1]
  417. veor $zero,$zero,$zero
  418. vmlal.u32 @ACC[2],$Bi,${A1}[0]
  419. vshl.i64 $Ni,@ACC[0]#hi,#16
  420. vmlal.u32 @ACC[3],$Bi,${A1}[1]
  421. vadd.u64 $Ni,$Ni,@ACC[0]#lo
  422. vmlal.u32 @ACC[4],$Bi,${A2}[0]
  423. vmul.u32 $Ni,$Ni,$M0
  424. vmlal.u32 @ACC[5],$Bi,${A2}[1]
  425. vst1.32 {$Bi},[sp,:64] @ put aside smashed b[8*i+0]
  426. vmlal.u32 @ACC[6],$Bi,${A3}[0]
  427. vzip.16 $Ni,$zero
  428. vmlal.u32 @ACC[7],$Bi,${A3}[1]
  429. ___
  430. for ($i=0; $i<7;) {
  431. $code.=<<___;
  432. vld1.32 {${Bi}[0]},[$bptr,:32]! @ *b++
  433. vmlal.u32 @ACC[0],$Ni,${N0}[0]
  434. veor $temp,$temp,$temp
  435. vmlal.u32 @ACC[1],$Ni,${N0}[1]
  436. vzip.16 $Bi,$temp
  437. vmlal.u32 @ACC[2],$Ni,${N1}[0]
  438. vshr.u64 @ACC[0]#lo,@ACC[0]#lo,#16
  439. vmlal.u32 @ACC[3],$Ni,${N1}[1]
  440. vmlal.u32 @ACC[4],$Ni,${N2}[0]
  441. vadd.u64 @ACC[0]#lo,@ACC[0]#lo,@ACC[0]#hi
  442. vmlal.u32 @ACC[5],$Ni,${N2}[1]
  443. vshr.u64 @ACC[0]#lo,@ACC[0]#lo,#16
  444. vmlal.u32 @ACC[6],$Ni,${N3}[0]
  445. vmlal.u32 @ACC[7],$Ni,${N3}[1]
  446. vadd.u64 @ACC[1]#lo,@ACC[1]#lo,@ACC[0]#lo
  447. vst1.32 {$Ni},[$bnptr,:64]! @ put aside smashed m[8*i+$i]
  448. ___
  449. push(@ACC,shift(@ACC)); $i++;
  450. $code.=<<___;
  451. vmlal.u32 @ACC[0],$Bi,${A0}[0]
  452. vld1.64 {@ACC[7]},[$tinptr,:128]!
  453. vmlal.u32 @ACC[1],$Bi,${A0}[1]
  454. veor $zero,$zero,$zero
  455. vmlal.u32 @ACC[2],$Bi,${A1}[0]
  456. vshl.i64 $Ni,@ACC[0]#hi,#16
  457. vmlal.u32 @ACC[3],$Bi,${A1}[1]
  458. vadd.u64 $Ni,$Ni,@ACC[0]#lo
  459. vmlal.u32 @ACC[4],$Bi,${A2}[0]
  460. vmul.u32 $Ni,$Ni,$M0
  461. vmlal.u32 @ACC[5],$Bi,${A2}[1]
  462. vst1.32 {$Bi},[$bnptr,:64]! @ put aside smashed b[8*i+$i]
  463. vmlal.u32 @ACC[6],$Bi,${A3}[0]
  464. vzip.16 $Ni,$zero
  465. vmlal.u32 @ACC[7],$Bi,${A3}[1]
  466. ___
  467. }
  468. $code.=<<___;
  469. vld1.32 {$Bi},[sp,:64] @ pull smashed b[8*i+0]
  470. vmlal.u32 @ACC[0],$Ni,${N0}[0]
  471. vld1.32 {$A0-$A3},[$aptr]!
  472. vmlal.u32 @ACC[1],$Ni,${N0}[1]
  473. vmlal.u32 @ACC[2],$Ni,${N1}[0]
  474. vshr.u64 @ACC[0]#lo,@ACC[0]#lo,#16
  475. vmlal.u32 @ACC[3],$Ni,${N1}[1]
  476. vmlal.u32 @ACC[4],$Ni,${N2}[0]
  477. vadd.u64 @ACC[0]#lo,@ACC[0]#lo,@ACC[0]#hi
  478. vmlal.u32 @ACC[5],$Ni,${N2}[1]
  479. vshr.u64 @ACC[0]#lo,@ACC[0]#lo,#16
  480. vmlal.u32 @ACC[6],$Ni,${N3}[0]
  481. vmlal.u32 @ACC[7],$Ni,${N3}[1]
  482. vadd.u64 @ACC[1]#lo,@ACC[1]#lo,@ACC[0]#lo
  483. vst1.32 {$Ni},[$bnptr,:64] @ put aside smashed m[8*i+$i]
  484. add $bnptr,sp,#8 @ rewind
  485. ___
  486. push(@ACC,shift(@ACC));
  487. $code.=<<___;
  488. sub $inner,$num,#8
  489. b .LNEON_8n_inner
  490. .align 4
  491. .LNEON_8n_inner:
  492. subs $inner,$inner,#8
  493. vmlal.u32 @ACC[0],$Bi,${A0}[0]
  494. vld1.64 {@ACC[7]},[$tinptr,:128]
  495. vmlal.u32 @ACC[1],$Bi,${A0}[1]
  496. vld1.32 {$Ni},[$bnptr,:64]! @ pull smashed m[8*i+0]
  497. vmlal.u32 @ACC[2],$Bi,${A1}[0]
  498. vld1.32 {$N0-$N3},[$nptr]!
  499. vmlal.u32 @ACC[3],$Bi,${A1}[1]
  500. it ne
  501. addne $tinptr,$tinptr,#16 @ don't advance in last iteration
  502. vmlal.u32 @ACC[4],$Bi,${A2}[0]
  503. vmlal.u32 @ACC[5],$Bi,${A2}[1]
  504. vmlal.u32 @ACC[6],$Bi,${A3}[0]
  505. vmlal.u32 @ACC[7],$Bi,${A3}[1]
  506. ___
  507. for ($i=1; $i<8; $i++) {
  508. $code.=<<___;
  509. vld1.32 {$Bi},[$bnptr,:64]! @ pull smashed b[8*i+$i]
  510. vmlal.u32 @ACC[0],$Ni,${N0}[0]
  511. vmlal.u32 @ACC[1],$Ni,${N0}[1]
  512. vmlal.u32 @ACC[2],$Ni,${N1}[0]
  513. vmlal.u32 @ACC[3],$Ni,${N1}[1]
  514. vmlal.u32 @ACC[4],$Ni,${N2}[0]
  515. vmlal.u32 @ACC[5],$Ni,${N2}[1]
  516. vmlal.u32 @ACC[6],$Ni,${N3}[0]
  517. vmlal.u32 @ACC[7],$Ni,${N3}[1]
  518. vst1.64 {@ACC[0]},[$toutptr,:128]!
  519. ___
  520. push(@ACC,shift(@ACC));
  521. $code.=<<___;
  522. vmlal.u32 @ACC[0],$Bi,${A0}[0]
  523. vld1.64 {@ACC[7]},[$tinptr,:128]
  524. vmlal.u32 @ACC[1],$Bi,${A0}[1]
  525. vld1.32 {$Ni},[$bnptr,:64]! @ pull smashed m[8*i+$i]
  526. vmlal.u32 @ACC[2],$Bi,${A1}[0]
  527. it ne
  528. addne $tinptr,$tinptr,#16 @ don't advance in last iteration
  529. vmlal.u32 @ACC[3],$Bi,${A1}[1]
  530. vmlal.u32 @ACC[4],$Bi,${A2}[0]
  531. vmlal.u32 @ACC[5],$Bi,${A2}[1]
  532. vmlal.u32 @ACC[6],$Bi,${A3}[0]
  533. vmlal.u32 @ACC[7],$Bi,${A3}[1]
  534. ___
  535. }
  536. $code.=<<___;
  537. it eq
  538. subeq $aptr,$aptr,$num,lsl#2 @ rewind
  539. vmlal.u32 @ACC[0],$Ni,${N0}[0]
  540. vld1.32 {$Bi},[sp,:64] @ pull smashed b[8*i+0]
  541. vmlal.u32 @ACC[1],$Ni,${N0}[1]
  542. vld1.32 {$A0-$A3},[$aptr]!
  543. vmlal.u32 @ACC[2],$Ni,${N1}[0]
  544. add $bnptr,sp,#8 @ rewind
  545. vmlal.u32 @ACC[3],$Ni,${N1}[1]
  546. vmlal.u32 @ACC[4],$Ni,${N2}[0]
  547. vmlal.u32 @ACC[5],$Ni,${N2}[1]
  548. vmlal.u32 @ACC[6],$Ni,${N3}[0]
  549. vst1.64 {@ACC[0]},[$toutptr,:128]!
  550. vmlal.u32 @ACC[7],$Ni,${N3}[1]
  551. bne .LNEON_8n_inner
  552. ___
  553. push(@ACC,shift(@ACC));
  554. $code.=<<___;
  555. add $tinptr,sp,#128
  556. vst1.64 {@ACC[0]-@ACC[1]},[$toutptr,:256]!
  557. veor q2,q2,q2 @ $N0-$N1
  558. vst1.64 {@ACC[2]-@ACC[3]},[$toutptr,:256]!
  559. veor q3,q3,q3 @ $N2-$N3
  560. vst1.64 {@ACC[4]-@ACC[5]},[$toutptr,:256]!
  561. vst1.64 {@ACC[6]},[$toutptr,:128]
  562. subs $outer,$outer,#8
  563. vld1.64 {@ACC[0]-@ACC[1]},[$tinptr,:256]!
  564. vld1.64 {@ACC[2]-@ACC[3]},[$tinptr,:256]!
  565. vld1.64 {@ACC[4]-@ACC[5]},[$tinptr,:256]!
  566. vld1.64 {@ACC[6]-@ACC[7]},[$tinptr,:256]!
  567. itt ne
  568. subne $nptr,$nptr,$num,lsl#2 @ rewind
  569. bne .LNEON_8n_outer
  570. add $toutptr,sp,#128
  571. vst1.64 {q2-q3}, [sp,:256]! @ start wiping stack frame
  572. vshr.u64 $temp,@ACC[0]#lo,#16
  573. vst1.64 {q2-q3},[sp,:256]!
  574. vadd.u64 @ACC[0]#hi,@ACC[0]#hi,$temp
  575. vst1.64 {q2-q3}, [sp,:256]!
  576. vshr.u64 $temp,@ACC[0]#hi,#16
  577. vst1.64 {q2-q3}, [sp,:256]!
  578. vzip.16 @ACC[0]#lo,@ACC[0]#hi
  579. mov $inner,$num
  580. b .LNEON_tail_entry
  581. .align 4
  582. .LNEON_tail:
  583. vadd.u64 @ACC[0]#lo,@ACC[0]#lo,$temp
  584. vshr.u64 $temp,@ACC[0]#lo,#16
  585. vld1.64 {@ACC[2]-@ACC[3]}, [$tinptr, :256]!
  586. vadd.u64 @ACC[0]#hi,@ACC[0]#hi,$temp
  587. vld1.64 {@ACC[4]-@ACC[5]}, [$tinptr, :256]!
  588. vshr.u64 $temp,@ACC[0]#hi,#16
  589. vld1.64 {@ACC[6]-@ACC[7]}, [$tinptr, :256]!
  590. vzip.16 @ACC[0]#lo,@ACC[0]#hi
  591. .LNEON_tail_entry:
  592. ___
  593. for ($i=1; $i<8; $i++) {
  594. $code.=<<___;
  595. vadd.u64 @ACC[1]#lo,@ACC[1]#lo,$temp
  596. vst1.32 {@ACC[0]#lo[0]}, [$toutptr, :32]!
  597. vshr.u64 $temp,@ACC[1]#lo,#16
  598. vadd.u64 @ACC[1]#hi,@ACC[1]#hi,$temp
  599. vshr.u64 $temp,@ACC[1]#hi,#16
  600. vzip.16 @ACC[1]#lo,@ACC[1]#hi
  601. ___
  602. push(@ACC,shift(@ACC));
  603. }
  604. push(@ACC,shift(@ACC));
  605. $code.=<<___;
  606. vld1.64 {@ACC[0]-@ACC[1]}, [$tinptr, :256]!
  607. subs $inner,$inner,#8
  608. vst1.32 {@ACC[7]#lo[0]}, [$toutptr, :32]!
  609. bne .LNEON_tail
  610. vst1.32 {${temp}[0]}, [$toutptr, :32] @ top-most bit
  611. sub $nptr,$nptr,$num,lsl#2 @ rewind $nptr
  612. subs $aptr,sp,#0 @ clear carry flag
  613. add $bptr,sp,$num,lsl#2
  614. .LNEON_sub:
  615. ldmia $aptr!, {r4-r7}
  616. ldmia $nptr!, {r8-r11}
  617. sbcs r8, r4,r8
  618. sbcs r9, r5,r9
  619. sbcs r10,r6,r10
  620. sbcs r11,r7,r11
  621. teq $aptr,$bptr @ preserves carry
  622. stmia $rptr!, {r8-r11}
  623. bne .LNEON_sub
  624. ldr r10, [$aptr] @ load top-most bit
  625. mov r11,sp
  626. veor q0,q0,q0
  627. sub r11,$bptr,r11 @ this is num*4
  628. veor q1,q1,q1
  629. mov $aptr,sp
  630. sub $rptr,$rptr,r11 @ rewind $rptr
  631. mov $nptr,$bptr @ second 3/4th of frame
  632. sbcs r10,r10,#0 @ result is carry flag
  633. .LNEON_copy_n_zap:
  634. ldmia $aptr!, {r4-r7}
  635. ldmia $rptr, {r8-r11}
  636. it cc
  637. movcc r8, r4
  638. vst1.64 {q0-q1}, [$nptr,:256]! @ wipe
  639. itt cc
  640. movcc r9, r5
  641. movcc r10,r6
  642. vst1.64 {q0-q1}, [$nptr,:256]! @ wipe
  643. it cc
  644. movcc r11,r7
  645. ldmia $aptr, {r4-r7}
  646. stmia $rptr!, {r8-r11}
  647. sub $aptr,$aptr,#16
  648. ldmia $rptr, {r8-r11}
  649. it cc
  650. movcc r8, r4
  651. vst1.64 {q0-q1}, [$aptr,:256]! @ wipe
  652. itt cc
  653. movcc r9, r5
  654. movcc r10,r6
  655. vst1.64 {q0-q1}, [$nptr,:256]! @ wipe
  656. it cc
  657. movcc r11,r7
  658. teq $aptr,$bptr @ preserves carry
  659. stmia $rptr!, {r8-r11}
  660. bne .LNEON_copy_n_zap
  661. mov sp,ip
  662. vldmia sp!,{d8-d15}
  663. ldmia sp!,{r4-r11}
  664. ret @ bx lr
  665. .size bn_mul8x_mont_neon,.-bn_mul8x_mont_neon
  666. #endif
  667. ___
  668. }
  669. $code.=<<___;
  670. .asciz "Montgomery multiplication for ARMv4/NEON, CRYPTOGAMS by <appro\@openssl.org>"
  671. .align 2
  672. #if __ARM_MAX_ARCH__>=7
  673. .comm OPENSSL_armcap_P,4,4
  674. #endif
  675. ___
  676. foreach (split("\n",$code)) {
  677. s/\`([^\`]*)\`/eval $1/ge;
  678. s/\bq([0-9]+)#(lo|hi)/sprintf "d%d",2*$1+($2 eq "hi")/ge or
  679. s/\bret\b/bx lr/g or
  680. s/\bbx\s+lr\b/.word\t0xe12fff1e/g; # make it possible to compile with -march=armv4
  681. print $_,"\n";
  682. }
  683. close STDOUT or die "error closing STDOUT: $!";