2
0

poly1305-x86.pl 51 KB

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