2 # Copyright 2004-2019 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (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
9 # Implemented as a Perl wrapper as we want to support several different
10 # architectures with single file. We pick up the target based on the
11 # file name we are asked to generate.
13 # It should be noted though that this perl code is nothing like
14 # <openssl>/crypto/perlasm/x86*. In this case perl is used pretty much
15 # as pre-processor to cover for platform differences in name decoration,
16 # linker tables, 32-/64-bit instruction sets...
18 # As you might know there're several PowerPC ABI in use. Most notably
19 # Linux and AIX use different 32-bit ABIs. Good news are that these ABIs
20 # are similar enough to implement leaf(!) functions, which would be ABI
21 # neutral. And that's what you find here: ABI neutral leaf functions.
22 # In case you wonder what that is...
26 # MEASUREMENTS WITH cc ON a 200 MhZ PowerPC 604e.
28 # The following is the performance of 32-bit compiler
31 # OpenSSL 0.9.6c 21 dec 2001
32 # built on: Tue Jun 11 11:06:51 EDT 2002
33 # options:bn(64,32) ...
34 #compiler: cc -DTHREADS -DAIX -DB_ENDIAN -DBN_LLONG -O3
35 # sign verify sign/s verify/s
36 #rsa 512 bits 0.0098s 0.0009s 102.0 1170.6
37 #rsa 1024 bits 0.0507s 0.0026s 19.7 387.5
38 #rsa 2048 bits 0.3036s 0.0085s 3.3 117.1
39 #rsa 4096 bits 2.0040s 0.0299s 0.5 33.4
40 #dsa 512 bits 0.0087s 0.0106s 114.3 94.5
41 #dsa 1024 bits 0.0256s 0.0313s 39.0 32.0
43 # Same benchmark with this assembler code:
45 #rsa 512 bits 0.0056s 0.0005s 178.6 2049.2
46 #rsa 1024 bits 0.0283s 0.0015s 35.3 674.1
47 #rsa 2048 bits 0.1744s 0.0050s 5.7 201.2
48 #rsa 4096 bits 1.1644s 0.0179s 0.9 55.7
49 #dsa 512 bits 0.0052s 0.0062s 191.6 162.0
50 #dsa 1024 bits 0.0149s 0.0180s 67.0 55.5
52 # Number of operations increases by at almost 75%
54 # Here are performance numbers for 64-bit compiler
57 # OpenSSL 0.9.6g [engine] 9 Aug 2002
58 # built on: Fri Apr 18 16:59:20 EDT 2003
59 # options:bn(64,64) ...
60 # compiler: cc -DTHREADS -D_REENTRANT -q64 -DB_ENDIAN -O3
61 # sign verify sign/s verify/s
62 #rsa 512 bits 0.0028s 0.0003s 357.1 3844.4
63 #rsa 1024 bits 0.0148s 0.0008s 67.5 1239.7
64 #rsa 2048 bits 0.0963s 0.0028s 10.4 353.0
65 #rsa 4096 bits 0.6538s 0.0102s 1.5 98.1
66 #dsa 512 bits 0.0026s 0.0032s 382.5 313.7
67 #dsa 1024 bits 0.0081s 0.0099s 122.8 100.6
69 # Same benchmark with this assembler code:
71 #rsa 512 bits 0.0020s 0.0002s 510.4 6273.7
72 #rsa 1024 bits 0.0088s 0.0005s 114.1 2128.3
73 #rsa 2048 bits 0.0540s 0.0016s 18.5 622.5
74 #rsa 4096 bits 0.3700s 0.0058s 2.7 171.0
75 #dsa 512 bits 0.0016s 0.0020s 610.7 507.1
76 #dsa 1024 bits 0.0047s 0.0058s 212.5 173.2
78 # Again, performance increases by at about 75%
80 # Mac OS X, Apple G5 1.8GHz (Note this is 32 bit code)
81 # OpenSSL 0.9.7c 30 Sep 2003
85 #rsa 512 bits 0.0011s 0.0001s 906.1 11012.5
86 #rsa 1024 bits 0.0060s 0.0003s 166.6 3363.1
87 #rsa 2048 bits 0.0370s 0.0010s 27.1 982.4
88 #rsa 4096 bits 0.2426s 0.0036s 4.1 280.4
89 #dsa 512 bits 0.0010s 0.0012s 1038.1 841.5
90 #dsa 1024 bits 0.0030s 0.0037s 329.6 269.7
91 #dsa 2048 bits 0.0101s 0.0127s 98.9 78.6
93 # Same benchmark with this assembler code:
95 #rsa 512 bits 0.0007s 0.0001s 1416.2 16645.9
96 #rsa 1024 bits 0.0036s 0.0002s 274.4 5380.6
97 #rsa 2048 bits 0.0222s 0.0006s 45.1 1589.5
98 #rsa 4096 bits 0.1469s 0.0022s 6.8 449.6
99 #dsa 512 bits 0.0006s 0.0007s 1664.2 1376.2
100 #dsa 1024 bits 0.0018s 0.0023s 545.0 442.2
101 #dsa 2048 bits 0.0061s 0.0075s 163.5 132.8
103 # Performance increase of ~60%
104 # Based on submission from Suresh N. Chari of IBM
108 if ($flavour =~ /32/) {
114 $LDU= "lwzu"; # load and update
116 $STU= "stwu"; # store and update
117 $UMULL= "mullw"; # unsigned multiply low
118 $UMULH= "mulhwu"; # unsigned multiply high
119 $UDIV= "divwu"; # unsigned divide
120 $UCMPI= "cmplwi"; # unsigned compare with immediate
121 $UCMP= "cmplw"; # unsigned compare
122 $CNTLZ= "cntlzw"; # count leading zeros
123 $SHL= "slw"; # shift left
124 $SHR= "srw"; # unsigned shift right
125 $SHRI= "srwi"; # unsigned shift right by immediate
126 $SHLI= "slwi"; # shift left by immediate
127 $CLRU= "clrlwi"; # clear upper bits
128 $INSR= "insrwi"; # insert right
129 $ROTL= "rotlwi"; # rotate left by immediate
130 $TR= "tw"; # conditional trap
131 } elsif ($flavour =~ /64/) {
136 # same as above, but 64-bit mnemonics...
138 $LDU= "ldu"; # load and update
140 $STU= "stdu"; # store and update
141 $UMULL= "mulld"; # unsigned multiply low
142 $UMULH= "mulhdu"; # unsigned multiply high
143 $UDIV= "divdu"; # unsigned divide
144 $UCMPI= "cmpldi"; # unsigned compare with immediate
145 $UCMP= "cmpld"; # unsigned compare
146 $CNTLZ= "cntlzd"; # count leading zeros
147 $SHL= "sld"; # shift left
148 $SHR= "srd"; # unsigned shift right
149 $SHRI= "srdi"; # unsigned shift right by immediate
150 $SHLI= "sldi"; # shift left by immediate
151 $CLRU= "clrldi"; # clear upper bits
152 $INSR= "insrdi"; # insert right
153 $ROTL= "rotldi"; # rotate left by immediate
154 $TR= "td"; # conditional trap
155 } else { die "nonsense $flavour"; }
157 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
158 ( $xlate="${dir}ppc-xlate.pl" and -f $xlate ) or
159 ( $xlate="${dir}../../perlasm/ppc-xlate.pl" and -f $xlate) or
160 die "can't locate ppc-xlate.pl";
162 open STDOUT,"| $^X $xlate $flavour ".shift || die "can't call $xlate: $!";
165 #--------------------------------------------------------------------
172 # Created by: Suresh Chari
173 # IBM Thomas J. Watson Research Library
177 # Description: Optimized assembly routines for OpenSSL crypto
178 # on the 32 bitPowerPC platform.
183 # 2. Fixed bn_add,bn_sub and bn_div_words, added comments,
184 # cleaned up code. Also made a single version which can
185 # be used for both the AIX and Linux compilers. See NOTE
187 # 12/05/03 Suresh Chari
188 # (with lots of help from) Andy Polyakov
190 # 1. Initial version 10/20/02 Suresh Chari
193 # The following file works for the xlc,cc
196 # NOTE: To get the file to link correctly with the gcc compiler
197 # you have to change the names of the routines and remove
198 # the first .(dot) character. This should automatically
199 # be done in the build process.
201 # Hand optimized assembly code for the following routines
214 # NOTE: It is possible to optimize this code more for
215 # specific PowerPC or Power architectures. On the Northstar
216 # architecture the optimizations in this file do
217 # NOT provide much improvement.
219 # If you have comments or suggestions to improve code send
220 # me a note at schari\@us.ibm.com
222 #--------------------------------------------------------------------------
224 # Defines to be used in the assembly code.
226 #.set r0,0 # we use it as storage for value of 0
227 #.set SP,1 # preserved
228 #.set RTOC,2 # preserved
229 #.set r3,3 # 1st argument/return value
230 #.set r4,4 # 2nd argument/volatile register
231 #.set r5,5 # 3rd argument/volatile register
239 #.set r13,13 # not used, nor any other "below" it...
241 # Declare function names to be global
242 # NOTE: For gcc these names MUST be changed to remove
243 # the first . i.e. for example change ".bn_sqr_comba4"
244 # to "bn_sqr_comba4". This should be automatically done
247 .globl .bn_sqr_comba4
248 .globl .bn_sqr_comba8
249 .globl .bn_mul_comba4
250 .globl .bn_mul_comba8
256 .globl .bn_mul_add_words
264 # NOTE: The following label name should be changed to
265 # "bn_sqr_comba4" i.e. remove the first dot
266 # for the gcc compiler. This should be automatically
273 # Optimized version of bn_sqr_comba4.
275 # void bn_sqr_comba4(BN_ULONG *r, BN_ULONG *a)
279 # Freely use registers r5,r6,r7,r8,r9,r10,r11 as follows:
281 # r5,r6 are the two BN_ULONGs being multiplied.
282 # r7,r8 are the results of the 32x32 giving 64 bit multiply.
283 # r9,r10, r11 are the equivalents of c1,c2, c3.
284 # Here's the assembly
287 xor r0,r0,r0 # set r0 = 0. Used in the addze
290 #sqr_add_c(a,0,c1,c2,c3)
293 $UMULH r10,r5,r5 #in first iteration. No need
294 #to add since c1=c2=c3=0.
295 # Note c3(r11) is NOT set to 0
298 $ST r9,`0*$BNSZ`(r3) # r[0]=c1;
299 # sqr_add_c2(a,1,0,c2,c3,c1);
304 addc r7,r7,r7 # compute (r7,r8)=2*(r7,r8)
306 addze r9,r0 # catch carry if any.
307 # r9= r0(=0) and carry
309 addc r10,r7,r10 # now add to temp result.
310 addze r11,r8 # r8 added to r11 which is 0
313 $ST r10,`1*$BNSZ`(r3) #r[1]=c2;
314 #sqr_add_c(a,1,c3,c1,c2)
320 #sqr_add_c2(a,2,0,c3,c1,c2)
332 $ST r11,`2*$BNSZ`(r3) #r[2]=c3
333 #sqr_add_c2(a,3,0,c1,c2,c3);
344 #sqr_add_c2(a,2,1,c1,c2,c3);
356 $ST r9,`3*$BNSZ`(r3) #r[3]=c1
357 #sqr_add_c(a,2,c2,c3,c1);
363 #sqr_add_c2(a,3,1,c2,c3,c1);
374 $ST r10,`4*$BNSZ`(r3) #r[4]=c2
375 #sqr_add_c2(a,3,2,c3,c1,c2);
386 $ST r11,`5*$BNSZ`(r3) #r[5] = c3
387 #sqr_add_c(a,3,c1,c2,c3);
393 $ST r9,`6*$BNSZ`(r3) #r[6]=c1
394 $ST r10,`7*$BNSZ`(r3) #r[7]=c2
397 .byte 0,12,0x14,0,0,0,2,0
399 .size .bn_sqr_comba4,.-.bn_sqr_comba4
402 # NOTE: The following label name should be changed to
403 # "bn_sqr_comba8" i.e. remove the first dot
404 # for the gcc compiler. This should be automatically
411 # This is an optimized version of the bn_sqr_comba8 routine.
412 # Tightly uses the adde instruction
415 # void bn_sqr_comba8(BN_ULONG *r, BN_ULONG *a)
419 # Freely use registers r5,r6,r7,r8,r9,r10,r11 as follows:
421 # r5,r6 are the two BN_ULONGs being multiplied.
422 # r7,r8 are the results of the 32x32 giving 64 bit multiply.
423 # r9,r10, r11 are the equivalents of c1,c2, c3.
425 # Possible optimization of loading all 8 longs of a into registers
426 # doesn't provide any speedup
429 xor r0,r0,r0 #set r0 = 0.Used in addze
432 #sqr_add_c(a,0,c1,c2,c3);
434 $UMULL r9,r5,r5 #1st iteration: no carries.
436 $ST r9,`0*$BNSZ`(r3) # r[0]=c1;
437 #sqr_add_c2(a,1,0,c2,c3,c1);
442 addc r10,r7,r10 #add the two register number
443 adde r11,r8,r0 # (r8,r7) to the three register
444 addze r9,r0 # number (r9,r11,r10).NOTE:r0=0
446 addc r10,r7,r10 #add the two register number
447 adde r11,r8,r11 # (r8,r7) to the three register
448 addze r9,r9 # number (r9,r11,r10).
450 $ST r10,`1*$BNSZ`(r3) # r[1]=c2
452 #sqr_add_c(a,1,c3,c1,c2);
458 #sqr_add_c2(a,2,0,c3,c1,c2);
471 $ST r11,`2*$BNSZ`(r3) #r[2]=c3
472 #sqr_add_c2(a,3,0,c1,c2,c3);
473 $LD r6,`3*$BNSZ`(r4) #r6 = a[3]. r5 is already a[0].
484 #sqr_add_c2(a,2,1,c1,c2,c3);
498 $ST r9,`3*$BNSZ`(r3) #r[3]=c1;
499 #sqr_add_c(a,2,c2,c3,c1);
506 #sqr_add_c2(a,3,1,c2,c3,c1);
518 #sqr_add_c2(a,4,0,c2,c3,c1);
531 $ST r10,`4*$BNSZ`(r3) #r[4]=c2;
532 #sqr_add_c2(a,5,0,c3,c1,c2);
544 #sqr_add_c2(a,4,1,c3,c1,c2);
557 #sqr_add_c2(a,3,2,c3,c1,c2);
570 $ST r11,`5*$BNSZ`(r3) #r[5]=c3;
571 #sqr_add_c(a,3,c1,c2,c3);
577 #sqr_add_c2(a,4,2,c1,c2,c3);
589 #sqr_add_c2(a,5,1,c1,c2,c3);
602 #sqr_add_c2(a,6,0,c1,c2,c3);
613 $ST r9,`6*$BNSZ`(r3) #r[6]=c1;
614 #sqr_add_c2(a,7,0,c2,c3,c1);
625 #sqr_add_c2(a,6,1,c2,c3,c1);
637 #sqr_add_c2(a,5,2,c2,c3,c1);
648 #sqr_add_c2(a,4,3,c2,c3,c1);
660 $ST r10,`7*$BNSZ`(r3) #r[7]=c2;
661 #sqr_add_c(a,4,c3,c1,c2);
667 #sqr_add_c2(a,5,3,c3,c1,c2);
677 #sqr_add_c2(a,6,2,c3,c1,c2);
689 #sqr_add_c2(a,7,1,c3,c1,c2);
700 $ST r11,`8*$BNSZ`(r3) #r[8]=c3;
701 #sqr_add_c2(a,7,2,c1,c2,c3);
712 #sqr_add_c2(a,6,3,c1,c2,c3);
723 #sqr_add_c2(a,5,4,c1,c2,c3);
734 $ST r9,`9*$BNSZ`(r3) #r[9]=c1;
735 #sqr_add_c(a,5,c2,c3,c1);
741 #sqr_add_c2(a,6,4,c2,c3,c1);
751 #sqr_add_c2(a,7,3,c2,c3,c1);
762 $ST r10,`10*$BNSZ`(r3) #r[10]=c2;
763 #sqr_add_c2(a,7,4,c3,c1,c2);
773 #sqr_add_c2(a,6,5,c3,c1,c2);
784 $ST r11,`11*$BNSZ`(r3) #r[11]=c3;
785 #sqr_add_c(a,6,c1,c2,c3);
791 #sqr_add_c2(a,7,5,c1,c2,c3)
801 $ST r9,`12*$BNSZ`(r3) #r[12]=c1;
803 #sqr_add_c2(a,7,6,c2,c3,c1)
813 $ST r10,`13*$BNSZ`(r3) #r[13]=c2;
814 #sqr_add_c(a,7,c3,c1,c2);
819 $ST r11,`14*$BNSZ`(r3) #r[14]=c3;
820 $ST r9, `15*$BNSZ`(r3) #r[15]=c1;
825 .byte 0,12,0x14,0,0,0,2,0
827 .size .bn_sqr_comba8,.-.bn_sqr_comba8
830 # NOTE: The following label name should be changed to
831 # "bn_mul_comba4" i.e. remove the first dot
832 # for the gcc compiler. This should be automatically
839 # This is an optimized version of the bn_mul_comba4 routine.
841 # void bn_mul_comba4(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
845 # r6, r7 are the 2 BN_ULONGs being multiplied.
846 # r8, r9 are the results of the 32x32 giving 64 multiply.
847 # r10, r11, r12 are the equivalents of c1, c2, and c3.
849 xor r0,r0,r0 #r0=0. Used in addze below.
850 #mul_add_c(a[0],b[0],c1,c2,c3);
855 $ST r10,`0*$BNSZ`(r3) #r[0]=c1
856 #mul_add_c(a[0],b[1],c2,c3,c1);
863 #mul_add_c(a[1],b[0],c2,c3,c1);
864 $LD r6, `1*$BNSZ`(r4)
865 $LD r7, `0*$BNSZ`(r5)
871 $ST r11,`1*$BNSZ`(r3) #r[1]=c2
872 #mul_add_c(a[2],b[0],c3,c1,c2);
879 #mul_add_c(a[1],b[1],c3,c1,c2);
887 #mul_add_c(a[0],b[2],c3,c1,c2);
895 $ST r12,`2*$BNSZ`(r3) #r[2]=c3
896 #mul_add_c(a[0],b[3],c1,c2,c3);
903 #mul_add_c(a[1],b[2],c1,c2,c3);
911 #mul_add_c(a[2],b[1],c1,c2,c3);
919 #mul_add_c(a[3],b[0],c1,c2,c3);
927 $ST r10,`3*$BNSZ`(r3) #r[3]=c1
928 #mul_add_c(a[3],b[1],c2,c3,c1);
935 #mul_add_c(a[2],b[2],c2,c3,c1);
943 #mul_add_c(a[1],b[3],c2,c3,c1);
951 $ST r11,`4*$BNSZ`(r3) #r[4]=c2
952 #mul_add_c(a[2],b[3],c3,c1,c2);
959 #mul_add_c(a[3],b[2],c3,c1,c2);
967 $ST r12,`5*$BNSZ`(r3) #r[5]=c3
968 #mul_add_c(a[3],b[3],c1,c2,c3);
975 $ST r10,`6*$BNSZ`(r3) #r[6]=c1
976 $ST r11,`7*$BNSZ`(r3) #r[7]=c2
979 .byte 0,12,0x14,0,0,0,3,0
981 .size .bn_mul_comba4,.-.bn_mul_comba4
984 # NOTE: The following label name should be changed to
985 # "bn_mul_comba8" i.e. remove the first dot
986 # for the gcc compiler. This should be automatically
993 # Optimized version of the bn_mul_comba8 routine.
995 # void bn_mul_comba8(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b)
999 # r6, r7 are the 2 BN_ULONGs being multiplied.
1000 # r8, r9 are the results of the 32x32 giving 64 multiply.
1001 # r10, r11, r12 are the equivalents of c1, c2, and c3.
1003 xor r0,r0,r0 #r0=0. Used in addze below.
1005 #mul_add_c(a[0],b[0],c1,c2,c3);
1006 $LD r6,`0*$BNSZ`(r4) #a[0]
1007 $LD r7,`0*$BNSZ`(r5) #b[0]
1010 $ST r10,`0*$BNSZ`(r3) #r[0]=c1;
1011 #mul_add_c(a[0],b[1],c2,c3,c1);
1012 $LD r7,`1*$BNSZ`(r5)
1016 addze r12,r9 # since we didn't set r12 to zero before.
1018 #mul_add_c(a[1],b[0],c2,c3,c1);
1019 $LD r6,`1*$BNSZ`(r4)
1020 $LD r7,`0*$BNSZ`(r5)
1026 $ST r11,`1*$BNSZ`(r3) #r[1]=c2;
1027 #mul_add_c(a[2],b[0],c3,c1,c2);
1028 $LD r6,`2*$BNSZ`(r4)
1034 #mul_add_c(a[1],b[1],c3,c1,c2);
1035 $LD r6,`1*$BNSZ`(r4)
1036 $LD r7,`1*$BNSZ`(r5)
1042 #mul_add_c(a[0],b[2],c3,c1,c2);
1043 $LD r6,`0*$BNSZ`(r4)
1044 $LD r7,`2*$BNSZ`(r5)
1050 $ST r12,`2*$BNSZ`(r3) #r[2]=c3;
1051 #mul_add_c(a[0],b[3],c1,c2,c3);
1052 $LD r7,`3*$BNSZ`(r5)
1058 #mul_add_c(a[1],b[2],c1,c2,c3);
1059 $LD r6,`1*$BNSZ`(r4)
1060 $LD r7,`2*$BNSZ`(r5)
1067 #mul_add_c(a[2],b[1],c1,c2,c3);
1068 $LD r6,`2*$BNSZ`(r4)
1069 $LD r7,`1*$BNSZ`(r5)
1075 #mul_add_c(a[3],b[0],c1,c2,c3);
1076 $LD r6,`3*$BNSZ`(r4)
1077 $LD r7,`0*$BNSZ`(r5)
1083 $ST r10,`3*$BNSZ`(r3) #r[3]=c1;
1084 #mul_add_c(a[4],b[0],c2,c3,c1);
1085 $LD r6,`4*$BNSZ`(r4)
1091 #mul_add_c(a[3],b[1],c2,c3,c1);
1092 $LD r6,`3*$BNSZ`(r4)
1093 $LD r7,`1*$BNSZ`(r5)
1099 #mul_add_c(a[2],b[2],c2,c3,c1);
1100 $LD r6,`2*$BNSZ`(r4)
1101 $LD r7,`2*$BNSZ`(r5)
1107 #mul_add_c(a[1],b[3],c2,c3,c1);
1108 $LD r6,`1*$BNSZ`(r4)
1109 $LD r7,`3*$BNSZ`(r5)
1115 #mul_add_c(a[0],b[4],c2,c3,c1);
1116 $LD r6,`0*$BNSZ`(r4)
1117 $LD r7,`4*$BNSZ`(r5)
1123 $ST r11,`4*$BNSZ`(r3) #r[4]=c2;
1124 #mul_add_c(a[0],b[5],c3,c1,c2);
1125 $LD r7,`5*$BNSZ`(r5)
1131 #mul_add_c(a[1],b[4],c3,c1,c2);
1132 $LD r6,`1*$BNSZ`(r4)
1133 $LD r7,`4*$BNSZ`(r5)
1139 #mul_add_c(a[2],b[3],c3,c1,c2);
1140 $LD r6,`2*$BNSZ`(r4)
1141 $LD r7,`3*$BNSZ`(r5)
1147 #mul_add_c(a[3],b[2],c3,c1,c2);
1148 $LD r6,`3*$BNSZ`(r4)
1149 $LD r7,`2*$BNSZ`(r5)
1155 #mul_add_c(a[4],b[1],c3,c1,c2);
1156 $LD r6,`4*$BNSZ`(r4)
1157 $LD r7,`1*$BNSZ`(r5)
1163 #mul_add_c(a[5],b[0],c3,c1,c2);
1164 $LD r6,`5*$BNSZ`(r4)
1165 $LD r7,`0*$BNSZ`(r5)
1171 $ST r12,`5*$BNSZ`(r3) #r[5]=c3;
1172 #mul_add_c(a[6],b[0],c1,c2,c3);
1173 $LD r6,`6*$BNSZ`(r4)
1179 #mul_add_c(a[5],b[1],c1,c2,c3);
1180 $LD r6,`5*$BNSZ`(r4)
1181 $LD r7,`1*$BNSZ`(r5)
1187 #mul_add_c(a[4],b[2],c1,c2,c3);
1188 $LD r6,`4*$BNSZ`(r4)
1189 $LD r7,`2*$BNSZ`(r5)
1195 #mul_add_c(a[3],b[3],c1,c2,c3);
1196 $LD r6,`3*$BNSZ`(r4)
1197 $LD r7,`3*$BNSZ`(r5)
1203 #mul_add_c(a[2],b[4],c1,c2,c3);
1204 $LD r6,`2*$BNSZ`(r4)
1205 $LD r7,`4*$BNSZ`(r5)
1211 #mul_add_c(a[1],b[5],c1,c2,c3);
1212 $LD r6,`1*$BNSZ`(r4)
1213 $LD r7,`5*$BNSZ`(r5)
1219 #mul_add_c(a[0],b[6],c1,c2,c3);
1220 $LD r6,`0*$BNSZ`(r4)
1221 $LD r7,`6*$BNSZ`(r5)
1227 $ST r10,`6*$BNSZ`(r3) #r[6]=c1;
1228 #mul_add_c(a[0],b[7],c2,c3,c1);
1229 $LD r7,`7*$BNSZ`(r5)
1235 #mul_add_c(a[1],b[6],c2,c3,c1);
1236 $LD r6,`1*$BNSZ`(r4)
1237 $LD r7,`6*$BNSZ`(r5)
1243 #mul_add_c(a[2],b[5],c2,c3,c1);
1244 $LD r6,`2*$BNSZ`(r4)
1245 $LD r7,`5*$BNSZ`(r5)
1251 #mul_add_c(a[3],b[4],c2,c3,c1);
1252 $LD r6,`3*$BNSZ`(r4)
1253 $LD r7,`4*$BNSZ`(r5)
1259 #mul_add_c(a[4],b[3],c2,c3,c1);
1260 $LD r6,`4*$BNSZ`(r4)
1261 $LD r7,`3*$BNSZ`(r5)
1267 #mul_add_c(a[5],b[2],c2,c3,c1);
1268 $LD r6,`5*$BNSZ`(r4)
1269 $LD r7,`2*$BNSZ`(r5)
1275 #mul_add_c(a[6],b[1],c2,c3,c1);
1276 $LD r6,`6*$BNSZ`(r4)
1277 $LD r7,`1*$BNSZ`(r5)
1283 #mul_add_c(a[7],b[0],c2,c3,c1);
1284 $LD r6,`7*$BNSZ`(r4)
1285 $LD r7,`0*$BNSZ`(r5)
1291 $ST r11,`7*$BNSZ`(r3) #r[7]=c2;
1292 #mul_add_c(a[7],b[1],c3,c1,c2);
1293 $LD r7,`1*$BNSZ`(r5)
1299 #mul_add_c(a[6],b[2],c3,c1,c2);
1300 $LD r6,`6*$BNSZ`(r4)
1301 $LD r7,`2*$BNSZ`(r5)
1307 #mul_add_c(a[5],b[3],c3,c1,c2);
1308 $LD r6,`5*$BNSZ`(r4)
1309 $LD r7,`3*$BNSZ`(r5)
1315 #mul_add_c(a[4],b[4],c3,c1,c2);
1316 $LD r6,`4*$BNSZ`(r4)
1317 $LD r7,`4*$BNSZ`(r5)
1323 #mul_add_c(a[3],b[5],c3,c1,c2);
1324 $LD r6,`3*$BNSZ`(r4)
1325 $LD r7,`5*$BNSZ`(r5)
1331 #mul_add_c(a[2],b[6],c3,c1,c2);
1332 $LD r6,`2*$BNSZ`(r4)
1333 $LD r7,`6*$BNSZ`(r5)
1339 #mul_add_c(a[1],b[7],c3,c1,c2);
1340 $LD r6,`1*$BNSZ`(r4)
1341 $LD r7,`7*$BNSZ`(r5)
1347 $ST r12,`8*$BNSZ`(r3) #r[8]=c3;
1348 #mul_add_c(a[2],b[7],c1,c2,c3);
1349 $LD r6,`2*$BNSZ`(r4)
1355 #mul_add_c(a[3],b[6],c1,c2,c3);
1356 $LD r6,`3*$BNSZ`(r4)
1357 $LD r7,`6*$BNSZ`(r5)
1363 #mul_add_c(a[4],b[5],c1,c2,c3);
1364 $LD r6,`4*$BNSZ`(r4)
1365 $LD r7,`5*$BNSZ`(r5)
1371 #mul_add_c(a[5],b[4],c1,c2,c3);
1372 $LD r6,`5*$BNSZ`(r4)
1373 $LD r7,`4*$BNSZ`(r5)
1379 #mul_add_c(a[6],b[3],c1,c2,c3);
1380 $LD r6,`6*$BNSZ`(r4)
1381 $LD r7,`3*$BNSZ`(r5)
1387 #mul_add_c(a[7],b[2],c1,c2,c3);
1388 $LD r6,`7*$BNSZ`(r4)
1389 $LD r7,`2*$BNSZ`(r5)
1395 $ST r10,`9*$BNSZ`(r3) #r[9]=c1;
1396 #mul_add_c(a[7],b[3],c2,c3,c1);
1397 $LD r7,`3*$BNSZ`(r5)
1403 #mul_add_c(a[6],b[4],c2,c3,c1);
1404 $LD r6,`6*$BNSZ`(r4)
1405 $LD r7,`4*$BNSZ`(r5)
1411 #mul_add_c(a[5],b[5],c2,c3,c1);
1412 $LD r6,`5*$BNSZ`(r4)
1413 $LD r7,`5*$BNSZ`(r5)
1419 #mul_add_c(a[4],b[6],c2,c3,c1);
1420 $LD r6,`4*$BNSZ`(r4)
1421 $LD r7,`6*$BNSZ`(r5)
1427 #mul_add_c(a[3],b[7],c2,c3,c1);
1428 $LD r6,`3*$BNSZ`(r4)
1429 $LD r7,`7*$BNSZ`(r5)
1435 $ST r11,`10*$BNSZ`(r3) #r[10]=c2;
1436 #mul_add_c(a[4],b[7],c3,c1,c2);
1437 $LD r6,`4*$BNSZ`(r4)
1443 #mul_add_c(a[5],b[6],c3,c1,c2);
1444 $LD r6,`5*$BNSZ`(r4)
1445 $LD r7,`6*$BNSZ`(r5)
1451 #mul_add_c(a[6],b[5],c3,c1,c2);
1452 $LD r6,`6*$BNSZ`(r4)
1453 $LD r7,`5*$BNSZ`(r5)
1459 #mul_add_c(a[7],b[4],c3,c1,c2);
1460 $LD r6,`7*$BNSZ`(r4)
1461 $LD r7,`4*$BNSZ`(r5)
1467 $ST r12,`11*$BNSZ`(r3) #r[11]=c3;
1468 #mul_add_c(a[7],b[5],c1,c2,c3);
1469 $LD r7,`5*$BNSZ`(r5)
1475 #mul_add_c(a[6],b[6],c1,c2,c3);
1476 $LD r6,`6*$BNSZ`(r4)
1477 $LD r7,`6*$BNSZ`(r5)
1483 #mul_add_c(a[5],b[7],c1,c2,c3);
1484 $LD r6,`5*$BNSZ`(r4)
1485 $LD r7,`7*$BNSZ`(r5)
1491 $ST r10,`12*$BNSZ`(r3) #r[12]=c1;
1492 #mul_add_c(a[6],b[7],c2,c3,c1);
1493 $LD r6,`6*$BNSZ`(r4)
1499 #mul_add_c(a[7],b[6],c2,c3,c1);
1500 $LD r6,`7*$BNSZ`(r4)
1501 $LD r7,`6*$BNSZ`(r5)
1507 $ST r11,`13*$BNSZ`(r3) #r[13]=c2;
1508 #mul_add_c(a[7],b[7],c3,c1,c2);
1509 $LD r7,`7*$BNSZ`(r5)
1514 $ST r12,`14*$BNSZ`(r3) #r[14]=c3;
1515 $ST r10,`15*$BNSZ`(r3) #r[15]=c1;
1518 .byte 0,12,0x14,0,0,0,3,0
1520 .size .bn_mul_comba8,.-.bn_mul_comba8
1523 # NOTE: The following label name should be changed to
1524 # "bn_sub_words" i.e. remove the first dot
1525 # for the gcc compiler. This should be automatically
1532 # Handcoded version of bn_sub_words
1534 #BN_ULONG bn_sub_words(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
1541 # Note: No loop unrolling done since this is not a performance
1544 xor r0,r0,r0 #set r0 = 0
1546 # check for r6 = 0 AND set carry bit.
1548 subfc. r7,r0,r6 # If r6 is 0 then result is 0.
1549 # if r6 > 0 then result !=0
1550 # In either case carry bit is set.
1551 beq Lppcasm_sub_adios
1556 Lppcasm_sub_mainloop:
1559 subfe r6,r8,r7 # r6 = r7+carry bit + onescomplement(r8)
1560 # if carry = 1 this is r7-r8. Else it
1561 # is r7-r8 -1 as we need.
1563 bdnz Lppcasm_sub_mainloop
1565 subfze r3,r0 # if carry bit is set then r3 = 0 else -1
1566 andi. r3,r3,1 # keep only last bit.
1569 .byte 0,12,0x14,0,0,0,4,0
1571 .size .bn_sub_words,.-.bn_sub_words
1574 # NOTE: The following label name should be changed to
1575 # "bn_add_words" i.e. remove the first dot
1576 # for the gcc compiler. This should be automatically
1583 # Handcoded version of bn_add_words
1585 #BN_ULONG bn_add_words(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
1592 # Note: No loop unrolling done since this is not a performance
1597 # check for r6 = 0. Is this needed?
1599 addic. r6,r6,0 #test r6 and clear carry bit.
1600 beq Lppcasm_add_adios
1605 Lppcasm_add_mainloop:
1610 bdnz Lppcasm_add_mainloop
1612 addze r3,r0 #return carry bit.
1615 .byte 0,12,0x14,0,0,0,4,0
1617 .size .bn_add_words,.-.bn_add_words
1620 # NOTE: The following label name should be changed to
1621 # "bn_div_words" i.e. remove the first dot
1622 # for the gcc compiler. This should be automatically
1629 # This is a cleaned up version of code generated by
1630 # the AIX compiler. The only optimization is to use
1631 # the PPC instruction to count leading zeros instead
1632 # of call to num_bits_word. Since this was compiled
1633 # only at level -O2 we can possibly squeeze it more?
1639 $UCMPI 0,r5,0 # compare r5 and 0
1640 bne Lppcasm_div1 # proceed if d!=0
1641 li r3,-1 # d=0 return -1
1646 $CNTLZ. r7,r5 #r7 = num leading 0s in d.
1647 beq Lppcasm_div2 #proceed if no leading zeros
1648 subf r8,r7,r8 #r8 = BN_num_bits_word(d)
1649 $SHR. r9,r3,r8 #are there any bits above r8'th?
1650 $TR 16,r9,r0 #if there're, signal to dump core...
1652 $UCMP 0,r3,r5 #h>=d?
1653 blt Lppcasm_div3 #goto Lppcasm_div3 if not
1654 subf r3,r5,r3 #h-=d ;
1655 Lppcasm_div3: #r7 = BN_BITS2-i. so r7=i
1656 cmpi 0,0,r7,0 # is (i == 0)?
1658 $SHL r3,r3,r7 # h = (h<< i)
1659 $SHR r8,r4,r8 # r8 = (l >> BN_BITS2 -i)
1660 $SHL r5,r5,r7 # d<<=i
1661 or r3,r3,r8 # h = (h<<i)|(l>>(BN_BITS2-i))
1662 $SHL r4,r4,r7 # l <<=i
1664 $SHRI r9,r5,`$BITS/2` # r9 = dh
1665 # dl will be computed when needed
1666 # as it saves registers.
1668 mtctr r6 #counter will be in count.
1669 Lppcasm_divouterloop:
1670 $SHRI r8,r3,`$BITS/2` #r8 = (h>>BN_BITS4)
1671 $SHRI r11,r4,`$BITS/2` #r11= (l&BN_MASK2h)>>BN_BITS4
1672 # compute here for innerloop.
1673 $UCMP 0,r8,r9 # is (h>>BN_BITS4)==dh
1674 bne Lppcasm_div5 # goto Lppcasm_div5 if not
1677 $CLRU r8,r8,`$BITS/2` #q = BN_MASK2l
1680 $UDIV r8,r3,r9 #q = h/dh
1682 $UMULL r12,r9,r8 #th = q*dh
1683 $CLRU r10,r5,`$BITS/2` #r10=dl
1684 $UMULL r6,r8,r10 #tl = q*dl
1686 Lppcasm_divinnerloop:
1687 subf r10,r12,r3 #t = h -th
1688 $SHRI r7,r10,`$BITS/2` #r7= (t &BN_MASK2H), sort of...
1689 addic. r7,r7,0 #test if r7 == 0. used below.
1690 # now want to compute
1691 # r7 = (t<<BN_BITS4)|((l&BN_MASK2h)>>BN_BITS4)
1692 # the following 2 instructions do that
1693 $SHLI r7,r10,`$BITS/2` # r7 = (t<<BN_BITS4)
1694 or r7,r7,r11 # r7|=((l&BN_MASK2h)>>BN_BITS4)
1695 $UCMP cr1,r6,r7 # compare (tl <= r7)
1696 bne Lppcasm_divinnerexit
1697 ble cr1,Lppcasm_divinnerexit
1699 subf r12,r9,r12 #th -=dh
1700 $CLRU r10,r5,`$BITS/2` #r10=dl. t is no longer needed in loop.
1701 subf r6,r10,r6 #tl -=dl
1702 b Lppcasm_divinnerloop
1703 Lppcasm_divinnerexit:
1704 $SHRI r10,r6,`$BITS/2` #t=(tl>>BN_BITS4)
1705 $SHLI r11,r6,`$BITS/2` #tl=(tl<<BN_BITS4)&BN_MASK2h;
1706 $UCMP cr1,r4,r11 # compare l and tl
1707 add r12,r12,r10 # th+=t
1708 bge cr1,Lppcasm_div7 # if (l>=tl) goto Lppcasm_div7
1709 addi r12,r12,1 # th++
1711 subf r11,r11,r4 #r11=l-tl
1712 $UCMP cr1,r3,r12 #compare h and th
1713 bge cr1,Lppcasm_div8 #if (h>=th) goto Lppcasm_div8
1717 subf r12,r12,r3 #r12 = h-th
1718 $SHLI r4,r11,`$BITS/2` #l=(l&BN_MASK2l)<<BN_BITS4
1720 # h = ((h<<BN_BITS4)|(l>>BN_BITS4))&BN_MASK2
1721 # the following 2 instructions will do this.
1722 $INSR r11,r12,`$BITS/2`,`$BITS/2` # r11 is the value we want rotated $BITS/2.
1723 $ROTL r3,r11,`$BITS/2` # rotate by $BITS/2 and store in r3
1724 bdz Lppcasm_div9 #if (count==0) break ;
1725 $SHLI r0,r8,`$BITS/2` #ret =q<<BN_BITS4
1726 b Lppcasm_divouterloop
1731 .byte 0,12,0x14,0,0,0,3,0
1733 .size .bn_div_words,.-.bn_div_words
1736 # NOTE: The following label name should be changed to
1737 # "bn_sqr_words" i.e. remove the first dot
1738 # for the gcc compiler. This should be automatically
1744 # Optimized version of bn_sqr_words
1746 # void bn_sqr_words(BN_ULONG *r, BN_ULONG *a, int n)
1755 # No unrolling done here. Not performance critical.
1757 addic. r5,r5,0 #test r5.
1758 beq Lppcasm_sqr_adios
1762 Lppcasm_sqr_mainloop:
1763 #sqr(r[0],r[1],a[0]);
1769 bdnz Lppcasm_sqr_mainloop
1773 .byte 0,12,0x14,0,0,0,3,0
1775 .size .bn_sqr_words,.-.bn_sqr_words
1778 # NOTE: The following label name should be changed to
1779 # "bn_mul_words" i.e. remove the first dot
1780 # for the gcc compiler. This should be automatically
1787 # BN_ULONG bn_mul_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w)
1794 xor r12,r12,r12 # used for carry
1795 rlwinm. r7,r5,30,2,31 # num >> 2
1799 #mul(rp[0],ap[0],w,c1);
1800 $LD r8,`0*$BNSZ`(r4)
1804 #addze r10,r10 #carry is NOT ignored.
1805 #will be taken care of
1806 #in second spin below
1808 $ST r9,`0*$BNSZ`(r3)
1809 #mul(rp[1],ap[1],w,c1);
1810 $LD r8,`1*$BNSZ`(r4)
1815 $ST r11,`1*$BNSZ`(r3)
1816 #mul(rp[2],ap[2],w,c1);
1817 $LD r8,`2*$BNSZ`(r4)
1822 $ST r9,`2*$BNSZ`(r3)
1823 #mul_add(rp[3],ap[3],w,c1);
1824 $LD r8,`3*$BNSZ`(r4)
1828 addze r12,r12 #this spin we collect carry into
1830 $ST r11,`3*$BNSZ`(r3)
1832 addi r3,r3,`4*$BNSZ`
1833 addi r4,r4,`4*$BNSZ`
1834 bdnz Lppcasm_mw_LOOP
1839 #mul(rp[0],ap[0],w,c1);
1840 $LD r8,`0*$BNSZ`(r4)
1845 $ST r9,`0*$BNSZ`(r3)
1853 #mul(rp[1],ap[1],w,c1);
1854 $LD r8,`1*$BNSZ`(r4)
1859 $ST r9,`1*$BNSZ`(r3)
1866 #mul_add(rp[2],ap[2],w,c1);
1867 $LD r8,`2*$BNSZ`(r4)
1872 $ST r9,`2*$BNSZ`(r3)
1879 .byte 0,12,0x14,0,0,0,4,0
1881 .size .bn_mul_words,.-.bn_mul_words
1884 # NOTE: The following label name should be changed to
1885 # "bn_mul_add_words" i.e. remove the first dot
1886 # for the gcc compiler. This should be automatically
1893 # BN_ULONG bn_mul_add_words(BN_ULONG *rp, BN_ULONG *ap, int num, BN_ULONG w)
1900 # empirical evidence suggests that unrolled version performs best!!
1902 xor r0,r0,r0 #r0 = 0
1903 xor r12,r12,r12 #r12 = 0 . used for carry
1904 rlwinm. r7,r5,30,2,31 # num >> 2
1905 beq Lppcasm_maw_leftover # if (num < 4) go LPPCASM_maw_leftover
1907 Lppcasm_maw_mainloop:
1908 #mul_add(rp[0],ap[0],w,c1);
1909 $LD r8,`0*$BNSZ`(r4)
1910 $LD r11,`0*$BNSZ`(r3)
1913 addc r9,r9,r12 #r12 is carry.
1917 #the above instruction addze
1918 #is NOT needed. Carry will NOT
1919 #be ignored. It's not affected
1920 #by multiply and will be collected
1922 $ST r9,`0*$BNSZ`(r3)
1924 #mul_add(rp[1],ap[1],w,c1);
1925 $LD r8,`1*$BNSZ`(r4)
1926 $LD r9,`1*$BNSZ`(r3)
1929 adde r11,r11,r10 #r10 is carry.
1933 $ST r11,`1*$BNSZ`(r3)
1935 #mul_add(rp[2],ap[2],w,c1);
1936 $LD r8,`2*$BNSZ`(r4)
1938 $LD r11,`2*$BNSZ`(r3)
1944 $ST r9,`2*$BNSZ`(r3)
1946 #mul_add(rp[3],ap[3],w,c1);
1947 $LD r8,`3*$BNSZ`(r4)
1949 $LD r9,`3*$BNSZ`(r3)
1955 $ST r11,`3*$BNSZ`(r3)
1956 addi r3,r3,`4*$BNSZ`
1957 addi r4,r4,`4*$BNSZ`
1958 bdnz Lppcasm_maw_mainloop
1960 Lppcasm_maw_leftover:
1962 beq Lppcasm_maw_adios
1965 #mul_add(rp[0],ap[0],w,c1);
1977 bdz Lppcasm_maw_adios
1978 #mul_add(rp[1],ap[1],w,c1);
1989 bdz Lppcasm_maw_adios
1990 #mul_add(rp[2],ap[2],w,c1);
2005 .byte 0,12,0x14,0,0,0,4,0
2007 .size .bn_mul_add_words,.-.bn_mul_add_words
2010 $data =~ s/\`([^\`]*)\`/eval $1/gem;