bn-586.pl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  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 and open STDOUT,">$output";
  12. &asm_init($ARGV[0]);
  13. $sse2=0;
  14. for (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); }
  15. &external_label("OPENSSL_ia32cap_P") if ($sse2);
  16. &bn_mul_add_words("bn_mul_add_words");
  17. &bn_mul_words("bn_mul_words");
  18. &bn_sqr_words("bn_sqr_words");
  19. &bn_div_words("bn_div_words");
  20. &bn_add_words("bn_add_words");
  21. &bn_sub_words("bn_sub_words");
  22. &bn_sub_part_words("bn_sub_part_words");
  23. &asm_finish();
  24. close STDOUT or die "error closing STDOUT";
  25. sub bn_mul_add_words
  26. {
  27. local($name)=@_;
  28. &function_begin_B($name,$sse2?"EXTRN\t_OPENSSL_ia32cap_P:DWORD":"");
  29. $r="eax";
  30. $a="edx";
  31. $c="ecx";
  32. if ($sse2) {
  33. &picmeup("eax","OPENSSL_ia32cap_P");
  34. &bt(&DWP(0,"eax"),26);
  35. &jnc(&label("maw_non_sse2"));
  36. &mov($r,&wparam(0));
  37. &mov($a,&wparam(1));
  38. &mov($c,&wparam(2));
  39. &movd("mm0",&wparam(3)); # mm0 = w
  40. &pxor("mm1","mm1"); # mm1 = carry_in
  41. &jmp(&label("maw_sse2_entry"));
  42. &set_label("maw_sse2_unrolled",16);
  43. &movd("mm3",&DWP(0,$r,"",0)); # mm3 = r[0]
  44. &paddq("mm1","mm3"); # mm1 = carry_in + r[0]
  45. &movd("mm2",&DWP(0,$a,"",0)); # mm2 = a[0]
  46. &pmuludq("mm2","mm0"); # mm2 = w*a[0]
  47. &movd("mm4",&DWP(4,$a,"",0)); # mm4 = a[1]
  48. &pmuludq("mm4","mm0"); # mm4 = w*a[1]
  49. &movd("mm6",&DWP(8,$a,"",0)); # mm6 = a[2]
  50. &pmuludq("mm6","mm0"); # mm6 = w*a[2]
  51. &movd("mm7",&DWP(12,$a,"",0)); # mm7 = a[3]
  52. &pmuludq("mm7","mm0"); # mm7 = w*a[3]
  53. &paddq("mm1","mm2"); # mm1 = carry_in + r[0] + w*a[0]
  54. &movd("mm3",&DWP(4,$r,"",0)); # mm3 = r[1]
  55. &paddq("mm3","mm4"); # mm3 = r[1] + w*a[1]
  56. &movd("mm5",&DWP(8,$r,"",0)); # mm5 = r[2]
  57. &paddq("mm5","mm6"); # mm5 = r[2] + w*a[2]
  58. &movd("mm4",&DWP(12,$r,"",0)); # mm4 = r[3]
  59. &paddq("mm7","mm4"); # mm7 = r[3] + w*a[3]
  60. &movd(&DWP(0,$r,"",0),"mm1");
  61. &movd("mm2",&DWP(16,$a,"",0)); # mm2 = a[4]
  62. &pmuludq("mm2","mm0"); # mm2 = w*a[4]
  63. &psrlq("mm1",32); # mm1 = carry0
  64. &movd("mm4",&DWP(20,$a,"",0)); # mm4 = a[5]
  65. &pmuludq("mm4","mm0"); # mm4 = w*a[5]
  66. &paddq("mm1","mm3"); # mm1 = carry0 + r[1] + w*a[1]
  67. &movd("mm6",&DWP(24,$a,"",0)); # mm6 = a[6]
  68. &pmuludq("mm6","mm0"); # mm6 = w*a[6]
  69. &movd(&DWP(4,$r,"",0),"mm1");
  70. &psrlq("mm1",32); # mm1 = carry1
  71. &movd("mm3",&DWP(28,$a,"",0)); # mm3 = a[7]
  72. &add($a,32);
  73. &pmuludq("mm3","mm0"); # mm3 = w*a[7]
  74. &paddq("mm1","mm5"); # mm1 = carry1 + r[2] + w*a[2]
  75. &movd("mm5",&DWP(16,$r,"",0)); # mm5 = r[4]
  76. &paddq("mm2","mm5"); # mm2 = r[4] + w*a[4]
  77. &movd(&DWP(8,$r,"",0),"mm1");
  78. &psrlq("mm1",32); # mm1 = carry2
  79. &paddq("mm1","mm7"); # mm1 = carry2 + r[3] + w*a[3]
  80. &movd("mm5",&DWP(20,$r,"",0)); # mm5 = r[5]
  81. &paddq("mm4","mm5"); # mm4 = r[5] + w*a[5]
  82. &movd(&DWP(12,$r,"",0),"mm1");
  83. &psrlq("mm1",32); # mm1 = carry3
  84. &paddq("mm1","mm2"); # mm1 = carry3 + r[4] + w*a[4]
  85. &movd("mm5",&DWP(24,$r,"",0)); # mm5 = r[6]
  86. &paddq("mm6","mm5"); # mm6 = r[6] + w*a[6]
  87. &movd(&DWP(16,$r,"",0),"mm1");
  88. &psrlq("mm1",32); # mm1 = carry4
  89. &paddq("mm1","mm4"); # mm1 = carry4 + r[5] + w*a[5]
  90. &movd("mm5",&DWP(28,$r,"",0)); # mm5 = r[7]
  91. &paddq("mm3","mm5"); # mm3 = r[7] + w*a[7]
  92. &movd(&DWP(20,$r,"",0),"mm1");
  93. &psrlq("mm1",32); # mm1 = carry5
  94. &paddq("mm1","mm6"); # mm1 = carry5 + r[6] + w*a[6]
  95. &movd(&DWP(24,$r,"",0),"mm1");
  96. &psrlq("mm1",32); # mm1 = carry6
  97. &paddq("mm1","mm3"); # mm1 = carry6 + r[7] + w*a[7]
  98. &movd(&DWP(28,$r,"",0),"mm1");
  99. &lea($r,&DWP(32,$r));
  100. &psrlq("mm1",32); # mm1 = carry_out
  101. &sub($c,8);
  102. &jz(&label("maw_sse2_exit"));
  103. &set_label("maw_sse2_entry");
  104. &test($c,0xfffffff8);
  105. &jnz(&label("maw_sse2_unrolled"));
  106. &set_label("maw_sse2_loop",4);
  107. &movd("mm2",&DWP(0,$a)); # mm2 = a[i]
  108. &movd("mm3",&DWP(0,$r)); # mm3 = r[i]
  109. &pmuludq("mm2","mm0"); # a[i] *= w
  110. &lea($a,&DWP(4,$a));
  111. &paddq("mm1","mm3"); # carry += r[i]
  112. &paddq("mm1","mm2"); # carry += a[i]*w
  113. &movd(&DWP(0,$r),"mm1"); # r[i] = carry_low
  114. &sub($c,1);
  115. &psrlq("mm1",32); # carry = carry_high
  116. &lea($r,&DWP(4,$r));
  117. &jnz(&label("maw_sse2_loop"));
  118. &set_label("maw_sse2_exit");
  119. &movd("eax","mm1"); # c = carry_out
  120. &emms();
  121. &ret();
  122. &set_label("maw_non_sse2",16);
  123. }
  124. # function_begin prologue
  125. &push("ebp");
  126. &push("ebx");
  127. &push("esi");
  128. &push("edi");
  129. &comment("");
  130. $Low="eax";
  131. $High="edx";
  132. $a="ebx";
  133. $w="ebp";
  134. $r="edi";
  135. $c="esi";
  136. &xor($c,$c); # clear carry
  137. &mov($r,&wparam(0)); #
  138. &mov("ecx",&wparam(2)); #
  139. &mov($a,&wparam(1)); #
  140. &and("ecx",0xfffffff8); # num / 8
  141. &mov($w,&wparam(3)); #
  142. &push("ecx"); # Up the stack for a tmp variable
  143. &jz(&label("maw_finish"));
  144. &set_label("maw_loop",16);
  145. for ($i=0; $i<32; $i+=4)
  146. {
  147. &comment("Round $i");
  148. &mov("eax",&DWP($i,$a)); # *a
  149. &mul($w); # *a * w
  150. &add("eax",$c); # L(t)+= c
  151. &adc("edx",0); # H(t)+=carry
  152. &add("eax",&DWP($i,$r)); # L(t)+= *r
  153. &adc("edx",0); # H(t)+=carry
  154. &mov(&DWP($i,$r),"eax"); # *r= L(t);
  155. &mov($c,"edx"); # c= H(t);
  156. }
  157. &comment("");
  158. &sub("ecx",8);
  159. &lea($a,&DWP(32,$a));
  160. &lea($r,&DWP(32,$r));
  161. &jnz(&label("maw_loop"));
  162. &set_label("maw_finish",0);
  163. &mov("ecx",&wparam(2)); # get num
  164. &and("ecx",7);
  165. &jnz(&label("maw_finish2")); # helps branch prediction
  166. &jmp(&label("maw_end"));
  167. &set_label("maw_finish2",1);
  168. for ($i=0; $i<7; $i++)
  169. {
  170. &comment("Tail Round $i");
  171. &mov("eax",&DWP($i*4,$a)); # *a
  172. &mul($w); # *a * w
  173. &add("eax",$c); # L(t)+=c
  174. &adc("edx",0); # H(t)+=carry
  175. &add("eax",&DWP($i*4,$r)); # L(t)+= *r
  176. &adc("edx",0); # H(t)+=carry
  177. &dec("ecx") if ($i != 7-1);
  178. &mov(&DWP($i*4,$r),"eax"); # *r= L(t);
  179. &mov($c,"edx"); # c= H(t);
  180. &jz(&label("maw_end")) if ($i != 7-1);
  181. }
  182. &set_label("maw_end",0);
  183. &mov("eax",$c);
  184. &pop("ecx"); # clear variable from
  185. &function_end($name);
  186. }
  187. sub bn_mul_words
  188. {
  189. local($name)=@_;
  190. &function_begin_B($name,$sse2?"EXTRN\t_OPENSSL_ia32cap_P:DWORD":"");
  191. $r="eax";
  192. $a="edx";
  193. $c="ecx";
  194. if ($sse2) {
  195. &picmeup("eax","OPENSSL_ia32cap_P");
  196. &bt(&DWP(0,"eax"),26);
  197. &jnc(&label("mw_non_sse2"));
  198. &mov($r,&wparam(0));
  199. &mov($a,&wparam(1));
  200. &mov($c,&wparam(2));
  201. &movd("mm0",&wparam(3)); # mm0 = w
  202. &pxor("mm1","mm1"); # mm1 = carry = 0
  203. &set_label("mw_sse2_loop",16);
  204. &movd("mm2",&DWP(0,$a)); # mm2 = a[i]
  205. &pmuludq("mm2","mm0"); # a[i] *= w
  206. &lea($a,&DWP(4,$a));
  207. &paddq("mm1","mm2"); # carry += a[i]*w
  208. &movd(&DWP(0,$r),"mm1"); # r[i] = carry_low
  209. &sub($c,1);
  210. &psrlq("mm1",32); # carry = carry_high
  211. &lea($r,&DWP(4,$r));
  212. &jnz(&label("mw_sse2_loop"));
  213. &movd("eax","mm1"); # return carry
  214. &emms();
  215. &ret();
  216. &set_label("mw_non_sse2",16);
  217. }
  218. # function_begin prologue
  219. &push("ebp");
  220. &push("ebx");
  221. &push("esi");
  222. &push("edi");
  223. &comment("");
  224. $Low="eax";
  225. $High="edx";
  226. $a="ebx";
  227. $w="ecx";
  228. $r="edi";
  229. $c="esi";
  230. $num="ebp";
  231. &xor($c,$c); # clear carry
  232. &mov($r,&wparam(0)); #
  233. &mov($a,&wparam(1)); #
  234. &mov($num,&wparam(2)); #
  235. &mov($w,&wparam(3)); #
  236. &and($num,0xfffffff8); # num / 8
  237. &jz(&label("mw_finish"));
  238. &set_label("mw_loop",0);
  239. for ($i=0; $i<32; $i+=4)
  240. {
  241. &comment("Round $i");
  242. &mov("eax",&DWP($i,$a,"",0)); # *a
  243. &mul($w); # *a * w
  244. &add("eax",$c); # L(t)+=c
  245. # XXX
  246. &adc("edx",0); # H(t)+=carry
  247. &mov(&DWP($i,$r,"",0),"eax"); # *r= L(t);
  248. &mov($c,"edx"); # c= H(t);
  249. }
  250. &comment("");
  251. &add($a,32);
  252. &add($r,32);
  253. &sub($num,8);
  254. &jz(&label("mw_finish"));
  255. &jmp(&label("mw_loop"));
  256. &set_label("mw_finish",0);
  257. &mov($num,&wparam(2)); # get num
  258. &and($num,7);
  259. &jnz(&label("mw_finish2"));
  260. &jmp(&label("mw_end"));
  261. &set_label("mw_finish2",1);
  262. for ($i=0; $i<7; $i++)
  263. {
  264. &comment("Tail Round $i");
  265. &mov("eax",&DWP($i*4,$a,"",0));# *a
  266. &mul($w); # *a * w
  267. &add("eax",$c); # L(t)+=c
  268. # XXX
  269. &adc("edx",0); # H(t)+=carry
  270. &mov(&DWP($i*4,$r,"",0),"eax");# *r= L(t);
  271. &mov($c,"edx"); # c= H(t);
  272. &dec($num) if ($i != 7-1);
  273. &jz(&label("mw_end")) if ($i != 7-1);
  274. }
  275. &set_label("mw_end",0);
  276. &mov("eax",$c);
  277. &function_end($name);
  278. }
  279. sub bn_sqr_words
  280. {
  281. local($name)=@_;
  282. &function_begin_B($name,$sse2?"EXTRN\t_OPENSSL_ia32cap_P:DWORD":"");
  283. $r="eax";
  284. $a="edx";
  285. $c="ecx";
  286. if ($sse2) {
  287. &picmeup("eax","OPENSSL_ia32cap_P");
  288. &bt(&DWP(0,"eax"),26);
  289. &jnc(&label("sqr_non_sse2"));
  290. &mov($r,&wparam(0));
  291. &mov($a,&wparam(1));
  292. &mov($c,&wparam(2));
  293. &set_label("sqr_sse2_loop",16);
  294. &movd("mm0",&DWP(0,$a)); # mm0 = a[i]
  295. &pmuludq("mm0","mm0"); # a[i] *= a[i]
  296. &lea($a,&DWP(4,$a)); # a++
  297. &movq(&QWP(0,$r),"mm0"); # r[i] = a[i]*a[i]
  298. &sub($c,1);
  299. &lea($r,&DWP(8,$r)); # r += 2
  300. &jnz(&label("sqr_sse2_loop"));
  301. &emms();
  302. &ret();
  303. &set_label("sqr_non_sse2",16);
  304. }
  305. # function_begin prologue
  306. &push("ebp");
  307. &push("ebx");
  308. &push("esi");
  309. &push("edi");
  310. &comment("");
  311. $r="esi";
  312. $a="edi";
  313. $num="ebx";
  314. &mov($r,&wparam(0)); #
  315. &mov($a,&wparam(1)); #
  316. &mov($num,&wparam(2)); #
  317. &and($num,0xfffffff8); # num / 8
  318. &jz(&label("sw_finish"));
  319. &set_label("sw_loop",0);
  320. for ($i=0; $i<32; $i+=4)
  321. {
  322. &comment("Round $i");
  323. &mov("eax",&DWP($i,$a,"",0)); # *a
  324. # XXX
  325. &mul("eax"); # *a * *a
  326. &mov(&DWP($i*2,$r,"",0),"eax"); #
  327. &mov(&DWP($i*2+4,$r,"",0),"edx");#
  328. }
  329. &comment("");
  330. &add($a,32);
  331. &add($r,64);
  332. &sub($num,8);
  333. &jnz(&label("sw_loop"));
  334. &set_label("sw_finish",0);
  335. &mov($num,&wparam(2)); # get num
  336. &and($num,7);
  337. &jz(&label("sw_end"));
  338. for ($i=0; $i<7; $i++)
  339. {
  340. &comment("Tail Round $i");
  341. &mov("eax",&DWP($i*4,$a,"",0)); # *a
  342. # XXX
  343. &mul("eax"); # *a * *a
  344. &mov(&DWP($i*8,$r,"",0),"eax"); #
  345. &dec($num) if ($i != 7-1);
  346. &mov(&DWP($i*8+4,$r,"",0),"edx");
  347. &jz(&label("sw_end")) if ($i != 7-1);
  348. }
  349. &set_label("sw_end",0);
  350. &function_end($name);
  351. }
  352. sub bn_div_words
  353. {
  354. local($name)=@_;
  355. &function_begin_B($name,"");
  356. &mov("edx",&wparam(0)); #
  357. &mov("eax",&wparam(1)); #
  358. &mov("ecx",&wparam(2)); #
  359. &div("ecx");
  360. &ret();
  361. &function_end_B($name);
  362. }
  363. sub bn_add_words
  364. {
  365. local($name)=@_;
  366. &function_begin($name,"");
  367. &comment("");
  368. $a="esi";
  369. $b="edi";
  370. $c="eax";
  371. $r="ebx";
  372. $tmp1="ecx";
  373. $tmp2="edx";
  374. $num="ebp";
  375. &mov($r,&wparam(0)); # get r
  376. &mov($a,&wparam(1)); # get a
  377. &mov($b,&wparam(2)); # get b
  378. &mov($num,&wparam(3)); # get num
  379. &xor($c,$c); # clear carry
  380. &and($num,0xfffffff8); # num / 8
  381. &jz(&label("aw_finish"));
  382. &set_label("aw_loop",0);
  383. for ($i=0; $i<8; $i++)
  384. {
  385. &comment("Round $i");
  386. &mov($tmp1,&DWP($i*4,$a,"",0)); # *a
  387. &mov($tmp2,&DWP($i*4,$b,"",0)); # *b
  388. &add($tmp1,$c);
  389. &mov($c,0);
  390. &adc($c,$c);
  391. &add($tmp1,$tmp2);
  392. &adc($c,0);
  393. &mov(&DWP($i*4,$r,"",0),$tmp1); # *r
  394. }
  395. &comment("");
  396. &add($a,32);
  397. &add($b,32);
  398. &add($r,32);
  399. &sub($num,8);
  400. &jnz(&label("aw_loop"));
  401. &set_label("aw_finish",0);
  402. &mov($num,&wparam(3)); # get num
  403. &and($num,7);
  404. &jz(&label("aw_end"));
  405. for ($i=0; $i<7; $i++)
  406. {
  407. &comment("Tail Round $i");
  408. &mov($tmp1,&DWP($i*4,$a,"",0)); # *a
  409. &mov($tmp2,&DWP($i*4,$b,"",0));# *b
  410. &add($tmp1,$c);
  411. &mov($c,0);
  412. &adc($c,$c);
  413. &add($tmp1,$tmp2);
  414. &adc($c,0);
  415. &dec($num) if ($i != 6);
  416. &mov(&DWP($i*4,$r,"",0),$tmp1); # *r
  417. &jz(&label("aw_end")) if ($i != 6);
  418. }
  419. &set_label("aw_end",0);
  420. # &mov("eax",$c); # $c is "eax"
  421. &function_end($name);
  422. }
  423. sub bn_sub_words
  424. {
  425. local($name)=@_;
  426. &function_begin($name,"");
  427. &comment("");
  428. $a="esi";
  429. $b="edi";
  430. $c="eax";
  431. $r="ebx";
  432. $tmp1="ecx";
  433. $tmp2="edx";
  434. $num="ebp";
  435. &mov($r,&wparam(0)); # get r
  436. &mov($a,&wparam(1)); # get a
  437. &mov($b,&wparam(2)); # get b
  438. &mov($num,&wparam(3)); # get num
  439. &xor($c,$c); # clear carry
  440. &and($num,0xfffffff8); # num / 8
  441. &jz(&label("aw_finish"));
  442. &set_label("aw_loop",0);
  443. for ($i=0; $i<8; $i++)
  444. {
  445. &comment("Round $i");
  446. &mov($tmp1,&DWP($i*4,$a,"",0)); # *a
  447. &mov($tmp2,&DWP($i*4,$b,"",0)); # *b
  448. &sub($tmp1,$c);
  449. &mov($c,0);
  450. &adc($c,$c);
  451. &sub($tmp1,$tmp2);
  452. &adc($c,0);
  453. &mov(&DWP($i*4,$r,"",0),$tmp1); # *r
  454. }
  455. &comment("");
  456. &add($a,32);
  457. &add($b,32);
  458. &add($r,32);
  459. &sub($num,8);
  460. &jnz(&label("aw_loop"));
  461. &set_label("aw_finish",0);
  462. &mov($num,&wparam(3)); # get num
  463. &and($num,7);
  464. &jz(&label("aw_end"));
  465. for ($i=0; $i<7; $i++)
  466. {
  467. &comment("Tail Round $i");
  468. &mov($tmp1,&DWP($i*4,$a,"",0)); # *a
  469. &mov($tmp2,&DWP($i*4,$b,"",0));# *b
  470. &sub($tmp1,$c);
  471. &mov($c,0);
  472. &adc($c,$c);
  473. &sub($tmp1,$tmp2);
  474. &adc($c,0);
  475. &dec($num) if ($i != 6);
  476. &mov(&DWP($i*4,$r,"",0),$tmp1); # *r
  477. &jz(&label("aw_end")) if ($i != 6);
  478. }
  479. &set_label("aw_end",0);
  480. # &mov("eax",$c); # $c is "eax"
  481. &function_end($name);
  482. }
  483. sub bn_sub_part_words
  484. {
  485. local($name)=@_;
  486. &function_begin($name,"");
  487. &comment("");
  488. $a="esi";
  489. $b="edi";
  490. $c="eax";
  491. $r="ebx";
  492. $tmp1="ecx";
  493. $tmp2="edx";
  494. $num="ebp";
  495. &mov($r,&wparam(0)); # get r
  496. &mov($a,&wparam(1)); # get a
  497. &mov($b,&wparam(2)); # get b
  498. &mov($num,&wparam(3)); # get num
  499. &xor($c,$c); # clear carry
  500. &and($num,0xfffffff8); # num / 8
  501. &jz(&label("aw_finish"));
  502. &set_label("aw_loop",0);
  503. for ($i=0; $i<8; $i++)
  504. {
  505. &comment("Round $i");
  506. &mov($tmp1,&DWP($i*4,$a,"",0)); # *a
  507. &mov($tmp2,&DWP($i*4,$b,"",0)); # *b
  508. &sub($tmp1,$c);
  509. &mov($c,0);
  510. &adc($c,$c);
  511. &sub($tmp1,$tmp2);
  512. &adc($c,0);
  513. &mov(&DWP($i*4,$r,"",0),$tmp1); # *r
  514. }
  515. &comment("");
  516. &add($a,32);
  517. &add($b,32);
  518. &add($r,32);
  519. &sub($num,8);
  520. &jnz(&label("aw_loop"));
  521. &set_label("aw_finish",0);
  522. &mov($num,&wparam(3)); # get num
  523. &and($num,7);
  524. &jz(&label("aw_end"));
  525. for ($i=0; $i<7; $i++)
  526. {
  527. &comment("Tail Round $i");
  528. &mov($tmp1,&DWP(0,$a,"",0)); # *a
  529. &mov($tmp2,&DWP(0,$b,"",0));# *b
  530. &sub($tmp1,$c);
  531. &mov($c,0);
  532. &adc($c,$c);
  533. &sub($tmp1,$tmp2);
  534. &adc($c,0);
  535. &mov(&DWP(0,$r,"",0),$tmp1); # *r
  536. &add($a, 4);
  537. &add($b, 4);
  538. &add($r, 4);
  539. &dec($num) if ($i != 6);
  540. &jz(&label("aw_end")) if ($i != 6);
  541. }
  542. &set_label("aw_end",0);
  543. &cmp(&wparam(4),0);
  544. &je(&label("pw_end"));
  545. &mov($num,&wparam(4)); # get dl
  546. &cmp($num,0);
  547. &je(&label("pw_end"));
  548. &jge(&label("pw_pos"));
  549. &comment("pw_neg");
  550. &mov($tmp2,0);
  551. &sub($tmp2,$num);
  552. &mov($num,$tmp2);
  553. &and($num,0xfffffff8); # num / 8
  554. &jz(&label("pw_neg_finish"));
  555. &set_label("pw_neg_loop",0);
  556. for ($i=0; $i<8; $i++)
  557. {
  558. &comment("dl<0 Round $i");
  559. &mov($tmp1,0);
  560. &mov($tmp2,&DWP($i*4,$b,"",0)); # *b
  561. &sub($tmp1,$c);
  562. &mov($c,0);
  563. &adc($c,$c);
  564. &sub($tmp1,$tmp2);
  565. &adc($c,0);
  566. &mov(&DWP($i*4,$r,"",0),$tmp1); # *r
  567. }
  568. &comment("");
  569. &add($b,32);
  570. &add($r,32);
  571. &sub($num,8);
  572. &jnz(&label("pw_neg_loop"));
  573. &set_label("pw_neg_finish",0);
  574. &mov($tmp2,&wparam(4)); # get dl
  575. &mov($num,0);
  576. &sub($num,$tmp2);
  577. &and($num,7);
  578. &jz(&label("pw_end"));
  579. for ($i=0; $i<7; $i++)
  580. {
  581. &comment("dl<0 Tail Round $i");
  582. &mov($tmp1,0);
  583. &mov($tmp2,&DWP($i*4,$b,"",0));# *b
  584. &sub($tmp1,$c);
  585. &mov($c,0);
  586. &adc($c,$c);
  587. &sub($tmp1,$tmp2);
  588. &adc($c,0);
  589. &dec($num) if ($i != 6);
  590. &mov(&DWP($i*4,$r,"",0),$tmp1); # *r
  591. &jz(&label("pw_end")) if ($i != 6);
  592. }
  593. &jmp(&label("pw_end"));
  594. &set_label("pw_pos",0);
  595. &and($num,0xfffffff8); # num / 8
  596. &jz(&label("pw_pos_finish"));
  597. &set_label("pw_pos_loop",0);
  598. for ($i=0; $i<8; $i++)
  599. {
  600. &comment("dl>0 Round $i");
  601. &mov($tmp1,&DWP($i*4,$a,"",0)); # *a
  602. &sub($tmp1,$c);
  603. &mov(&DWP($i*4,$r,"",0),$tmp1); # *r
  604. &jnc(&label("pw_nc".$i));
  605. }
  606. &comment("");
  607. &add($a,32);
  608. &add($r,32);
  609. &sub($num,8);
  610. &jnz(&label("pw_pos_loop"));
  611. &set_label("pw_pos_finish",0);
  612. &mov($num,&wparam(4)); # get dl
  613. &and($num,7);
  614. &jz(&label("pw_end"));
  615. for ($i=0; $i<7; $i++)
  616. {
  617. &comment("dl>0 Tail Round $i");
  618. &mov($tmp1,&DWP($i*4,$a,"",0)); # *a
  619. &sub($tmp1,$c);
  620. &mov(&DWP($i*4,$r,"",0),$tmp1); # *r
  621. &jnc(&label("pw_tail_nc".$i));
  622. &dec($num) if ($i != 6);
  623. &jz(&label("pw_end")) if ($i != 6);
  624. }
  625. &mov($c,1);
  626. &jmp(&label("pw_end"));
  627. &set_label("pw_nc_loop",0);
  628. for ($i=0; $i<8; $i++)
  629. {
  630. &mov($tmp1,&DWP($i*4,$a,"",0)); # *a
  631. &mov(&DWP($i*4,$r,"",0),$tmp1); # *r
  632. &set_label("pw_nc".$i,0);
  633. }
  634. &comment("");
  635. &add($a,32);
  636. &add($r,32);
  637. &sub($num,8);
  638. &jnz(&label("pw_nc_loop"));
  639. &mov($num,&wparam(4)); # get dl
  640. &and($num,7);
  641. &jz(&label("pw_nc_end"));
  642. for ($i=0; $i<7; $i++)
  643. {
  644. &mov($tmp1,&DWP($i*4,$a,"",0)); # *a
  645. &mov(&DWP($i*4,$r,"",0),$tmp1); # *r
  646. &set_label("pw_tail_nc".$i,0);
  647. &dec($num) if ($i != 6);
  648. &jz(&label("pw_nc_end")) if ($i != 6);
  649. }
  650. &set_label("pw_nc_end",0);
  651. &mov($c,0);
  652. &set_label("pw_end",0);
  653. # &mov("eax",$c); # $c is "eax"
  654. &function_end($name);
  655. }