Import of old SSLeay release: SSLeay 0.8.1b
[openssl.git] / crypto / des / asm / readme
1 First up, let me say I don't like writing in assembler.  It is not portable,\r
2 dependant on the particular CPU architecture release and is generally a pig\r
3 to debug and get right.  Having said that, the x86 architecture is probably\r
4 the most important for speed due to number of boxes and since\r
5 it appears to be the worst architecture to to get\r
6 good C compilers for.  So due to this, I have lowered myself to do\r
7 assembler for the inner DES routines in libdes :-).\r
8 \r
9 The file to implement in assembler is des_enc.c.  Replace the following\r
10 4 functions\r
11 des_encrypt(DES_LONG data[2],des_key_schedule ks, int encrypt);\r
12 des_encrypt2(DES_LONG data[2],des_key_schedule ks, int encrypt);\r
13 des_encrypt3(DES_LONG data[2],des_key_schedule ks1,ks2,ks3);\r
14 des_decrypt3(DES_LONG data[2],des_key_schedule ks1,ks2,ks3);\r
15 \r
16 They encrypt/decrypt the 64 bits held in 'data' using\r
17 the 'ks' key schedules.   The only difference between the 4 functions is that\r
18 des_encrypt2() does not perform IP() or FP() on the data (this is an\r
19 optimization for when doing triple DES and des_encrypt3() and des_decrypt3()\r
20 perform triple des.  The triple DES routines are in here because it does\r
21 make a big difference to have them located near the des_encrypt2 function\r
22 at link time..\r
23 \r
24 Now as we all know, there are lots of different operating systems running on\r
25 x86 boxes, and unfortunately they normally try to make sure their assembler\r
26 formating is not the same as the other peoples.\r
27 The 4 main formats I know of are\r
28 Microsoft       Windows 95/Windows NT\r
29 Elf             Includes Linux and FreeBSD(?).\r
30 a.out           The older Linux.\r
31 Solaris         Same as Elf but different comments :-(.\r
32 \r
33 Now I was not overly keen to write 4 different copies of the same code,\r
34 so I wrote a few perl routines to output the correct assembler, given\r
35 a target assembler type.  This code is ugly and is just a hack.\r
36 The libraries are x86unix.pl and x86ms.pl.\r
37 des586.pl, des686.pl and des-som[23].pl are the programs to actually\r
38 generate the assembler.\r
39 \r
40 So to generate elf assembler\r
41 perl des-som3.pl elf >dx86-elf.s\r
42 For Windows 95/NT\r
43 perl des-som2.pl win32 >win32.asm\r
44 \r
45 [ update 4 Jan 1996 ]\r
46 I have added another way to do things.\r
47 perl des-som3.pl cpp >dx86-cpp.s\r
48 generates a file that will be included by dx86unix.cpp when it is compiled.\r
49 To build for elf, a.out, solaris, bsdi etc,\r
50 cc -E -DELF asm/dx86unix.cpp | as -o asm/dx86-elf.o\r
51 cc -E -DSOL asm/dx86unix.cpp | as -o asm/dx86-sol.o\r
52 cc -E -DOUT asm/dx86unix.cpp | as -o asm/dx86-out.o\r
53 cc -E -DBSDI asm/dx86unix.cpp | as -o asm/dx86bsdi.o\r
54 This was done to cut down the number of files in the distribution.\r
55 \r
56 Now the ugly part.  I acquired my copy of Intels\r
57 "Optimization's For Intel's 32-Bit Processors" and found a few interesting\r
58 things.  First, the aim of the exersize is to 'extract' one byte at a time\r
59 from a word and do an array lookup.  This involves getting the byte from\r
60 the 4 locations in the word and moving it to a new word and doing the lookup.\r
61 The most obvious way to do this is\r
62 xor     eax,    eax                             # clear word\r
63 movb    al,     cl                              # get low byte\r
64 xor     edi     DWORD PTR 0x100+des_SP[eax]     # xor in word\r
65 movb    al,     ch                              # get next byte\r
66 xor     edi     DWORD PTR 0x300+des_SP[eax]     # xor in word\r
67 shr     ecx     16\r
68 which seems ok.  For the pentium, this system appears to be the best.\r
69 One has to do instruction interleaving to keep both functional units\r
70 operating, but it is basically very efficient.\r
71 \r
72 Now the crunch.  When a full register is used after a partial write, eg.\r
73 mov     al,     cl\r
74 xor     edi,    DWORD PTR 0x100+des_SP[eax]\r
75 386     - 1 cycle stall\r
76 486     - 1 cycle stall\r
77 586     - 0 cycle stall\r
78 686     - at least 7 cycle stall (page 22 of the above mentioned document).\r
79 \r
80 So the technique that produces the best results on a pentium, according to\r
81 the documentation, will produce hideous results on a pentium pro.\r
82 \r
83 To get around this, des686.pl will generate code that is not as fast on\r
84 a pentium, should be very good on a pentium pro.\r
85 mov     eax,    ecx                             # copy word \r
86 shr     ecx,    8                               # line up next byte\r
87 and     eax,    0fch                            # mask byte\r
88 xor     edi     DWORD PTR 0x100+des_SP[eax]     # xor in array lookup\r
89 mov     eax,    ecx                             # get word\r
90 shr     ecx     8                               # line up next byte\r
91 and     eax,    0fch                            # mask byte\r
92 xor     edi     DWORD PTR 0x300+des_SP[eax]     # xor in array lookup\r
93 \r
94 Due to the execution units in the pentium, this actually works quite well.\r
95 For a pentium pro it should be very good.  This is the type of output\r
96 Visual C++ generates.\r
97 \r
98 There is a third option.  instead of using\r
99 mov     al,     ch\r
100 which is bad on the pentium pro, one may be able to use\r
101 movzx   eax,    ch\r
102 which may not incur the partial write penalty.  On the pentium,\r
103 this instruction takes 4 cycles so is not worth using but on the\r
104 pentium pro it appears it may be worth while.  I need access to one to\r
105 experiment :-).\r
106 \r
107 eric (20 Oct 1996)\r
108 \r
109 22 Nov 1996 - I have asked people to run the 2 different version on pentium\r
110 pros and it appears that the intel documentation is wrong.  The\r
111 mov al,bh is still faster on a pentium pro, so just use the des586.pl\r
112 install des686.pl\r
113 \r
114 3 Dec 1996 - I added des_encrypt3/des_decrypt3 because I have moved these\r
115 functions into des_enc.c because it does make a massive performance\r
116 difference on some boxes to have the functions code located close to\r
117 the des_encrypt2() function.\r
118 \r
119 9 Jan 1997 - des-som2.pl is now the correct perl script to use for\r
120 pentiums.  It contains an inner loop from\r
121 Svend Olaf Mikkelsen <svolaf@inet.uni-c.dk> which does raw ecb DES calls at\r
122 273,000 per second.  He had a previous version at 250,000 and the best\r
123 I was able to get was 203,000.  The content has not changed, this is all\r
124 due to instruction sequencing (and actual instructions choice) which is able\r
125 to keep both functional units of the pentium going.\r
126 We may have lost the ugly register usage restrictions when x86 went 32 bit\r
127 but for the pentium it has been replaced by evil instruction ordering tricks.\r
128 \r
129 13 Jan 1997 - des-som3.pl, more optimizations from Svend Olaf.\r
130 raw DES at 281,000 per second on a pentium 100.\r
131 \r