forked from microsoft/PQCrypto-SIDH
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fp_x64_asm.S
3037 lines (2728 loc) · 66.4 KB
/
fp_x64_asm.S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//*******************************************************************************************
// SIDH: an efficient supersingular isogeny cryptography library
//
// Abstract: field arithmetic in x64 assembly for P751 on Linux
//*******************************************************************************************
.intel_syntax noprefix
// Registers that are used for parameter passing:
#define reg_p1 rdi
#define reg_p2 rsi
#define reg_p3 rdx
.text
//***********************************************************************
// Field addition
// Operation: c [reg_p3] = a [reg_p1] + b [reg_p2]
//***********************************************************************
.global fpadd751_asm
fpadd751_asm:
push r12
push r13
push r14
push r15
mov r8, [reg_p1]
mov r9, [reg_p1+8]
mov r10, [reg_p1+16]
mov r11, [reg_p1+24]
mov r12, [reg_p1+32]
mov r13, [reg_p1+40]
mov r14, [reg_p1+48]
mov r15, [reg_p1+56]
mov rcx, [reg_p1+64]
add r8, [reg_p2]
adc r9, [reg_p2+8]
adc r10, [reg_p2+16]
adc r11, [reg_p2+24]
adc r12, [reg_p2+32]
adc r13, [reg_p2+40]
adc r14, [reg_p2+48]
adc r15, [reg_p2+56]
adc rcx, [reg_p2+64]
mov rax, [reg_p1+72]
adc rax, [reg_p2+72]
mov [reg_p3+72], rax
mov rax, [reg_p1+80]
adc rax, [reg_p2+80]
mov [reg_p3+80], rax
mov rax, [reg_p1+88]
adc rax, [reg_p2+88]
mov [reg_p3+88], rax
mov rax, [rip+p751x2]
sub r8, rax
mov rax, [rip+p751x2+8]
sbb r9, rax
sbb r10, rax
sbb r11, rax
sbb r12, rax
mov rax, [rip+p751x2+40]
sbb r13, rax
mov rax, [rip+p751x2+48]
sbb r14, rax
mov rax, [rip+p751x2+56]
sbb r15, rax
mov rax, [rip+p751x2+64]
sbb rcx, rax
mov [reg_p3], r8
mov [reg_p3+8], r9
mov [reg_p3+16], r10
mov [reg_p3+24], r11
mov [reg_p3+32], r12
mov [reg_p3+40], r13
mov [reg_p3+48], r14
mov [reg_p3+56], r15
mov [reg_p3+64], rcx
mov r8, [reg_p3+72]
mov r9, [reg_p3+80]
mov r10, [reg_p3+88]
mov rax, [rip+p751x2+72]
sbb r8, rax
mov rax, [rip+p751x2+80]
sbb r9, rax
mov rax, [rip+p751x2+88]
sbb r10, rax
mov [reg_p3+72], r8
mov [reg_p3+80], r9
mov [reg_p3+88], r10
mov rax, 0
sbb rax, 0
mov rsi, [rip+p751x2]
and rsi, rax
mov r8, [rip+p751x2+8]
and r8, rax
mov r9, [rip+p751x2+40]
and r9, rax
mov r10, [rip+p751x2+48]
and r10, rax
mov r11, [rip+p751x2+56]
and r11, rax
mov r12, [rip+p751x2+64]
and r12, rax
mov r13, [rip+p751x2+72]
and r13, rax
mov r14, [rip+p751x2+80]
and r14, rax
mov r15, [rip+p751x2+88]
and r15, rax
add rsi, [reg_p3]
mov [reg_p3], rsi
mov rax, [reg_p3+8]
adc rax, r8
mov [reg_p3+8], rax
mov rax, [reg_p3+16]
adc rax, r8
mov [reg_p3+16], rax
mov rax, [reg_p3+24]
adc rax, r8
mov [reg_p3+24], rax
mov rax, [reg_p3+32]
adc rax, r8
mov [reg_p3+32], rax
adc r9, [reg_p3+40]
adc r10, [reg_p3+48]
adc r11, [reg_p3+56]
adc r12, [reg_p3+64]
adc r13, [reg_p3+72]
adc r14, [reg_p3+80]
adc r15, [reg_p3+88]
mov [reg_p3+40], r9
mov [reg_p3+48], r10
mov [reg_p3+56], r11
mov [reg_p3+64], r12
mov [reg_p3+72], r13
mov [reg_p3+80], r14
mov [reg_p3+88], r15
pop r15
pop r14
pop r13
pop r12
ret
//***********************************************************************
// Field subtraction
// Operation: c [reg_p3] = a [reg_p1] - b [reg_p2]
//***********************************************************************
.global fpsub751_asm
fpsub751_asm:
push r12
push r13
push r14
push r15
mov r8, [reg_p1]
mov r9, [reg_p1+8]
mov r10, [reg_p1+16]
mov r11, [reg_p1+24]
mov r12, [reg_p1+32]
mov r13, [reg_p1+40]
mov r14, [reg_p1+48]
mov r15, [reg_p1+56]
mov rcx, [reg_p1+64]
sub r8, [reg_p2]
sbb r9, [reg_p2+8]
sbb r10, [reg_p2+16]
sbb r11, [reg_p2+24]
sbb r12, [reg_p2+32]
sbb r13, [reg_p2+40]
sbb r14, [reg_p2+48]
sbb r15, [reg_p2+56]
sbb rcx, [reg_p2+64]
mov [reg_p3], r8
mov [reg_p3+8], r9
mov [reg_p3+16], r10
mov [reg_p3+24], r11
mov [reg_p3+32], r12
mov [reg_p3+40], r13
mov [reg_p3+48], r14
mov [reg_p3+56], r15
mov [reg_p3+64], rcx
mov rax, [reg_p1+72]
sbb rax, [reg_p2+72]
mov [reg_p3+72], rax
mov rax, [reg_p1+80]
sbb rax, [reg_p2+80]
mov [reg_p3+80], rax
mov rax, [reg_p1+88]
sbb rax, [reg_p2+88]
mov [reg_p3+88], rax
mov rax, 0
sbb rax, 0
mov rsi, [rip+p751x2]
and rsi, rax
mov r8, [rip+p751x2+8]
and r8, rax
mov r9, [rip+p751x2+40]
and r9, rax
mov r10, [rip+p751x2+48]
and r10, rax
mov r11, [rip+p751x2+56]
and r11, rax
mov r12, [rip+p751x2+64]
and r12, rax
mov r13, [rip+p751x2+72]
and r13, rax
mov r14, [rip+p751x2+80]
and r14, rax
mov r15, [rip+p751x2+88]
and r15, rax
mov rax, [reg_p3]
add rax, rsi
mov [reg_p3], rax
mov rax, [reg_p3+8]
adc rax, r8
mov [reg_p3+8], rax
mov rax, [reg_p3+16]
adc rax, r8
mov [reg_p3+16], rax
mov rax, [reg_p3+24]
adc rax, r8
mov [reg_p3+24], rax
mov rax, [reg_p3+32]
adc rax, r8
mov [reg_p3+32], rax
adc r9, [reg_p3+40]
adc r10, [reg_p3+48]
adc r11, [reg_p3+56]
adc r12, [reg_p3+64]
adc r13, [reg_p3+72]
adc r14, [reg_p3+80]
adc r15, [reg_p3+88]
mov [reg_p3+40], r9
mov [reg_p3+48], r10
mov [reg_p3+56], r11
mov [reg_p3+64], r12
mov [reg_p3+72], r13
mov [reg_p3+80], r14
mov [reg_p3+88], r15
pop r15
pop r14
pop r13
pop r12
ret
#ifdef _MULX_
/////////////////////////////////////////////////////////////////////////// MACRO
// Schoolbook integer multiplication
// Inputs: memory pointers M0 and M1
// Outputs: memory pointer C
// Temps: stack space for two 64-bit values (case w/o _ADX_), regs T0:T7
///////////////////////////////////////////////////////////////////////////
#ifdef _ADX_
.macro MUL384_SCHOOL M0, M1, C, S, T0, T1, T2, T3, T4, T5, T6, T7
mov rdx, \M0
mulx \T0, \T1, \M1
mulx \T2, \T3, 8\M1
mov \C, \T1 // C0_final
xor rax, rax
mulx \T4, \T5, 16\M1
adox \T0, \T3
adox \T2, \T5
mulx \T1, \T3, 24\M1
adox \T4, \T3
mulx \T5, \T6, 32\M1
adox \T1, \T6
mulx \T3, \T7, 40\M1
adox \T5, \T7
adox \T3, rax
mov rdx, 8\M0
mulx \T6, \T7, \M1
xor rax, rax
adcx \T0, \T7
mov 8\C, \T0 // C1_final
adcx \T2, \T6
mulx \T6, \T7, 8\M1
adox \T2, \T7
adcx \T4, \T6
mulx \T0, \T6, 16\M1
adox \T4, \T6
adcx \T0, \T1
mulx \T1, \T7, 24\M1
adcx \T1, \T5
mulx \T5, \T6, 32\M1
adcx \T3, \T5
mulx \T5, rdx, 40\M1
adcx \T5, rax
adox \T0, \T7
adox \T1, \T6
adox \T3, rdx
adox \T5, rax
mov rdx, 16\M0
mulx \T6, \T7, \M1
xor rax, rax
adcx \T2, \T7
mov 16\C, \T2 // C2_final
adcx \T4, \T6
mulx \T6, \T7, 8\M1
adox \T4, \T7
adcx \T0, \T6
mulx \T2, \T6, 16\M1
adox \T0, \T6
adcx \T1, \T2
mulx \T2, \T7, 24\M1
adcx \T3, \T2
mulx \T2, \T6, 32\M1
adcx \T5, \T2
mulx \T2, rdx, 40\M1
adcx \T2, rax
adox \T1, \T7
adox \T3, \T6
adox \T5, rdx
adox \T2, rax
mov rdx, 24\M0
mulx \T6, \T7, \M1
xor rax, rax
adcx \T4, \T7
mov 24\C, \T4 // C3_final
adcx \T0, \T6
mulx \T6, \T7, 8\M1
adox \T0, \T7
adcx \T1, \T6
mulx \T4, \T6, 16\M1
adox \T1, \T6
adcx \T3, \T4
mulx \T4, \T7, 24\M1
adcx \T5, \T4
mulx \T4, \T6, 32\M1
adcx \T2, \T4
mulx \T4, rdx, 40\M1
adcx \T4, rax
adox \T3, \T7
adox \T5, \T6
adox \T2, rdx
adox \T4, rax
mov rdx, 32\M0
mulx \T6, \T7, \M1
xor rax, rax
adcx \T0, \T7
mov 32\C, \T0 // C4_final
adcx \T1, \T6
mulx \T6, \T7, 8\M1
adox \T1, \T7
adcx \T3, \T6
mulx \T0, \T6, 16\M1
adox \T3, \T6
adcx \T5, \T0
mulx \T0, \T7, 24\M1
adcx \T2, \T0
mulx \T0, \T6, 32\M1
adcx \T4, \T0
mulx \T0, rdx, 40\M1
adcx \T0, rax
adox \T5, \T7
adox \T2, \T6
adox \T4, rdx
adox \T0, rax
mov rdx, 40\M0
mulx \T6, \T7, \M1
xor rax, rax
adcx \T1, \T7
mov 40\C, \T1 // C5_final
adcx \T3, \T6
mulx \T6, \T7, 8\M1
adox \T3, \T7
adcx \T5, \T6
mulx \T1, \T6, 16\M1
adox \T5, \T6
adcx \T2, \T1
mulx \T1, \T7, 24\M1
adcx \T4, \T1
mulx \T1, \T6, 32\M1
adcx \T0, \T1
mulx \T1, rdx, 40\M1
adcx \T1, rax
adox \T2, \T7
adox \T4, \T6
adox \T0, rdx
adox \T1, rax
mov 48\C, \T3
mov 56\C, \T5
mov 64\C, \T2
mov 72\C, \T4
mov 80\C, \T0
mov 88\C, \T1
.endm
#else
.macro MUL384_SCHOOL M0, M1, C, S, T0, T1, T2, T3, T4, T5, T6, T7
mov rdx, \M0
mulx \T0, \T1, \M1
mulx \T2, \T3, 8\M1
mov \C, \T1 // C0_final
xor rax, rax
mulx \T4, \T5, 16\M1
add \T0, \T3
adc \T2, \T5
mulx \T1, \T3, 24\M1
adc \T4, \T3
mulx \T5, \T6, 32\M1
adc \T1, \T6
mulx \T3, \T7, 40\M1
adc \T5, \T7
adc \T3, rax
mov rdx, 8\M0
mulx \T6, \T7, \M1
add \T0, \T7
mov 8\C, \T0 // C1_final
adc \T2, \T6
mulx \T6, \T7, 8\M1
mov \S, \T7 // store T7
adc \T4, \T6
mulx \T0, \T6, 16\M1
mov 8\S, \T6 // store T6
adc \T0, \T1
mulx \T1, \T7, 24\M1
adc \T1, \T5
mulx \T5, \T6, 32\M1
adc \T3, \T5
mulx \T5, rdx, 40\M1
adc \T5, rax
xor rax, rax
add \T2, \S
adc \T4, 8\S
adc \T0, \T7
adc \T1, \T6
adc \T3, rdx
adc \T5, rax
mov rdx, 16\M0
mulx \T6, \T7, \M1
add \T2, \T7
mov 16\C, \T2 // C2_final
adc \T4, \T6
mulx \T6, \T7, 8\M1
mov \S, \T7 // store T7
adc \T0, \T6
mulx \T2, \T6, 16\M1
mov 8\S, \T6 // store T6
adc \T1, \T2
mulx \T2, \T7, 24\M1
adc \T3, \T2
mulx \T2, \T6, 32\M1
adc \T5, \T2
mulx \T2, rdx, 40\M1
adc \T2, rax
xor rax, rax
add \T4, \S
adc \T0, 8\S
adc \T1, \T7
adc \T3, \T6
adc \T5, rdx
adc \T2, rax
mov rdx, 24\M0
mulx \T6, \T7, \M1
add \T4, \T7
mov 24\C, \T4 // C3_final
adc \T0, \T6
mulx \T6, \T7, 8\M1
mov \S, \T7 // store T7
adc \T1, \T6
mulx \T4, \T6, 16\M1
mov 8\S, \T6 // store T6
adc \T3, \T4
mulx \T4, \T7, 24\M1
adc \T5, \T4
mulx \T4, \T6, 32\M1
adc \T2, \T4
mulx \T4, rdx, 40\M1
adc \T4, rax
xor rax, rax
add \T0, \S
adc \T1, 8\S
adc \T3, \T7
adc \T5, \T6
adc \T2, rdx
adc \T4, rax
mov rdx, 32\M0
mulx \T6, \T7, \M1
add \T0, \T7
mov 32\C, \T0 // C4_final
adc \T1, \T6
mulx \T6, \T7, 8\M1
mov \S, \T7 // store T7
adc \T3, \T6
mulx \T0, \T6, 16\M1
mov 8\S, \T6 // store T6
adc \T5, \T0
mulx \T0, \T7, 24\M1
adc \T2, \T0
mulx \T0, \T6, 32\M1
adc \T4, \T0
mulx \T0, rdx, 40\M1
adc \T0, rax
xor rax, rax
add \T1, \S
adc \T3, 8\S
adc \T5, \T7
adc \T2, \T6
adc \T4, rdx
adc \T0, rax
mov rdx, 40\M0
mulx \T6, \T7, \M1
add \T1, \T7
mov 40\C, \T1 // C5_final
adc \T3, \T6
mulx \T6, \T7, 8\M1
mov \S, \T7 // store T7
adc \T5, \T6
mulx \T1, \T6, 16\M1
mov 8\S, \T6 // store T6
adc \T2, \T1
mulx \T1, \T7, 24\M1
adc \T4, \T1
mulx \T1, \T6, 32\M1
adc \T0, \T1
mulx \T1, rdx, 40\M1
adc \T1, rax
add \T3, \S
adc \T5, 8\S
adc \T2, \T7
adc \T4, \T6
adc \T0, rdx
adc \T1, 0
mov 48\C, \T3
mov 56\C, \T5
mov 64\C, \T2
mov 72\C, \T4
mov 80\C, \T0
mov 88\C, \T1
.endm
#endif
//*****************************************************************************
// 751-bit multiplication using Karatsuba (one level), schoolbook (two levels)
//*****************************************************************************
.global mul751_asm
mul751_asm:
push r12
push r13
push r14
push r15
mov rcx, reg_p3
// [rsp] <- AH + AL, rax <- mask
xor rax, rax
mov r8, [reg_p1]
mov r9, [reg_p1+8]
mov r10, [reg_p1+16]
mov r11, [reg_p1+24]
mov r12, [reg_p1+32]
mov r13, [reg_p1+40]
push rbx
push rbp
sub rsp, 152
add r8, [reg_p1+48]
adc r9, [reg_p1+56]
adc r10, [reg_p1+64]
adc r11, [reg_p1+72]
adc r12, [reg_p1+80]
adc r13, [reg_p1+88]
sbb rax, 0
mov [rsp], r8
mov [rsp+8], r9
mov [rsp+16], r10
mov [rsp+24], r11
mov [rsp+32], r12
mov [rsp+40], r13
// [rsp+48] <- BH + BL, rdx <- mask
xor rdx, rdx
mov r8, [reg_p2]
mov r9, [reg_p2+8]
mov rbx, [reg_p2+16]
mov rbp, [reg_p2+24]
mov r14, [reg_p2+32]
mov r15, [reg_p2+40]
add r8, [reg_p2+48]
adc r9, [reg_p2+56]
adc rbx, [reg_p2+64]
adc rbp, [reg_p2+72]
adc r14, [reg_p2+80]
adc r15, [reg_p2+88]
sbb rdx, 0
mov [rsp+48], r8
mov [rsp+56], r9
mov [rsp+64], rbx
mov [rsp+72], rbp
mov [rsp+80], r14
mov [rsp+88], r15
// [rcx] <- masked (BH + BL)
and r8, rax
and r9, rax
and rbx, rax
and rbp, rax
and r14, rax
and r15, rax
mov [rcx], r8
mov [rcx+8], r9
mov [rcx+16], rbx /////
mov [rcx+24], rbp /////
// r8-r13 <- masked (AH + AL)
mov r8, [rsp]
mov r9, [rsp+8]
and r8, rdx
and r9, rdx
and r10, rdx
and r11, rdx
and r12, rdx
and r13, rdx
// [rsp+96] <- masked (AH + AL) + masked (AH + AL)
mov rax, [rcx]
mov rdx, [rcx+8]
add r8, rax
adc r9, rdx
adc r10, rbx
adc r11, rbp
adc r12, r14
adc r13, r15
mov [rsp+96], r8
mov [rsp+104], r9
mov [rsp+112], r10
mov [rsp+120], r11
// [rcx] <- AL x BL
MUL384_SCHOOL [reg_p1], [reg_p2], [rcx], [rsp+128], r8, r9, r10, r11, rbx, rbp, r14, r15 // Result C0-C5
// [rcx+96] <- (AH+AL) x (BH+BL), low part
MUL384_SCHOOL [rsp], [rsp+48], [rcx+96], [rsp+128], r8, r9, r10, r11, rbx, rbp, r14, r15
// [rsp] <- AH x BH
MUL384_SCHOOL [reg_p1+48], [reg_p2+48], [rsp], [rsp+128], r8, r9, r10, r11, rbx, rbp, r14, r15
// r8-r13 <- (AH+AL) x (BH+BL), final step
mov r8, [rsp+96]
mov r9, [rsp+104]
mov r10, [rsp+112]
mov r11, [rsp+120]
mov rax, [rcx+144]
add r8, rax
mov rax, [rcx+152]
adc r9, rax
mov rax, [rcx+160]
adc r10, rax
mov rax, [rcx+168]
adc r11, rax
mov rax, [rcx+176]
adc r12, rax
mov rax, [rcx+184]
adc r13, rax
// rdi,rdx,rbx,rbp,r14,r15,r8-r13 <- (AH+AL) x (BH+BL) - ALxBL
mov rdi, [rcx+96]
sub rdi, [rcx]
mov rdx, [rcx+104]
sbb rdx, [rcx+8]
mov rbx, [rcx+112]
sbb rbx, [rcx+16]
mov rbp, [rcx+120]
sbb rbp, [rcx+24]
mov r14, [rcx+128]
sbb r14, [rcx+32]
mov r15, [rcx+136]
sbb r15, [rcx+40]
sbb r8, [rcx+48]
sbb r9, [rcx+56]
sbb r10, [rcx+64]
sbb r11, [rcx+72]
sbb r12, [rcx+80]
sbb r13, [rcx+88]
// rdi,rdx,rbx,rbp,r14,r15,r8-r13 <- (AH+AL) x (BH+BL) - ALxBL - AHxBH
sub rdi, [rsp]
sbb rdx, [rsp+8]
sbb rbx, [rsp+16]
sbb rbp, [rsp+24]
sbb r14, [rsp+32]
sbb r15, [rsp+40]
sbb r8, [rsp+48]
sbb r9, [rsp+56]
sbb r10, [rsp+64]
sbb r11, [rsp+72]
sbb r12, [rsp+80]
sbb r13, [rsp+88]
mov rax, [rcx+48]
add rax, rdi
mov [rcx+48], rax // Result C6-C11
mov rax, [rcx+56]
adc rax, rdx
mov [rcx+56], rax
mov rax, [rcx+64]
adc rax, rbx
mov [rcx+64], rax
mov rax, [rcx+72]
adc rax, rbp
mov [rcx+72], rax
mov rax, [rcx+80]
adc rax, r14
mov [rcx+80], rax
mov rax, [rcx+88]
adc rax, r15
mov [rcx+88], rax
mov rax, [rsp]
adc r8, rax
mov [rcx+96], r8 // Result C8-C15
mov rax, [rsp+8]
adc r9, rax
mov [rcx+104], r9
mov rax, [rsp+16]
adc r10, rax
mov [rcx+112], r10
mov rax, [rsp+24]
adc r11, rax
mov [rcx+120], r11
mov rax, [rsp+32]
adc r12, rax
mov [rcx+128], r12
mov rax, [rsp+40]
adc r13, rax
mov [rcx+136], r13
mov r8, [rsp+48]
mov r9, [rsp+56]
mov r10, [rsp+64]
mov r11, [rsp+72]
mov r12, [rsp+80]
mov r13, [rsp+88]
adc r8, 0
adc r9, 0
adc r10, 0
adc r11, 0
adc r12, 0
adc r13, 0
add rsp, 152
mov [rcx+144], r8
mov [rcx+152], r9
mov [rcx+160], r10
mov [rcx+168], r11
mov [rcx+176], r12
mov [rcx+184], r13
pop rbp
pop rbx
pop r15
pop r14
pop r13
pop r12
ret
#else
//***********************************************************************
// Integer multiplication
// Based on Karatsuba method
// Operation: c [reg_p3] = a [reg_p1] * b [reg_p2]
// NOTE: a=c or b=c are not allowed
//***********************************************************************
.global mul751_asm
mul751_asm:
push r12
push r13
push r14
mov rcx, reg_p3
// rcx[0-5] <- AH+AL
xor rax, rax
mov r8, [reg_p1+48]
mov r9, [reg_p1+56]
mov r10, [reg_p1+64]
mov r11, [reg_p1+72]
mov r12, [reg_p1+80]
mov r13, [reg_p1+88]
add r8, [reg_p1]
adc r9, [reg_p1+8]
adc r10, [reg_p1+16]
adc r11, [reg_p1+24]
adc r12, [reg_p1+32]
adc r13, [reg_p1+40]
push r15
mov [rcx], r8
mov [rcx+8], r9
mov [rcx+16], r10
mov [rcx+24], r11
mov [rcx+32], r12
mov [rcx+40], r13
sbb rax, 0
sub rsp, 96 // Allocating space in stack
// rcx[6-11] <- BH+BL
xor rdx, rdx
mov r8, [reg_p2+48]
mov r9, [reg_p2+56]
mov r10, [reg_p2+64]
mov r11, [reg_p2+72]
mov r12, [reg_p2+80]
mov r13, [reg_p2+88]
add r8, [reg_p2]
adc r9, [reg_p2+8]
adc r10, [reg_p2+16]
adc r11, [reg_p2+24]
adc r12, [reg_p2+32]
adc r13, [reg_p2+40]
mov [rcx+48], r8
mov [rcx+56], r9
mov [rcx+64], r10
mov [rcx+72], r11
mov [rcx+80], r12
mov [rcx+88], r13
sbb rdx, 0
mov [rsp+80], rax
mov [rsp+88], rdx
// (rsp[0-8],r10,r8,r9) <- (AH+AL)*(BH+BL)
mov r11, [rcx]
mov rax, r8
mul r11
mov [rsp], rax // c0
mov r14, rdx
xor r15, r15
mov rax, r9
mul r11
xor r9, r9
add r14, rax
adc r9, rdx
mov r12, [rcx+8]
mov rax, r8
mul r12
add r14, rax
mov [rsp+8], r14 // c1
adc r9, rdx
adc r15, 0
xor r8, r8
mov rax, r10
mul r11
add r9, rax
mov r13, [rcx+48]
adc r15, rdx
adc r8, 0
mov rax, [rcx+16]
mul r13
add r9, rax
adc r15, rdx
mov rax, [rcx+56]
adc r8, 0
mul r12
add r9, rax
mov [rsp+16], r9 // c2
adc r15, rdx
adc r8, 0
xor r9, r9
mov rax, [rcx+72]
mul r11
add r15, rax
adc r8, rdx
adc r9, 0
mov rax, [rcx+24]
mul r13
add r15, rax
adc r8, rdx
adc r9, 0
mov rax, r10
mul r12
add r15, rax
adc r8, rdx
adc r9, 0
mov r14, [rcx+16]
mov rax, [rcx+56]
mul r14
add r15, rax
mov [rsp+24], r15 // c3
adc r8, rdx
adc r9, 0
xor r10, r10
mov rax, [rcx+80]
mul r11
add r8, rax
adc r9, rdx
adc r10, 0
mov rax, [rcx+64]
mul r14
add r8, rax
adc r9, rdx
adc r10, 0
mov r15, [rcx+48]
mov rax, [rcx+32]
mul r15
add r8, rax
adc r9, rdx
adc r10, 0
mov rax, [rcx+72]
mul r12
add r8, rax
adc r9, rdx
adc r10, 0
mov r13, [rcx+24]
mov rax, [rcx+56]
mul r13
add r8, rax
mov [rsp+32], r8 // c4
adc r9, rdx
adc r10, 0
xor r8, r8
mov rax, [rcx+88]
mul r11
add r9, rax
adc r10, rdx
adc r8, 0
mov rax, [rcx+64]
mul r13
add r9, rax
adc r10, rdx
adc r8, 0
mov rax, [rcx+72]
mul r14
add r9, rax
adc r10, rdx
adc r8, 0
mov rax, [rcx+40]
mul r15
add r9, rax
adc r10, rdx
adc r8, 0
mov rax, [rcx+80]
mul r12
add r9, rax
adc r10, rdx
adc r8, 0
mov r15, [rcx+32]
mov rax, [rcx+56]
mul r15
add r9, rax
mov [rsp+40], r9 // c5
adc r10, rdx
adc r8, 0
xor r9, r9
mov rax, [rcx+64]
mul r15
add r10, rax
adc r8, rdx
adc r9, 0
mov rax, [rcx+88]
mul r12