co-586.pl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #! /usr/bin/env perl
  2. # Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
  9. push(@INC,"${dir}","${dir}../../perlasm");
  10. require "x86asm.pl";
  11. $output = pop;
  12. open STDOUT,">$output";
  13. &asm_init($ARGV[0]);
  14. &bn_mul_comba("bn_mul_comba8",8);
  15. &bn_mul_comba("bn_mul_comba4",4);
  16. &bn_sqr_comba("bn_sqr_comba8",8);
  17. &bn_sqr_comba("bn_sqr_comba4",4);
  18. &asm_finish();
  19. close STDOUT;
  20. sub mul_add_c
  21. {
  22. local($a,$ai,$b,$bi,$c0,$c1,$c2,$pos,$i,$na,$nb)=@_;
  23. # pos == -1 if eax and edx are pre-loaded, 0 to load from next
  24. # words, and 1 if load return value
  25. &comment("mul a[$ai]*b[$bi]");
  26. # "eax" and "edx" will always be pre-loaded.
  27. # &mov("eax",&DWP($ai*4,$a,"",0)) ;
  28. # &mov("edx",&DWP($bi*4,$b,"",0));
  29. &mul("edx");
  30. &add($c0,"eax");
  31. &mov("eax",&DWP(($na)*4,$a,"",0)) if $pos == 0; # laod next a
  32. &mov("eax",&wparam(0)) if $pos > 0; # load r[]
  33. ###
  34. &adc($c1,"edx");
  35. &mov("edx",&DWP(($nb)*4,$b,"",0)) if $pos == 0; # laod next b
  36. &mov("edx",&DWP(($nb)*4,$b,"",0)) if $pos == 1; # laod next b
  37. ###
  38. &adc($c2,0);
  39. # is pos > 1, it means it is the last loop
  40. &mov(&DWP($i*4,"eax","",0),$c0) if $pos > 0; # save r[];
  41. &mov("eax",&DWP(($na)*4,$a,"",0)) if $pos == 1; # laod next a
  42. }
  43. sub sqr_add_c
  44. {
  45. local($r,$a,$ai,$bi,$c0,$c1,$c2,$pos,$i,$na,$nb)=@_;
  46. # pos == -1 if eax and edx are pre-loaded, 0 to load from next
  47. # words, and 1 if load return value
  48. &comment("sqr a[$ai]*a[$bi]");
  49. # "eax" and "edx" will always be pre-loaded.
  50. # &mov("eax",&DWP($ai*4,$a,"",0)) ;
  51. # &mov("edx",&DWP($bi*4,$b,"",0));
  52. if ($ai == $bi)
  53. { &mul("eax");}
  54. else
  55. { &mul("edx");}
  56. &add($c0,"eax");
  57. &mov("eax",&DWP(($na)*4,$a,"",0)) if $pos == 0; # load next a
  58. ###
  59. &adc($c1,"edx");
  60. &mov("edx",&DWP(($nb)*4,$a,"",0)) if ($pos == 1) && ($na != $nb);
  61. ###
  62. &adc($c2,0);
  63. # is pos > 1, it means it is the last loop
  64. &mov(&DWP($i*4,$r,"",0),$c0) if $pos > 0; # save r[];
  65. &mov("eax",&DWP(($na)*4,$a,"",0)) if $pos == 1; # load next b
  66. }
  67. sub sqr_add_c2
  68. {
  69. local($r,$a,$ai,$bi,$c0,$c1,$c2,$pos,$i,$na,$nb)=@_;
  70. # pos == -1 if eax and edx are pre-loaded, 0 to load from next
  71. # words, and 1 if load return value
  72. &comment("sqr a[$ai]*a[$bi]");
  73. # "eax" and "edx" will always be pre-loaded.
  74. # &mov("eax",&DWP($ai*4,$a,"",0)) ;
  75. # &mov("edx",&DWP($bi*4,$a,"",0));
  76. if ($ai == $bi)
  77. { &mul("eax");}
  78. else
  79. { &mul("edx");}
  80. &add("eax","eax");
  81. ###
  82. &adc("edx","edx");
  83. ###
  84. &adc($c2,0);
  85. &add($c0,"eax");
  86. &adc($c1,"edx");
  87. &mov("eax",&DWP(($na)*4,$a,"",0)) if $pos == 0; # load next a
  88. &mov("eax",&DWP(($na)*4,$a,"",0)) if $pos == 1; # load next b
  89. &adc($c2,0);
  90. &mov(&DWP($i*4,$r,"",0),$c0) if $pos > 0; # save r[];
  91. &mov("edx",&DWP(($nb)*4,$a,"",0)) if ($pos <= 1) && ($na != $nb);
  92. ###
  93. }
  94. sub bn_mul_comba
  95. {
  96. local($name,$num)=@_;
  97. local($a,$b,$c0,$c1,$c2);
  98. local($i,$as,$ae,$bs,$be,$ai,$bi);
  99. local($tot,$end);
  100. &function_begin_B($name,"");
  101. $c0="ebx";
  102. $c1="ecx";
  103. $c2="ebp";
  104. $a="esi";
  105. $b="edi";
  106. $as=0;
  107. $ae=0;
  108. $bs=0;
  109. $be=0;
  110. $tot=$num+$num-1;
  111. &push("esi");
  112. &mov($a,&wparam(1));
  113. &push("edi");
  114. &mov($b,&wparam(2));
  115. &push("ebp");
  116. &push("ebx");
  117. &xor($c0,$c0);
  118. &mov("eax",&DWP(0,$a,"",0)); # load the first word
  119. &xor($c1,$c1);
  120. &mov("edx",&DWP(0,$b,"",0)); # load the first second
  121. for ($i=0; $i<$tot; $i++)
  122. {
  123. $ai=$as;
  124. $bi=$bs;
  125. $end=$be+1;
  126. &comment("################## Calculate word $i");
  127. for ($j=$bs; $j<$end; $j++)
  128. {
  129. &xor($c2,$c2) if ($j == $bs);
  130. if (($j+1) == $end)
  131. {
  132. $v=1;
  133. $v=2 if (($i+1) == $tot);
  134. }
  135. else
  136. { $v=0; }
  137. if (($j+1) != $end)
  138. {
  139. $na=($ai-1);
  140. $nb=($bi+1);
  141. }
  142. else
  143. {
  144. $na=$as+($i < ($num-1));
  145. $nb=$bs+($i >= ($num-1));
  146. }
  147. #printf STDERR "[$ai,$bi] -> [$na,$nb]\n";
  148. &mul_add_c($a,$ai,$b,$bi,$c0,$c1,$c2,$v,$i,$na,$nb);
  149. if ($v)
  150. {
  151. &comment("saved r[$i]");
  152. # &mov("eax",&wparam(0));
  153. # &mov(&DWP($i*4,"eax","",0),$c0);
  154. ($c0,$c1,$c2)=($c1,$c2,$c0);
  155. }
  156. $ai--;
  157. $bi++;
  158. }
  159. $as++ if ($i < ($num-1));
  160. $ae++ if ($i >= ($num-1));
  161. $bs++ if ($i >= ($num-1));
  162. $be++ if ($i < ($num-1));
  163. }
  164. &comment("save r[$i]");
  165. # &mov("eax",&wparam(0));
  166. &mov(&DWP($i*4,"eax","",0),$c0);
  167. &pop("ebx");
  168. &pop("ebp");
  169. &pop("edi");
  170. &pop("esi");
  171. &ret();
  172. &function_end_B($name);
  173. }
  174. sub bn_sqr_comba
  175. {
  176. local($name,$num)=@_;
  177. local($r,$a,$c0,$c1,$c2)=@_;
  178. local($i,$as,$ae,$bs,$be,$ai,$bi);
  179. local($b,$tot,$end,$half);
  180. &function_begin_B($name,"");
  181. $c0="ebx";
  182. $c1="ecx";
  183. $c2="ebp";
  184. $a="esi";
  185. $r="edi";
  186. &push("esi");
  187. &push("edi");
  188. &push("ebp");
  189. &push("ebx");
  190. &mov($r,&wparam(0));
  191. &mov($a,&wparam(1));
  192. &xor($c0,$c0);
  193. &xor($c1,$c1);
  194. &mov("eax",&DWP(0,$a,"",0)); # load the first word
  195. $as=0;
  196. $ae=0;
  197. $bs=0;
  198. $be=0;
  199. $tot=$num+$num-1;
  200. for ($i=0; $i<$tot; $i++)
  201. {
  202. $ai=$as;
  203. $bi=$bs;
  204. $end=$be+1;
  205. &comment("############### Calculate word $i");
  206. for ($j=$bs; $j<$end; $j++)
  207. {
  208. &xor($c2,$c2) if ($j == $bs);
  209. if (($ai-1) < ($bi+1))
  210. {
  211. $v=1;
  212. $v=2 if ($i+1) == $tot;
  213. }
  214. else
  215. { $v=0; }
  216. if (!$v)
  217. {
  218. $na=$ai-1;
  219. $nb=$bi+1;
  220. }
  221. else
  222. {
  223. $na=$as+($i < ($num-1));
  224. $nb=$bs+($i >= ($num-1));
  225. }
  226. if ($ai == $bi)
  227. {
  228. &sqr_add_c($r,$a,$ai,$bi,
  229. $c0,$c1,$c2,$v,$i,$na,$nb);
  230. }
  231. else
  232. {
  233. &sqr_add_c2($r,$a,$ai,$bi,
  234. $c0,$c1,$c2,$v,$i,$na,$nb);
  235. }
  236. if ($v)
  237. {
  238. &comment("saved r[$i]");
  239. #&mov(&DWP($i*4,$r,"",0),$c0);
  240. ($c0,$c1,$c2)=($c1,$c2,$c0);
  241. last;
  242. }
  243. $ai--;
  244. $bi++;
  245. }
  246. $as++ if ($i < ($num-1));
  247. $ae++ if ($i >= ($num-1));
  248. $bs++ if ($i >= ($num-1));
  249. $be++ if ($i < ($num-1));
  250. }
  251. &mov(&DWP($i*4,$r,"",0),$c0);
  252. &pop("ebx");
  253. &pop("ebp");
  254. &pop("edi");
  255. &pop("esi");
  256. &ret();
  257. &function_end_B($name);
  258. }