Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ghash-x86.pl 40 KiB

Build 32-bit assembly with SSE2 enabled. This affects bignum and sha. Also now that we're passing the SSE2 flag, revert the change to ghash-x86.pl which unconditionally sets $sse2, just to minimize upstream divergence. Chromium assumes SSE2 support, so relying on it is okay. See https://crbug.com/349320. Note: this change needs to be mirrored in Chromium to take. bssl speed numbers: SSE2: Did 552 RSA 2048 signing operations in 3007814us (183.5 ops/sec) Did 19003 RSA 2048 verify operations in 3070779us (6188.3 ops/sec) Did 72 RSA 4096 signing operations in 3055885us (23.6 ops/sec) Did 4650 RSA 4096 verify operations in 3024926us (1537.2 ops/sec) Without SSE2: Did 350 RSA 2048 signing operations in 3042021us (115.1 ops/sec) Did 11760 RSA 2048 verify operations in 3003197us (3915.8 ops/sec) Did 46 RSA 4096 signing operations in 3042692us (15.1 ops/sec) Did 3400 RSA 4096 verify operations in 3083035us (1102.8 ops/sec) SSE2: Did 16407000 SHA-1 (16 bytes) operations in 3000141us (5468743.0 ops/sec): 87.5 MB/s Did 4367000 SHA-1 (256 bytes) operations in 3000436us (1455455.1 ops/sec): 372.6 MB/s Did 185000 SHA-1 (8192 bytes) operations in 3002666us (61611.9 ops/sec): 504.7 MB/s Did 9444000 SHA-256 (16 bytes) operations in 3000052us (3147945.4 ops/sec): 50.4 MB/s Did 2283000 SHA-256 (256 bytes) operations in 3000457us (760884.1 ops/sec): 194.8 MB/s Did 89000 SHA-256 (8192 bytes) operations in 3016024us (29509.0 ops/sec): 241.7 MB/s Did 5550000 SHA-512 (16 bytes) operations in 3000350us (1849784.2 ops/sec): 29.6 MB/s Did 1820000 SHA-512 (256 bytes) operations in 3001039us (606456.6 ops/sec): 155.3 MB/s Did 93000 SHA-512 (8192 bytes) operations in 3007874us (30918.8 ops/sec): 253.3 MB/s Without SSE2: Did 10573000 SHA-1 (16 bytes) operations in 3000261us (3524026.7 ops/sec): 56.4 MB/s Did 2937000 SHA-1 (256 bytes) operations in 3000621us (978797.4 ops/sec): 250.6 MB/s Did 123000 SHA-1 (8192 bytes) operations in 3033202us (40551.2 ops/sec): 332.2 MB/s Did 5846000 SHA-256 (16 bytes) operations in 3000294us (1948475.7 ops/sec): 31.2 MB/s Did 1377000 SHA-256 (256 bytes) operations in 3000335us (458948.8 ops/sec): 117.5 MB/s Did 54000 SHA-256 (8192 bytes) operations in 3027962us (17833.8 ops/sec): 146.1 MB/s Did 2075000 SHA-512 (16 bytes) operations in 3000967us (691443.8 ops/sec): 11.1 MB/s Did 638000 SHA-512 (256 bytes) operations in 3000576us (212625.8 ops/sec): 54.4 MB/s Did 30000 SHA-512 (8192 bytes) operations in 3042797us (9859.3 ops/sec): 80.8 MB/s BUG=430237 Change-Id: I47d1c1ffcd71afe4f4a192272f8cb92af9505ee1 Reviewed-on: https://boringssl-review.googlesource.com/4130 Reviewed-by: Adam Langley <agl@google.com>
il y a 9 ans
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. #!/usr/bin/env perl
  2. #
  3. # ====================================================================
  4. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  5. # project. The module is, however, dual licensed under OpenSSL and
  6. # CRYPTOGAMS licenses depending on where you obtain it. For further
  7. # details see http://www.openssl.org/~appro/cryptogams/.
  8. # ====================================================================
  9. #
  10. # March, May, June 2010
  11. #
  12. # The module implements "4-bit" GCM GHASH function and underlying
  13. # single multiplication operation in GF(2^128). "4-bit" means that it
  14. # uses 256 bytes per-key table [+64/128 bytes fixed table]. It has two
  15. # code paths: vanilla x86 and vanilla SSE. Former will be executed on
  16. # 486 and Pentium, latter on all others. SSE GHASH features so called
  17. # "528B" variant of "4-bit" method utilizing additional 256+16 bytes
  18. # of per-key storage [+512 bytes shared table]. Performance results
  19. # are for streamed GHASH subroutine and are expressed in cycles per
  20. # processed byte, less is better:
  21. #
  22. # gcc 2.95.3(*) SSE assembler x86 assembler
  23. #
  24. # Pentium 105/111(**) - 50
  25. # PIII 68 /75 12.2 24
  26. # P4 125/125 17.8 84(***)
  27. # Opteron 66 /70 10.1 30
  28. # Core2 54 /67 8.4 18
  29. # Atom 105/105 16.8 53
  30. # VIA Nano 69 /71 13.0 27
  31. #
  32. # (*) gcc 3.4.x was observed to generate few percent slower code,
  33. # which is one of reasons why 2.95.3 results were chosen,
  34. # another reason is lack of 3.4.x results for older CPUs;
  35. # comparison with SSE results is not completely fair, because C
  36. # results are for vanilla "256B" implementation, while
  37. # assembler results are for "528B";-)
  38. # (**) second number is result for code compiled with -fPIC flag,
  39. # which is actually more relevant, because assembler code is
  40. # position-independent;
  41. # (***) see comment in non-MMX routine for further details;
  42. #
  43. # To summarize, it's >2-5 times faster than gcc-generated code. To
  44. # anchor it to something else SHA1 assembler processes one byte in
  45. # ~7 cycles on contemporary x86 cores. As for choice of MMX/SSE
  46. # in particular, see comment at the end of the file...
  47. # May 2010
  48. #
  49. # Add PCLMULQDQ version performing at 2.10 cycles per processed byte.
  50. # The question is how close is it to theoretical limit? The pclmulqdq
  51. # instruction latency appears to be 14 cycles and there can't be more
  52. # than 2 of them executing at any given time. This means that single
  53. # Karatsuba multiplication would take 28 cycles *plus* few cycles for
  54. # pre- and post-processing. Then multiplication has to be followed by
  55. # modulo-reduction. Given that aggregated reduction method [see
  56. # "Carry-less Multiplication and Its Usage for Computing the GCM Mode"
  57. # white paper by Intel] allows you to perform reduction only once in
  58. # a while we can assume that asymptotic performance can be estimated
  59. # as (28+Tmod/Naggr)/16, where Tmod is time to perform reduction
  60. # and Naggr is the aggregation factor.
  61. #
  62. # Before we proceed to this implementation let's have closer look at
  63. # the best-performing code suggested by Intel in their white paper.
  64. # By tracing inter-register dependencies Tmod is estimated as ~19
  65. # cycles and Naggr chosen by Intel is 4, resulting in 2.05 cycles per
  66. # processed byte. As implied, this is quite optimistic estimate,
  67. # because it does not account for Karatsuba pre- and post-processing,
  68. # which for a single multiplication is ~5 cycles. Unfortunately Intel
  69. # does not provide performance data for GHASH alone. But benchmarking
  70. # AES_GCM_encrypt ripped out of Fig. 15 of the white paper with aadt
  71. # alone resulted in 2.46 cycles per byte of out 16KB buffer. Note that
  72. # the result accounts even for pre-computing of degrees of the hash
  73. # key H, but its portion is negligible at 16KB buffer size.
  74. #
  75. # Moving on to the implementation in question. Tmod is estimated as
  76. # ~13 cycles and Naggr is 2, giving asymptotic performance of ...
  77. # 2.16. How is it possible that measured performance is better than
  78. # optimistic theoretical estimate? There is one thing Intel failed
  79. # to recognize. By serializing GHASH with CTR in same subroutine
  80. # former's performance is really limited to above (Tmul + Tmod/Naggr)
  81. # equation. But if GHASH procedure is detached, the modulo-reduction
  82. # can be interleaved with Naggr-1 multiplications at instruction level
  83. # and under ideal conditions even disappear from the equation. So that
  84. # optimistic theoretical estimate for this implementation is ...
  85. # 28/16=1.75, and not 2.16. Well, it's probably way too optimistic,
  86. # at least for such small Naggr. I'd argue that (28+Tproc/Naggr),
  87. # where Tproc is time required for Karatsuba pre- and post-processing,
  88. # is more realistic estimate. In this case it gives ... 1.91 cycles.
  89. # Or in other words, depending on how well we can interleave reduction
  90. # and one of the two multiplications the performance should be betwen
  91. # 1.91 and 2.16. As already mentioned, this implementation processes
  92. # one byte out of 8KB buffer in 2.10 cycles, while x86_64 counterpart
  93. # - in 2.02. x86_64 performance is better, because larger register
  94. # bank allows to interleave reduction and multiplication better.
  95. #
  96. # Does it make sense to increase Naggr? To start with it's virtually
  97. # impossible in 32-bit mode, because of limited register bank
  98. # capacity. Otherwise improvement has to be weighed agiainst slower
  99. # setup, as well as code size and complexity increase. As even
  100. # optimistic estimate doesn't promise 30% performance improvement,
  101. # there are currently no plans to increase Naggr.
  102. #
  103. # Special thanks to David Woodhouse <dwmw2@infradead.org> for
  104. # providing access to a Westmere-based system on behalf of Intel
  105. # Open Source Technology Centre.
  106. # January 2010
  107. #
  108. # Tweaked to optimize transitions between integer and FP operations
  109. # on same XMM register, PCLMULQDQ subroutine was measured to process
  110. # one byte in 2.07 cycles on Sandy Bridge, and in 2.12 - on Westmere.
  111. # The minor regression on Westmere is outweighed by ~15% improvement
  112. # on Sandy Bridge. Strangely enough attempt to modify 64-bit code in
  113. # similar manner resulted in almost 20% degradation on Sandy Bridge,
  114. # where original 64-bit code processes one byte in 1.95 cycles.
  115. #####################################################################
  116. # For reference, AMD Bulldozer processes one byte in 1.98 cycles in
  117. # 32-bit mode and 1.89 in 64-bit.
  118. # February 2013
  119. #
  120. # Overhaul: aggregate Karatsuba post-processing, improve ILP in
  121. # reduction_alg9. Resulting performance is 1.96 cycles per byte on
  122. # Westmere, 1.95 - on Sandy/Ivy Bridge, 1.76 - on Bulldozer.
  123. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  124. push(@INC,"${dir}","${dir}../../perlasm");
  125. require "x86asm.pl";
  126. &asm_init($ARGV[0],"ghash-x86.pl",$x86only = $ARGV[$#ARGV] eq "386");
  127. $sse2=0;
  128. for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
  129. ($Zhh,$Zhl,$Zlh,$Zll) = ("ebp","edx","ecx","ebx");
  130. $inp = "edi";
  131. $Htbl = "esi";
  132. $unroll = 0; # Affects x86 loop. Folded loop performs ~7% worse
  133. # than unrolled, which has to be weighted against
  134. # 2.5x x86-specific code size reduction.
  135. sub x86_loop {
  136. my $off = shift;
  137. my $rem = "eax";
  138. &mov ($Zhh,&DWP(4,$Htbl,$Zll));
  139. &mov ($Zhl,&DWP(0,$Htbl,$Zll));
  140. &mov ($Zlh,&DWP(12,$Htbl,$Zll));
  141. &mov ($Zll,&DWP(8,$Htbl,$Zll));
  142. &xor ($rem,$rem); # avoid partial register stalls on PIII
  143. # shrd practically kills P4, 2.5x deterioration, but P4 has
  144. # MMX code-path to execute. shrd runs tad faster [than twice
  145. # the shifts, move's and or's] on pre-MMX Pentium (as well as
  146. # PIII and Core2), *but* minimizes code size, spares register
  147. # and thus allows to fold the loop...
  148. if (!$unroll) {
  149. my $cnt = $inp;
  150. &mov ($cnt,15);
  151. &jmp (&label("x86_loop"));
  152. &set_label("x86_loop",16);
  153. for($i=1;$i<=2;$i++) {
  154. &mov (&LB($rem),&LB($Zll));
  155. &shrd ($Zll,$Zlh,4);
  156. &and (&LB($rem),0xf);
  157. &shrd ($Zlh,$Zhl,4);
  158. &shrd ($Zhl,$Zhh,4);
  159. &shr ($Zhh,4);
  160. &xor ($Zhh,&DWP($off+16,"esp",$rem,4));
  161. &mov (&LB($rem),&BP($off,"esp",$cnt));
  162. if ($i&1) {
  163. &and (&LB($rem),0xf0);
  164. } else {
  165. &shl (&LB($rem),4);
  166. }
  167. &xor ($Zll,&DWP(8,$Htbl,$rem));
  168. &xor ($Zlh,&DWP(12,$Htbl,$rem));
  169. &xor ($Zhl,&DWP(0,$Htbl,$rem));
  170. &xor ($Zhh,&DWP(4,$Htbl,$rem));
  171. if ($i&1) {
  172. &dec ($cnt);
  173. &js (&label("x86_break"));
  174. } else {
  175. &jmp (&label("x86_loop"));
  176. }
  177. }
  178. &set_label("x86_break",16);
  179. } else {
  180. for($i=1;$i<32;$i++) {
  181. &comment($i);
  182. &mov (&LB($rem),&LB($Zll));
  183. &shrd ($Zll,$Zlh,4);
  184. &and (&LB($rem),0xf);
  185. &shrd ($Zlh,$Zhl,4);
  186. &shrd ($Zhl,$Zhh,4);
  187. &shr ($Zhh,4);
  188. &xor ($Zhh,&DWP($off+16,"esp",$rem,4));
  189. if ($i&1) {
  190. &mov (&LB($rem),&BP($off+15-($i>>1),"esp"));
  191. &and (&LB($rem),0xf0);
  192. } else {
  193. &mov (&LB($rem),&BP($off+15-($i>>1),"esp"));
  194. &shl (&LB($rem),4);
  195. }
  196. &xor ($Zll,&DWP(8,$Htbl,$rem));
  197. &xor ($Zlh,&DWP(12,$Htbl,$rem));
  198. &xor ($Zhl,&DWP(0,$Htbl,$rem));
  199. &xor ($Zhh,&DWP(4,$Htbl,$rem));
  200. }
  201. }
  202. &bswap ($Zll);
  203. &bswap ($Zlh);
  204. &bswap ($Zhl);
  205. if (!$x86only) {
  206. &bswap ($Zhh);
  207. } else {
  208. &mov ("eax",$Zhh);
  209. &bswap ("eax");
  210. &mov ($Zhh,"eax");
  211. }
  212. }
  213. if ($unroll) {
  214. &function_begin_B("_x86_gmult_4bit_inner");
  215. &x86_loop(4);
  216. &ret ();
  217. &function_end_B("_x86_gmult_4bit_inner");
  218. }
  219. sub deposit_rem_4bit {
  220. my $bias = shift;
  221. &mov (&DWP($bias+0, "esp"),0x0000<<16);
  222. &mov (&DWP($bias+4, "esp"),0x1C20<<16);
  223. &mov (&DWP($bias+8, "esp"),0x3840<<16);
  224. &mov (&DWP($bias+12,"esp"),0x2460<<16);
  225. &mov (&DWP($bias+16,"esp"),0x7080<<16);
  226. &mov (&DWP($bias+20,"esp"),0x6CA0<<16);
  227. &mov (&DWP($bias+24,"esp"),0x48C0<<16);
  228. &mov (&DWP($bias+28,"esp"),0x54E0<<16);
  229. &mov (&DWP($bias+32,"esp"),0xE100<<16);
  230. &mov (&DWP($bias+36,"esp"),0xFD20<<16);
  231. &mov (&DWP($bias+40,"esp"),0xD940<<16);
  232. &mov (&DWP($bias+44,"esp"),0xC560<<16);
  233. &mov (&DWP($bias+48,"esp"),0x9180<<16);
  234. &mov (&DWP($bias+52,"esp"),0x8DA0<<16);
  235. &mov (&DWP($bias+56,"esp"),0xA9C0<<16);
  236. &mov (&DWP($bias+60,"esp"),0xB5E0<<16);
  237. }
  238. $suffix = $x86only ? "" : "_x86";
  239. &function_begin("gcm_gmult_4bit".$suffix);
  240. &stack_push(16+4+1); # +1 for stack alignment
  241. &mov ($inp,&wparam(0)); # load Xi
  242. &mov ($Htbl,&wparam(1)); # load Htable
  243. &mov ($Zhh,&DWP(0,$inp)); # load Xi[16]
  244. &mov ($Zhl,&DWP(4,$inp));
  245. &mov ($Zlh,&DWP(8,$inp));
  246. &mov ($Zll,&DWP(12,$inp));
  247. &deposit_rem_4bit(16);
  248. &mov (&DWP(0,"esp"),$Zhh); # copy Xi[16] on stack
  249. &mov (&DWP(4,"esp"),$Zhl);
  250. &mov (&DWP(8,"esp"),$Zlh);
  251. &mov (&DWP(12,"esp"),$Zll);
  252. &shr ($Zll,20);
  253. &and ($Zll,0xf0);
  254. if ($unroll) {
  255. &call ("_x86_gmult_4bit_inner");
  256. } else {
  257. &x86_loop(0);
  258. &mov ($inp,&wparam(0));
  259. }
  260. &mov (&DWP(12,$inp),$Zll);
  261. &mov (&DWP(8,$inp),$Zlh);
  262. &mov (&DWP(4,$inp),$Zhl);
  263. &mov (&DWP(0,$inp),$Zhh);
  264. &stack_pop(16+4+1);
  265. &function_end("gcm_gmult_4bit".$suffix);
  266. &function_begin("gcm_ghash_4bit".$suffix);
  267. &stack_push(16+4+1); # +1 for 64-bit alignment
  268. &mov ($Zll,&wparam(0)); # load Xi
  269. &mov ($Htbl,&wparam(1)); # load Htable
  270. &mov ($inp,&wparam(2)); # load in
  271. &mov ("ecx",&wparam(3)); # load len
  272. &add ("ecx",$inp);
  273. &mov (&wparam(3),"ecx");
  274. &mov ($Zhh,&DWP(0,$Zll)); # load Xi[16]
  275. &mov ($Zhl,&DWP(4,$Zll));
  276. &mov ($Zlh,&DWP(8,$Zll));
  277. &mov ($Zll,&DWP(12,$Zll));
  278. &deposit_rem_4bit(16);
  279. &set_label("x86_outer_loop",16);
  280. &xor ($Zll,&DWP(12,$inp)); # xor with input
  281. &xor ($Zlh,&DWP(8,$inp));
  282. &xor ($Zhl,&DWP(4,$inp));
  283. &xor ($Zhh,&DWP(0,$inp));
  284. &mov (&DWP(12,"esp"),$Zll); # dump it on stack
  285. &mov (&DWP(8,"esp"),$Zlh);
  286. &mov (&DWP(4,"esp"),$Zhl);
  287. &mov (&DWP(0,"esp"),$Zhh);
  288. &shr ($Zll,20);
  289. &and ($Zll,0xf0);
  290. if ($unroll) {
  291. &call ("_x86_gmult_4bit_inner");
  292. } else {
  293. &x86_loop(0);
  294. &mov ($inp,&wparam(2));
  295. }
  296. &lea ($inp,&DWP(16,$inp));
  297. &cmp ($inp,&wparam(3));
  298. &mov (&wparam(2),$inp) if (!$unroll);
  299. &jb (&label("x86_outer_loop"));
  300. &mov ($inp,&wparam(0)); # load Xi
  301. &mov (&DWP(12,$inp),$Zll);
  302. &mov (&DWP(8,$inp),$Zlh);
  303. &mov (&DWP(4,$inp),$Zhl);
  304. &mov (&DWP(0,$inp),$Zhh);
  305. &stack_pop(16+4+1);
  306. &function_end("gcm_ghash_4bit".$suffix);
  307. if (!$x86only) {{{
  308. &static_label("rem_4bit");
  309. if (!$sse2) {{ # pure-MMX "May" version...
  310. $S=12; # shift factor for rem_4bit
  311. &function_begin_B("_mmx_gmult_4bit_inner");
  312. # MMX version performs 3.5 times better on P4 (see comment in non-MMX
  313. # routine for further details), 100% better on Opteron, ~70% better
  314. # on Core2 and PIII... In other words effort is considered to be well
  315. # spent... Since initial release the loop was unrolled in order to
  316. # "liberate" register previously used as loop counter. Instead it's
  317. # used to optimize critical path in 'Z.hi ^= rem_4bit[Z.lo&0xf]'.
  318. # The path involves move of Z.lo from MMX to integer register,
  319. # effective address calculation and finally merge of value to Z.hi.
  320. # Reference to rem_4bit is scheduled so late that I had to >>4
  321. # rem_4bit elements. This resulted in 20-45% procent improvement
  322. # on contemporary -archs.
  323. {
  324. my $cnt;
  325. my $rem_4bit = "eax";
  326. my @rem = ($Zhh,$Zll);
  327. my $nhi = $Zhl;
  328. my $nlo = $Zlh;
  329. my ($Zlo,$Zhi) = ("mm0","mm1");
  330. my $tmp = "mm2";
  331. &xor ($nlo,$nlo); # avoid partial register stalls on PIII
  332. &mov ($nhi,$Zll);
  333. &mov (&LB($nlo),&LB($nhi));
  334. &shl (&LB($nlo),4);
  335. &and ($nhi,0xf0);
  336. &movq ($Zlo,&QWP(8,$Htbl,$nlo));
  337. &movq ($Zhi,&QWP(0,$Htbl,$nlo));
  338. &movd ($rem[0],$Zlo);
  339. for ($cnt=28;$cnt>=-2;$cnt--) {
  340. my $odd = $cnt&1;
  341. my $nix = $odd ? $nlo : $nhi;
  342. &shl (&LB($nlo),4) if ($odd);
  343. &psrlq ($Zlo,4);
  344. &movq ($tmp,$Zhi);
  345. &psrlq ($Zhi,4);
  346. &pxor ($Zlo,&QWP(8,$Htbl,$nix));
  347. &mov (&LB($nlo),&BP($cnt/2,$inp)) if (!$odd && $cnt>=0);
  348. &psllq ($tmp,60);
  349. &and ($nhi,0xf0) if ($odd);
  350. &pxor ($Zhi,&QWP(0,$rem_4bit,$rem[1],8)) if ($cnt<28);
  351. &and ($rem[0],0xf);
  352. &pxor ($Zhi,&QWP(0,$Htbl,$nix));
  353. &mov ($nhi,$nlo) if (!$odd && $cnt>=0);
  354. &movd ($rem[1],$Zlo);
  355. &pxor ($Zlo,$tmp);
  356. push (@rem,shift(@rem)); # "rotate" registers
  357. }
  358. &mov ($inp,&DWP(4,$rem_4bit,$rem[1],8)); # last rem_4bit[rem]
  359. &psrlq ($Zlo,32); # lower part of Zlo is already there
  360. &movd ($Zhl,$Zhi);
  361. &psrlq ($Zhi,32);
  362. &movd ($Zlh,$Zlo);
  363. &movd ($Zhh,$Zhi);
  364. &shl ($inp,4); # compensate for rem_4bit[i] being >>4
  365. &bswap ($Zll);
  366. &bswap ($Zhl);
  367. &bswap ($Zlh);
  368. &xor ($Zhh,$inp);
  369. &bswap ($Zhh);
  370. &ret ();
  371. }
  372. &function_end_B("_mmx_gmult_4bit_inner");
  373. &function_begin("gcm_gmult_4bit_mmx");
  374. &mov ($inp,&wparam(0)); # load Xi
  375. &mov ($Htbl,&wparam(1)); # load Htable
  376. &call (&label("pic_point"));
  377. &set_label("pic_point");
  378. &blindpop("eax");
  379. &lea ("eax",&DWP(&label("rem_4bit")."-".&label("pic_point"),"eax"));
  380. &movz ($Zll,&BP(15,$inp));
  381. &call ("_mmx_gmult_4bit_inner");
  382. &mov ($inp,&wparam(0)); # load Xi
  383. &emms ();
  384. &mov (&DWP(12,$inp),$Zll);
  385. &mov (&DWP(4,$inp),$Zhl);
  386. &mov (&DWP(8,$inp),$Zlh);
  387. &mov (&DWP(0,$inp),$Zhh);
  388. &function_end("gcm_gmult_4bit_mmx");
  389. # Streamed version performs 20% better on P4, 7% on Opteron,
  390. # 10% on Core2 and PIII...
  391. &function_begin("gcm_ghash_4bit_mmx");
  392. &mov ($Zhh,&wparam(0)); # load Xi
  393. &mov ($Htbl,&wparam(1)); # load Htable
  394. &mov ($inp,&wparam(2)); # load in
  395. &mov ($Zlh,&wparam(3)); # load len
  396. &call (&label("pic_point"));
  397. &set_label("pic_point");
  398. &blindpop("eax");
  399. &lea ("eax",&DWP(&label("rem_4bit")."-".&label("pic_point"),"eax"));
  400. &add ($Zlh,$inp);
  401. &mov (&wparam(3),$Zlh); # len to point at the end of input
  402. &stack_push(4+1); # +1 for stack alignment
  403. &mov ($Zll,&DWP(12,$Zhh)); # load Xi[16]
  404. &mov ($Zhl,&DWP(4,$Zhh));
  405. &mov ($Zlh,&DWP(8,$Zhh));
  406. &mov ($Zhh,&DWP(0,$Zhh));
  407. &jmp (&label("mmx_outer_loop"));
  408. &set_label("mmx_outer_loop",16);
  409. &xor ($Zll,&DWP(12,$inp));
  410. &xor ($Zhl,&DWP(4,$inp));
  411. &xor ($Zlh,&DWP(8,$inp));
  412. &xor ($Zhh,&DWP(0,$inp));
  413. &mov (&wparam(2),$inp);
  414. &mov (&DWP(12,"esp"),$Zll);
  415. &mov (&DWP(4,"esp"),$Zhl);
  416. &mov (&DWP(8,"esp"),$Zlh);
  417. &mov (&DWP(0,"esp"),$Zhh);
  418. &mov ($inp,"esp");
  419. &shr ($Zll,24);
  420. &call ("_mmx_gmult_4bit_inner");
  421. &mov ($inp,&wparam(2));
  422. &lea ($inp,&DWP(16,$inp));
  423. &cmp ($inp,&wparam(3));
  424. &jb (&label("mmx_outer_loop"));
  425. &mov ($inp,&wparam(0)); # load Xi
  426. &emms ();
  427. &mov (&DWP(12,$inp),$Zll);
  428. &mov (&DWP(4,$inp),$Zhl);
  429. &mov (&DWP(8,$inp),$Zlh);
  430. &mov (&DWP(0,$inp),$Zhh);
  431. &stack_pop(4+1);
  432. &function_end("gcm_ghash_4bit_mmx");
  433. }} else {{ # "June" MMX version...
  434. # ... has slower "April" gcm_gmult_4bit_mmx with folded
  435. # loop. This is done to conserve code size...
  436. $S=16; # shift factor for rem_4bit
  437. sub mmx_loop() {
  438. # MMX version performs 2.8 times better on P4 (see comment in non-MMX
  439. # routine for further details), 40% better on Opteron and Core2, 50%
  440. # better on PIII... In other words effort is considered to be well
  441. # spent...
  442. my $inp = shift;
  443. my $rem_4bit = shift;
  444. my $cnt = $Zhh;
  445. my $nhi = $Zhl;
  446. my $nlo = $Zlh;
  447. my $rem = $Zll;
  448. my ($Zlo,$Zhi) = ("mm0","mm1");
  449. my $tmp = "mm2";
  450. &xor ($nlo,$nlo); # avoid partial register stalls on PIII
  451. &mov ($nhi,$Zll);
  452. &mov (&LB($nlo),&LB($nhi));
  453. &mov ($cnt,14);
  454. &shl (&LB($nlo),4);
  455. &and ($nhi,0xf0);
  456. &movq ($Zlo,&QWP(8,$Htbl,$nlo));
  457. &movq ($Zhi,&QWP(0,$Htbl,$nlo));
  458. &movd ($rem,$Zlo);
  459. &jmp (&label("mmx_loop"));
  460. &set_label("mmx_loop",16);
  461. &psrlq ($Zlo,4);
  462. &and ($rem,0xf);
  463. &movq ($tmp,$Zhi);
  464. &psrlq ($Zhi,4);
  465. &pxor ($Zlo,&QWP(8,$Htbl,$nhi));
  466. &mov (&LB($nlo),&BP(0,$inp,$cnt));
  467. &psllq ($tmp,60);
  468. &pxor ($Zhi,&QWP(0,$rem_4bit,$rem,8));
  469. &dec ($cnt);
  470. &movd ($rem,$Zlo);
  471. &pxor ($Zhi,&QWP(0,$Htbl,$nhi));
  472. &mov ($nhi,$nlo);
  473. &pxor ($Zlo,$tmp);
  474. &js (&label("mmx_break"));
  475. &shl (&LB($nlo),4);
  476. &and ($rem,0xf);
  477. &psrlq ($Zlo,4);
  478. &and ($nhi,0xf0);
  479. &movq ($tmp,$Zhi);
  480. &psrlq ($Zhi,4);
  481. &pxor ($Zlo,&QWP(8,$Htbl,$nlo));
  482. &psllq ($tmp,60);
  483. &pxor ($Zhi,&QWP(0,$rem_4bit,$rem,8));
  484. &movd ($rem,$Zlo);
  485. &pxor ($Zhi,&QWP(0,$Htbl,$nlo));
  486. &pxor ($Zlo,$tmp);
  487. &jmp (&label("mmx_loop"));
  488. &set_label("mmx_break",16);
  489. &shl (&LB($nlo),4);
  490. &and ($rem,0xf);
  491. &psrlq ($Zlo,4);
  492. &and ($nhi,0xf0);
  493. &movq ($tmp,$Zhi);
  494. &psrlq ($Zhi,4);
  495. &pxor ($Zlo,&QWP(8,$Htbl,$nlo));
  496. &psllq ($tmp,60);
  497. &pxor ($Zhi,&QWP(0,$rem_4bit,$rem,8));
  498. &movd ($rem,$Zlo);
  499. &pxor ($Zhi,&QWP(0,$Htbl,$nlo));
  500. &pxor ($Zlo,$tmp);
  501. &psrlq ($Zlo,4);
  502. &and ($rem,0xf);
  503. &movq ($tmp,$Zhi);
  504. &psrlq ($Zhi,4);
  505. &pxor ($Zlo,&QWP(8,$Htbl,$nhi));
  506. &psllq ($tmp,60);
  507. &pxor ($Zhi,&QWP(0,$rem_4bit,$rem,8));
  508. &movd ($rem,$Zlo);
  509. &pxor ($Zhi,&QWP(0,$Htbl,$nhi));
  510. &pxor ($Zlo,$tmp);
  511. &psrlq ($Zlo,32); # lower part of Zlo is already there
  512. &movd ($Zhl,$Zhi);
  513. &psrlq ($Zhi,32);
  514. &movd ($Zlh,$Zlo);
  515. &movd ($Zhh,$Zhi);
  516. &bswap ($Zll);
  517. &bswap ($Zhl);
  518. &bswap ($Zlh);
  519. &bswap ($Zhh);
  520. }
  521. &function_begin("gcm_gmult_4bit_mmx");
  522. &mov ($inp,&wparam(0)); # load Xi
  523. &mov ($Htbl,&wparam(1)); # load Htable
  524. &call (&label("pic_point"));
  525. &set_label("pic_point");
  526. &blindpop("eax");
  527. &lea ("eax",&DWP(&label("rem_4bit")."-".&label("pic_point"),"eax"));
  528. &movz ($Zll,&BP(15,$inp));
  529. &mmx_loop($inp,"eax");
  530. &emms ();
  531. &mov (&DWP(12,$inp),$Zll);
  532. &mov (&DWP(4,$inp),$Zhl);
  533. &mov (&DWP(8,$inp),$Zlh);
  534. &mov (&DWP(0,$inp),$Zhh);
  535. &function_end("gcm_gmult_4bit_mmx");
  536. ######################################################################
  537. # Below subroutine is "528B" variant of "4-bit" GCM GHASH function
  538. # (see gcm128.c for details). It provides further 20-40% performance
  539. # improvement over above mentioned "May" version.
  540. &static_label("rem_8bit");
  541. &function_begin("gcm_ghash_4bit_mmx");
  542. { my ($Zlo,$Zhi) = ("mm7","mm6");
  543. my $rem_8bit = "esi";
  544. my $Htbl = "ebx";
  545. # parameter block
  546. &mov ("eax",&wparam(0)); # Xi
  547. &mov ("ebx",&wparam(1)); # Htable
  548. &mov ("ecx",&wparam(2)); # inp
  549. &mov ("edx",&wparam(3)); # len
  550. &mov ("ebp","esp"); # original %esp
  551. &call (&label("pic_point"));
  552. &set_label ("pic_point");
  553. &blindpop ($rem_8bit);
  554. &lea ($rem_8bit,&DWP(&label("rem_8bit")."-".&label("pic_point"),$rem_8bit));
  555. &sub ("esp",512+16+16); # allocate stack frame...
  556. &and ("esp",-64); # ...and align it
  557. &sub ("esp",16); # place for (u8)(H[]<<4)
  558. &add ("edx","ecx"); # pointer to the end of input
  559. &mov (&DWP(528+16+0,"esp"),"eax"); # save Xi
  560. &mov (&DWP(528+16+8,"esp"),"edx"); # save inp+len
  561. &mov (&DWP(528+16+12,"esp"),"ebp"); # save original %esp
  562. { my @lo = ("mm0","mm1","mm2");
  563. my @hi = ("mm3","mm4","mm5");
  564. my @tmp = ("mm6","mm7");
  565. my ($off1,$off2,$i) = (0,0,);
  566. &add ($Htbl,128); # optimize for size
  567. &lea ("edi",&DWP(16+128,"esp"));
  568. &lea ("ebp",&DWP(16+256+128,"esp"));
  569. # decompose Htable (low and high parts are kept separately),
  570. # generate Htable[]>>4, (u8)(Htable[]<<4), save to stack...
  571. for ($i=0;$i<18;$i++) {
  572. &mov ("edx",&DWP(16*$i+8-128,$Htbl)) if ($i<16);
  573. &movq ($lo[0],&QWP(16*$i+8-128,$Htbl)) if ($i<16);
  574. &psllq ($tmp[1],60) if ($i>1);
  575. &movq ($hi[0],&QWP(16*$i+0-128,$Htbl)) if ($i<16);
  576. &por ($lo[2],$tmp[1]) if ($i>1);
  577. &movq (&QWP($off1-128,"edi"),$lo[1]) if ($i>0 && $i<17);
  578. &psrlq ($lo[1],4) if ($i>0 && $i<17);
  579. &movq (&QWP($off1,"edi"),$hi[1]) if ($i>0 && $i<17);
  580. &movq ($tmp[0],$hi[1]) if ($i>0 && $i<17);
  581. &movq (&QWP($off2-128,"ebp"),$lo[2]) if ($i>1);
  582. &psrlq ($hi[1],4) if ($i>0 && $i<17);
  583. &movq (&QWP($off2,"ebp"),$hi[2]) if ($i>1);
  584. &shl ("edx",4) if ($i<16);
  585. &mov (&BP($i,"esp"),&LB("edx")) if ($i<16);
  586. unshift (@lo,pop(@lo)); # "rotate" registers
  587. unshift (@hi,pop(@hi));
  588. unshift (@tmp,pop(@tmp));
  589. $off1 += 8 if ($i>0);
  590. $off2 += 8 if ($i>1);
  591. }
  592. }
  593. &movq ($Zhi,&QWP(0,"eax"));
  594. &mov ("ebx",&DWP(8,"eax"));
  595. &mov ("edx",&DWP(12,"eax")); # load Xi
  596. &set_label("outer",16);
  597. { my $nlo = "eax";
  598. my $dat = "edx";
  599. my @nhi = ("edi","ebp");
  600. my @rem = ("ebx","ecx");
  601. my @red = ("mm0","mm1","mm2");
  602. my $tmp = "mm3";
  603. &xor ($dat,&DWP(12,"ecx")); # merge input data
  604. &xor ("ebx",&DWP(8,"ecx"));
  605. &pxor ($Zhi,&QWP(0,"ecx"));
  606. &lea ("ecx",&DWP(16,"ecx")); # inp+=16
  607. #&mov (&DWP(528+12,"esp"),$dat); # save inp^Xi
  608. &mov (&DWP(528+8,"esp"),"ebx");
  609. &movq (&QWP(528+0,"esp"),$Zhi);
  610. &mov (&DWP(528+16+4,"esp"),"ecx"); # save inp
  611. &xor ($nlo,$nlo);
  612. &rol ($dat,8);
  613. &mov (&LB($nlo),&LB($dat));
  614. &mov ($nhi[1],$nlo);
  615. &and (&LB($nlo),0x0f);
  616. &shr ($nhi[1],4);
  617. &pxor ($red[0],$red[0]);
  618. &rol ($dat,8); # next byte
  619. &pxor ($red[1],$red[1]);
  620. &pxor ($red[2],$red[2]);
  621. # Just like in "May" verson modulo-schedule for critical path in
  622. # 'Z.hi ^= rem_8bit[Z.lo&0xff^((u8)H[nhi]<<4)]<<48'. Final 'pxor'
  623. # is scheduled so late that rem_8bit[] has to be shifted *right*
  624. # by 16, which is why last argument to pinsrw is 2, which
  625. # corresponds to <<32=<<48>>16...
  626. for ($j=11,$i=0;$i<15;$i++) {
  627. if ($i>0) {
  628. &pxor ($Zlo,&QWP(16,"esp",$nlo,8)); # Z^=H[nlo]
  629. &rol ($dat,8); # next byte
  630. &pxor ($Zhi,&QWP(16+128,"esp",$nlo,8));
  631. &pxor ($Zlo,$tmp);
  632. &pxor ($Zhi,&QWP(16+256+128,"esp",$nhi[0],8));
  633. &xor (&LB($rem[1]),&BP(0,"esp",$nhi[0])); # rem^(H[nhi]<<4)
  634. } else {
  635. &movq ($Zlo,&QWP(16,"esp",$nlo,8));
  636. &movq ($Zhi,&QWP(16+128,"esp",$nlo,8));
  637. }
  638. &mov (&LB($nlo),&LB($dat));
  639. &mov ($dat,&DWP(528+$j,"esp")) if (--$j%4==0);
  640. &movd ($rem[0],$Zlo);
  641. &movz ($rem[1],&LB($rem[1])) if ($i>0);
  642. &psrlq ($Zlo,8); # Z>>=8
  643. &movq ($tmp,$Zhi);
  644. &mov ($nhi[0],$nlo);
  645. &psrlq ($Zhi,8);
  646. &pxor ($Zlo,&QWP(16+256+0,"esp",$nhi[1],8)); # Z^=H[nhi]>>4
  647. &and (&LB($nlo),0x0f);
  648. &psllq ($tmp,56);
  649. &pxor ($Zhi,$red[1]) if ($i>1);
  650. &shr ($nhi[0],4);
  651. &pinsrw ($red[0],&WP(0,$rem_8bit,$rem[1],2),2) if ($i>0);
  652. unshift (@red,pop(@red)); # "rotate" registers
  653. unshift (@rem,pop(@rem));
  654. unshift (@nhi,pop(@nhi));
  655. }
  656. &pxor ($Zlo,&QWP(16,"esp",$nlo,8)); # Z^=H[nlo]
  657. &pxor ($Zhi,&QWP(16+128,"esp",$nlo,8));
  658. &xor (&LB($rem[1]),&BP(0,"esp",$nhi[0])); # rem^(H[nhi]<<4)
  659. &pxor ($Zlo,$tmp);
  660. &pxor ($Zhi,&QWP(16+256+128,"esp",$nhi[0],8));
  661. &movz ($rem[1],&LB($rem[1]));
  662. &pxor ($red[2],$red[2]); # clear 2nd word
  663. &psllq ($red[1],4);
  664. &movd ($rem[0],$Zlo);
  665. &psrlq ($Zlo,4); # Z>>=4
  666. &movq ($tmp,$Zhi);
  667. &psrlq ($Zhi,4);
  668. &shl ($rem[0],4); # rem<<4
  669. &pxor ($Zlo,&QWP(16,"esp",$nhi[1],8)); # Z^=H[nhi]
  670. &psllq ($tmp,60);
  671. &movz ($rem[0],&LB($rem[0]));
  672. &pxor ($Zlo,$tmp);
  673. &pxor ($Zhi,&QWP(16+128,"esp",$nhi[1],8));
  674. &pinsrw ($red[0],&WP(0,$rem_8bit,$rem[1],2),2);
  675. &pxor ($Zhi,$red[1]);
  676. &movd ($dat,$Zlo);
  677. &pinsrw ($red[2],&WP(0,$rem_8bit,$rem[0],2),3); # last is <<48
  678. &psllq ($red[0],12); # correct by <<16>>4
  679. &pxor ($Zhi,$red[0]);
  680. &psrlq ($Zlo,32);
  681. &pxor ($Zhi,$red[2]);
  682. &mov ("ecx",&DWP(528+16+4,"esp")); # restore inp
  683. &movd ("ebx",$Zlo);
  684. &movq ($tmp,$Zhi); # 01234567
  685. &psllw ($Zhi,8); # 1.3.5.7.
  686. &psrlw ($tmp,8); # .0.2.4.6
  687. &por ($Zhi,$tmp); # 10325476
  688. &bswap ($dat);
  689. &pshufw ($Zhi,$Zhi,0b00011011); # 76543210
  690. &bswap ("ebx");
  691. &cmp ("ecx",&DWP(528+16+8,"esp")); # are we done?
  692. &jne (&label("outer"));
  693. }
  694. &mov ("eax",&DWP(528+16+0,"esp")); # restore Xi
  695. &mov (&DWP(12,"eax"),"edx");
  696. &mov (&DWP(8,"eax"),"ebx");
  697. &movq (&QWP(0,"eax"),$Zhi);
  698. &mov ("esp",&DWP(528+16+12,"esp")); # restore original %esp
  699. &emms ();
  700. }
  701. &function_end("gcm_ghash_4bit_mmx");
  702. }}
  703. if ($sse2) {{
  704. ######################################################################
  705. # PCLMULQDQ version.
  706. $Xip="eax";
  707. $Htbl="edx";
  708. $const="ecx";
  709. $inp="esi";
  710. $len="ebx";
  711. ($Xi,$Xhi)=("xmm0","xmm1"); $Hkey="xmm2";
  712. ($T1,$T2,$T3)=("xmm3","xmm4","xmm5");
  713. ($Xn,$Xhn)=("xmm6","xmm7");
  714. &static_label("bswap");
  715. sub clmul64x64_T2 { # minimal "register" pressure
  716. my ($Xhi,$Xi,$Hkey,$HK)=@_;
  717. &movdqa ($Xhi,$Xi); #
  718. &pshufd ($T1,$Xi,0b01001110);
  719. &pshufd ($T2,$Hkey,0b01001110) if (!defined($HK));
  720. &pxor ($T1,$Xi); #
  721. &pxor ($T2,$Hkey) if (!defined($HK));
  722. $HK=$T2 if (!defined($HK));
  723. &pclmulqdq ($Xi,$Hkey,0x00); #######
  724. &pclmulqdq ($Xhi,$Hkey,0x11); #######
  725. &pclmulqdq ($T1,$HK,0x00); #######
  726. &xorps ($T1,$Xi); #
  727. &xorps ($T1,$Xhi); #
  728. &movdqa ($T2,$T1); #
  729. &psrldq ($T1,8);
  730. &pslldq ($T2,8); #
  731. &pxor ($Xhi,$T1);
  732. &pxor ($Xi,$T2); #
  733. }
  734. sub clmul64x64_T3 {
  735. # Even though this subroutine offers visually better ILP, it
  736. # was empirically found to be a tad slower than above version.
  737. # At least in gcm_ghash_clmul context. But it's just as well,
  738. # because loop modulo-scheduling is possible only thanks to
  739. # minimized "register" pressure...
  740. my ($Xhi,$Xi,$Hkey)=@_;
  741. &movdqa ($T1,$Xi); #
  742. &movdqa ($Xhi,$Xi);
  743. &pclmulqdq ($Xi,$Hkey,0x00); #######
  744. &pclmulqdq ($Xhi,$Hkey,0x11); #######
  745. &pshufd ($T2,$T1,0b01001110); #
  746. &pshufd ($T3,$Hkey,0b01001110);
  747. &pxor ($T2,$T1); #
  748. &pxor ($T3,$Hkey);
  749. &pclmulqdq ($T2,$T3,0x00); #######
  750. &pxor ($T2,$Xi); #
  751. &pxor ($T2,$Xhi); #
  752. &movdqa ($T3,$T2); #
  753. &psrldq ($T2,8);
  754. &pslldq ($T3,8); #
  755. &pxor ($Xhi,$T2);
  756. &pxor ($Xi,$T3); #
  757. }
  758. if (1) { # Algorithm 9 with <<1 twist.
  759. # Reduction is shorter and uses only two
  760. # temporary registers, which makes it better
  761. # candidate for interleaving with 64x64
  762. # multiplication. Pre-modulo-scheduled loop
  763. # was found to be ~20% faster than Algorithm 5
  764. # below. Algorithm 9 was therefore chosen for
  765. # further optimization...
  766. sub reduction_alg9 { # 17/11 times faster than Intel version
  767. my ($Xhi,$Xi) = @_;
  768. # 1st phase
  769. &movdqa ($T2,$Xi); #
  770. &movdqa ($T1,$Xi);
  771. &psllq ($Xi,5);
  772. &pxor ($T1,$Xi); #
  773. &psllq ($Xi,1);
  774. &pxor ($Xi,$T1); #
  775. &psllq ($Xi,57); #
  776. &movdqa ($T1,$Xi); #
  777. &pslldq ($Xi,8);
  778. &psrldq ($T1,8); #
  779. &pxor ($Xi,$T2);
  780. &pxor ($Xhi,$T1); #
  781. # 2nd phase
  782. &movdqa ($T2,$Xi);
  783. &psrlq ($Xi,1);
  784. &pxor ($Xhi,$T2); #
  785. &pxor ($T2,$Xi);
  786. &psrlq ($Xi,5);
  787. &pxor ($Xi,$T2); #
  788. &psrlq ($Xi,1); #
  789. &pxor ($Xi,$Xhi) #
  790. }
  791. &function_begin_B("gcm_init_clmul");
  792. &mov ($Htbl,&wparam(0));
  793. &mov ($Xip,&wparam(1));
  794. &call (&label("pic"));
  795. &set_label("pic");
  796. &blindpop ($const);
  797. &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const));
  798. &movdqu ($Hkey,&QWP(0,$Xip));
  799. &pshufd ($Hkey,$Hkey,0b01001110);# dword swap
  800. # <<1 twist
  801. &pshufd ($T2,$Hkey,0b11111111); # broadcast uppermost dword
  802. &movdqa ($T1,$Hkey);
  803. &psllq ($Hkey,1);
  804. &pxor ($T3,$T3); #
  805. &psrlq ($T1,63);
  806. &pcmpgtd ($T3,$T2); # broadcast carry bit
  807. &pslldq ($T1,8);
  808. &por ($Hkey,$T1); # H<<=1
  809. # magic reduction
  810. &pand ($T3,&QWP(16,$const)); # 0x1c2_polynomial
  811. &pxor ($Hkey,$T3); # if(carry) H^=0x1c2_polynomial
  812. # calculate H^2
  813. &movdqa ($Xi,$Hkey);
  814. &clmul64x64_T2 ($Xhi,$Xi,$Hkey);
  815. &reduction_alg9 ($Xhi,$Xi);
  816. &pshufd ($T1,$Hkey,0b01001110);
  817. &pshufd ($T2,$Xi,0b01001110);
  818. &pxor ($T1,$Hkey); # Karatsuba pre-processing
  819. &movdqu (&QWP(0,$Htbl),$Hkey); # save H
  820. &pxor ($T2,$Xi); # Karatsuba pre-processing
  821. &movdqu (&QWP(16,$Htbl),$Xi); # save H^2
  822. &palignr ($T2,$T1,8); # low part is H.lo^H.hi
  823. &movdqu (&QWP(32,$Htbl),$T2); # save Karatsuba "salt"
  824. &ret ();
  825. &function_end_B("gcm_init_clmul");
  826. &function_begin_B("gcm_gmult_clmul");
  827. &mov ($Xip,&wparam(0));
  828. &mov ($Htbl,&wparam(1));
  829. &call (&label("pic"));
  830. &set_label("pic");
  831. &blindpop ($const);
  832. &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const));
  833. &movdqu ($Xi,&QWP(0,$Xip));
  834. &movdqa ($T3,&QWP(0,$const));
  835. &movups ($Hkey,&QWP(0,$Htbl));
  836. &pshufb ($Xi,$T3);
  837. &movups ($T2,&QWP(32,$Htbl));
  838. &clmul64x64_T2 ($Xhi,$Xi,$Hkey,$T2);
  839. &reduction_alg9 ($Xhi,$Xi);
  840. &pshufb ($Xi,$T3);
  841. &movdqu (&QWP(0,$Xip),$Xi);
  842. &ret ();
  843. &function_end_B("gcm_gmult_clmul");
  844. &function_begin("gcm_ghash_clmul");
  845. &mov ($Xip,&wparam(0));
  846. &mov ($Htbl,&wparam(1));
  847. &mov ($inp,&wparam(2));
  848. &mov ($len,&wparam(3));
  849. &call (&label("pic"));
  850. &set_label("pic");
  851. &blindpop ($const);
  852. &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const));
  853. &movdqu ($Xi,&QWP(0,$Xip));
  854. &movdqa ($T3,&QWP(0,$const));
  855. &movdqu ($Hkey,&QWP(0,$Htbl));
  856. &pshufb ($Xi,$T3);
  857. &sub ($len,0x10);
  858. &jz (&label("odd_tail"));
  859. #######
  860. # Xi+2 =[H*(Ii+1 + Xi+1)] mod P =
  861. # [(H*Ii+1) + (H*Xi+1)] mod P =
  862. # [(H*Ii+1) + H^2*(Ii+Xi)] mod P
  863. #
  864. &movdqu ($T1,&QWP(0,$inp)); # Ii
  865. &movdqu ($Xn,&QWP(16,$inp)); # Ii+1
  866. &pshufb ($T1,$T3);
  867. &pshufb ($Xn,$T3);
  868. &movdqu ($T3,&QWP(32,$Htbl));
  869. &pxor ($Xi,$T1); # Ii+Xi
  870. &pshufd ($T1,$Xn,0b01001110); # H*Ii+1
  871. &movdqa ($Xhn,$Xn);
  872. &pxor ($T1,$Xn); #
  873. &lea ($inp,&DWP(32,$inp)); # i+=2
  874. &pclmulqdq ($Xn,$Hkey,0x00); #######
  875. &pclmulqdq ($Xhn,$Hkey,0x11); #######
  876. &pclmulqdq ($T1,$T3,0x00); #######
  877. &movups ($Hkey,&QWP(16,$Htbl)); # load H^2
  878. &nop ();
  879. &sub ($len,0x20);
  880. &jbe (&label("even_tail"));
  881. &jmp (&label("mod_loop"));
  882. &set_label("mod_loop",32);
  883. &pshufd ($T2,$Xi,0b01001110); # H^2*(Ii+Xi)
  884. &movdqa ($Xhi,$Xi);
  885. &pxor ($T2,$Xi); #
  886. &nop ();
  887. &pclmulqdq ($Xi,$Hkey,0x00); #######
  888. &pclmulqdq ($Xhi,$Hkey,0x11); #######
  889. &pclmulqdq ($T2,$T3,0x10); #######
  890. &movups ($Hkey,&QWP(0,$Htbl)); # load H
  891. &xorps ($Xi,$Xn); # (H*Ii+1) + H^2*(Ii+Xi)
  892. &movdqa ($T3,&QWP(0,$const));
  893. &xorps ($Xhi,$Xhn);
  894. &movdqu ($Xhn,&QWP(0,$inp)); # Ii
  895. &pxor ($T1,$Xi); # aggregated Karatsuba post-processing
  896. &movdqu ($Xn,&QWP(16,$inp)); # Ii+1
  897. &pxor ($T1,$Xhi); #
  898. &pshufb ($Xhn,$T3);
  899. &pxor ($T2,$T1); #
  900. &movdqa ($T1,$T2); #
  901. &psrldq ($T2,8);
  902. &pslldq ($T1,8); #
  903. &pxor ($Xhi,$T2);
  904. &pxor ($Xi,$T1); #
  905. &pshufb ($Xn,$T3);
  906. &pxor ($Xhi,$Xhn); # "Ii+Xi", consume early
  907. &movdqa ($Xhn,$Xn); #&clmul64x64_TX ($Xhn,$Xn,$Hkey); H*Ii+1
  908. &movdqa ($T2,$Xi); #&reduction_alg9($Xhi,$Xi); 1st phase
  909. &movdqa ($T1,$Xi);
  910. &psllq ($Xi,5);
  911. &pxor ($T1,$Xi); #
  912. &psllq ($Xi,1);
  913. &pxor ($Xi,$T1); #
  914. &pclmulqdq ($Xn,$Hkey,0x00); #######
  915. &movups ($T3,&QWP(32,$Htbl));
  916. &psllq ($Xi,57); #
  917. &movdqa ($T1,$Xi); #
  918. &pslldq ($Xi,8);
  919. &psrldq ($T1,8); #
  920. &pxor ($Xi,$T2);
  921. &pxor ($Xhi,$T1); #
  922. &pshufd ($T1,$Xhn,0b01001110);
  923. &movdqa ($T2,$Xi); # 2nd phase
  924. &psrlq ($Xi,1);
  925. &pxor ($T1,$Xhn);
  926. &pxor ($Xhi,$T2); #
  927. &pclmulqdq ($Xhn,$Hkey,0x11); #######
  928. &movups ($Hkey,&QWP(16,$Htbl)); # load H^2
  929. &pxor ($T2,$Xi);
  930. &psrlq ($Xi,5);
  931. &pxor ($Xi,$T2); #
  932. &psrlq ($Xi,1); #
  933. &pxor ($Xi,$Xhi) #
  934. &pclmulqdq ($T1,$T3,0x00); #######
  935. &lea ($inp,&DWP(32,$inp));
  936. &sub ($len,0x20);
  937. &ja (&label("mod_loop"));
  938. &set_label("even_tail");
  939. &pshufd ($T2,$Xi,0b01001110); # H^2*(Ii+Xi)
  940. &movdqa ($Xhi,$Xi);
  941. &pxor ($T2,$Xi); #
  942. &pclmulqdq ($Xi,$Hkey,0x00); #######
  943. &pclmulqdq ($Xhi,$Hkey,0x11); #######
  944. &pclmulqdq ($T2,$T3,0x10); #######
  945. &movdqa ($T3,&QWP(0,$const));
  946. &xorps ($Xi,$Xn); # (H*Ii+1) + H^2*(Ii+Xi)
  947. &xorps ($Xhi,$Xhn);
  948. &pxor ($T1,$Xi); # aggregated Karatsuba post-processing
  949. &pxor ($T1,$Xhi); #
  950. &pxor ($T2,$T1); #
  951. &movdqa ($T1,$T2); #
  952. &psrldq ($T2,8);
  953. &pslldq ($T1,8); #
  954. &pxor ($Xhi,$T2);
  955. &pxor ($Xi,$T1); #
  956. &reduction_alg9 ($Xhi,$Xi);
  957. &test ($len,$len);
  958. &jnz (&label("done"));
  959. &movups ($Hkey,&QWP(0,$Htbl)); # load H
  960. &set_label("odd_tail");
  961. &movdqu ($T1,&QWP(0,$inp)); # Ii
  962. &pshufb ($T1,$T3);
  963. &pxor ($Xi,$T1); # Ii+Xi
  964. &clmul64x64_T2 ($Xhi,$Xi,$Hkey); # H*(Ii+Xi)
  965. &reduction_alg9 ($Xhi,$Xi);
  966. &set_label("done");
  967. &pshufb ($Xi,$T3);
  968. &movdqu (&QWP(0,$Xip),$Xi);
  969. &function_end("gcm_ghash_clmul");
  970. } else { # Algorith 5. Kept for reference purposes.
  971. sub reduction_alg5 { # 19/16 times faster than Intel version
  972. my ($Xhi,$Xi)=@_;
  973. # <<1
  974. &movdqa ($T1,$Xi); #
  975. &movdqa ($T2,$Xhi);
  976. &pslld ($Xi,1);
  977. &pslld ($Xhi,1); #
  978. &psrld ($T1,31);
  979. &psrld ($T2,31); #
  980. &movdqa ($T3,$T1);
  981. &pslldq ($T1,4);
  982. &psrldq ($T3,12); #
  983. &pslldq ($T2,4);
  984. &por ($Xhi,$T3); #
  985. &por ($Xi,$T1);
  986. &por ($Xhi,$T2); #
  987. # 1st phase
  988. &movdqa ($T1,$Xi);
  989. &movdqa ($T2,$Xi);
  990. &movdqa ($T3,$Xi); #
  991. &pslld ($T1,31);
  992. &pslld ($T2,30);
  993. &pslld ($Xi,25); #
  994. &pxor ($T1,$T2);
  995. &pxor ($T1,$Xi); #
  996. &movdqa ($T2,$T1); #
  997. &pslldq ($T1,12);
  998. &psrldq ($T2,4); #
  999. &pxor ($T3,$T1);
  1000. # 2nd phase
  1001. &pxor ($Xhi,$T3); #
  1002. &movdqa ($Xi,$T3);
  1003. &movdqa ($T1,$T3);
  1004. &psrld ($Xi,1); #
  1005. &psrld ($T1,2);
  1006. &psrld ($T3,7); #
  1007. &pxor ($Xi,$T1);
  1008. &pxor ($Xhi,$T2);
  1009. &pxor ($Xi,$T3); #
  1010. &pxor ($Xi,$Xhi); #
  1011. }
  1012. &function_begin_B("gcm_init_clmul");
  1013. &mov ($Htbl,&wparam(0));
  1014. &mov ($Xip,&wparam(1));
  1015. &call (&label("pic"));
  1016. &set_label("pic");
  1017. &blindpop ($const);
  1018. &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const));
  1019. &movdqu ($Hkey,&QWP(0,$Xip));
  1020. &pshufd ($Hkey,$Hkey,0b01001110);# dword swap
  1021. # calculate H^2
  1022. &movdqa ($Xi,$Hkey);
  1023. &clmul64x64_T3 ($Xhi,$Xi,$Hkey);
  1024. &reduction_alg5 ($Xhi,$Xi);
  1025. &movdqu (&QWP(0,$Htbl),$Hkey); # save H
  1026. &movdqu (&QWP(16,$Htbl),$Xi); # save H^2
  1027. &ret ();
  1028. &function_end_B("gcm_init_clmul");
  1029. &function_begin_B("gcm_gmult_clmul");
  1030. &mov ($Xip,&wparam(0));
  1031. &mov ($Htbl,&wparam(1));
  1032. &call (&label("pic"));
  1033. &set_label("pic");
  1034. &blindpop ($const);
  1035. &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const));
  1036. &movdqu ($Xi,&QWP(0,$Xip));
  1037. &movdqa ($Xn,&QWP(0,$const));
  1038. &movdqu ($Hkey,&QWP(0,$Htbl));
  1039. &pshufb ($Xi,$Xn);
  1040. &clmul64x64_T3 ($Xhi,$Xi,$Hkey);
  1041. &reduction_alg5 ($Xhi,$Xi);
  1042. &pshufb ($Xi,$Xn);
  1043. &movdqu (&QWP(0,$Xip),$Xi);
  1044. &ret ();
  1045. &function_end_B("gcm_gmult_clmul");
  1046. &function_begin("gcm_ghash_clmul");
  1047. &mov ($Xip,&wparam(0));
  1048. &mov ($Htbl,&wparam(1));
  1049. &mov ($inp,&wparam(2));
  1050. &mov ($len,&wparam(3));
  1051. &call (&label("pic"));
  1052. &set_label("pic");
  1053. &blindpop ($const);
  1054. &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const));
  1055. &movdqu ($Xi,&QWP(0,$Xip));
  1056. &movdqa ($T3,&QWP(0,$const));
  1057. &movdqu ($Hkey,&QWP(0,$Htbl));
  1058. &pshufb ($Xi,$T3);
  1059. &sub ($len,0x10);
  1060. &jz (&label("odd_tail"));
  1061. #######
  1062. # Xi+2 =[H*(Ii+1 + Xi+1)] mod P =
  1063. # [(H*Ii+1) + (H*Xi+1)] mod P =
  1064. # [(H*Ii+1) + H^2*(Ii+Xi)] mod P
  1065. #
  1066. &movdqu ($T1,&QWP(0,$inp)); # Ii
  1067. &movdqu ($Xn,&QWP(16,$inp)); # Ii+1
  1068. &pshufb ($T1,$T3);
  1069. &pshufb ($Xn,$T3);
  1070. &pxor ($Xi,$T1); # Ii+Xi
  1071. &clmul64x64_T3 ($Xhn,$Xn,$Hkey); # H*Ii+1
  1072. &movdqu ($Hkey,&QWP(16,$Htbl)); # load H^2
  1073. &sub ($len,0x20);
  1074. &lea ($inp,&DWP(32,$inp)); # i+=2
  1075. &jbe (&label("even_tail"));
  1076. &set_label("mod_loop");
  1077. &clmul64x64_T3 ($Xhi,$Xi,$Hkey); # H^2*(Ii+Xi)
  1078. &movdqu ($Hkey,&QWP(0,$Htbl)); # load H
  1079. &pxor ($Xi,$Xn); # (H*Ii+1) + H^2*(Ii+Xi)
  1080. &pxor ($Xhi,$Xhn);
  1081. &reduction_alg5 ($Xhi,$Xi);
  1082. #######
  1083. &movdqa ($T3,&QWP(0,$const));
  1084. &movdqu ($T1,&QWP(0,$inp)); # Ii
  1085. &movdqu ($Xn,&QWP(16,$inp)); # Ii+1
  1086. &pshufb ($T1,$T3);
  1087. &pshufb ($Xn,$T3);
  1088. &pxor ($Xi,$T1); # Ii+Xi
  1089. &clmul64x64_T3 ($Xhn,$Xn,$Hkey); # H*Ii+1
  1090. &movdqu ($Hkey,&QWP(16,$Htbl)); # load H^2
  1091. &sub ($len,0x20);
  1092. &lea ($inp,&DWP(32,$inp));
  1093. &ja (&label("mod_loop"));
  1094. &set_label("even_tail");
  1095. &clmul64x64_T3 ($Xhi,$Xi,$Hkey); # H^2*(Ii+Xi)
  1096. &pxor ($Xi,$Xn); # (H*Ii+1) + H^2*(Ii+Xi)
  1097. &pxor ($Xhi,$Xhn);
  1098. &reduction_alg5 ($Xhi,$Xi);
  1099. &movdqa ($T3,&QWP(0,$const));
  1100. &test ($len,$len);
  1101. &jnz (&label("done"));
  1102. &movdqu ($Hkey,&QWP(0,$Htbl)); # load H
  1103. &set_label("odd_tail");
  1104. &movdqu ($T1,&QWP(0,$inp)); # Ii
  1105. &pshufb ($T1,$T3);
  1106. &pxor ($Xi,$T1); # Ii+Xi
  1107. &clmul64x64_T3 ($Xhi,$Xi,$Hkey); # H*(Ii+Xi)
  1108. &reduction_alg5 ($Xhi,$Xi);
  1109. &movdqa ($T3,&QWP(0,$const));
  1110. &set_label("done");
  1111. &pshufb ($Xi,$T3);
  1112. &movdqu (&QWP(0,$Xip),$Xi);
  1113. &function_end("gcm_ghash_clmul");
  1114. }
  1115. &set_label("bswap",64);
  1116. &data_byte(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0);
  1117. &data_byte(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xc2); # 0x1c2_polynomial
  1118. &set_label("rem_8bit",64);
  1119. &data_short(0x0000,0x01C2,0x0384,0x0246,0x0708,0x06CA,0x048C,0x054E);
  1120. &data_short(0x0E10,0x0FD2,0x0D94,0x0C56,0x0918,0x08DA,0x0A9C,0x0B5E);
  1121. &data_short(0x1C20,0x1DE2,0x1FA4,0x1E66,0x1B28,0x1AEA,0x18AC,0x196E);
  1122. &data_short(0x1230,0x13F2,0x11B4,0x1076,0x1538,0x14FA,0x16BC,0x177E);
  1123. &data_short(0x3840,0x3982,0x3BC4,0x3A06,0x3F48,0x3E8A,0x3CCC,0x3D0E);
  1124. &data_short(0x3650,0x3792,0x35D4,0x3416,0x3158,0x309A,0x32DC,0x331E);
  1125. &data_short(0x2460,0x25A2,0x27E4,0x2626,0x2368,0x22AA,0x20EC,0x212E);
  1126. &data_short(0x2A70,0x2BB2,0x29F4,0x2836,0x2D78,0x2CBA,0x2EFC,0x2F3E);
  1127. &data_short(0x7080,0x7142,0x7304,0x72C6,0x7788,0x764A,0x740C,0x75CE);
  1128. &data_short(0x7E90,0x7F52,0x7D14,0x7CD6,0x7998,0x785A,0x7A1C,0x7BDE);
  1129. &data_short(0x6CA0,0x6D62,0x6F24,0x6EE6,0x6BA8,0x6A6A,0x682C,0x69EE);
  1130. &data_short(0x62B0,0x6372,0x6134,0x60F6,0x65B8,0x647A,0x663C,0x67FE);
  1131. &data_short(0x48C0,0x4902,0x4B44,0x4A86,0x4FC8,0x4E0A,0x4C4C,0x4D8E);
  1132. &data_short(0x46D0,0x4712,0x4554,0x4496,0x41D8,0x401A,0x425C,0x439E);
  1133. &data_short(0x54E0,0x5522,0x5764,0x56A6,0x53E8,0x522A,0x506C,0x51AE);
  1134. &data_short(0x5AF0,0x5B32,0x5974,0x58B6,0x5DF8,0x5C3A,0x5E7C,0x5FBE);
  1135. &data_short(0xE100,0xE0C2,0xE284,0xE346,0xE608,0xE7CA,0xE58C,0xE44E);
  1136. &data_short(0xEF10,0xEED2,0xEC94,0xED56,0xE818,0xE9DA,0xEB9C,0xEA5E);
  1137. &data_short(0xFD20,0xFCE2,0xFEA4,0xFF66,0xFA28,0xFBEA,0xF9AC,0xF86E);
  1138. &data_short(0xF330,0xF2F2,0xF0B4,0xF176,0xF438,0xF5FA,0xF7BC,0xF67E);
  1139. &data_short(0xD940,0xD882,0xDAC4,0xDB06,0xDE48,0xDF8A,0xDDCC,0xDC0E);
  1140. &data_short(0xD750,0xD692,0xD4D4,0xD516,0xD058,0xD19A,0xD3DC,0xD21E);
  1141. &data_short(0xC560,0xC4A2,0xC6E4,0xC726,0xC268,0xC3AA,0xC1EC,0xC02E);
  1142. &data_short(0xCB70,0xCAB2,0xC8F4,0xC936,0xCC78,0xCDBA,0xCFFC,0xCE3E);
  1143. &data_short(0x9180,0x9042,0x9204,0x93C6,0x9688,0x974A,0x950C,0x94CE);
  1144. &data_short(0x9F90,0x9E52,0x9C14,0x9DD6,0x9898,0x995A,0x9B1C,0x9ADE);
  1145. &data_short(0x8DA0,0x8C62,0x8E24,0x8FE6,0x8AA8,0x8B6A,0x892C,0x88EE);
  1146. &data_short(0x83B0,0x8272,0x8034,0x81F6,0x84B8,0x857A,0x873C,0x86FE);
  1147. &data_short(0xA9C0,0xA802,0xAA44,0xAB86,0xAEC8,0xAF0A,0xAD4C,0xAC8E);
  1148. &data_short(0xA7D0,0xA612,0xA454,0xA596,0xA0D8,0xA11A,0xA35C,0xA29E);
  1149. &data_short(0xB5E0,0xB422,0xB664,0xB7A6,0xB2E8,0xB32A,0xB16C,0xB0AE);
  1150. &data_short(0xBBF0,0xBA32,0xB874,0xB9B6,0xBCF8,0xBD3A,0xBF7C,0xBEBE);
  1151. }} # $sse2
  1152. &set_label("rem_4bit",64);
  1153. &data_word(0,0x0000<<$S,0,0x1C20<<$S,0,0x3840<<$S,0,0x2460<<$S);
  1154. &data_word(0,0x7080<<$S,0,0x6CA0<<$S,0,0x48C0<<$S,0,0x54E0<<$S);
  1155. &data_word(0,0xE100<<$S,0,0xFD20<<$S,0,0xD940<<$S,0,0xC560<<$S);
  1156. &data_word(0,0x9180<<$S,0,0x8DA0<<$S,0,0xA9C0<<$S,0,0xB5E0<<$S);
  1157. }}} # !$x86only
  1158. &asciz("GHASH for x86, CRYPTOGAMS by <appro\@openssl.org>");
  1159. &asm_finish();
  1160. # A question was risen about choice of vanilla MMX. Or rather why wasn't
  1161. # SSE2 chosen instead? In addition to the fact that MMX runs on legacy
  1162. # CPUs such as PIII, "4-bit" MMX version was observed to provide better
  1163. # performance than *corresponding* SSE2 one even on contemporary CPUs.
  1164. # SSE2 results were provided by Peter-Michael Hager. He maintains SSE2
  1165. # implementation featuring full range of lookup-table sizes, but with
  1166. # per-invocation lookup table setup. Latter means that table size is
  1167. # chosen depending on how much data is to be hashed in every given call,
  1168. # more data - larger table. Best reported result for Core2 is ~4 cycles
  1169. # per processed byte out of 64KB block. This number accounts even for
  1170. # 64KB table setup overhead. As discussed in gcm128.c we choose to be
  1171. # more conservative in respect to lookup table sizes, but how do the
  1172. # results compare? Minimalistic "256B" MMX version delivers ~11 cycles
  1173. # on same platform. As also discussed in gcm128.c, next in line "8-bit
  1174. # Shoup's" or "4KB" method should deliver twice the performance of
  1175. # "256B" one, in other words not worse than ~6 cycles per byte. It
  1176. # should be also be noted that in SSE2 case improvement can be "super-
  1177. # linear," i.e. more than twice, mostly because >>8 maps to single
  1178. # instruction on SSE2 register. This is unlike "4-bit" case when >>4
  1179. # maps to same amount of instructions in both MMX and SSE2 cases.
  1180. # Bottom line is that switch to SSE2 is considered to be justifiable
  1181. # only in case we choose to implement "8-bit" method...