comba.pl 5.3 KB

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