2
0

poly1305-x86.pl 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. #! /usr/bin/env perl
  2. # Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (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. # ====================================================================
  10. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  11. # project. The module is, however, dual licensed under OpenSSL and
  12. # CRYPTOGAMS licenses depending on where you obtain it. For further
  13. # details see http://www.openssl.org/~appro/cryptogams/.
  14. # ====================================================================
  15. #
  16. # This module implements Poly1305 hash for x86.
  17. #
  18. # April 2015
  19. #
  20. # Numbers are cycles per processed byte with poly1305_blocks alone,
  21. # measured with rdtsc at fixed clock frequency.
  22. #
  23. # IALU/gcc-3.4(*) SSE2(**) AVX2
  24. # Pentium 15.7/+80% -
  25. # PIII 6.21/+90% -
  26. # P4 19.8/+40% 3.24
  27. # Core 2 4.85/+90% 1.80
  28. # Westmere 4.58/+100% 1.43
  29. # Sandy Bridge 3.90/+100% 1.36
  30. # Haswell 3.88/+70% 1.18 0.72
  31. # Skylake 3.10/+60% 1.14 0.62
  32. # Silvermont 11.0/+40% 4.80
  33. # Goldmont 4.10/+200% 2.10
  34. # VIA Nano 6.71/+90% 2.47
  35. # Sledgehammer 3.51/+180% 4.27
  36. # Bulldozer 4.53/+140% 1.31
  37. #
  38. # (*) gcc 4.8 for some reason generated worse code;
  39. # (**) besides SSE2 there are floating-point and AVX options; FP
  40. # is deemed unnecessary, because pre-SSE2 processor are too
  41. # old to care about, while it's not the fastest option on
  42. # SSE2-capable ones; AVX is omitted, because it doesn't give
  43. # a lot of improvement, 5-10% depending on processor;
  44. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  45. push(@INC,"${dir}","${dir}../../perlasm");
  46. require "x86asm.pl";
  47. $output=pop and open STDOUT,">$output";
  48. &asm_init($ARGV[0],$ARGV[$#ARGV] eq "386");
  49. $sse2=$avx=0;
  50. for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
  51. if ($sse2) {
  52. &static_label("const_sse2");
  53. &static_label("enter_blocks");
  54. &static_label("enter_emit");
  55. &external_label("OPENSSL_ia32cap_P");
  56. if (`$ENV{CC} -Wa,-v -c -o /dev/null -x assembler /dev/null 2>&1`
  57. =~ /GNU assembler version ([2-9]\.[0-9]+)/) {
  58. $avx = ($1>=2.19) + ($1>=2.22);
  59. }
  60. if (!$avx && $ARGV[0] eq "win32n" &&
  61. `nasm -v 2>&1` =~ /NASM version ([2-9]\.[0-9]+)/) {
  62. $avx = ($1>=2.09) + ($1>=2.10);
  63. }
  64. if (!$avx && `$ENV{CC} -v 2>&1` =~ /((?:^clang|LLVM) version|based on LLVM) ([0-9]+\.[0-9]+)/) {
  65. $avx = ($2>=3.0) + ($2>3.0);
  66. }
  67. }
  68. ########################################################################
  69. # Layout of opaque area is following.
  70. #
  71. # unsigned __int32 h[5]; # current hash value base 2^32
  72. # unsigned __int32 pad; # is_base2_26 in vector context
  73. # unsigned __int32 r[4]; # key value base 2^32
  74. &align(64);
  75. &function_begin("poly1305_init");
  76. &mov ("edi",&wparam(0)); # context
  77. &mov ("esi",&wparam(1)); # key
  78. &mov ("ebp",&wparam(2)); # function table
  79. &xor ("eax","eax");
  80. &mov (&DWP(4*0,"edi"),"eax"); # zero hash value
  81. &mov (&DWP(4*1,"edi"),"eax");
  82. &mov (&DWP(4*2,"edi"),"eax");
  83. &mov (&DWP(4*3,"edi"),"eax");
  84. &mov (&DWP(4*4,"edi"),"eax");
  85. &mov (&DWP(4*5,"edi"),"eax"); # is_base2_26
  86. &cmp ("esi",0);
  87. &je (&label("nokey"));
  88. if ($sse2) {
  89. &call (&label("pic_point"));
  90. &set_label("pic_point");
  91. &blindpop("ebx");
  92. &lea ("eax",&DWP("poly1305_blocks-".&label("pic_point"),"ebx"));
  93. &lea ("edx",&DWP("poly1305_emit-".&label("pic_point"),"ebx"));
  94. &picmeup("edi","OPENSSL_ia32cap_P","ebx",&label("pic_point"));
  95. &mov ("ecx",&DWP(0,"edi"));
  96. &and ("ecx",1<<26|1<<24);
  97. &cmp ("ecx",1<<26|1<<24); # SSE2 and XMM?
  98. &jne (&label("no_sse2"));
  99. &lea ("eax",&DWP("_poly1305_blocks_sse2-".&label("pic_point"),"ebx"));
  100. &lea ("edx",&DWP("_poly1305_emit_sse2-".&label("pic_point"),"ebx"));
  101. if ($avx>1) {
  102. &mov ("ecx",&DWP(8,"edi"));
  103. &test ("ecx",1<<5); # AVX2?
  104. &jz (&label("no_sse2"));
  105. &lea ("eax",&DWP("_poly1305_blocks_avx2-".&label("pic_point"),"ebx"));
  106. }
  107. &set_label("no_sse2");
  108. &mov ("edi",&wparam(0)); # reload context
  109. &mov (&DWP(0,"ebp"),"eax"); # fill function table
  110. &mov (&DWP(4,"ebp"),"edx");
  111. }
  112. &mov ("eax",&DWP(4*0,"esi")); # load input key
  113. &mov ("ebx",&DWP(4*1,"esi"));
  114. &mov ("ecx",&DWP(4*2,"esi"));
  115. &mov ("edx",&DWP(4*3,"esi"));
  116. &and ("eax",0x0fffffff);
  117. &and ("ebx",0x0ffffffc);
  118. &and ("ecx",0x0ffffffc);
  119. &and ("edx",0x0ffffffc);
  120. &mov (&DWP(4*6,"edi"),"eax");
  121. &mov (&DWP(4*7,"edi"),"ebx");
  122. &mov (&DWP(4*8,"edi"),"ecx");
  123. &mov (&DWP(4*9,"edi"),"edx");
  124. &mov ("eax",$sse2);
  125. &set_label("nokey");
  126. &function_end("poly1305_init");
  127. ($h0,$h1,$h2,$h3,$h4,
  128. $d0,$d1,$d2,$d3,
  129. $r0,$r1,$r2,$r3,
  130. $s1,$s2,$s3)=map(4*$_,(0..15));
  131. &function_begin("poly1305_blocks");
  132. &mov ("edi",&wparam(0)); # ctx
  133. &mov ("esi",&wparam(1)); # inp
  134. &mov ("ecx",&wparam(2)); # len
  135. &set_label("enter_blocks");
  136. &and ("ecx",-15);
  137. &jz (&label("nodata"));
  138. &stack_push(16);
  139. &mov ("eax",&DWP(4*6,"edi")); # r0
  140. &mov ("ebx",&DWP(4*7,"edi")); # r1
  141. &lea ("ebp",&DWP(0,"esi","ecx")); # end of input
  142. &mov ("ecx",&DWP(4*8,"edi")); # r2
  143. &mov ("edx",&DWP(4*9,"edi")); # r3
  144. &mov (&wparam(2),"ebp");
  145. &mov ("ebp","esi");
  146. &mov (&DWP($r0,"esp"),"eax"); # r0
  147. &mov ("eax","ebx");
  148. &shr ("eax",2);
  149. &mov (&DWP($r1,"esp"),"ebx"); # r1
  150. &add ("eax","ebx"); # s1
  151. &mov ("ebx","ecx");
  152. &shr ("ebx",2);
  153. &mov (&DWP($r2,"esp"),"ecx"); # r2
  154. &add ("ebx","ecx"); # s2
  155. &mov ("ecx","edx");
  156. &shr ("ecx",2);
  157. &mov (&DWP($r3,"esp"),"edx"); # r3
  158. &add ("ecx","edx"); # s3
  159. &mov (&DWP($s1,"esp"),"eax"); # s1
  160. &mov (&DWP($s2,"esp"),"ebx"); # s2
  161. &mov (&DWP($s3,"esp"),"ecx"); # s3
  162. &mov ("eax",&DWP(4*0,"edi")); # load hash value
  163. &mov ("ebx",&DWP(4*1,"edi"));
  164. &mov ("ecx",&DWP(4*2,"edi"));
  165. &mov ("esi",&DWP(4*3,"edi"));
  166. &mov ("edi",&DWP(4*4,"edi"));
  167. &jmp (&label("loop"));
  168. &set_label("loop",32);
  169. &add ("eax",&DWP(4*0,"ebp")); # accumulate input
  170. &adc ("ebx",&DWP(4*1,"ebp"));
  171. &adc ("ecx",&DWP(4*2,"ebp"));
  172. &adc ("esi",&DWP(4*3,"ebp"));
  173. &lea ("ebp",&DWP(4*4,"ebp"));
  174. &adc ("edi",&wparam(3)); # padbit
  175. &mov (&DWP($h0,"esp"),"eax"); # put aside hash[+inp]
  176. &mov (&DWP($h3,"esp"),"esi");
  177. &mul (&DWP($r0,"esp")); # h0*r0
  178. &mov (&DWP($h4,"esp"),"edi");
  179. &mov ("edi","eax");
  180. &mov ("eax","ebx"); # h1
  181. &mov ("esi","edx");
  182. &mul (&DWP($s3,"esp")); # h1*s3
  183. &add ("edi","eax");
  184. &mov ("eax","ecx"); # h2
  185. &adc ("esi","edx");
  186. &mul (&DWP($s2,"esp")); # h2*s2
  187. &add ("edi","eax");
  188. &mov ("eax",&DWP($h3,"esp"));
  189. &adc ("esi","edx");
  190. &mul (&DWP($s1,"esp")); # h3*s1
  191. &add ("edi","eax");
  192. &mov ("eax",&DWP($h0,"esp"));
  193. &adc ("esi","edx");
  194. &mul (&DWP($r1,"esp")); # h0*r1
  195. &mov (&DWP($d0,"esp"),"edi");
  196. &xor ("edi","edi");
  197. &add ("esi","eax");
  198. &mov ("eax","ebx"); # h1
  199. &adc ("edi","edx");
  200. &mul (&DWP($r0,"esp")); # h1*r0
  201. &add ("esi","eax");
  202. &mov ("eax","ecx"); # h2
  203. &adc ("edi","edx");
  204. &mul (&DWP($s3,"esp")); # h2*s3
  205. &add ("esi","eax");
  206. &mov ("eax",&DWP($h3,"esp"));
  207. &adc ("edi","edx");
  208. &mul (&DWP($s2,"esp")); # h3*s2
  209. &add ("esi","eax");
  210. &mov ("eax",&DWP($h4,"esp"));
  211. &adc ("edi","edx");
  212. &imul ("eax",&DWP($s1,"esp")); # h4*s1
  213. &add ("esi","eax");
  214. &mov ("eax",&DWP($h0,"esp"));
  215. &adc ("edi",0);
  216. &mul (&DWP($r2,"esp")); # h0*r2
  217. &mov (&DWP($d1,"esp"),"esi");
  218. &xor ("esi","esi");
  219. &add ("edi","eax");
  220. &mov ("eax","ebx"); # h1
  221. &adc ("esi","edx");
  222. &mul (&DWP($r1,"esp")); # h1*r1
  223. &add ("edi","eax");
  224. &mov ("eax","ecx"); # h2
  225. &adc ("esi","edx");
  226. &mul (&DWP($r0,"esp")); # h2*r0
  227. &add ("edi","eax");
  228. &mov ("eax",&DWP($h3,"esp"));
  229. &adc ("esi","edx");
  230. &mul (&DWP($s3,"esp")); # h3*s3
  231. &add ("edi","eax");
  232. &mov ("eax",&DWP($h4,"esp"));
  233. &adc ("esi","edx");
  234. &imul ("eax",&DWP($s2,"esp")); # h4*s2
  235. &add ("edi","eax");
  236. &mov ("eax",&DWP($h0,"esp"));
  237. &adc ("esi",0);
  238. &mul (&DWP($r3,"esp")); # h0*r3
  239. &mov (&DWP($d2,"esp"),"edi");
  240. &xor ("edi","edi");
  241. &add ("esi","eax");
  242. &mov ("eax","ebx"); # h1
  243. &adc ("edi","edx");
  244. &mul (&DWP($r2,"esp")); # h1*r2
  245. &add ("esi","eax");
  246. &mov ("eax","ecx"); # h2
  247. &adc ("edi","edx");
  248. &mul (&DWP($r1,"esp")); # h2*r1
  249. &add ("esi","eax");
  250. &mov ("eax",&DWP($h3,"esp"));
  251. &adc ("edi","edx");
  252. &mul (&DWP($r0,"esp")); # h3*r0
  253. &add ("esi","eax");
  254. &mov ("ecx",&DWP($h4,"esp"));
  255. &adc ("edi","edx");
  256. &mov ("edx","ecx");
  257. &imul ("ecx",&DWP($s3,"esp")); # h4*s3
  258. &add ("esi","ecx");
  259. &mov ("eax",&DWP($d0,"esp"));
  260. &adc ("edi",0);
  261. &imul ("edx",&DWP($r0,"esp")); # h4*r0
  262. &add ("edx","edi");
  263. &mov ("ebx",&DWP($d1,"esp"));
  264. &mov ("ecx",&DWP($d2,"esp"));
  265. &mov ("edi","edx"); # last reduction step
  266. &shr ("edx",2);
  267. &and ("edi",3);
  268. &lea ("edx",&DWP(0,"edx","edx",4)); # *5
  269. &add ("eax","edx");
  270. &adc ("ebx",0);
  271. &adc ("ecx",0);
  272. &adc ("esi",0);
  273. &adc ("edi",0);
  274. &cmp ("ebp",&wparam(2)); # done yet?
  275. &jne (&label("loop"));
  276. &mov ("edx",&wparam(0)); # ctx
  277. &stack_pop(16);
  278. &mov (&DWP(4*0,"edx"),"eax"); # store hash value
  279. &mov (&DWP(4*1,"edx"),"ebx");
  280. &mov (&DWP(4*2,"edx"),"ecx");
  281. &mov (&DWP(4*3,"edx"),"esi");
  282. &mov (&DWP(4*4,"edx"),"edi");
  283. &set_label("nodata");
  284. &function_end("poly1305_blocks");
  285. &function_begin("poly1305_emit");
  286. &mov ("ebp",&wparam(0)); # context
  287. &set_label("enter_emit");
  288. &mov ("edi",&wparam(1)); # output
  289. &mov ("eax",&DWP(4*0,"ebp")); # load hash value
  290. &mov ("ebx",&DWP(4*1,"ebp"));
  291. &mov ("ecx",&DWP(4*2,"ebp"));
  292. &mov ("edx",&DWP(4*3,"ebp"));
  293. &mov ("esi",&DWP(4*4,"ebp"));
  294. &add ("eax",5); # compare to modulus
  295. &adc ("ebx",0);
  296. &adc ("ecx",0);
  297. &adc ("edx",0);
  298. &adc ("esi",0);
  299. &shr ("esi",2); # did it carry/borrow?
  300. &neg ("esi"); # do we choose hash-modulus?
  301. &and ("eax","esi");
  302. &and ("ebx","esi");
  303. &and ("ecx","esi");
  304. &and ("edx","esi");
  305. &mov (&DWP(4*0,"edi"),"eax");
  306. &mov (&DWP(4*1,"edi"),"ebx");
  307. &mov (&DWP(4*2,"edi"),"ecx");
  308. &mov (&DWP(4*3,"edi"),"edx");
  309. &not ("esi"); # or original hash value?
  310. &mov ("eax",&DWP(4*0,"ebp"));
  311. &mov ("ebx",&DWP(4*1,"ebp"));
  312. &mov ("ecx",&DWP(4*2,"ebp"));
  313. &mov ("edx",&DWP(4*3,"ebp"));
  314. &mov ("ebp",&wparam(2));
  315. &and ("eax","esi");
  316. &and ("ebx","esi");
  317. &and ("ecx","esi");
  318. &and ("edx","esi");
  319. &or ("eax",&DWP(4*0,"edi"));
  320. &or ("ebx",&DWP(4*1,"edi"));
  321. &or ("ecx",&DWP(4*2,"edi"));
  322. &or ("edx",&DWP(4*3,"edi"));
  323. &add ("eax",&DWP(4*0,"ebp")); # accumulate key
  324. &adc ("ebx",&DWP(4*1,"ebp"));
  325. &adc ("ecx",&DWP(4*2,"ebp"));
  326. &adc ("edx",&DWP(4*3,"ebp"));
  327. &mov (&DWP(4*0,"edi"),"eax");
  328. &mov (&DWP(4*1,"edi"),"ebx");
  329. &mov (&DWP(4*2,"edi"),"ecx");
  330. &mov (&DWP(4*3,"edi"),"edx");
  331. &function_end("poly1305_emit");
  332. if ($sse2) {
  333. ########################################################################
  334. # Layout of opaque area is following.
  335. #
  336. # unsigned __int32 h[5]; # current hash value base 2^26
  337. # unsigned __int32 is_base2_26;
  338. # unsigned __int32 r[4]; # key value base 2^32
  339. # unsigned __int32 pad[2];
  340. # struct { unsigned __int32 r^4, r^3, r^2, r^1; } r[9];
  341. #
  342. # where r^n are base 2^26 digits of degrees of multiplier key. There are
  343. # 5 digits, but last four are interleaved with multiples of 5, totalling
  344. # in 9 elements: r0, r1, 5*r1, r2, 5*r2, r3, 5*r3, r4, 5*r4.
  345. my ($D0,$D1,$D2,$D3,$D4,$T0,$T1,$T2)=map("xmm$_",(0..7));
  346. my $MASK=$T2; # borrow and keep in mind
  347. &align (32);
  348. &function_begin_B("_poly1305_init_sse2");
  349. &movdqu ($D4,&QWP(4*6,"edi")); # key base 2^32
  350. &lea ("edi",&DWP(16*3,"edi")); # size optimization
  351. &mov ("ebp","esp");
  352. &sub ("esp",16*(9+5));
  353. &and ("esp",-16);
  354. #&pand ($D4,&QWP(96,"ebx")); # magic mask
  355. &movq ($MASK,&QWP(64,"ebx"));
  356. &movdqa ($D0,$D4);
  357. &movdqa ($D1,$D4);
  358. &movdqa ($D2,$D4);
  359. &pand ($D0,$MASK); # -> base 2^26
  360. &psrlq ($D1,26);
  361. &psrldq ($D2,6);
  362. &pand ($D1,$MASK);
  363. &movdqa ($D3,$D2);
  364. &psrlq ($D2,4)
  365. &psrlq ($D3,30);
  366. &pand ($D2,$MASK);
  367. &pand ($D3,$MASK);
  368. &psrldq ($D4,13);
  369. &lea ("edx",&DWP(16*9,"esp")); # size optimization
  370. &mov ("ecx",2);
  371. &set_label("square");
  372. &movdqa (&QWP(16*0,"esp"),$D0);
  373. &movdqa (&QWP(16*1,"esp"),$D1);
  374. &movdqa (&QWP(16*2,"esp"),$D2);
  375. &movdqa (&QWP(16*3,"esp"),$D3);
  376. &movdqa (&QWP(16*4,"esp"),$D4);
  377. &movdqa ($T1,$D1);
  378. &movdqa ($T0,$D2);
  379. &pslld ($T1,2);
  380. &pslld ($T0,2);
  381. &paddd ($T1,$D1); # *5
  382. &paddd ($T0,$D2); # *5
  383. &movdqa (&QWP(16*5,"esp"),$T1);
  384. &movdqa (&QWP(16*6,"esp"),$T0);
  385. &movdqa ($T1,$D3);
  386. &movdqa ($T0,$D4);
  387. &pslld ($T1,2);
  388. &pslld ($T0,2);
  389. &paddd ($T1,$D3); # *5
  390. &paddd ($T0,$D4); # *5
  391. &movdqa (&QWP(16*7,"esp"),$T1);
  392. &movdqa (&QWP(16*8,"esp"),$T0);
  393. &pshufd ($T1,$D0,0b01000100);
  394. &movdqa ($T0,$D1);
  395. &pshufd ($D1,$D1,0b01000100);
  396. &pshufd ($D2,$D2,0b01000100);
  397. &pshufd ($D3,$D3,0b01000100);
  398. &pshufd ($D4,$D4,0b01000100);
  399. &movdqa (&QWP(16*0,"edx"),$T1);
  400. &movdqa (&QWP(16*1,"edx"),$D1);
  401. &movdqa (&QWP(16*2,"edx"),$D2);
  402. &movdqa (&QWP(16*3,"edx"),$D3);
  403. &movdqa (&QWP(16*4,"edx"),$D4);
  404. ################################################################
  405. # d4 = h4*r0 + h3*r1 + h2*r2 + h1*r3 + h0*r4
  406. # d3 = h3*r0 + h2*r1 + h1*r2 + h0*r3 + h4*5*r4
  407. # d2 = h2*r0 + h1*r1 + h0*r2 + h4*5*r3 + h3*5*r4
  408. # d1 = h1*r0 + h0*r1 + h4*5*r2 + h3*5*r3 + h2*5*r4
  409. # d0 = h0*r0 + h4*5*r1 + h3*5*r2 + h2*5*r3 + h1*5*r4
  410. &pmuludq ($D4,$D0); # h4*r0
  411. &pmuludq ($D3,$D0); # h3*r0
  412. &pmuludq ($D2,$D0); # h2*r0
  413. &pmuludq ($D1,$D0); # h1*r0
  414. &pmuludq ($D0,$T1); # h0*r0
  415. sub pmuladd {
  416. my $load = shift;
  417. my $base = shift; $base = "esp" if (!defined($base));
  418. ################################################################
  419. # As for choice to "rotate" $T0-$T2 in order to move paddq
  420. # past next multiplication. While it makes code harder to read
  421. # and doesn't have significant effect on most processors, it
  422. # makes a lot of difference on Atom, up to 30% improvement.
  423. &movdqa ($T1,$T0);
  424. &pmuludq ($T0,&QWP(16*3,$base)); # r1*h3
  425. &movdqa ($T2,$T1);
  426. &pmuludq ($T1,&QWP(16*2,$base)); # r1*h2
  427. &paddq ($D4,$T0);
  428. &movdqa ($T0,$T2);
  429. &pmuludq ($T2,&QWP(16*1,$base)); # r1*h1
  430. &paddq ($D3,$T1);
  431. &$load ($T1,5); # s1
  432. &pmuludq ($T0,&QWP(16*0,$base)); # r1*h0
  433. &paddq ($D2,$T2);
  434. &pmuludq ($T1,&QWP(16*4,$base)); # s1*h4
  435. &$load ($T2,2); # r2^n
  436. &paddq ($D1,$T0);
  437. &movdqa ($T0,$T2);
  438. &pmuludq ($T2,&QWP(16*2,$base)); # r2*h2
  439. &paddq ($D0,$T1);
  440. &movdqa ($T1,$T0);
  441. &pmuludq ($T0,&QWP(16*1,$base)); # r2*h1
  442. &paddq ($D4,$T2);
  443. &$load ($T2,6); # s2^n
  444. &pmuludq ($T1,&QWP(16*0,$base)); # r2*h0
  445. &paddq ($D3,$T0);
  446. &movdqa ($T0,$T2);
  447. &pmuludq ($T2,&QWP(16*4,$base)); # s2*h4
  448. &paddq ($D2,$T1);
  449. &pmuludq ($T0,&QWP(16*3,$base)); # s2*h3
  450. &$load ($T1,3); # r3^n
  451. &paddq ($D1,$T2);
  452. &movdqa ($T2,$T1);
  453. &pmuludq ($T1,&QWP(16*1,$base)); # r3*h1
  454. &paddq ($D0,$T0);
  455. &$load ($T0,7); # s3^n
  456. &pmuludq ($T2,&QWP(16*0,$base)); # r3*h0
  457. &paddq ($D4,$T1);
  458. &movdqa ($T1,$T0);
  459. &pmuludq ($T0,&QWP(16*4,$base)); # s3*h4
  460. &paddq ($D3,$T2);
  461. &movdqa ($T2,$T1);
  462. &pmuludq ($T1,&QWP(16*3,$base)); # s3*h3
  463. &paddq ($D2,$T0);
  464. &pmuludq ($T2,&QWP(16*2,$base)); # s3*h2
  465. &$load ($T0,4); # r4^n
  466. &paddq ($D1,$T1);
  467. &$load ($T1,8); # s4^n
  468. &pmuludq ($T0,&QWP(16*0,$base)); # r4*h0
  469. &paddq ($D0,$T2);
  470. &movdqa ($T2,$T1);
  471. &pmuludq ($T1,&QWP(16*4,$base)); # s4*h4
  472. &paddq ($D4,$T0);
  473. &movdqa ($T0,$T2);
  474. &pmuludq ($T2,&QWP(16*1,$base)); # s4*h1
  475. &paddq ($D3,$T1);
  476. &movdqa ($T1,$T0);
  477. &pmuludq ($T0,&QWP(16*2,$base)); # s4*h2
  478. &paddq ($D0,$T2);
  479. &pmuludq ($T1,&QWP(16*3,$base)); # s4*h3
  480. &movdqa ($MASK,&QWP(64,"ebx"));
  481. &paddq ($D1,$T0);
  482. &paddq ($D2,$T1);
  483. }
  484. &pmuladd (sub { my ($reg,$i)=@_;
  485. &movdqa ($reg,&QWP(16*$i,"esp"));
  486. },"edx");
  487. sub lazy_reduction {
  488. my $extra = shift;
  489. ################################################################
  490. # lazy reduction as discussed in "NEON crypto" by D.J. Bernstein
  491. # and P. Schwabe
  492. #
  493. # [(*) see discussion in poly1305-armv4 module]
  494. &movdqa ($T0,$D3);
  495. &pand ($D3,$MASK);
  496. &psrlq ($T0,26);
  497. &$extra () if (defined($extra));
  498. &paddq ($T0,$D4); # h3 -> h4
  499. &movdqa ($T1,$D0);
  500. &pand ($D0,$MASK);
  501. &psrlq ($T1,26);
  502. &movdqa ($D4,$T0);
  503. &paddq ($T1,$D1); # h0 -> h1
  504. &psrlq ($T0,26);
  505. &pand ($D4,$MASK);
  506. &movdqa ($D1,$T1);
  507. &psrlq ($T1,26);
  508. &paddd ($D0,$T0); # favour paddd when
  509. # possible, because
  510. # paddq is "broken"
  511. # on Atom
  512. &psllq ($T0,2);
  513. &paddq ($T1,$D2); # h1 -> h2
  514. &paddq ($T0,$D0); # h4 -> h0 (*)
  515. &pand ($D1,$MASK);
  516. &movdqa ($D2,$T1);
  517. &psrlq ($T1,26);
  518. &pand ($D2,$MASK);
  519. &paddd ($T1,$D3); # h2 -> h3
  520. &movdqa ($D0,$T0);
  521. &psrlq ($T0,26);
  522. &movdqa ($D3,$T1);
  523. &psrlq ($T1,26);
  524. &pand ($D0,$MASK);
  525. &paddd ($D1,$T0); # h0 -> h1
  526. &pand ($D3,$MASK);
  527. &paddd ($D4,$T1); # h3 -> h4
  528. }
  529. &lazy_reduction ();
  530. &dec ("ecx");
  531. &jz (&label("square_break"));
  532. &punpcklqdq ($D0,&QWP(16*0,"esp")); # 0:r^1:0:r^2
  533. &punpcklqdq ($D1,&QWP(16*1,"esp"));
  534. &punpcklqdq ($D2,&QWP(16*2,"esp"));
  535. &punpcklqdq ($D3,&QWP(16*3,"esp"));
  536. &punpcklqdq ($D4,&QWP(16*4,"esp"));
  537. &jmp (&label("square"));
  538. &set_label("square_break");
  539. &psllq ($D0,32); # -> r^3:0:r^4:0
  540. &psllq ($D1,32);
  541. &psllq ($D2,32);
  542. &psllq ($D3,32);
  543. &psllq ($D4,32);
  544. &por ($D0,&QWP(16*0,"esp")); # r^3:r^1:r^4:r^2
  545. &por ($D1,&QWP(16*1,"esp"));
  546. &por ($D2,&QWP(16*2,"esp"));
  547. &por ($D3,&QWP(16*3,"esp"));
  548. &por ($D4,&QWP(16*4,"esp"));
  549. &pshufd ($D0,$D0,0b10001101); # -> r^1:r^2:r^3:r^4
  550. &pshufd ($D1,$D1,0b10001101);
  551. &pshufd ($D2,$D2,0b10001101);
  552. &pshufd ($D3,$D3,0b10001101);
  553. &pshufd ($D4,$D4,0b10001101);
  554. &movdqu (&QWP(16*0,"edi"),$D0); # save the table
  555. &movdqu (&QWP(16*1,"edi"),$D1);
  556. &movdqu (&QWP(16*2,"edi"),$D2);
  557. &movdqu (&QWP(16*3,"edi"),$D3);
  558. &movdqu (&QWP(16*4,"edi"),$D4);
  559. &movdqa ($T1,$D1);
  560. &movdqa ($T0,$D2);
  561. &pslld ($T1,2);
  562. &pslld ($T0,2);
  563. &paddd ($T1,$D1); # *5
  564. &paddd ($T0,$D2); # *5
  565. &movdqu (&QWP(16*5,"edi"),$T1);
  566. &movdqu (&QWP(16*6,"edi"),$T0);
  567. &movdqa ($T1,$D3);
  568. &movdqa ($T0,$D4);
  569. &pslld ($T1,2);
  570. &pslld ($T0,2);
  571. &paddd ($T1,$D3); # *5
  572. &paddd ($T0,$D4); # *5
  573. &movdqu (&QWP(16*7,"edi"),$T1);
  574. &movdqu (&QWP(16*8,"edi"),$T0);
  575. &mov ("esp","ebp");
  576. &lea ("edi",&DWP(-16*3,"edi")); # size de-optimization
  577. &ret ();
  578. &function_end_B("_poly1305_init_sse2");
  579. &align (32);
  580. &function_begin("_poly1305_blocks_sse2");
  581. &mov ("edi",&wparam(0)); # ctx
  582. &mov ("esi",&wparam(1)); # inp
  583. &mov ("ecx",&wparam(2)); # len
  584. &mov ("eax",&DWP(4*5,"edi")); # is_base2_26
  585. &and ("ecx",-16);
  586. &jz (&label("nodata"));
  587. &cmp ("ecx",64);
  588. &jae (&label("enter_sse2"));
  589. &test ("eax","eax"); # is_base2_26?
  590. &jz (&label("enter_blocks"));
  591. &set_label("enter_sse2",16);
  592. &call (&label("pic_point"));
  593. &set_label("pic_point");
  594. &blindpop("ebx");
  595. &lea ("ebx",&DWP(&label("const_sse2")."-".&label("pic_point"),"ebx"));
  596. &test ("eax","eax"); # is_base2_26?
  597. &jnz (&label("base2_26"));
  598. &call ("_poly1305_init_sse2");
  599. ################################################# base 2^32 -> base 2^26
  600. &mov ("eax",&DWP(0,"edi"));
  601. &mov ("ecx",&DWP(3,"edi"));
  602. &mov ("edx",&DWP(6,"edi"));
  603. &mov ("esi",&DWP(9,"edi"));
  604. &mov ("ebp",&DWP(13,"edi"));
  605. &mov (&DWP(4*5,"edi"),1); # is_base2_26
  606. &shr ("ecx",2);
  607. &and ("eax",0x3ffffff);
  608. &shr ("edx",4);
  609. &and ("ecx",0x3ffffff);
  610. &shr ("esi",6);
  611. &and ("edx",0x3ffffff);
  612. &movd ($D0,"eax");
  613. &movd ($D1,"ecx");
  614. &movd ($D2,"edx");
  615. &movd ($D3,"esi");
  616. &movd ($D4,"ebp");
  617. &mov ("esi",&wparam(1)); # [reload] inp
  618. &mov ("ecx",&wparam(2)); # [reload] len
  619. &jmp (&label("base2_32"));
  620. &set_label("base2_26",16);
  621. &movd ($D0,&DWP(4*0,"edi")); # load hash value
  622. &movd ($D1,&DWP(4*1,"edi"));
  623. &movd ($D2,&DWP(4*2,"edi"));
  624. &movd ($D3,&DWP(4*3,"edi"));
  625. &movd ($D4,&DWP(4*4,"edi"));
  626. &movdqa ($MASK,&QWP(64,"ebx"));
  627. &set_label("base2_32");
  628. &mov ("eax",&wparam(3)); # padbit
  629. &mov ("ebp","esp");
  630. &sub ("esp",16*(5+5+5+9+9));
  631. &and ("esp",-16);
  632. &lea ("edi",&DWP(16*3,"edi")); # size optimization
  633. &shl ("eax",24); # padbit
  634. &test ("ecx",31);
  635. &jz (&label("even"));
  636. ################################################################
  637. # process single block, with SSE2, because it's still faster
  638. # even though half of result is discarded
  639. &movdqu ($T1,&QWP(0,"esi")); # input
  640. &lea ("esi",&DWP(16,"esi"));
  641. &movdqa ($T0,$T1); # -> base 2^26 ...
  642. &pand ($T1,$MASK);
  643. &paddd ($D0,$T1); # ... and accumulate
  644. &movdqa ($T1,$T0);
  645. &psrlq ($T0,26);
  646. &psrldq ($T1,6);
  647. &pand ($T0,$MASK);
  648. &paddd ($D1,$T0);
  649. &movdqa ($T0,$T1);
  650. &psrlq ($T1,4);
  651. &pand ($T1,$MASK);
  652. &paddd ($D2,$T1);
  653. &movdqa ($T1,$T0);
  654. &psrlq ($T0,30);
  655. &pand ($T0,$MASK);
  656. &psrldq ($T1,7);
  657. &paddd ($D3,$T0);
  658. &movd ($T0,"eax"); # padbit
  659. &paddd ($D4,$T1);
  660. &movd ($T1,&DWP(16*0+12,"edi")); # r0
  661. &paddd ($D4,$T0);
  662. &movdqa (&QWP(16*0,"esp"),$D0);
  663. &movdqa (&QWP(16*1,"esp"),$D1);
  664. &movdqa (&QWP(16*2,"esp"),$D2);
  665. &movdqa (&QWP(16*3,"esp"),$D3);
  666. &movdqa (&QWP(16*4,"esp"),$D4);
  667. ################################################################
  668. # d4 = h4*r0 + h3*r1 + h2*r2 + h1*r3 + h0*r4
  669. # d3 = h3*r0 + h2*r1 + h1*r2 + h0*r3 + h4*5*r4
  670. # d2 = h2*r0 + h1*r1 + h0*r2 + h4*5*r3 + h3*5*r4
  671. # d1 = h1*r0 + h0*r1 + h4*5*r2 + h3*5*r3 + h2*5*r4
  672. # d0 = h0*r0 + h4*5*r1 + h3*5*r2 + h2*5*r3 + h1*5*r4
  673. &pmuludq ($D0,$T1); # h4*r0
  674. &pmuludq ($D1,$T1); # h3*r0
  675. &pmuludq ($D2,$T1); # h2*r0
  676. &movd ($T0,&DWP(16*1+12,"edi")); # r1
  677. &pmuludq ($D3,$T1); # h1*r0
  678. &pmuludq ($D4,$T1); # h0*r0
  679. &pmuladd (sub { my ($reg,$i)=@_;
  680. &movd ($reg,&DWP(16*$i+12,"edi"));
  681. });
  682. &lazy_reduction ();
  683. &sub ("ecx",16);
  684. &jz (&label("done"));
  685. &set_label("even");
  686. &lea ("edx",&DWP(16*(5+5+5+9),"esp"));# size optimization
  687. &lea ("eax",&DWP(-16*2,"esi"));
  688. &sub ("ecx",64);
  689. ################################################################
  690. # expand and copy pre-calculated table to stack
  691. &movdqu ($T0,&QWP(16*0,"edi")); # r^1:r^2:r^3:r^4
  692. &pshufd ($T1,$T0,0b01000100); # duplicate r^3:r^4
  693. &cmovb ("esi","eax");
  694. &pshufd ($T0,$T0,0b11101110); # duplicate r^1:r^2
  695. &movdqa (&QWP(16*0,"edx"),$T1);
  696. &lea ("eax",&DWP(16*10,"esp"));
  697. &movdqu ($T1,&QWP(16*1,"edi"));
  698. &movdqa (&QWP(16*(0-9),"edx"),$T0);
  699. &pshufd ($T0,$T1,0b01000100);
  700. &pshufd ($T1,$T1,0b11101110);
  701. &movdqa (&QWP(16*1,"edx"),$T0);
  702. &movdqu ($T0,&QWP(16*2,"edi"));
  703. &movdqa (&QWP(16*(1-9),"edx"),$T1);
  704. &pshufd ($T1,$T0,0b01000100);
  705. &pshufd ($T0,$T0,0b11101110);
  706. &movdqa (&QWP(16*2,"edx"),$T1);
  707. &movdqu ($T1,&QWP(16*3,"edi"));
  708. &movdqa (&QWP(16*(2-9),"edx"),$T0);
  709. &pshufd ($T0,$T1,0b01000100);
  710. &pshufd ($T1,$T1,0b11101110);
  711. &movdqa (&QWP(16*3,"edx"),$T0);
  712. &movdqu ($T0,&QWP(16*4,"edi"));
  713. &movdqa (&QWP(16*(3-9),"edx"),$T1);
  714. &pshufd ($T1,$T0,0b01000100);
  715. &pshufd ($T0,$T0,0b11101110);
  716. &movdqa (&QWP(16*4,"edx"),$T1);
  717. &movdqu ($T1,&QWP(16*5,"edi"));
  718. &movdqa (&QWP(16*(4-9),"edx"),$T0);
  719. &pshufd ($T0,$T1,0b01000100);
  720. &pshufd ($T1,$T1,0b11101110);
  721. &movdqa (&QWP(16*5,"edx"),$T0);
  722. &movdqu ($T0,&QWP(16*6,"edi"));
  723. &movdqa (&QWP(16*(5-9),"edx"),$T1);
  724. &pshufd ($T1,$T0,0b01000100);
  725. &pshufd ($T0,$T0,0b11101110);
  726. &movdqa (&QWP(16*6,"edx"),$T1);
  727. &movdqu ($T1,&QWP(16*7,"edi"));
  728. &movdqa (&QWP(16*(6-9),"edx"),$T0);
  729. &pshufd ($T0,$T1,0b01000100);
  730. &pshufd ($T1,$T1,0b11101110);
  731. &movdqa (&QWP(16*7,"edx"),$T0);
  732. &movdqu ($T0,&QWP(16*8,"edi"));
  733. &movdqa (&QWP(16*(7-9),"edx"),$T1);
  734. &pshufd ($T1,$T0,0b01000100);
  735. &pshufd ($T0,$T0,0b11101110);
  736. &movdqa (&QWP(16*8,"edx"),$T1);
  737. &movdqa (&QWP(16*(8-9),"edx"),$T0);
  738. sub load_input {
  739. my ($inpbase,$offbase)=@_;
  740. &movdqu ($T0,&QWP($inpbase+0,"esi")); # load input
  741. &movdqu ($T1,&QWP($inpbase+16,"esi"));
  742. &lea ("esi",&DWP(16*2,"esi"));
  743. &movdqa (&QWP($offbase+16*2,"esp"),$D2);
  744. &movdqa (&QWP($offbase+16*3,"esp"),$D3);
  745. &movdqa (&QWP($offbase+16*4,"esp"),$D4);
  746. &movdqa ($D2,$T0); # splat input
  747. &movdqa ($D3,$T1);
  748. &psrldq ($D2,6);
  749. &psrldq ($D3,6);
  750. &movdqa ($D4,$T0);
  751. &punpcklqdq ($D2,$D3); # 2:3
  752. &punpckhqdq ($D4,$T1); # 4
  753. &punpcklqdq ($T0,$T1); # 0:1
  754. &movdqa ($D3,$D2);
  755. &psrlq ($D2,4);
  756. &psrlq ($D3,30);
  757. &movdqa ($T1,$T0);
  758. &psrlq ($D4,40); # 4
  759. &psrlq ($T1,26);
  760. &pand ($T0,$MASK); # 0
  761. &pand ($T1,$MASK); # 1
  762. &pand ($D2,$MASK); # 2
  763. &pand ($D3,$MASK); # 3
  764. &por ($D4,&QWP(0,"ebx")); # padbit, yes, always
  765. &movdqa (&QWP($offbase+16*0,"esp"),$D0) if ($offbase);
  766. &movdqa (&QWP($offbase+16*1,"esp"),$D1) if ($offbase);
  767. }
  768. &load_input (16*2,16*5);
  769. &jbe (&label("skip_loop"));
  770. &jmp (&label("loop"));
  771. &set_label("loop",32);
  772. ################################################################
  773. # ((inp[0]*r^4+inp[2]*r^2+inp[4])*r^4+inp[6]*r^2
  774. # ((inp[1]*r^4+inp[3]*r^2+inp[5])*r^3+inp[7]*r
  775. # \___________________/
  776. # ((inp[0]*r^4+inp[2]*r^2+inp[4])*r^4+inp[6]*r^2+inp[8])*r^2
  777. # ((inp[1]*r^4+inp[3]*r^2+inp[5])*r^4+inp[7]*r^2+inp[9])*r
  778. # \___________________/ \____________________/
  779. ################################################################
  780. &movdqa ($T2,&QWP(16*(0-9),"edx")); # r0^2
  781. &movdqa (&QWP(16*1,"eax"),$T1);
  782. &movdqa (&QWP(16*2,"eax"),$D2);
  783. &movdqa (&QWP(16*3,"eax"),$D3);
  784. &movdqa (&QWP(16*4,"eax"),$D4);
  785. ################################################################
  786. # d4 = h4*r0 + h0*r4 + h1*r3 + h2*r2 + h3*r1
  787. # d3 = h3*r0 + h0*r3 + h1*r2 + h2*r1 + h4*5*r4
  788. # d2 = h2*r0 + h0*r2 + h1*r1 + h3*5*r4 + h4*5*r3
  789. # d1 = h1*r0 + h0*r1 + h2*5*r4 + h3*5*r3 + h4*5*r2
  790. # d0 = h0*r0 + h1*5*r4 + h2*5*r3 + h3*5*r2 + h4*5*r1
  791. &movdqa ($D1,$T0);
  792. &pmuludq ($T0,$T2); # h0*r0
  793. &movdqa ($D0,$T1);
  794. &pmuludq ($T1,$T2); # h1*r0
  795. &pmuludq ($D2,$T2); # h2*r0
  796. &pmuludq ($D3,$T2); # h3*r0
  797. &pmuludq ($D4,$T2); # h4*r0
  798. sub pmuladd_alt {
  799. my $addr = shift;
  800. &pmuludq ($D0,&$addr(8)); # h1*s4
  801. &movdqa ($T2,$D1);
  802. &pmuludq ($D1,&$addr(1)); # h0*r1
  803. &paddq ($D0,$T0);
  804. &movdqa ($T0,$T2);
  805. &pmuludq ($T2,&$addr(2)); # h0*r2
  806. &paddq ($D1,$T1);
  807. &movdqa ($T1,$T0);
  808. &pmuludq ($T0,&$addr(3)); # h0*r3
  809. &paddq ($D2,$T2);
  810. &movdqa ($T2,&QWP(16*1,"eax")); # pull h1
  811. &pmuludq ($T1,&$addr(4)); # h0*r4
  812. &paddq ($D3,$T0);
  813. &movdqa ($T0,$T2);
  814. &pmuludq ($T2,&$addr(1)); # h1*r1
  815. &paddq ($D4,$T1);
  816. &movdqa ($T1,$T0);
  817. &pmuludq ($T0,&$addr(2)); # h1*r2
  818. &paddq ($D2,$T2);
  819. &movdqa ($T2,&QWP(16*2,"eax")); # pull h2
  820. &pmuludq ($T1,&$addr(3)); # h1*r3
  821. &paddq ($D3,$T0);
  822. &movdqa ($T0,$T2);
  823. &pmuludq ($T2,&$addr(7)); # h2*s3
  824. &paddq ($D4,$T1);
  825. &movdqa ($T1,$T0);
  826. &pmuludq ($T0,&$addr(8)); # h2*s4
  827. &paddq ($D0,$T2);
  828. &movdqa ($T2,$T1);
  829. &pmuludq ($T1,&$addr(1)); # h2*r1
  830. &paddq ($D1,$T0);
  831. &movdqa ($T0,&QWP(16*3,"eax")); # pull h3
  832. &pmuludq ($T2,&$addr(2)); # h2*r2
  833. &paddq ($D3,$T1);
  834. &movdqa ($T1,$T0);
  835. &pmuludq ($T0,&$addr(6)); # h3*s2
  836. &paddq ($D4,$T2);
  837. &movdqa ($T2,$T1);
  838. &pmuludq ($T1,&$addr(7)); # h3*s3
  839. &paddq ($D0,$T0);
  840. &movdqa ($T0,$T2);
  841. &pmuludq ($T2,&$addr(8)); # h3*s4
  842. &paddq ($D1,$T1);
  843. &movdqa ($T1,&QWP(16*4,"eax")); # pull h4
  844. &pmuludq ($T0,&$addr(1)); # h3*r1
  845. &paddq ($D2,$T2);
  846. &movdqa ($T2,$T1);
  847. &pmuludq ($T1,&$addr(8)); # h4*s4
  848. &paddq ($D4,$T0);
  849. &movdqa ($T0,$T2);
  850. &pmuludq ($T2,&$addr(5)); # h4*s1
  851. &paddq ($D3,$T1);
  852. &movdqa ($T1,$T0);
  853. &pmuludq ($T0,&$addr(6)); # h4*s2
  854. &paddq ($D0,$T2);
  855. &movdqa ($MASK,&QWP(64,"ebx"));
  856. &pmuludq ($T1,&$addr(7)); # h4*s3
  857. &paddq ($D1,$T0);
  858. &paddq ($D2,$T1);
  859. }
  860. &pmuladd_alt (sub { my $i=shift; &QWP(16*($i-9),"edx"); });
  861. &load_input (-16*2,0);
  862. &lea ("eax",&DWP(-16*2,"esi"));
  863. &sub ("ecx",64);
  864. &paddd ($T0,&QWP(16*(5+0),"esp")); # add hash value
  865. &paddd ($T1,&QWP(16*(5+1),"esp"));
  866. &paddd ($D2,&QWP(16*(5+2),"esp"));
  867. &paddd ($D3,&QWP(16*(5+3),"esp"));
  868. &paddd ($D4,&QWP(16*(5+4),"esp"));
  869. &cmovb ("esi","eax");
  870. &lea ("eax",&DWP(16*10,"esp"));
  871. &movdqa ($T2,&QWP(16*0,"edx")); # r0^4
  872. &movdqa (&QWP(16*1,"esp"),$D1);
  873. &movdqa (&QWP(16*1,"eax"),$T1);
  874. &movdqa (&QWP(16*2,"eax"),$D2);
  875. &movdqa (&QWP(16*3,"eax"),$D3);
  876. &movdqa (&QWP(16*4,"eax"),$D4);
  877. ################################################################
  878. # d4 += h4*r0 + h0*r4 + h1*r3 + h2*r2 + h3*r1
  879. # d3 += h3*r0 + h0*r3 + h1*r2 + h2*r1 + h4*5*r4
  880. # d2 += h2*r0 + h0*r2 + h1*r1 + h3*5*r4 + h4*5*r3
  881. # d1 += h1*r0 + h0*r1 + h2*5*r4 + h3*5*r3 + h4*5*r2
  882. # d0 += h0*r0 + h1*5*r4 + h2*5*r3 + h3*5*r2 + h4*5*r1
  883. &movdqa ($D1,$T0);
  884. &pmuludq ($T0,$T2); # h0*r0
  885. &paddq ($T0,$D0);
  886. &movdqa ($D0,$T1);
  887. &pmuludq ($T1,$T2); # h1*r0
  888. &pmuludq ($D2,$T2); # h2*r0
  889. &pmuludq ($D3,$T2); # h3*r0
  890. &pmuludq ($D4,$T2); # h4*r0
  891. &paddq ($T1,&QWP(16*1,"esp"));
  892. &paddq ($D2,&QWP(16*2,"esp"));
  893. &paddq ($D3,&QWP(16*3,"esp"));
  894. &paddq ($D4,&QWP(16*4,"esp"));
  895. &pmuladd_alt (sub { my $i=shift; &QWP(16*$i,"edx"); });
  896. &lazy_reduction ();
  897. &load_input (16*2,16*5);
  898. &ja (&label("loop"));
  899. &set_label("skip_loop");
  900. ################################################################
  901. # multiply (inp[0:1]+hash) or inp[2:3] by r^2:r^1
  902. &pshufd ($T2,&QWP(16*(0-9),"edx"),0x10);# r0^n
  903. &add ("ecx",32);
  904. &jnz (&label("long_tail"));
  905. &paddd ($T0,$D0); # add hash value
  906. &paddd ($T1,$D1);
  907. &paddd ($D2,&QWP(16*7,"esp"));
  908. &paddd ($D3,&QWP(16*8,"esp"));
  909. &paddd ($D4,&QWP(16*9,"esp"));
  910. &set_label("long_tail");
  911. &movdqa (&QWP(16*0,"eax"),$T0);
  912. &movdqa (&QWP(16*1,"eax"),$T1);
  913. &movdqa (&QWP(16*2,"eax"),$D2);
  914. &movdqa (&QWP(16*3,"eax"),$D3);
  915. &movdqa (&QWP(16*4,"eax"),$D4);
  916. ################################################################
  917. # d4 = h4*r0 + h3*r1 + h2*r2 + h1*r3 + h0*r4
  918. # d3 = h3*r0 + h2*r1 + h1*r2 + h0*r3 + h4*5*r4
  919. # d2 = h2*r0 + h1*r1 + h0*r2 + h4*5*r3 + h3*5*r4
  920. # d1 = h1*r0 + h0*r1 + h4*5*r2 + h3*5*r3 + h2*5*r4
  921. # d0 = h0*r0 + h4*5*r1 + h3*5*r2 + h2*5*r3 + h1*5*r4
  922. &pmuludq ($T0,$T2); # h0*r0
  923. &pmuludq ($T1,$T2); # h1*r0
  924. &pmuludq ($D2,$T2); # h2*r0
  925. &movdqa ($D0,$T0);
  926. &pshufd ($T0,&QWP(16*(1-9),"edx"),0x10);# r1^n
  927. &pmuludq ($D3,$T2); # h3*r0
  928. &movdqa ($D1,$T1);
  929. &pmuludq ($D4,$T2); # h4*r0
  930. &pmuladd (sub { my ($reg,$i)=@_;
  931. &pshufd ($reg,&QWP(16*($i-9),"edx"),0x10);
  932. },"eax");
  933. &jz (&label("short_tail"));
  934. &load_input (-16*2,0);
  935. &pshufd ($T2,&QWP(16*0,"edx"),0x10); # r0^n
  936. &paddd ($T0,&QWP(16*5,"esp")); # add hash value
  937. &paddd ($T1,&QWP(16*6,"esp"));
  938. &paddd ($D2,&QWP(16*7,"esp"));
  939. &paddd ($D3,&QWP(16*8,"esp"));
  940. &paddd ($D4,&QWP(16*9,"esp"));
  941. ################################################################
  942. # multiply inp[0:1] by r^4:r^3 and accumulate
  943. &movdqa (&QWP(16*0,"esp"),$T0);
  944. &pmuludq ($T0,$T2); # h0*r0
  945. &movdqa (&QWP(16*1,"esp"),$T1);
  946. &pmuludq ($T1,$T2); # h1*r0
  947. &paddq ($D0,$T0);
  948. &movdqa ($T0,$D2);
  949. &pmuludq ($D2,$T2); # h2*r0
  950. &paddq ($D1,$T1);
  951. &movdqa ($T1,$D3);
  952. &pmuludq ($D3,$T2); # h3*r0
  953. &paddq ($D2,&QWP(16*2,"esp"));
  954. &movdqa (&QWP(16*2,"esp"),$T0);
  955. &pshufd ($T0,&QWP(16*1,"edx"),0x10); # r1^n
  956. &paddq ($D3,&QWP(16*3,"esp"));
  957. &movdqa (&QWP(16*3,"esp"),$T1);
  958. &movdqa ($T1,$D4);
  959. &pmuludq ($D4,$T2); # h4*r0
  960. &paddq ($D4,&QWP(16*4,"esp"));
  961. &movdqa (&QWP(16*4,"esp"),$T1);
  962. &pmuladd (sub { my ($reg,$i)=@_;
  963. &pshufd ($reg,&QWP(16*$i,"edx"),0x10);
  964. });
  965. &set_label("short_tail");
  966. ################################################################
  967. # horizontal addition
  968. &pshufd ($T1,$D4,0b01001110);
  969. &pshufd ($T0,$D3,0b01001110);
  970. &paddq ($D4,$T1);
  971. &paddq ($D3,$T0);
  972. &pshufd ($T1,$D0,0b01001110);
  973. &pshufd ($T0,$D1,0b01001110);
  974. &paddq ($D0,$T1);
  975. &paddq ($D1,$T0);
  976. &pshufd ($T1,$D2,0b01001110);
  977. #&paddq ($D2,$T1);
  978. &lazy_reduction (sub { &paddq ($D2,$T1) });
  979. &set_label("done");
  980. &movd (&DWP(-16*3+4*0,"edi"),$D0); # store hash value
  981. &movd (&DWP(-16*3+4*1,"edi"),$D1);
  982. &movd (&DWP(-16*3+4*2,"edi"),$D2);
  983. &movd (&DWP(-16*3+4*3,"edi"),$D3);
  984. &movd (&DWP(-16*3+4*4,"edi"),$D4);
  985. &mov ("esp","ebp");
  986. &set_label("nodata");
  987. &function_end("_poly1305_blocks_sse2");
  988. &align (32);
  989. &function_begin("_poly1305_emit_sse2");
  990. &mov ("ebp",&wparam(0)); # context
  991. &cmp (&DWP(4*5,"ebp"),0); # is_base2_26?
  992. &je (&label("enter_emit"));
  993. &mov ("eax",&DWP(4*0,"ebp")); # load hash value
  994. &mov ("edi",&DWP(4*1,"ebp"));
  995. &mov ("ecx",&DWP(4*2,"ebp"));
  996. &mov ("edx",&DWP(4*3,"ebp"));
  997. &mov ("esi",&DWP(4*4,"ebp"));
  998. &mov ("ebx","edi"); # base 2^26 -> base 2^32
  999. &shl ("edi",26);
  1000. &shr ("ebx",6);
  1001. &add ("eax","edi");
  1002. &mov ("edi","ecx");
  1003. &adc ("ebx",0);
  1004. &shl ("edi",20);
  1005. &shr ("ecx",12);
  1006. &add ("ebx","edi");
  1007. &mov ("edi","edx");
  1008. &adc ("ecx",0);
  1009. &shl ("edi",14);
  1010. &shr ("edx",18);
  1011. &add ("ecx","edi");
  1012. &mov ("edi","esi");
  1013. &adc ("edx",0);
  1014. &shl ("edi",8);
  1015. &shr ("esi",24);
  1016. &add ("edx","edi");
  1017. &adc ("esi",0); # can be partially reduced
  1018. &mov ("edi","esi"); # final reduction
  1019. &and ("esi",3);
  1020. &shr ("edi",2);
  1021. &lea ("ebp",&DWP(0,"edi","edi",4)); # *5
  1022. &mov ("edi",&wparam(1)); # output
  1023. &add ("eax","ebp");
  1024. &mov ("ebp",&wparam(2)); # key
  1025. &adc ("ebx",0);
  1026. &adc ("ecx",0);
  1027. &adc ("edx",0);
  1028. &adc ("esi",0);
  1029. &movd ($D0,"eax"); # offload original hash value
  1030. &add ("eax",5); # compare to modulus
  1031. &movd ($D1,"ebx");
  1032. &adc ("ebx",0);
  1033. &movd ($D2,"ecx");
  1034. &adc ("ecx",0);
  1035. &movd ($D3,"edx");
  1036. &adc ("edx",0);
  1037. &adc ("esi",0);
  1038. &shr ("esi",2); # did it carry/borrow?
  1039. &neg ("esi"); # do we choose (hash-modulus) ...
  1040. &and ("eax","esi");
  1041. &and ("ebx","esi");
  1042. &and ("ecx","esi");
  1043. &and ("edx","esi");
  1044. &mov (&DWP(4*0,"edi"),"eax");
  1045. &movd ("eax",$D0);
  1046. &mov (&DWP(4*1,"edi"),"ebx");
  1047. &movd ("ebx",$D1);
  1048. &mov (&DWP(4*2,"edi"),"ecx");
  1049. &movd ("ecx",$D2);
  1050. &mov (&DWP(4*3,"edi"),"edx");
  1051. &movd ("edx",$D3);
  1052. &not ("esi"); # ... or original hash value?
  1053. &and ("eax","esi");
  1054. &and ("ebx","esi");
  1055. &or ("eax",&DWP(4*0,"edi"));
  1056. &and ("ecx","esi");
  1057. &or ("ebx",&DWP(4*1,"edi"));
  1058. &and ("edx","esi");
  1059. &or ("ecx",&DWP(4*2,"edi"));
  1060. &or ("edx",&DWP(4*3,"edi"));
  1061. &add ("eax",&DWP(4*0,"ebp")); # accumulate key
  1062. &adc ("ebx",&DWP(4*1,"ebp"));
  1063. &mov (&DWP(4*0,"edi"),"eax");
  1064. &adc ("ecx",&DWP(4*2,"ebp"));
  1065. &mov (&DWP(4*1,"edi"),"ebx");
  1066. &adc ("edx",&DWP(4*3,"ebp"));
  1067. &mov (&DWP(4*2,"edi"),"ecx");
  1068. &mov (&DWP(4*3,"edi"),"edx");
  1069. &function_end("_poly1305_emit_sse2");
  1070. if ($avx>1) {
  1071. ########################################################################
  1072. # Note that poly1305_init_avx2 operates on %xmm, I could have used
  1073. # poly1305_init_sse2...
  1074. &align (32);
  1075. &function_begin_B("_poly1305_init_avx2");
  1076. &vmovdqu ($D4,&QWP(4*6,"edi")); # key base 2^32
  1077. &lea ("edi",&DWP(16*3,"edi")); # size optimization
  1078. &mov ("ebp","esp");
  1079. &sub ("esp",16*(9+5));
  1080. &and ("esp",-16);
  1081. #&vpand ($D4,$D4,&QWP(96,"ebx")); # magic mask
  1082. &vmovdqa ($MASK,&QWP(64,"ebx"));
  1083. &vpand ($D0,$D4,$MASK); # -> base 2^26
  1084. &vpsrlq ($D1,$D4,26);
  1085. &vpsrldq ($D3,$D4,6);
  1086. &vpand ($D1,$D1,$MASK);
  1087. &vpsrlq ($D2,$D3,4)
  1088. &vpsrlq ($D3,$D3,30);
  1089. &vpand ($D2,$D2,$MASK);
  1090. &vpand ($D3,$D3,$MASK);
  1091. &vpsrldq ($D4,$D4,13);
  1092. &lea ("edx",&DWP(16*9,"esp")); # size optimization
  1093. &mov ("ecx",2);
  1094. &set_label("square");
  1095. &vmovdqa (&QWP(16*0,"esp"),$D0);
  1096. &vmovdqa (&QWP(16*1,"esp"),$D1);
  1097. &vmovdqa (&QWP(16*2,"esp"),$D2);
  1098. &vmovdqa (&QWP(16*3,"esp"),$D3);
  1099. &vmovdqa (&QWP(16*4,"esp"),$D4);
  1100. &vpslld ($T1,$D1,2);
  1101. &vpslld ($T0,$D2,2);
  1102. &vpaddd ($T1,$T1,$D1); # *5
  1103. &vpaddd ($T0,$T0,$D2); # *5
  1104. &vmovdqa (&QWP(16*5,"esp"),$T1);
  1105. &vmovdqa (&QWP(16*6,"esp"),$T0);
  1106. &vpslld ($T1,$D3,2);
  1107. &vpslld ($T0,$D4,2);
  1108. &vpaddd ($T1,$T1,$D3); # *5
  1109. &vpaddd ($T0,$T0,$D4); # *5
  1110. &vmovdqa (&QWP(16*7,"esp"),$T1);
  1111. &vmovdqa (&QWP(16*8,"esp"),$T0);
  1112. &vpshufd ($T0,$D0,0b01000100);
  1113. &vmovdqa ($T1,$D1);
  1114. &vpshufd ($D1,$D1,0b01000100);
  1115. &vpshufd ($D2,$D2,0b01000100);
  1116. &vpshufd ($D3,$D3,0b01000100);
  1117. &vpshufd ($D4,$D4,0b01000100);
  1118. &vmovdqa (&QWP(16*0,"edx"),$T0);
  1119. &vmovdqa (&QWP(16*1,"edx"),$D1);
  1120. &vmovdqa (&QWP(16*2,"edx"),$D2);
  1121. &vmovdqa (&QWP(16*3,"edx"),$D3);
  1122. &vmovdqa (&QWP(16*4,"edx"),$D4);
  1123. ################################################################
  1124. # d4 = h4*r0 + h3*r1 + h2*r2 + h1*r3 + h0*r4
  1125. # d3 = h3*r0 + h2*r1 + h1*r2 + h0*r3 + h4*5*r4
  1126. # d2 = h2*r0 + h1*r1 + h0*r2 + h4*5*r3 + h3*5*r4
  1127. # d1 = h1*r0 + h0*r1 + h4*5*r2 + h3*5*r3 + h2*5*r4
  1128. # d0 = h0*r0 + h4*5*r1 + h3*5*r2 + h2*5*r3 + h1*5*r4
  1129. &vpmuludq ($D4,$D4,$D0); # h4*r0
  1130. &vpmuludq ($D3,$D3,$D0); # h3*r0
  1131. &vpmuludq ($D2,$D2,$D0); # h2*r0
  1132. &vpmuludq ($D1,$D1,$D0); # h1*r0
  1133. &vpmuludq ($D0,$T0,$D0); # h0*r0
  1134. &vpmuludq ($T0,$T1,&QWP(16*3,"edx")); # r1*h3
  1135. &vpaddq ($D4,$D4,$T0);
  1136. &vpmuludq ($T2,$T1,&QWP(16*2,"edx")); # r1*h2
  1137. &vpaddq ($D3,$D3,$T2);
  1138. &vpmuludq ($T0,$T1,&QWP(16*1,"edx")); # r1*h1
  1139. &vpaddq ($D2,$D2,$T0);
  1140. &vmovdqa ($T2,&QWP(16*5,"esp")); # s1
  1141. &vpmuludq ($T1,$T1,&QWP(16*0,"edx")); # r1*h0
  1142. &vpaddq ($D1,$D1,$T1);
  1143. &vmovdqa ($T0,&QWP(16*2,"esp")); # r2
  1144. &vpmuludq ($T2,$T2,&QWP(16*4,"edx")); # s1*h4
  1145. &vpaddq ($D0,$D0,$T2);
  1146. &vpmuludq ($T1,$T0,&QWP(16*2,"edx")); # r2*h2
  1147. &vpaddq ($D4,$D4,$T1);
  1148. &vpmuludq ($T2,$T0,&QWP(16*1,"edx")); # r2*h1
  1149. &vpaddq ($D3,$D3,$T2);
  1150. &vmovdqa ($T1,&QWP(16*6,"esp")); # s2
  1151. &vpmuludq ($T0,$T0,&QWP(16*0,"edx")); # r2*h0
  1152. &vpaddq ($D2,$D2,$T0);
  1153. &vpmuludq ($T2,$T1,&QWP(16*4,"edx")); # s2*h4
  1154. &vpaddq ($D1,$D1,$T2);
  1155. &vmovdqa ($T0,&QWP(16*3,"esp")); # r3
  1156. &vpmuludq ($T1,$T1,&QWP(16*3,"edx")); # s2*h3
  1157. &vpaddq ($D0,$D0,$T1);
  1158. &vpmuludq ($T2,$T0,&QWP(16*1,"edx")); # r3*h1
  1159. &vpaddq ($D4,$D4,$T2);
  1160. &vmovdqa ($T1,&QWP(16*7,"esp")); # s3
  1161. &vpmuludq ($T0,$T0,&QWP(16*0,"edx")); # r3*h0
  1162. &vpaddq ($D3,$D3,$T0);
  1163. &vpmuludq ($T2,$T1,&QWP(16*4,"edx")); # s3*h4
  1164. &vpaddq ($D2,$D2,$T2);
  1165. &vpmuludq ($T0,$T1,&QWP(16*3,"edx")); # s3*h3
  1166. &vpaddq ($D1,$D1,$T0);
  1167. &vmovdqa ($T2,&QWP(16*4,"esp")); # r4
  1168. &vpmuludq ($T1,$T1,&QWP(16*2,"edx")); # s3*h2
  1169. &vpaddq ($D0,$D0,$T1);
  1170. &vmovdqa ($T0,&QWP(16*8,"esp")); # s4
  1171. &vpmuludq ($T2,$T2,&QWP(16*0,"edx")); # r4*h0
  1172. &vpaddq ($D4,$D4,$T2);
  1173. &vpmuludq ($T1,$T0,&QWP(16*4,"edx")); # s4*h4
  1174. &vpaddq ($D3,$D3,$T1);
  1175. &vpmuludq ($T2,$T0,&QWP(16*1,"edx")); # s4*h1
  1176. &vpaddq ($D0,$D0,$T2);
  1177. &vpmuludq ($T1,$T0,&QWP(16*2,"edx")); # s4*h2
  1178. &vpaddq ($D1,$D1,$T1);
  1179. &vmovdqa ($MASK,&QWP(64,"ebx"));
  1180. &vpmuludq ($T0,$T0,&QWP(16*3,"edx")); # s4*h3
  1181. &vpaddq ($D2,$D2,$T0);
  1182. ################################################################
  1183. # lazy reduction
  1184. &vpsrlq ($T0,$D3,26);
  1185. &vpand ($D3,$D3,$MASK);
  1186. &vpsrlq ($T1,$D0,26);
  1187. &vpand ($D0,$D0,$MASK);
  1188. &vpaddq ($D4,$D4,$T0); # h3 -> h4
  1189. &vpaddq ($D1,$D1,$T1); # h0 -> h1
  1190. &vpsrlq ($T0,$D4,26);
  1191. &vpand ($D4,$D4,$MASK);
  1192. &vpsrlq ($T1,$D1,26);
  1193. &vpand ($D1,$D1,$MASK);
  1194. &vpaddq ($D2,$D2,$T1); # h1 -> h2
  1195. &vpaddd ($D0,$D0,$T0);
  1196. &vpsllq ($T0,$T0,2);
  1197. &vpsrlq ($T1,$D2,26);
  1198. &vpand ($D2,$D2,$MASK);
  1199. &vpaddd ($D0,$D0,$T0); # h4 -> h0
  1200. &vpaddd ($D3,$D3,$T1); # h2 -> h3
  1201. &vpsrlq ($T1,$D3,26);
  1202. &vpsrlq ($T0,$D0,26);
  1203. &vpand ($D0,$D0,$MASK);
  1204. &vpand ($D3,$D3,$MASK);
  1205. &vpaddd ($D1,$D1,$T0); # h0 -> h1
  1206. &vpaddd ($D4,$D4,$T1); # h3 -> h4
  1207. &dec ("ecx");
  1208. &jz (&label("square_break"));
  1209. &vpunpcklqdq ($D0,$D0,&QWP(16*0,"esp")); # 0:r^1:0:r^2
  1210. &vpunpcklqdq ($D1,$D1,&QWP(16*1,"esp"));
  1211. &vpunpcklqdq ($D2,$D2,&QWP(16*2,"esp"));
  1212. &vpunpcklqdq ($D3,$D3,&QWP(16*3,"esp"));
  1213. &vpunpcklqdq ($D4,$D4,&QWP(16*4,"esp"));
  1214. &jmp (&label("square"));
  1215. &set_label("square_break");
  1216. &vpsllq ($D0,$D0,32); # -> r^3:0:r^4:0
  1217. &vpsllq ($D1,$D1,32);
  1218. &vpsllq ($D2,$D2,32);
  1219. &vpsllq ($D3,$D3,32);
  1220. &vpsllq ($D4,$D4,32);
  1221. &vpor ($D0,$D0,&QWP(16*0,"esp")); # r^3:r^1:r^4:r^2
  1222. &vpor ($D1,$D1,&QWP(16*1,"esp"));
  1223. &vpor ($D2,$D2,&QWP(16*2,"esp"));
  1224. &vpor ($D3,$D3,&QWP(16*3,"esp"));
  1225. &vpor ($D4,$D4,&QWP(16*4,"esp"));
  1226. &vpshufd ($D0,$D0,0b10001101); # -> r^1:r^2:r^3:r^4
  1227. &vpshufd ($D1,$D1,0b10001101);
  1228. &vpshufd ($D2,$D2,0b10001101);
  1229. &vpshufd ($D3,$D3,0b10001101);
  1230. &vpshufd ($D4,$D4,0b10001101);
  1231. &vmovdqu (&QWP(16*0,"edi"),$D0); # save the table
  1232. &vmovdqu (&QWP(16*1,"edi"),$D1);
  1233. &vmovdqu (&QWP(16*2,"edi"),$D2);
  1234. &vmovdqu (&QWP(16*3,"edi"),$D3);
  1235. &vmovdqu (&QWP(16*4,"edi"),$D4);
  1236. &vpslld ($T1,$D1,2);
  1237. &vpslld ($T0,$D2,2);
  1238. &vpaddd ($T1,$T1,$D1); # *5
  1239. &vpaddd ($T0,$T0,$D2); # *5
  1240. &vmovdqu (&QWP(16*5,"edi"),$T1);
  1241. &vmovdqu (&QWP(16*6,"edi"),$T0);
  1242. &vpslld ($T1,$D3,2);
  1243. &vpslld ($T0,$D4,2);
  1244. &vpaddd ($T1,$T1,$D3); # *5
  1245. &vpaddd ($T0,$T0,$D4); # *5
  1246. &vmovdqu (&QWP(16*7,"edi"),$T1);
  1247. &vmovdqu (&QWP(16*8,"edi"),$T0);
  1248. &mov ("esp","ebp");
  1249. &lea ("edi",&DWP(-16*3,"edi")); # size de-optimization
  1250. &ret ();
  1251. &function_end_B("_poly1305_init_avx2");
  1252. ########################################################################
  1253. # now it's time to switch to %ymm
  1254. my ($D0,$D1,$D2,$D3,$D4,$T0,$T1,$T2)=map("ymm$_",(0..7));
  1255. my $MASK=$T2;
  1256. sub X { my $reg=shift; $reg=~s/^ymm/xmm/; $reg; }
  1257. &align (32);
  1258. &function_begin("_poly1305_blocks_avx2");
  1259. &mov ("edi",&wparam(0)); # ctx
  1260. &mov ("esi",&wparam(1)); # inp
  1261. &mov ("ecx",&wparam(2)); # len
  1262. &mov ("eax",&DWP(4*5,"edi")); # is_base2_26
  1263. &and ("ecx",-16);
  1264. &jz (&label("nodata"));
  1265. &cmp ("ecx",64);
  1266. &jae (&label("enter_avx2"));
  1267. &test ("eax","eax"); # is_base2_26?
  1268. &jz (&label("enter_blocks"));
  1269. &set_label("enter_avx2");
  1270. &vzeroupper ();
  1271. &call (&label("pic_point"));
  1272. &set_label("pic_point");
  1273. &blindpop("ebx");
  1274. &lea ("ebx",&DWP(&label("const_sse2")."-".&label("pic_point"),"ebx"));
  1275. &test ("eax","eax"); # is_base2_26?
  1276. &jnz (&label("base2_26"));
  1277. &call ("_poly1305_init_avx2");
  1278. ################################################# base 2^32 -> base 2^26
  1279. &mov ("eax",&DWP(0,"edi"));
  1280. &mov ("ecx",&DWP(3,"edi"));
  1281. &mov ("edx",&DWP(6,"edi"));
  1282. &mov ("esi",&DWP(9,"edi"));
  1283. &mov ("ebp",&DWP(13,"edi"));
  1284. &shr ("ecx",2);
  1285. &and ("eax",0x3ffffff);
  1286. &shr ("edx",4);
  1287. &and ("ecx",0x3ffffff);
  1288. &shr ("esi",6);
  1289. &and ("edx",0x3ffffff);
  1290. &mov (&DWP(4*0,"edi"),"eax");
  1291. &mov (&DWP(4*1,"edi"),"ecx");
  1292. &mov (&DWP(4*2,"edi"),"edx");
  1293. &mov (&DWP(4*3,"edi"),"esi");
  1294. &mov (&DWP(4*4,"edi"),"ebp");
  1295. &mov (&DWP(4*5,"edi"),1); # is_base2_26
  1296. &mov ("esi",&wparam(1)); # [reload] inp
  1297. &mov ("ecx",&wparam(2)); # [reload] len
  1298. &set_label("base2_26");
  1299. &mov ("eax",&wparam(3)); # padbit
  1300. &mov ("ebp","esp");
  1301. &sub ("esp",32*(5+9));
  1302. &and ("esp",-512); # ensure that frame
  1303. # doesn't cross page
  1304. # boundary, which is
  1305. # essential for
  1306. # misaligned 32-byte
  1307. # loads
  1308. ################################################################
  1309. # expand and copy pre-calculated table to stack
  1310. &vmovdqu (&X($D0),&QWP(16*(3+0),"edi"));
  1311. &lea ("edx",&DWP(32*5+128,"esp")); # +128 size optimization
  1312. &vmovdqu (&X($D1),&QWP(16*(3+1),"edi"));
  1313. &vmovdqu (&X($D2),&QWP(16*(3+2),"edi"));
  1314. &vmovdqu (&X($D3),&QWP(16*(3+3),"edi"));
  1315. &vmovdqu (&X($D4),&QWP(16*(3+4),"edi"));
  1316. &lea ("edi",&DWP(16*3,"edi")); # size optimization
  1317. &vpermq ($D0,$D0,0b01000000); # 00001234 -> 12343434
  1318. &vpermq ($D1,$D1,0b01000000);
  1319. &vpermq ($D2,$D2,0b01000000);
  1320. &vpermq ($D3,$D3,0b01000000);
  1321. &vpermq ($D4,$D4,0b01000000);
  1322. &vpshufd ($D0,$D0,0b11001000); # 12343434 -> 14243444
  1323. &vpshufd ($D1,$D1,0b11001000);
  1324. &vpshufd ($D2,$D2,0b11001000);
  1325. &vpshufd ($D3,$D3,0b11001000);
  1326. &vpshufd ($D4,$D4,0b11001000);
  1327. &vmovdqa (&QWP(32*0-128,"edx"),$D0);
  1328. &vmovdqu (&X($D0),&QWP(16*5,"edi"));
  1329. &vmovdqa (&QWP(32*1-128,"edx"),$D1);
  1330. &vmovdqu (&X($D1),&QWP(16*6,"edi"));
  1331. &vmovdqa (&QWP(32*2-128,"edx"),$D2);
  1332. &vmovdqu (&X($D2),&QWP(16*7,"edi"));
  1333. &vmovdqa (&QWP(32*3-128,"edx"),$D3);
  1334. &vmovdqu (&X($D3),&QWP(16*8,"edi"));
  1335. &vmovdqa (&QWP(32*4-128,"edx"),$D4);
  1336. &vpermq ($D0,$D0,0b01000000);
  1337. &vpermq ($D1,$D1,0b01000000);
  1338. &vpermq ($D2,$D2,0b01000000);
  1339. &vpermq ($D3,$D3,0b01000000);
  1340. &vpshufd ($D0,$D0,0b11001000);
  1341. &vpshufd ($D1,$D1,0b11001000);
  1342. &vpshufd ($D2,$D2,0b11001000);
  1343. &vpshufd ($D3,$D3,0b11001000);
  1344. &vmovdqa (&QWP(32*5-128,"edx"),$D0);
  1345. &vmovd (&X($D0),&DWP(-16*3+4*0,"edi"));# load hash value
  1346. &vmovdqa (&QWP(32*6-128,"edx"),$D1);
  1347. &vmovd (&X($D1),&DWP(-16*3+4*1,"edi"));
  1348. &vmovdqa (&QWP(32*7-128,"edx"),$D2);
  1349. &vmovd (&X($D2),&DWP(-16*3+4*2,"edi"));
  1350. &vmovdqa (&QWP(32*8-128,"edx"),$D3);
  1351. &vmovd (&X($D3),&DWP(-16*3+4*3,"edi"));
  1352. &vmovd (&X($D4),&DWP(-16*3+4*4,"edi"));
  1353. &vmovdqa ($MASK,&QWP(64,"ebx"));
  1354. &neg ("eax"); # padbit
  1355. &test ("ecx",63);
  1356. &jz (&label("even"));
  1357. &mov ("edx","ecx");
  1358. &and ("ecx",-64);
  1359. &and ("edx",63);
  1360. &vmovdqu (&X($T0),&QWP(16*0,"esi"));
  1361. &cmp ("edx",32);
  1362. &jb (&label("one"));
  1363. &vmovdqu (&X($T1),&QWP(16*1,"esi"));
  1364. &je (&label("two"));
  1365. &vinserti128 ($T0,$T0,&QWP(16*2,"esi"),1);
  1366. &lea ("esi",&DWP(16*3,"esi"));
  1367. &lea ("ebx",&DWP(8,"ebx")); # three padbits
  1368. &lea ("edx",&DWP(32*5+128+8,"esp")); # --:r^1:r^2:r^3 (*)
  1369. &jmp (&label("tail"));
  1370. &set_label("two");
  1371. &lea ("esi",&DWP(16*2,"esi"));
  1372. &lea ("ebx",&DWP(16,"ebx")); # two padbits
  1373. &lea ("edx",&DWP(32*5+128+16,"esp"));# --:--:r^1:r^2 (*)
  1374. &jmp (&label("tail"));
  1375. &set_label("one");
  1376. &lea ("esi",&DWP(16*1,"esi"));
  1377. &vpxor ($T1,$T1,$T1);
  1378. &lea ("ebx",&DWP(32,"ebx","eax",8)); # one or no padbits
  1379. &lea ("edx",&DWP(32*5+128+24,"esp"));# --:--:--:r^1 (*)
  1380. &jmp (&label("tail"));
  1381. # (*) spots marked with '--' are data from next table entry, but they
  1382. # are multiplied by 0 and therefore rendered insignificant
  1383. &set_label("even",32);
  1384. &vmovdqu (&X($T0),&QWP(16*0,"esi")); # load input
  1385. &vmovdqu (&X($T1),&QWP(16*1,"esi"));
  1386. &vinserti128 ($T0,$T0,&QWP(16*2,"esi"),1);
  1387. &vinserti128 ($T1,$T1,&QWP(16*3,"esi"),1);
  1388. &lea ("esi",&DWP(16*4,"esi"));
  1389. &sub ("ecx",64);
  1390. &jz (&label("tail"));
  1391. &set_label("loop");
  1392. ################################################################
  1393. # ((inp[0]*r^4+r[4])*r^4+r[8])*r^4
  1394. # ((inp[1]*r^4+r[5])*r^4+r[9])*r^3
  1395. # ((inp[2]*r^4+r[6])*r^4+r[10])*r^2
  1396. # ((inp[3]*r^4+r[7])*r^4+r[11])*r^1
  1397. # \________/ \_______/
  1398. ################################################################
  1399. sub vsplat_input {
  1400. &vmovdqa (&QWP(32*2,"esp"),$D2);
  1401. &vpsrldq ($D2,$T0,6); # splat input
  1402. &vmovdqa (&QWP(32*0,"esp"),$D0);
  1403. &vpsrldq ($D0,$T1,6);
  1404. &vmovdqa (&QWP(32*1,"esp"),$D1);
  1405. &vpunpckhqdq ($D1,$T0,$T1); # 4
  1406. &vpunpcklqdq ($T0,$T0,$T1); # 0:1
  1407. &vpunpcklqdq ($D2,$D2,$D0); # 2:3
  1408. &vpsrlq ($D0,$D2,30);
  1409. &vpsrlq ($D2,$D2,4);
  1410. &vpsrlq ($T1,$T0,26);
  1411. &vpsrlq ($D1,$D1,40); # 4
  1412. &vpand ($D2,$D2,$MASK); # 2
  1413. &vpand ($T0,$T0,$MASK); # 0
  1414. &vpand ($T1,$T1,$MASK); # 1
  1415. &vpand ($D0,$D0,$MASK); # 3 (*)
  1416. &vpor ($D1,$D1,&QWP(0,"ebx")); # padbit, yes, always
  1417. # (*) note that output is counterintuitive, inp[3:4] is
  1418. # returned in $D1-2, while $D3-4 are preserved;
  1419. }
  1420. &vsplat_input ();
  1421. sub vpmuladd {
  1422. my $addr = shift;
  1423. &vpaddq ($D2,$D2,&QWP(32*2,"esp")); # add hash value
  1424. &vpaddq ($T0,$T0,&QWP(32*0,"esp"));
  1425. &vpaddq ($T1,$T1,&QWP(32*1,"esp"));
  1426. &vpaddq ($D0,$D0,$D3);
  1427. &vpaddq ($D1,$D1,$D4);
  1428. ################################################################
  1429. # d3 = h2*r1 + h0*r3 + h1*r2 + h3*r0 + h4*5*r4
  1430. # d4 = h2*r2 + h0*r4 + h1*r3 + h3*r1 + h4*r0
  1431. # d0 = h2*5*r3 + h0*r0 + h1*5*r4 + h3*5*r2 + h4*5*r1
  1432. # d1 = h2*5*r4 + h0*r1 + h1*r0 + h3*5*r3 + h4*5*r2
  1433. # d2 = h2*r0 + h0*r2 + h1*r1 + h3*5*r4 + h4*5*r3
  1434. &vpmuludq ($D3,$D2,&$addr(1)); # d3 = h2*r1
  1435. &vmovdqa (QWP(32*1,"esp"),$T1);
  1436. &vpmuludq ($D4,$D2,&$addr(2)); # d4 = h2*r2
  1437. &vmovdqa (QWP(32*3,"esp"),$D0);
  1438. &vpmuludq ($D0,$D2,&$addr(7)); # d0 = h2*s3
  1439. &vmovdqa (QWP(32*4,"esp"),$D1);
  1440. &vpmuludq ($D1,$D2,&$addr(8)); # d1 = h2*s4
  1441. &vpmuludq ($D2,$D2,&$addr(0)); # d2 = h2*r0
  1442. &vpmuludq ($T2,$T0,&$addr(3)); # h0*r3
  1443. &vpaddq ($D3,$D3,$T2); # d3 += h0*r3
  1444. &vpmuludq ($T1,$T0,&$addr(4)); # h0*r4
  1445. &vpaddq ($D4,$D4,$T1); # d4 + h0*r4
  1446. &vpmuludq ($T2,$T0,&$addr(0)); # h0*r0
  1447. &vpaddq ($D0,$D0,$T2); # d0 + h0*r0
  1448. &vmovdqa ($T2,&QWP(32*1,"esp")); # h1
  1449. &vpmuludq ($T1,$T0,&$addr(1)); # h0*r1
  1450. &vpaddq ($D1,$D1,$T1); # d1 += h0*r1
  1451. &vpmuludq ($T0,$T0,&$addr(2)); # h0*r2
  1452. &vpaddq ($D2,$D2,$T0); # d2 += h0*r2
  1453. &vpmuludq ($T1,$T2,&$addr(2)); # h1*r2
  1454. &vpaddq ($D3,$D3,$T1); # d3 += h1*r2
  1455. &vpmuludq ($T0,$T2,&$addr(3)); # h1*r3
  1456. &vpaddq ($D4,$D4,$T0); # d4 += h1*r3
  1457. &vpmuludq ($T1,$T2,&$addr(8)); # h1*s4
  1458. &vpaddq ($D0,$D0,$T1); # d0 += h1*s4
  1459. &vmovdqa ($T1,&QWP(32*3,"esp")); # h3
  1460. &vpmuludq ($T0,$T2,&$addr(0)); # h1*r0
  1461. &vpaddq ($D1,$D1,$T0); # d1 += h1*r0
  1462. &vpmuludq ($T2,$T2,&$addr(1)); # h1*r1
  1463. &vpaddq ($D2,$D2,$T2); # d2 += h1*r1
  1464. &vpmuludq ($T0,$T1,&$addr(0)); # h3*r0
  1465. &vpaddq ($D3,$D3,$T0); # d3 += h3*r0
  1466. &vpmuludq ($T2,$T1,&$addr(1)); # h3*r1
  1467. &vpaddq ($D4,$D4,$T2); # d4 += h3*r1
  1468. &vpmuludq ($T0,$T1,&$addr(6)); # h3*s2
  1469. &vpaddq ($D0,$D0,$T0); # d0 += h3*s2
  1470. &vmovdqa ($T0,&QWP(32*4,"esp")); # h4
  1471. &vpmuludq ($T2,$T1,&$addr(7)); # h3*s3
  1472. &vpaddq ($D1,$D1,$T2); # d1+= h3*s3
  1473. &vpmuludq ($T1,$T1,&$addr(8)); # h3*s4
  1474. &vpaddq ($D2,$D2,$T1); # d2 += h3*s4
  1475. &vpmuludq ($T2,$T0,&$addr(8)); # h4*s4
  1476. &vpaddq ($D3,$D3,$T2); # d3 += h4*s4
  1477. &vpmuludq ($T1,$T0,&$addr(5)); # h4*s1
  1478. &vpaddq ($D0,$D0,$T1); # d0 += h4*s1
  1479. &vpmuludq ($T2,$T0,&$addr(0)); # h4*r0
  1480. &vpaddq ($D4,$D4,$T2); # d4 += h4*r0
  1481. &vmovdqa ($MASK,&QWP(64,"ebx"));
  1482. &vpmuludq ($T1,$T0,&$addr(6)); # h4*s2
  1483. &vpaddq ($D1,$D1,$T1); # d1 += h4*s2
  1484. &vpmuludq ($T0,$T0,&$addr(7)); # h4*s3
  1485. &vpaddq ($D2,$D2,$T0); # d2 += h4*s3
  1486. }
  1487. &vpmuladd (sub { my $i=shift; &QWP(32*$i-128,"edx"); });
  1488. sub vlazy_reduction {
  1489. ################################################################
  1490. # lazy reduction
  1491. &vpsrlq ($T0,$D3,26);
  1492. &vpand ($D3,$D3,$MASK);
  1493. &vpsrlq ($T1,$D0,26);
  1494. &vpand ($D0,$D0,$MASK);
  1495. &vpaddq ($D4,$D4,$T0); # h3 -> h4
  1496. &vpaddq ($D1,$D1,$T1); # h0 -> h1
  1497. &vpsrlq ($T0,$D4,26);
  1498. &vpand ($D4,$D4,$MASK);
  1499. &vpsrlq ($T1,$D1,26);
  1500. &vpand ($D1,$D1,$MASK);
  1501. &vpaddq ($D2,$D2,$T1); # h1 -> h2
  1502. &vpaddq ($D0,$D0,$T0);
  1503. &vpsllq ($T0,$T0,2);
  1504. &vpsrlq ($T1,$D2,26);
  1505. &vpand ($D2,$D2,$MASK);
  1506. &vpaddq ($D0,$D0,$T0); # h4 -> h0
  1507. &vpaddq ($D3,$D3,$T1); # h2 -> h3
  1508. &vpsrlq ($T1,$D3,26);
  1509. &vpsrlq ($T0,$D0,26);
  1510. &vpand ($D0,$D0,$MASK);
  1511. &vpand ($D3,$D3,$MASK);
  1512. &vpaddq ($D1,$D1,$T0); # h0 -> h1
  1513. &vpaddq ($D4,$D4,$T1); # h3 -> h4
  1514. }
  1515. &vlazy_reduction();
  1516. &vmovdqu (&X($T0),&QWP(16*0,"esi")); # load input
  1517. &vmovdqu (&X($T1),&QWP(16*1,"esi"));
  1518. &vinserti128 ($T0,$T0,&QWP(16*2,"esi"),1);
  1519. &vinserti128 ($T1,$T1,&QWP(16*3,"esi"),1);
  1520. &lea ("esi",&DWP(16*4,"esi"));
  1521. &sub ("ecx",64);
  1522. &jnz (&label("loop"));
  1523. &set_label("tail");
  1524. &vsplat_input ();
  1525. &and ("ebx",-64); # restore pointer
  1526. &vpmuladd (sub { my $i=shift; &QWP(4+32*$i-128,"edx"); });
  1527. ################################################################
  1528. # horizontal addition
  1529. &vpsrldq ($T0,$D4,8);
  1530. &vpsrldq ($T1,$D3,8);
  1531. &vpaddq ($D4,$D4,$T0);
  1532. &vpsrldq ($T0,$D0,8);
  1533. &vpaddq ($D3,$D3,$T1);
  1534. &vpsrldq ($T1,$D1,8);
  1535. &vpaddq ($D0,$D0,$T0);
  1536. &vpsrldq ($T0,$D2,8);
  1537. &vpaddq ($D1,$D1,$T1);
  1538. &vpermq ($T1,$D4,2); # keep folding
  1539. &vpaddq ($D2,$D2,$T0);
  1540. &vpermq ($T0,$D3,2);
  1541. &vpaddq ($D4,$D4,$T1);
  1542. &vpermq ($T1,$D0,2);
  1543. &vpaddq ($D3,$D3,$T0);
  1544. &vpermq ($T0,$D1,2);
  1545. &vpaddq ($D0,$D0,$T1);
  1546. &vpermq ($T1,$D2,2);
  1547. &vpaddq ($D1,$D1,$T0);
  1548. &vpaddq ($D2,$D2,$T1);
  1549. &vlazy_reduction();
  1550. &cmp ("ecx",0);
  1551. &je (&label("done"));
  1552. ################################################################
  1553. # clear all but single word
  1554. &vpshufd (&X($D0),&X($D0),0b11111100);
  1555. &lea ("edx",&DWP(32*5+128,"esp")); # restore pointer
  1556. &vpshufd (&X($D1),&X($D1),0b11111100);
  1557. &vpshufd (&X($D2),&X($D2),0b11111100);
  1558. &vpshufd (&X($D3),&X($D3),0b11111100);
  1559. &vpshufd (&X($D4),&X($D4),0b11111100);
  1560. &jmp (&label("even"));
  1561. &set_label("done",16);
  1562. &vmovd (&DWP(-16*3+4*0,"edi"),&X($D0));# store hash value
  1563. &vmovd (&DWP(-16*3+4*1,"edi"),&X($D1));
  1564. &vmovd (&DWP(-16*3+4*2,"edi"),&X($D2));
  1565. &vmovd (&DWP(-16*3+4*3,"edi"),&X($D3));
  1566. &vmovd (&DWP(-16*3+4*4,"edi"),&X($D4));
  1567. &vzeroupper ();
  1568. &mov ("esp","ebp");
  1569. &set_label("nodata");
  1570. &function_end("_poly1305_blocks_avx2");
  1571. }
  1572. &set_label("const_sse2",64);
  1573. &data_word(1<<24,0, 1<<24,0, 1<<24,0, 1<<24,0);
  1574. &data_word(0,0, 0,0, 0,0, 0,0);
  1575. &data_word(0x03ffffff,0,0x03ffffff,0, 0x03ffffff,0, 0x03ffffff,0);
  1576. &data_word(0x0fffffff,0x0ffffffc,0x0ffffffc,0x0ffffffc);
  1577. }
  1578. &asciz ("Poly1305 for x86, CRYPTOGAMS by <appro\@openssl.org>");
  1579. &align (4);
  1580. &asm_finish();
  1581. close STDOUT or die "error closing STDOUT";