2
0

x86-gf2m.pl 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. # May 2011
  11. #
  12. # The module implements bn_GF2m_mul_2x2 polynomial multiplication used
  13. # in bn_gf2m.c. It's kind of low-hanging mechanical port from C for
  14. # the time being... Except that it has three code paths: pure integer
  15. # code suitable for any x86 CPU, MMX code suitable for PIII and later
  16. # and PCLMULQDQ suitable for Westmere and later. Improvement varies
  17. # from one benchmark and µ-arch to another. Below are interval values
  18. # for 163- and 571-bit ECDH benchmarks relative to compiler-generated
  19. # code:
  20. #
  21. # PIII 16%-30%
  22. # P4 12%-12%
  23. # Opteron 18%-40%
  24. # Core2 19%-44%
  25. # Atom 38%-64%
  26. # Westmere 53%-121%(PCLMULQDQ)/20%-32%(MMX)
  27. # Sandy Bridge 72%-127%(PCLMULQDQ)/27%-23%(MMX)
  28. #
  29. # Note that above improvement coefficients are not coefficients for
  30. # bn_GF2m_mul_2x2 itself. For example 120% ECDH improvement is result
  31. # of bn_GF2m_mul_2x2 being >4x faster. As it gets faster, benchmark
  32. # is more and more dominated by other subroutines, most notably by
  33. # BN_GF2m_mod[_mul]_arr...
  34. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  35. push(@INC,"${dir}","${dir}../../perlasm");
  36. require "x86asm.pl";
  37. &asm_init($ARGV[0],$0,$x86only = $ARGV[$#ARGV] eq "386");
  38. $sse2=0;
  39. for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
  40. &external_label("OPENSSL_ia32cap_P") if ($sse2);
  41. $a="eax";
  42. $b="ebx";
  43. ($a1,$a2,$a4)=("ecx","edx","ebp");
  44. $R="mm0";
  45. @T=("mm1","mm2");
  46. ($A,$B,$B30,$B31)=("mm2","mm3","mm4","mm5");
  47. @i=("esi","edi");
  48. if (!$x86only) {
  49. &function_begin_B("_mul_1x1_mmx");
  50. &sub ("esp",32+4);
  51. &mov ($a1,$a);
  52. &lea ($a2,&DWP(0,$a,$a));
  53. &and ($a1,0x3fffffff);
  54. &lea ($a4,&DWP(0,$a2,$a2));
  55. &mov (&DWP(0*4,"esp"),0);
  56. &and ($a2,0x7fffffff);
  57. &movd ($A,$a);
  58. &movd ($B,$b);
  59. &mov (&DWP(1*4,"esp"),$a1); # a1
  60. &xor ($a1,$a2); # a1^a2
  61. &pxor ($B31,$B31);
  62. &pxor ($B30,$B30);
  63. &mov (&DWP(2*4,"esp"),$a2); # a2
  64. &xor ($a2,$a4); # a2^a4
  65. &mov (&DWP(3*4,"esp"),$a1); # a1^a2
  66. &pcmpgtd($B31,$A); # broadcast 31st bit
  67. &paddd ($A,$A); # $A<<=1
  68. &xor ($a1,$a2); # a1^a4=a1^a2^a2^a4
  69. &mov (&DWP(4*4,"esp"),$a4); # a4
  70. &xor ($a4,$a2); # a2=a4^a2^a4
  71. &pand ($B31,$B);
  72. &pcmpgtd($B30,$A); # broadcast 30th bit
  73. &mov (&DWP(5*4,"esp"),$a1); # a1^a4
  74. &xor ($a4,$a1); # a1^a2^a4
  75. &psllq ($B31,31);
  76. &pand ($B30,$B);
  77. &mov (&DWP(6*4,"esp"),$a2); # a2^a4
  78. &mov (@i[0],0x7);
  79. &mov (&DWP(7*4,"esp"),$a4); # a1^a2^a4
  80. &mov ($a4,@i[0]);
  81. &and (@i[0],$b);
  82. &shr ($b,3);
  83. &mov (@i[1],$a4);
  84. &psllq ($B30,30);
  85. &and (@i[1],$b);
  86. &shr ($b,3);
  87. &movd ($R,&DWP(0,"esp",@i[0],4));
  88. &mov (@i[0],$a4);
  89. &and (@i[0],$b);
  90. &shr ($b,3);
  91. for($n=1;$n<9;$n++) {
  92. &movd (@T[1],&DWP(0,"esp",@i[1],4));
  93. &mov (@i[1],$a4);
  94. &psllq (@T[1],3*$n);
  95. &and (@i[1],$b);
  96. &shr ($b,3);
  97. &pxor ($R,@T[1]);
  98. push(@i,shift(@i)); push(@T,shift(@T));
  99. }
  100. &movd (@T[1],&DWP(0,"esp",@i[1],4));
  101. &pxor ($R,$B30);
  102. &psllq (@T[1],3*$n++);
  103. &pxor ($R,@T[1]);
  104. &movd (@T[0],&DWP(0,"esp",@i[0],4));
  105. &pxor ($R,$B31);
  106. &psllq (@T[0],3*$n);
  107. &add ("esp",32+4);
  108. &pxor ($R,@T[0]);
  109. &ret ();
  110. &function_end_B("_mul_1x1_mmx");
  111. }
  112. ($lo,$hi)=("eax","edx");
  113. @T=("ecx","ebp");
  114. &function_begin_B("_mul_1x1_ialu");
  115. &sub ("esp",32+4);
  116. &mov ($a1,$a);
  117. &lea ($a2,&DWP(0,$a,$a));
  118. &lea ($a4,&DWP(0,"",$a,4));
  119. &and ($a1,0x3fffffff);
  120. &lea (@i[1],&DWP(0,$lo,$lo));
  121. &sar ($lo,31); # broadcast 31st bit
  122. &mov (&DWP(0*4,"esp"),0);
  123. &and ($a2,0x7fffffff);
  124. &mov (&DWP(1*4,"esp"),$a1); # a1
  125. &xor ($a1,$a2); # a1^a2
  126. &mov (&DWP(2*4,"esp"),$a2); # a2
  127. &xor ($a2,$a4); # a2^a4
  128. &mov (&DWP(3*4,"esp"),$a1); # a1^a2
  129. &xor ($a1,$a2); # a1^a4=a1^a2^a2^a4
  130. &mov (&DWP(4*4,"esp"),$a4); # a4
  131. &xor ($a4,$a2); # a2=a4^a2^a4
  132. &mov (&DWP(5*4,"esp"),$a1); # a1^a4
  133. &xor ($a4,$a1); # a1^a2^a4
  134. &sar (@i[1],31); # broardcast 30th bit
  135. &and ($lo,$b);
  136. &mov (&DWP(6*4,"esp"),$a2); # a2^a4
  137. &and (@i[1],$b);
  138. &mov (&DWP(7*4,"esp"),$a4); # a1^a2^a4
  139. &mov ($hi,$lo);
  140. &shl ($lo,31);
  141. &mov (@T[0],@i[1]);
  142. &shr ($hi,1);
  143. &mov (@i[0],0x7);
  144. &shl (@i[1],30);
  145. &and (@i[0],$b);
  146. &shr (@T[0],2);
  147. &xor ($lo,@i[1]);
  148. &shr ($b,3);
  149. &mov (@i[1],0x7); # 5-byte instruction!?
  150. &and (@i[1],$b);
  151. &shr ($b,3);
  152. &xor ($hi,@T[0]);
  153. &xor ($lo,&DWP(0,"esp",@i[0],4));
  154. &mov (@i[0],0x7);
  155. &and (@i[0],$b);
  156. &shr ($b,3);
  157. for($n=1;$n<9;$n++) {
  158. &mov (@T[1],&DWP(0,"esp",@i[1],4));
  159. &mov (@i[1],0x7);
  160. &mov (@T[0],@T[1]);
  161. &shl (@T[1],3*$n);
  162. &and (@i[1],$b);
  163. &shr (@T[0],32-3*$n);
  164. &xor ($lo,@T[1]);
  165. &shr ($b,3);
  166. &xor ($hi,@T[0]);
  167. push(@i,shift(@i)); push(@T,shift(@T));
  168. }
  169. &mov (@T[1],&DWP(0,"esp",@i[1],4));
  170. &mov (@T[0],@T[1]);
  171. &shl (@T[1],3*$n);
  172. &mov (@i[1],&DWP(0,"esp",@i[0],4));
  173. &shr (@T[0],32-3*$n); $n++;
  174. &mov (@i[0],@i[1]);
  175. &xor ($lo,@T[1]);
  176. &shl (@i[1],3*$n);
  177. &xor ($hi,@T[0]);
  178. &shr (@i[0],32-3*$n);
  179. &xor ($lo,@i[1]);
  180. &xor ($hi,@i[0]);
  181. &add ("esp",32+4);
  182. &ret ();
  183. &function_end_B("_mul_1x1_ialu");
  184. # void bn_GF2m_mul_2x2(BN_ULONG *r, BN_ULONG a1, BN_ULONG a0, BN_ULONG b1, BN_ULONG b0);
  185. &function_begin_B("bn_GF2m_mul_2x2");
  186. if (!$x86only) {
  187. &picmeup("edx","OPENSSL_ia32cap_P");
  188. &mov ("eax",&DWP(0,"edx"));
  189. &mov ("edx",&DWP(4,"edx"));
  190. &test ("eax",1<<23); # check MMX bit
  191. &jz (&label("ialu"));
  192. if ($sse2) {
  193. &test ("eax",1<<24); # check FXSR bit
  194. &jz (&label("mmx"));
  195. &test ("edx",1<<1); # check PCLMULQDQ bit
  196. &jz (&label("mmx"));
  197. &movups ("xmm0",&QWP(8,"esp"));
  198. &shufps ("xmm0","xmm0",0b10110001);
  199. &pclmulqdq ("xmm0","xmm0",1);
  200. &mov ("eax",&DWP(4,"esp"));
  201. &movups (&QWP(0,"eax"),"xmm0");
  202. &ret ();
  203. &set_label("mmx",16);
  204. }
  205. &push ("ebp");
  206. &push ("ebx");
  207. &push ("esi");
  208. &push ("edi");
  209. &mov ($a,&wparam(1));
  210. &mov ($b,&wparam(3));
  211. &call ("_mul_1x1_mmx"); # a1·b1
  212. &movq ("mm7",$R);
  213. &mov ($a,&wparam(2));
  214. &mov ($b,&wparam(4));
  215. &call ("_mul_1x1_mmx"); # a0·b0
  216. &movq ("mm6",$R);
  217. &mov ($a,&wparam(1));
  218. &mov ($b,&wparam(3));
  219. &xor ($a,&wparam(2));
  220. &xor ($b,&wparam(4));
  221. &call ("_mul_1x1_mmx"); # (a0+a1)·(b0+b1)
  222. &pxor ($R,"mm7");
  223. &mov ($a,&wparam(0));
  224. &pxor ($R,"mm6"); # (a0+a1)·(b0+b1)-a1·b1-a0·b0
  225. &movq ($A,$R);
  226. &psllq ($R,32);
  227. &pop ("edi");
  228. &psrlq ($A,32);
  229. &pop ("esi");
  230. &pxor ($R,"mm6");
  231. &pop ("ebx");
  232. &pxor ($A,"mm7");
  233. &movq (&QWP(0,$a),$R);
  234. &pop ("ebp");
  235. &movq (&QWP(8,$a),$A);
  236. &emms ();
  237. &ret ();
  238. &set_label("ialu",16);
  239. }
  240. &push ("ebp");
  241. &push ("ebx");
  242. &push ("esi");
  243. &push ("edi");
  244. &stack_push(4+1);
  245. &mov ($a,&wparam(1));
  246. &mov ($b,&wparam(3));
  247. &call ("_mul_1x1_ialu"); # a1·b1
  248. &mov (&DWP(8,"esp"),$lo);
  249. &mov (&DWP(12,"esp"),$hi);
  250. &mov ($a,&wparam(2));
  251. &mov ($b,&wparam(4));
  252. &call ("_mul_1x1_ialu"); # a0·b0
  253. &mov (&DWP(0,"esp"),$lo);
  254. &mov (&DWP(4,"esp"),$hi);
  255. &mov ($a,&wparam(1));
  256. &mov ($b,&wparam(3));
  257. &xor ($a,&wparam(2));
  258. &xor ($b,&wparam(4));
  259. &call ("_mul_1x1_ialu"); # (a0+a1)·(b0+b1)
  260. &mov ("ebp",&wparam(0));
  261. @r=("ebx","ecx","edi","esi");
  262. &mov (@r[0],&DWP(0,"esp"));
  263. &mov (@r[1],&DWP(4,"esp"));
  264. &mov (@r[2],&DWP(8,"esp"));
  265. &mov (@r[3],&DWP(12,"esp"));
  266. &xor ($lo,$hi);
  267. &xor ($hi,@r[1]);
  268. &xor ($lo,@r[0]);
  269. &mov (&DWP(0,"ebp"),@r[0]);
  270. &xor ($hi,@r[2]);
  271. &mov (&DWP(12,"ebp"),@r[3]);
  272. &xor ($lo,@r[3]);
  273. &stack_pop(4+1);
  274. &xor ($hi,@r[3]);
  275. &pop ("edi");
  276. &xor ($lo,$hi);
  277. &pop ("esi");
  278. &mov (&DWP(8,"ebp"),$hi);
  279. &pop ("ebx");
  280. &mov (&DWP(4,"ebp"),$lo);
  281. &pop ("ebp");
  282. &ret ();
  283. &function_end_B("bn_GF2m_mul_2x2");
  284. &asciz ("GF(2^m) Multiplication for x86, CRYPTOGAMS by <appro\@openssl.org>");
  285. &asm_finish();