cef83942c91840cc4e9ceedaf86fc57c1a8b0ed4
[openssl.git] / crypto / bn / asm / c64xplus-gf2m.pl
1 #!/usr/bin/env perl
2 #
3 # ====================================================================
4 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
8 # ====================================================================
9 #
10 # February 2012
11 #
12 # The module implements bn_GF2m_mul_2x2 polynomial multiplication
13 # used in bn_gf2m.c. It's kind of low-hanging mechanical port from
14 # C for the time being... The subroutine runs in 37 cycles, which is
15 # 4.5x faster than compiler-generated code. Though comparison is
16 # totally unfair, because this module utilizes Galois Field Multiply
17 # instruction.
18
19 while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
20 open STDOUT,">$output";
21
22 ($rp,$a1,$a0,$b1,$b0)=("A4","B4","A6","B6","A8");   # argument vector
23
24 ($Alo,$Alox0,$Alox1,$Alox2,$Alox3)=map("A$_",(16..20));
25 ($Ahi,$Ahix0,$Ahix1,$Ahix2,$Ahix3)=map("B$_",(16..20));
26 ($B_0,$B_1,$B_2,$B_3)=("B5","A5","A7","B7");
27 ($A,$B)=($Alo,$B_1);
28 $xFF="B1";
29
30 sub mul_1x1_upper {
31 my ($A,$B)=@_;
32 $code.=<<___;
33         EXTU    $B,8,24,$B_2            ; smash $B to 4 bytes
34 ||      AND     $B,$xFF,$B_0
35 ||      SHRU    $B,24,$B_3
36         SHRU    $A,16,   $Ahi           ; smash $A to two halfwords
37 ||      EXTU    $A,16,16,$Alo
38
39         XORMPY  $Alo,$B_2,$Alox2        ; 16x8 bits muliplication
40 ||      XORMPY  $Ahi,$B_2,$Ahix2
41 ||      EXTU    $B,16,24,$B_1
42         XORMPY  $Alo,$B_0,$Alox0
43 ||      XORMPY  $Ahi,$B_0,$Ahix0
44         XORMPY  $Alo,$B_3,$Alox3
45 ||      XORMPY  $Ahi,$B_3,$Ahix3
46         XORMPY  $Alo,$B_1,$Alox1
47 ||      XORMPY  $Ahi,$B_1,$Ahix1
48 ___
49 }
50 sub mul_1x1_merged {
51 my ($OUTlo,$OUThi,$A,$B)=@_;
52 $code.=<<___;
53          EXTU   $B,8,24,$B_2            ; smash $B to 4 bytes
54 ||       AND    $B,$xFF,$B_0
55 ||       SHRU   $B,24,$B_3
56          SHRU   $A,16,   $Ahi           ; smash $A to two halfwords
57 ||       EXTU   $A,16,16,$Alo
58
59         XOR     $Ahix0,$Alox2,$Ahix0
60 ||      MV      $Ahix2,$OUThi
61 ||       XORMPY $Alo,$B_2,$Alox2
62          XORMPY $Ahi,$B_2,$Ahix2
63 ||       EXTU   $B,16,24,$B_1
64 ||       XORMPY $Alo,$B_0,A1            ; $Alox0
65         XOR     $Ahix1,$Alox3,$Ahix1
66 ||      SHL     $Ahix0,16,$OUTlo
67 ||      SHRU    $Ahix0,16,$Ahix0
68         XOR     $Alox0,$OUTlo,$OUTlo
69 ||      XOR     $Ahix0,$OUThi,$OUThi
70 ||       XORMPY $Ahi,$B_0,$Ahix0
71 ||       XORMPY $Alo,$B_3,$Alox3
72 ||      SHL     $Alox1,8,$Alox1
73 ||      SHL     $Ahix3,8,$Ahix3
74         XOR     $Alox1,$OUTlo,$OUTlo
75 ||      XOR     $Ahix3,$OUThi,$OUThi
76 ||       XORMPY $Ahi,$B_3,$Ahix3
77 ||      SHL     $Ahix1,24,$Alox1
78 ||      SHRU    $Ahix1,8, $Ahix1
79         XOR     $Alox1,$OUTlo,$OUTlo
80 ||      XOR     $Ahix1,$OUThi,$OUThi
81 ||       XORMPY $Alo,$B_1,$Alox1
82 ||       XORMPY $Ahi,$B_1,$Ahix1
83 ||       MV     A1,$Alox0
84 ___
85 }
86 sub mul_1x1_lower {
87 my ($OUTlo,$OUThi)=@_;
88 $code.=<<___;
89         ;NOP
90         XOR     $Ahix0,$Alox2,$Ahix0
91 ||      MV      $Ahix2,$OUThi
92         NOP
93         XOR     $Ahix1,$Alox3,$Ahix1
94 ||      SHL     $Ahix0,16,$OUTlo
95 ||      SHRU    $Ahix0,16,$Ahix0
96         XOR     $Alox0,$OUTlo,$OUTlo
97 ||      XOR     $Ahix0,$OUThi,$OUThi
98 ||      SHL     $Alox1,8,$Alox1
99 ||      SHL     $Ahix3,8,$Ahix3
100         XOR     $Alox1,$OUTlo,$OUTlo
101 ||      XOR     $Ahix3,$OUThi,$OUThi
102 ||      SHL     $Ahix1,24,$Alox1
103 ||      SHRU    $Ahix1,8, $Ahix1
104         XOR     $Alox1,$OUTlo,$OUTlo
105 ||      XOR     $Ahix1,$OUThi,$OUThi
106 ___
107 }
108 $code.=<<___;
109         .text
110
111         .global _bn_GF2m_mul_2x2
112 _bn_GF2m_mul_2x2:
113         .asmfunc
114         MVK     0xFF,$xFF
115 ___
116         &mul_1x1_upper($a0,$b0);                # a0·b0
117 $code.=<<___;
118 ||      MV      $b1,$B
119         MV      $a1,$A
120 ___
121         &mul_1x1_merged("A28","B28",$A,$B);     # a0·b0/a1·b1
122 $code.=<<___;
123 ||      XOR     $b0,$b1,$B
124         XOR     $a0,$a1,$A
125 ___
126         &mul_1x1_merged("A31","B31",$A,$B);     # a1·b1/(a0+a1)·(b0+b1)
127 $code.=<<___;
128         XOR     A28,A31,A29
129 ||      XOR     B28,B31,B29                     ; a0·b0+a1·b1
130 ___
131         &mul_1x1_lower("A30","B30");            # (a0+a1)·(b0+b1)
132 $code.=<<___;
133 ||      BNOP    B3
134         XOR     A29,A30,A30
135 ||      XOR     B29,B30,B30                     ; (a0+a1)·(b0+b1)-a0·b0-a1·b1
136         XOR     B28,A30,A30
137 ||      STW     A28,*${rp}[0]
138         XOR     B30,A31,A31
139 ||      STW     A30,*${rp}[1]
140         STW     A31,*${rp}[2]
141         STW     B31,*${rp}[3]
142         .endasmfunc
143 ___
144
145 print $code;
146 close STDOUT;