4af7fba7b3562a2c7925f816d9ba8b59f43fcdb5
[openssl.git] / crypto / rc4 / asm / rc4-ia64.S
1 // ====================================================================
2 // Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
3 // project.
4 //
5 // Rights for redistribution and usage in source and binary forms are
6 // granted according to the OpenSSL license. Warranty of any kind is
7 // disclaimed.
8 // ====================================================================
9
10 .ident  "rc4-ia64.S, Version 1.0"
11 .ident  "IA-64 ISA artwork by Andy Polyakov <appro@fy.chalmers.se>"
12
13 // What's wrong with compiler generated code? Because of the nature of
14 // C language, compiler doesn't [dare to] reorder load and stores. But
15 // being memory-bound, RC4 should benefit from reorder [on in-order-
16 // execution core such as IA-64]. But what can we reorder? At the very
17 // least we can safely reorder references to key schedule in respect
18 // to input and output streams. Secondly, less obvious, it's possible
19 // to pull up some references to elements of the key schedule itself.
20 // Fact is that such prior loads are not safe only for "degenerated"
21 // key schedule, when all elements equal to the same value, which is
22 // never the case [key schedule setup routine makes sure it's not].
23 // Furthermore. In order to compress loop body to the minimum, I chose
24 // to deploy deposit instruction, which substitutes for the whole
25 // key->data+((x&255)<<log2(sizeof(key->data[0]))). This unfortunately
26 // requires key->data to be aligned at sizeof(key->data) boundary.
27 // This is why you'll find "RC4_INT pad[512-256-2];" addenum to RC4_KEY
28 // and "d=(RC4_INT *)(((size_t)(d+255))&~(sizeof(key->data)-1));" in
29 // rc4_skey.c [and rc4_enc.c, where it's retained for debugging
30 // purposes]. Throughput is ~210MBps on 900MHz CPU, which is is >3x
31 // faster than gcc generated code and +30% - if compared to HP-UX C.
32 // Unrolling loop below should give >30% on top of that...
33
34 .text
35 .explicit
36
37 #if defined(_HPUX_SOURCE) && !defined(_LP64)
38 # define ADDP   addp4
39 #else
40 # define ADDP   add
41 #endif
42
43 #define SZ      4       // this is set to sizeof(RC4_INT)
44 // SZ==4 seems to be optimal. At least SZ==8 is not any faster, not for
45 // assembler implementation, while SZ==1 code is ~30% slower.
46 #if SZ==1       // RC4_INT is unsigned char
47 # define        LDKEY   ld1
48 # define        STKEY   st1
49 # define        OFF     0
50 #elif SZ==4     // RC4_INT is unsigned int
51 # define        LDKEY   ld4
52 # define        STKEY   st4
53 # define        OFF     2
54 #elif SZ==8     // RC4_INT is unsigned long
55 # define        LDKEY   ld8
56 # define        STKEY   st8
57 # define        OFF     3
58 #endif
59
60 out=r8;         // [expanded] output pointer
61 inp=r9;         // [expanded] output pointer
62 prsave=r10;
63 key=r28;        // [expanded] pointer to RC4_KEY
64 ksch=r29;       // (key->data+255)[&~(sizeof(key->data)-1)]
65 xx=r30;
66 yy=r31;
67
68 // void RC4(RC4_KEY *key,size_t len,const void *inp,void *out);
69 .global RC4#
70 .proc   RC4#
71 .align  32
72 .skip   16
73 RC4:
74         .prologue
75         .fframe 0
76         .save   ar.pfs,r2
77         .save   ar.lc,r3
78         .save   pr,prsave
79 { .mii; alloc   r2=ar.pfs,4,12,0,16
80         mov     prsave=pr
81         ADDP    key=0,in0               };;
82 { .mib; cmp.eq  p6,p0=0,in1                     // len==0?
83         mov     r3=ar.lc
84 (p6)    br.ret.spnt.many        b0      };;     // emergency exit
85
86         .body
87         .rotr   dat[4],key_x[4],tx[2],rnd[2],key_y[2],ty[1];
88
89 { .mib; LDKEY   xx=[key],SZ                     // load key->x
90         add     in1=-1,in1                      // adjust len for loop counter
91         nop.b   0                       }
92 { .mib; ADDP    inp=0,in2
93         ADDP    out=0,in3
94         brp.loop.imp    .Ltop,.Lexit-16 };;
95 { .mmi; LDKEY   yy=[key]                        // load key->y
96         add     ksch=(255+1)*SZ,key             // as ksch will be used with
97                                                 // deposit instruction only,
98                                                 // I don't have to &~255...
99         mov     ar.lc=in1               }
100 { .mmi; nop.m   0
101         add     xx=1,xx
102         mov     pr.rot=1<<16            };;
103 { .mii; nop.m   0
104         dep     key_x[1]=xx,ksch,OFF,8
105         mov     ar.ec=3                 };;     // note that epilogue counter
106                                                 // is off by 1. I compensate
107                                                 // for this at exit...
108 .Ltop:
109 // The loop is scheduled for 3*(n+2) spin-rate on Itanium 2, which
110 // theoretically gives asymptotic performance of clock frequency
111 // divided by 3 bytes per seconds, or 500MBps on 1.5GHz CPU. Measured
112 // performance however is distinctly lower than 1/4:-( The culplrit
113 // seems to be *(out++)=dat, which inadvertently splits the bundle,
114 // even though there is M-unit available... Unrolling is due...
115 // Unrolled loop should collect output with variable shift instruction
116 // in order to avoid starvation for integer shifter... Only output
117 // pointer has to be aligned... It should be possible to get pretty
118 // close to theoretical peak...
119 { .mmi; (p16)   LDKEY   tx[0]=[key_x[1]]                // tx=key[xx]
120         (p17)   LDKEY   ty[0]=[key_y[1]]                // ty=key[yy]   
121         (p18)   dep     rnd[1]=rnd[1],ksch,OFF,8}       // &key[(tx+ty)&255]
122 { .mmi; (p19)   st1     [out]=dat[3],1                  // *(out++)=dat
123         (p16)   add     xx=1,xx                         // x++
124         (p0)    nop.i   0                       };;
125 { .mmi; (p18)   LDKEY   rnd[1]=[rnd[1]]                 // rnd=key[(tx+ty)&255]
126         (p16)   ld1     dat[0]=[inp],1                  // dat=*(inp++)
127         (p16)   dep     key_x[0]=xx,ksch,OFF,8  }       // &key[xx&255]
128 { .mmi; (p0)    nop.m   0
129         (p16)   add     yy=yy,tx[0]                     // y+=tx
130         (p0)    nop.i   0                       };;
131 { .mmi; (p17)   STKEY   [key_y[1]]=tx[1]                // key[yy]=tx
132         (p17)   STKEY   [key_x[2]]=ty[0]                // key[xx]=ty
133         (p16)   dep     key_y[0]=yy,ksch,OFF,8  }       // &key[yy&255]
134 { .mmb; (p17)   add     rnd[0]=tx[1],ty[0]              // tx+=ty
135         (p18)   xor     dat[2]=dat[2],rnd[1]            // dat^=rnd
136         br.ctop.sptk    .Ltop                   };;
137 .Lexit:
138 { .mib; STKEY   [key]=yy,-SZ                    // save key->y
139         mov     pr=prsave,0x1ffff
140         nop.b   0                       }
141 { .mib; st1     [out]=dat[3],1                  // compensate for truncated
142                                                 // epilogue counter
143         add     xx=-1,xx
144         nop.b   0                       };;
145 { .mib; STKEY   [key]=xx                        // save key->x
146         mov     ar.lc=r3
147         br.ret.sptk.many        b0      };;
148 .endp   RC4#