Unified - adapt the generation of rc4 assembler to use GENERATE
[openssl.git] / crypto / rc4 / asm / rc4-c64xplus.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 # RC4 for C64x+.
11 #
12 # April 2014
13 #
14 # RC4 subroutine processes one byte in 7.0 cycles, which is 3x faster
15 # than TI CGT-generated code. Loop is scheduled in such way that
16 # there is only one reference to memory in each cycle. This is done
17 # to avoid L1D memory banking conflicts, see SPRU871 TI publication
18 # for further details. Otherwise it should be possible to schedule
19 # the loop for iteration interval of 6...
20
21 ($KEY,$LEN,$INP,$OUT)=("A4","B4","A6","B6");
22
23 ($KEYA,$XX,$TY,$xx,$ONE,$ret)=map("A$_",(5,7,8,9,1,2));
24 ($KEYB,$YY,$TX,$tx,$SUM,$dat)=map("B$_",(5,7,8,9,1,2));
25
26 $code.=<<___;
27         .text
28
29         .if     .ASSEMBLER_VERSION<7000000
30         .asg    0,__TI_EABI__
31         .endif
32         .if     __TI_EABI__
33         .nocmp
34         .asg    RC4,_RC4
35         .asg    RC4_set_key,_RC4_set_key
36         .asg    RC4_options,_RC4_options
37         .endif
38
39         .global _RC4
40         .align  16
41 _RC4:
42         .asmfunc
43         MV      $LEN,B0
44   [!B0] BNOP    B3                      ; if (len==0) return;
45 ||[B0]  ADD     $KEY,2,$KEYA
46 ||[B0]  ADD     $KEY,2,$KEYB
47   [B0]  MVK     1,$ONE
48 ||[B0]  LDBU    *${KEYA}[-2],$XX        ; key->x
49   [B0]  LDBU    *${KEYB}[-1],$YY        ; key->y
50 ||      NOP     4
51
52         ADD4    $ONE,$XX,$XX
53         LDBU    *${KEYA}[$XX],$TX
54 ||      MVC     $LEN,ILC
55         NOP     4
56 ;;==================================================
57         SPLOOP  7
58 ||      ADD4    $TX,$YY,$YY
59
60         LDBU    *${KEYB}[$YY],$TY
61 ||      MVD     $XX,$xx
62 ||      ADD4    $ONE,$XX,$XX
63         LDBU    *${KEYA}[$XX],$tx
64         CMPEQ   $YY,$XX,B0
65 ||      NOP     3
66         STB     $TX,*${KEYB}[$YY]
67 ||[B0]  ADD4    $TX,$YY,$YY
68         STB     $TY,*${KEYA}[$xx]
69 ||[!B0] ADD4    $tx,$YY,$YY
70 ||[!B0] MVD     $tx,$TX
71         ADD4    $TY,$TX,$SUM            ; [0,0] $TX is not replaced by $tx yet!
72 ||      NOP     2
73         LDBU    *$INP++,$dat
74 ||      NOP     2
75         LDBU    *${KEYB}[$SUM],$ret
76 ||      NOP     5
77         XOR.L   $dat,$ret,$ret
78         SPKERNEL
79 ||      STB     $ret,*$OUT++
80 ;;==================================================
81         SUB4    $XX,$ONE,$XX
82 ||      NOP     5
83         STB     $XX,*${KEYA}[-2]        ; key->x
84 ||      SUB4    $YY,$TX,$YY
85 ||      BNOP    B3      
86         STB     $YY,*${KEYB}[-1]        ; key->y
87 ||      NOP     5
88         .endasmfunc
89
90         .global _RC4_set_key
91         .align  16
92 _RC4_set_key:
93         .asmfunc
94         .if     .BIG_ENDIAN
95         MVK     0x00000404,$ONE
96 ||      MVK     0x00000203,B0
97         MVKH    0x04040000,$ONE
98 ||      MVKH    0x00010000,B0
99         .else
100         MVK     0x00000404,$ONE
101 ||      MVK     0x00000100,B0
102         MVKH    0x04040000,$ONE
103 ||      MVKH    0x03020000,B0
104         .endif
105         ADD     $KEY,2,$KEYA
106 ||      ADD     $KEY,2,$KEYB
107 ||      ADD     $INP,$LEN,$ret          ; end of input
108         LDBU    *${INP}++,$dat
109 ||      MVK     0,$TX
110         STH     $TX,*${KEY}++           ; key->x=key->y=0
111 ||      MV      B0,A0
112 ||      MVK     64-4,B0
113
114 ;;==================================================
115         SPLOOPD 1
116 ||      MVC     B0,ILC
117
118         STNW    A0,*${KEY}++
119 ||      ADD4    $ONE,A0,A0
120         SPKERNEL
121 ;;==================================================
122
123         MVK     0,$YY
124 ||      MVK     0,$XX
125         MVK     1,$ONE
126 ||      MVK     256-1,B0
127
128 ;;==================================================
129         SPLOOPD 8
130 ||      MVC     B0,ILC
131
132         ADD4    $dat,$YY,$YY
133 ||      CMPEQ   $INP,$ret,A0            ; end of input?
134         LDBU    *${KEYB}[$YY],$TY
135 ||      MVD     $XX,$xx
136 ||      ADD4    $ONE,$XX,$XX
137         LDBU    *${KEYA}[$XX],$tx
138 ||[A0]  SUB     $INP,$LEN,$INP          ; rewind
139         LDBU    *${INP}++,$dat
140 ||      CMPEQ   $YY,$XX,B0
141 ||      NOP     3
142         STB     $TX,*${KEYB}[$YY]
143 ||[B0]  ADD4    $TX,$YY,$YY
144         STB     $TY,*${KEYA}[$xx]
145 ||[!B0] ADD4    $tx,$YY,$YY
146 ||[!B0] MV      $tx,$TX
147         SPKERNEL
148 ;;==================================================
149
150         BNOP    B3,5
151         .endasmfunc
152
153         .global _RC4_options
154         .align  16
155 _RC4_options:
156 _rc4_options:
157         .asmfunc
158         BNOP    B3,1
159         ADDKPC  _rc4_options,B4
160         .if     __TI_EABI__
161         MVKL    \$PCR_OFFSET(rc4_options,_rc4_options),A4
162         MVKH    \$PCR_OFFSET(rc4_options,_rc4_options),A4
163         .else
164         MVKL    (rc4_options-_rc4_options),A4
165         MVKH    (rc4_options-_rc4_options),A4
166         .endif
167         ADD     B4,A4,A4
168         .endasmfunc
169
170         .if     __TI_EABI__
171         .sect   ".text:rc4_options.const"
172         .else
173         .sect   ".const:rc4_options"
174         .endif
175         .align  4
176 rc4_options:
177         .cstring "rc4(sploop,char)"
178         .cstring "RC4 for C64+, CRYPTOGAMS by <appro\@openssl.org>"
179         .align  4
180 ___
181
182 print $code;
183 close STDOUT;