1160814Ssimon#!/usr/bin/env perl
2160814Ssimon#
3160814Ssimon# ====================================================================
4160814Ssimon# Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
5194206Ssimon# project. The module is, however, dual licensed under OpenSSL and
6194206Ssimon# CRYPTOGAMS licenses depending on where you obtain it. For further
7194206Ssimon# details see http://www.openssl.org/~appro/cryptogams/.
8160814Ssimon# ====================================================================
9160814Ssimon#
10160814Ssimon# SHA256/512_Transform for Itanium.
11160814Ssimon#
12160814Ssimon# sha512_block runs in 1003 cycles on Itanium 2, which is almost 50%
13160814Ssimon# faster than gcc and >60%(!) faster than code generated by HP-UX
14160814Ssimon# compiler (yes, HP-UX is generating slower code, because unlike gcc,
15160814Ssimon# it failed to deploy "shift right pair," 'shrp' instruction, which
16160814Ssimon# substitutes for 64-bit rotate).
17160814Ssimon#
18160814Ssimon# 924 cycles long sha256_block outperforms gcc by over factor of 2(!)
19160814Ssimon# and HP-UX compiler - by >40% (yes, gcc won sha512_block, but lost
20160814Ssimon# this one big time). Note that "formally" 924 is about 100 cycles
21160814Ssimon# too much. I mean it's 64 32-bit rounds vs. 80 virtually identical
22160814Ssimon# 64-bit ones and 1003*64/80 gives 802. Extra cycles, 2 per round,
23160814Ssimon# are spent on extra work to provide for 32-bit rotations. 32-bit
24160814Ssimon# rotations are still handled by 'shrp' instruction and for this
25160814Ssimon# reason lower 32 bits are deposited to upper half of 64-bit register
26160814Ssimon# prior 'shrp' issue. And in order to minimize the amount of such
27160814Ssimon# operations, X[16] values are *maintained* with copies of lower
28160814Ssimon# halves in upper halves, which is why you'll spot such instructions
29160814Ssimon# as custom 'mux2', "parallel 32-bit add," 'padd4' and "parallel
30160814Ssimon# 32-bit unsigned right shift," 'pshr4.u' instructions here.
31160814Ssimon#
32160814Ssimon# Rules of engagement.
33160814Ssimon#
34160814Ssimon# There is only one integer shifter meaning that if I have two rotate,
35160814Ssimon# deposit or extract instructions in adjacent bundles, they shall
36160814Ssimon# split [at run-time if they have to]. But note that variable and
37160814Ssimon# parallel shifts are performed by multi-media ALU and *are* pairable
38160814Ssimon# with rotates [and alike]. On the backside MMALU is rather slow: it
39160814Ssimon# takes 2 extra cycles before the result of integer operation is
40160814Ssimon# available *to* MMALU and 2(*) extra cycles before the result of MM
41160814Ssimon# operation is available "back" *to* integer ALU, not to mention that
42160814Ssimon# MMALU itself has 2 cycles latency. However! I explicitly scheduled
43160814Ssimon# these MM instructions to avoid MM stalls, so that all these extra
44160814Ssimon# latencies get "hidden" in instruction-level parallelism.
45160814Ssimon#
46160814Ssimon# (*) 2 cycles on Itanium 1 and 1 cycle on Itanium 2. But I schedule
47160814Ssimon#     for 2 in order to provide for best *overall* performance,
48160814Ssimon#     because on Itanium 1 stall on MM result is accompanied by
49160814Ssimon#     pipeline flush, which takes 6 cycles:-(
50160814Ssimon#
51160814Ssimon# Resulting performance numbers for 900MHz Itanium 2 system:
52160814Ssimon#
53160814Ssimon# The 'numbers' are in 1000s of bytes per second processed.
54160814Ssimon# type     16 bytes    64 bytes   256 bytes  1024 bytes  8192 bytes
55160814Ssimon# sha1(*)   6210.14k   20376.30k   52447.83k   85870.05k  105478.12k
56160814Ssimon# sha256    7476.45k   20572.05k   41538.34k   56062.29k   62093.18k
57160814Ssimon# sha512    4996.56k   20026.28k   47597.20k   85278.79k  111501.31k
58160814Ssimon#
59160814Ssimon# (*) SHA1 numbers are for HP-UX compiler and are presented purely
60160814Ssimon#     for reference purposes. I bet it can improved too...
61160814Ssimon#
62160814Ssimon# To generate code, pass the file name with either 256 or 512 in its
63160814Ssimon# name and compiler flags.
64160814Ssimon
65160814Ssimon$output=shift;
66160814Ssimon
67160814Ssimonif ($output =~ /512.*\.[s|asm]/) {
68160814Ssimon	$SZ=8;
69160814Ssimon	$BITS=8*$SZ;
70160814Ssimon	$LDW="ld8";
71160814Ssimon	$STW="st8";
72160814Ssimon	$ADD="add";
73160814Ssimon	$SHRU="shr.u";
74160814Ssimon	$TABLE="K512";
75194206Ssimon	$func="sha512_block_data_order";
76160814Ssimon	@Sigma0=(28,34,39);
77160814Ssimon	@Sigma1=(14,18,41);
78160814Ssimon	@sigma0=(1,  8, 7);
79160814Ssimon	@sigma1=(19,61, 6);
80160814Ssimon	$rounds=80;
81160814Ssimon} elsif ($output =~ /256.*\.[s|asm]/) {
82160814Ssimon	$SZ=4;
83160814Ssimon	$BITS=8*$SZ;
84160814Ssimon	$LDW="ld4";
85160814Ssimon	$STW="st4";
86160814Ssimon	$ADD="padd4";
87160814Ssimon	$SHRU="pshr4.u";
88160814Ssimon	$TABLE="K256";
89194206Ssimon	$func="sha256_block_data_order";
90160814Ssimon	@Sigma0=( 2,13,22);
91160814Ssimon	@Sigma1=( 6,11,25);
92160814Ssimon	@sigma0=( 7,18, 3);
93160814Ssimon	@sigma1=(17,19,10);
94160814Ssimon	$rounds=64;
95160814Ssimon} else { die "nonsense $output"; }
96160814Ssimon
97160814Ssimonopen STDOUT,">$output" || die "can't open $output: $!";
98160814Ssimon
99160814Ssimonif ($^O eq "hpux") {
100160814Ssimon    $ADDP="addp4";
101160814Ssimon    for (@ARGV) { $ADDP="add" if (/[\+DD|\-mlp]64/); }
102160814Ssimon} else { $ADDP="add"; }
103160814Ssimonfor (@ARGV)  {	$big_endian=1 if (/\-DB_ENDIAN/);
104160814Ssimon		$big_endian=0 if (/\-DL_ENDIAN/);  }
105160814Ssimonif (!defined($big_endian))
106160814Ssimon             {	$big_endian=(unpack('L',pack('N',1))==1);  }
107160814Ssimon
108160814Ssimon$code=<<___;
109194206Ssimon.ident  \"$output, version 1.1\"
110160814Ssimon.ident  \"IA-64 ISA artwork by Andy Polyakov <appro\@fy.chalmers.se>\"
111160814Ssimon.explicit
112160814Ssimon.text
113160814Ssimon
114194206Ssimonpfssave=r2;
115194206Ssimonlcsave=r3;
116160814Ssimonprsave=r14;
117160814SsimonK=r15;
118160814SsimonA=r16;	B=r17;	C=r18;	D=r19;
119160814SsimonE=r20;	F=r21;	G=r22;	H=r23;
120160814SsimonT1=r24;	T2=r25;
121160814Ssimons0=r26;	s1=r27;	t0=r28;	t1=r29;
122160814SsimonKtbl=r30;
123160814Ssimonctx=r31;	// 1st arg
124160814Ssimoninput=r48;	// 2nd arg
125160814Ssimonnum=r49;	// 3rd arg
126160814Ssimonsgm0=r50;	sgm1=r51;	// small constants
127194206SsimonA_=r54;	B_=r55;	C_=r56;	D_=r57;
128194206SsimonE_=r58;	F_=r59;	G_=r60;	H_=r61;
129160814Ssimon
130160814Ssimon// void $func (SHA_CTX *ctx, const void *in,size_t num[,int host])
131160814Ssimon.global	$func#
132160814Ssimon.proc	$func#
133160814Ssimon.align	32
134160814Ssimon$func:
135160814Ssimon	.prologue
136194206Ssimon	.save	ar.pfs,pfssave
137194206Ssimon{ .mmi;	alloc	pfssave=ar.pfs,3,27,0,16
138160814Ssimon	$ADDP	ctx=0,r32		// 1st arg
139194206Ssimon	.save	ar.lc,lcsave
140194206Ssimon	mov	lcsave=ar.lc	}
141160814Ssimon{ .mmi;	$ADDP	input=0,r33		// 2nd arg
142194206Ssimon	mov	num=r34			// 3rd arg
143194206Ssimon	.save	pr,prsave
144160814Ssimon	mov	prsave=pr	};;
145160814Ssimon
146160814Ssimon	.body
147160814Ssimon{ .mib;	add	r8=0*$SZ,ctx
148160814Ssimon	add	r9=1*$SZ,ctx
149194206Ssimon	brp.loop.imp	.L_first16,.L_first16_end-16	}
150160814Ssimon{ .mib;	add	r10=2*$SZ,ctx
151160814Ssimon	add	r11=3*$SZ,ctx
152194206Ssimon	brp.loop.imp	.L_rest,.L_rest_end-16		};;
153194206Ssimon
154160814Ssimon// load A-H
155194206Ssimon.Lpic_point:
156194206Ssimon{ .mmi;	$LDW	A_=[r8],4*$SZ
157194206Ssimon	$LDW	B_=[r9],4*$SZ
158194206Ssimon	mov	Ktbl=ip		}
159194206Ssimon{ .mmi;	$LDW	C_=[r10],4*$SZ
160194206Ssimon	$LDW	D_=[r11],4*$SZ
161194206Ssimon	mov	sgm0=$sigma0[2]	};;
162194206Ssimon{ .mmi;	$LDW	E_=[r8]
163194206Ssimon	$LDW	F_=[r9]
164194206Ssimon	add	Ktbl=($TABLE#-.Lpic_point),Ktbl		}
165194206Ssimon{ .mmi;	$LDW	G_=[r10]
166194206Ssimon	$LDW	H_=[r11]
167194206Ssimon	cmp.ne	p0,p16=0,r0	};;	// used in sha256_block
168194206Ssimon___
169194206Ssimon$code.=<<___ if ($BITS==64);
170194206Ssimon{ .mii;	and	r8=7,input
171194206Ssimon	and	input=~7,input;;
172194206Ssimon	cmp.eq	p9,p0=1,r8	}
173194206Ssimon{ .mmi;	cmp.eq	p10,p0=2,r8
174194206Ssimon	cmp.eq	p11,p0=3,r8
175194206Ssimon	cmp.eq	p12,p0=4,r8	}
176194206Ssimon{ .mmi;	cmp.eq	p13,p0=5,r8
177194206Ssimon	cmp.eq	p14,p0=6,r8
178194206Ssimon	cmp.eq	p15,p0=7,r8	};;
179194206Ssimon___
180194206Ssimon$code.=<<___;
181194206Ssimon.L_outer:
182194206Ssimon.rotr	X[16]
183194206Ssimon{ .mmi;	mov	A=A_
184194206Ssimon	mov	B=B_
185194206Ssimon	mov	ar.lc=14	}
186194206Ssimon{ .mmi;	mov	C=C_
187194206Ssimon	mov	D=D_
188194206Ssimon	mov	E=E_		}
189194206Ssimon{ .mmi;	mov	F=F_
190194206Ssimon	mov	G=G_
191194206Ssimon	mov	ar.ec=2		}
192194206Ssimon{ .mmi;	ld1	X[15]=[input],$SZ		// eliminated in 64-bit
193194206Ssimon	mov	H=H_
194160814Ssimon	mov	sgm1=$sigma1[2]	};;
195160814Ssimon
196194206Ssimon___
197194206Ssimon$t0="t0", $t1="t1", $code.=<<___ if ($BITS==32);
198160814Ssimon.align	32
199160814Ssimon.L_first16:
200194206Ssimon{ .mmi;		add	r9=1-$SZ,input
201194206Ssimon		add	r10=2-$SZ,input
202194206Ssimon		add	r11=3-$SZ,input	};;
203194206Ssimon{ .mmi;		ld1	r9=[r9]
204194206Ssimon		ld1	r10=[r10]
205160814Ssimon		dep.z	$t1=E,32,32	}
206194206Ssimon{ .mmi;		$LDW	K=[Ktbl],$SZ
207194206Ssimon		ld1	r11=[r11]
208160814Ssimon		zxt4	E=E		};;
209194206Ssimon{ .mii;		or	$t1=$t1,E
210194206Ssimon		dep	X[15]=X[15],r9,8,8
211194206Ssimon		dep	r11=r10,r11,8,8	};;
212194206Ssimon{ .mmi;		and	T1=F,E
213194206Ssimon		and	T2=A,B
214194206Ssimon		dep	X[15]=X[15],r11,16,16	}
215160814Ssimon{ .mmi;		andcm	r8=G,E
216160814Ssimon		and	r9=A,C
217160814Ssimon		mux2	$t0=A,0x44	};;	// copy lower half to upper
218194206Ssimon{ .mmi;	(p16)	ld1	X[15-1]=[input],$SZ	// prefetch
219194206Ssimon		xor	T1=T1,r8		// T1=((e & f) ^ (~e & g))
220160814Ssimon		_rotr	r11=$t1,$Sigma1[0] }	// ROTR(e,14)
221160814Ssimon{ .mib;		and	r10=B,C
222160814Ssimon		xor	T2=T2,r9	};;
223160814Ssimon___
224160814Ssimon$t0="A", $t1="E", $code.=<<___ if ($BITS==64);
225194206Ssimon// in 64-bit mode I load whole X[16] at once and take care of alignment...
226194206Ssimon{ .mmi;	add	r8=1*$SZ,input
227194206Ssimon	add	r9=2*$SZ,input
228194206Ssimon	add	r10=3*$SZ,input		};;
229194206Ssimon{ .mmb;	$LDW	X[15]=[input],4*$SZ
230194206Ssimon	$LDW	X[14]=[r8],4*$SZ
231194206Ssimon(p9)	br.cond.dpnt.many	.L1byte	};;
232194206Ssimon{ .mmb;	$LDW	X[13]=[r9],4*$SZ
233194206Ssimon	$LDW	X[12]=[r10],4*$SZ
234194206Ssimon(p10)	br.cond.dpnt.many	.L2byte	};;
235194206Ssimon{ .mmb;	$LDW	X[11]=[input],4*$SZ
236194206Ssimon	$LDW	X[10]=[r8],4*$SZ
237194206Ssimon(p11)	br.cond.dpnt.many	.L3byte	};;
238194206Ssimon{ .mmb;	$LDW	X[ 9]=[r9],4*$SZ
239194206Ssimon	$LDW	X[ 8]=[r10],4*$SZ
240194206Ssimon(p12)	br.cond.dpnt.many	.L4byte	};;
241194206Ssimon{ .mmb;	$LDW	X[ 7]=[input],4*$SZ
242194206Ssimon	$LDW	X[ 6]=[r8],4*$SZ
243194206Ssimon(p13)	br.cond.dpnt.many	.L5byte	};;
244194206Ssimon{ .mmb;	$LDW	X[ 5]=[r9],4*$SZ
245194206Ssimon	$LDW	X[ 4]=[r10],4*$SZ
246194206Ssimon(p14)	br.cond.dpnt.many	.L6byte	};;
247194206Ssimon{ .mmb;	$LDW	X[ 3]=[input],4*$SZ
248194206Ssimon	$LDW	X[ 2]=[r8],4*$SZ
249194206Ssimon(p15)	br.cond.dpnt.many	.L7byte	};;
250194206Ssimon{ .mmb;	$LDW	X[ 1]=[r9],4*$SZ
251194206Ssimon	$LDW	X[ 0]=[r10],4*$SZ
252194206Ssimon	br.many	.L_first16		};;
253194206Ssimon.L1byte:
254194206Ssimon{ .mmi;	$LDW	X[13]=[r9],4*$SZ
255194206Ssimon	$LDW	X[12]=[r10],4*$SZ
256194206Ssimon	shrp	X[15]=X[15],X[14],56	};;
257194206Ssimon{ .mmi;	$LDW	X[11]=[input],4*$SZ
258194206Ssimon	$LDW	X[10]=[r8],4*$SZ
259194206Ssimon	shrp	X[14]=X[14],X[13],56	}
260194206Ssimon{ .mmi;	$LDW	X[ 9]=[r9],4*$SZ
261194206Ssimon	$LDW	X[ 8]=[r10],4*$SZ
262194206Ssimon	shrp	X[13]=X[13],X[12],56	};;
263194206Ssimon{ .mmi;	$LDW	X[ 7]=[input],4*$SZ
264194206Ssimon	$LDW	X[ 6]=[r8],4*$SZ
265194206Ssimon	shrp	X[12]=X[12],X[11],56	}
266194206Ssimon{ .mmi;	$LDW	X[ 5]=[r9],4*$SZ
267194206Ssimon	$LDW	X[ 4]=[r10],4*$SZ
268194206Ssimon	shrp	X[11]=X[11],X[10],56	};;
269194206Ssimon{ .mmi;	$LDW	X[ 3]=[input],4*$SZ
270194206Ssimon	$LDW	X[ 2]=[r8],4*$SZ
271194206Ssimon	shrp	X[10]=X[10],X[ 9],56	}
272194206Ssimon{ .mmi;	$LDW	X[ 1]=[r9],4*$SZ
273194206Ssimon	$LDW	X[ 0]=[r10],4*$SZ
274194206Ssimon	shrp	X[ 9]=X[ 9],X[ 8],56	};;
275194206Ssimon{ .mii;	$LDW	T1=[input]
276194206Ssimon	shrp	X[ 8]=X[ 8],X[ 7],56
277194206Ssimon	shrp	X[ 7]=X[ 7],X[ 6],56	}
278194206Ssimon{ .mii;	shrp	X[ 6]=X[ 6],X[ 5],56
279194206Ssimon	shrp	X[ 5]=X[ 5],X[ 4],56	};;
280194206Ssimon{ .mii;	shrp	X[ 4]=X[ 4],X[ 3],56
281194206Ssimon	shrp	X[ 3]=X[ 3],X[ 2],56	}
282194206Ssimon{ .mii;	shrp	X[ 2]=X[ 2],X[ 1],56
283194206Ssimon	shrp	X[ 1]=X[ 1],X[ 0],56	}
284194206Ssimon{ .mib;	shrp	X[ 0]=X[ 0],T1,56
285194206Ssimon	br.many	.L_first16		};;
286194206Ssimon.L2byte:
287194206Ssimon{ .mmi;	$LDW	X[11]=[input],4*$SZ
288194206Ssimon	$LDW	X[10]=[r8],4*$SZ
289194206Ssimon	shrp	X[15]=X[15],X[14],48	}
290194206Ssimon{ .mmi;	$LDW	X[ 9]=[r9],4*$SZ
291194206Ssimon	$LDW	X[ 8]=[r10],4*$SZ
292194206Ssimon	shrp	X[14]=X[14],X[13],48	};;
293194206Ssimon{ .mmi;	$LDW	X[ 7]=[input],4*$SZ
294194206Ssimon	$LDW	X[ 6]=[r8],4*$SZ
295194206Ssimon	shrp	X[13]=X[13],X[12],48	}
296194206Ssimon{ .mmi;	$LDW	X[ 5]=[r9],4*$SZ
297194206Ssimon	$LDW	X[ 4]=[r10],4*$SZ
298194206Ssimon	shrp	X[12]=X[12],X[11],48	};;
299194206Ssimon{ .mmi;	$LDW	X[ 3]=[input],4*$SZ
300194206Ssimon	$LDW	X[ 2]=[r8],4*$SZ
301194206Ssimon	shrp	X[11]=X[11],X[10],48	}
302194206Ssimon{ .mmi;	$LDW	X[ 1]=[r9],4*$SZ
303194206Ssimon	$LDW	X[ 0]=[r10],4*$SZ
304194206Ssimon	shrp	X[10]=X[10],X[ 9],48	};;
305194206Ssimon{ .mii;	$LDW	T1=[input]
306194206Ssimon	shrp	X[ 9]=X[ 9],X[ 8],48
307194206Ssimon	shrp	X[ 8]=X[ 8],X[ 7],48	}
308194206Ssimon{ .mii;	shrp	X[ 7]=X[ 7],X[ 6],48
309194206Ssimon	shrp	X[ 6]=X[ 6],X[ 5],48	};;
310194206Ssimon{ .mii;	shrp	X[ 5]=X[ 5],X[ 4],48
311194206Ssimon	shrp	X[ 4]=X[ 4],X[ 3],48	}
312194206Ssimon{ .mii;	shrp	X[ 3]=X[ 3],X[ 2],48
313194206Ssimon	shrp	X[ 2]=X[ 2],X[ 1],48	}
314194206Ssimon{ .mii;	shrp	X[ 1]=X[ 1],X[ 0],48
315194206Ssimon	shrp	X[ 0]=X[ 0],T1,48	}
316194206Ssimon{ .mfb;	br.many	.L_first16		};;
317194206Ssimon.L3byte:
318194206Ssimon{ .mmi;	$LDW	X[ 9]=[r9],4*$SZ
319194206Ssimon	$LDW	X[ 8]=[r10],4*$SZ
320194206Ssimon	shrp	X[15]=X[15],X[14],40	};;
321194206Ssimon{ .mmi;	$LDW	X[ 7]=[input],4*$SZ
322194206Ssimon	$LDW	X[ 6]=[r8],4*$SZ
323194206Ssimon	shrp	X[14]=X[14],X[13],40	}
324194206Ssimon{ .mmi;	$LDW	X[ 5]=[r9],4*$SZ
325194206Ssimon	$LDW	X[ 4]=[r10],4*$SZ
326194206Ssimon	shrp	X[13]=X[13],X[12],40	};;
327194206Ssimon{ .mmi;	$LDW	X[ 3]=[input],4*$SZ
328194206Ssimon	$LDW	X[ 2]=[r8],4*$SZ
329194206Ssimon	shrp	X[12]=X[12],X[11],40	}
330194206Ssimon{ .mmi;	$LDW	X[ 1]=[r9],4*$SZ
331194206Ssimon	$LDW	X[ 0]=[r10],4*$SZ
332194206Ssimon	shrp	X[11]=X[11],X[10],40	};;
333194206Ssimon{ .mii;	$LDW	T1=[input]
334194206Ssimon	shrp	X[10]=X[10],X[ 9],40
335194206Ssimon	shrp	X[ 9]=X[ 9],X[ 8],40	}
336194206Ssimon{ .mii;	shrp	X[ 8]=X[ 8],X[ 7],40
337194206Ssimon	shrp	X[ 7]=X[ 7],X[ 6],40	};;
338194206Ssimon{ .mii;	shrp	X[ 6]=X[ 6],X[ 5],40
339194206Ssimon	shrp	X[ 5]=X[ 5],X[ 4],40	}
340194206Ssimon{ .mii;	shrp	X[ 4]=X[ 4],X[ 3],40
341194206Ssimon	shrp	X[ 3]=X[ 3],X[ 2],40	}
342194206Ssimon{ .mii;	shrp	X[ 2]=X[ 2],X[ 1],40
343194206Ssimon	shrp	X[ 1]=X[ 1],X[ 0],40	}
344194206Ssimon{ .mib;	shrp	X[ 0]=X[ 0],T1,40
345194206Ssimon	br.many	.L_first16		};;
346194206Ssimon.L4byte:
347194206Ssimon{ .mmi;	$LDW	X[ 7]=[input],4*$SZ
348194206Ssimon	$LDW	X[ 6]=[r8],4*$SZ
349194206Ssimon	shrp	X[15]=X[15],X[14],32	}
350194206Ssimon{ .mmi;	$LDW	X[ 5]=[r9],4*$SZ
351194206Ssimon	$LDW	X[ 4]=[r10],4*$SZ
352194206Ssimon	shrp	X[14]=X[14],X[13],32	};;
353194206Ssimon{ .mmi;	$LDW	X[ 3]=[input],4*$SZ
354194206Ssimon	$LDW	X[ 2]=[r8],4*$SZ
355194206Ssimon	shrp	X[13]=X[13],X[12],32	}
356194206Ssimon{ .mmi;	$LDW	X[ 1]=[r9],4*$SZ
357194206Ssimon	$LDW	X[ 0]=[r10],4*$SZ
358194206Ssimon	shrp	X[12]=X[12],X[11],32	};;
359194206Ssimon{ .mii;	$LDW	T1=[input]
360194206Ssimon	shrp	X[11]=X[11],X[10],32
361194206Ssimon	shrp	X[10]=X[10],X[ 9],32	}
362194206Ssimon{ .mii;	shrp	X[ 9]=X[ 9],X[ 8],32
363194206Ssimon	shrp	X[ 8]=X[ 8],X[ 7],32	};;
364194206Ssimon{ .mii;	shrp	X[ 7]=X[ 7],X[ 6],32
365194206Ssimon	shrp	X[ 6]=X[ 6],X[ 5],32	}
366194206Ssimon{ .mii;	shrp	X[ 5]=X[ 5],X[ 4],32
367194206Ssimon	shrp	X[ 4]=X[ 4],X[ 3],32	}
368194206Ssimon{ .mii;	shrp	X[ 3]=X[ 3],X[ 2],32
369194206Ssimon	shrp	X[ 2]=X[ 2],X[ 1],32	}
370194206Ssimon{ .mii;	shrp	X[ 1]=X[ 1],X[ 0],32
371194206Ssimon	shrp	X[ 0]=X[ 0],T1,32	}
372194206Ssimon{ .mfb;	br.many	.L_first16		};;
373194206Ssimon.L5byte:
374194206Ssimon{ .mmi;	$LDW	X[ 5]=[r9],4*$SZ
375194206Ssimon	$LDW	X[ 4]=[r10],4*$SZ
376194206Ssimon	shrp	X[15]=X[15],X[14],24	};;
377194206Ssimon{ .mmi;	$LDW	X[ 3]=[input],4*$SZ
378194206Ssimon	$LDW	X[ 2]=[r8],4*$SZ
379194206Ssimon	shrp	X[14]=X[14],X[13],24	}
380194206Ssimon{ .mmi;	$LDW	X[ 1]=[r9],4*$SZ
381194206Ssimon	$LDW	X[ 0]=[r10],4*$SZ
382194206Ssimon	shrp	X[13]=X[13],X[12],24	};;
383194206Ssimon{ .mii;	$LDW	T1=[input]
384194206Ssimon	shrp	X[12]=X[12],X[11],24
385194206Ssimon	shrp	X[11]=X[11],X[10],24	}
386194206Ssimon{ .mii;	shrp	X[10]=X[10],X[ 9],24
387194206Ssimon	shrp	X[ 9]=X[ 9],X[ 8],24	};;
388194206Ssimon{ .mii;	shrp	X[ 8]=X[ 8],X[ 7],24
389194206Ssimon	shrp	X[ 7]=X[ 7],X[ 6],24	}
390194206Ssimon{ .mii;	shrp	X[ 6]=X[ 6],X[ 5],24
391194206Ssimon	shrp	X[ 5]=X[ 5],X[ 4],24	}
392194206Ssimon{ .mii;	shrp	X[ 4]=X[ 4],X[ 3],24
393194206Ssimon	shrp	X[ 3]=X[ 3],X[ 2],24	}
394194206Ssimon{ .mii;	shrp	X[ 2]=X[ 2],X[ 1],24
395194206Ssimon	shrp	X[ 1]=X[ 1],X[ 0],24	}
396194206Ssimon{ .mib;	shrp	X[ 0]=X[ 0],T1,24
397194206Ssimon	br.many	.L_first16		};;
398194206Ssimon.L6byte:
399194206Ssimon{ .mmi;	$LDW	X[ 3]=[input],4*$SZ
400194206Ssimon	$LDW	X[ 2]=[r8],4*$SZ
401194206Ssimon	shrp	X[15]=X[15],X[14],16	}
402194206Ssimon{ .mmi;	$LDW	X[ 1]=[r9],4*$SZ
403194206Ssimon	$LDW	X[ 0]=[r10],4*$SZ
404194206Ssimon	shrp	X[14]=X[14],X[13],16	};;
405194206Ssimon{ .mii;	$LDW	T1=[input]
406194206Ssimon	shrp	X[13]=X[13],X[12],16
407194206Ssimon	shrp	X[12]=X[12],X[11],16	}
408194206Ssimon{ .mii;	shrp	X[11]=X[11],X[10],16
409194206Ssimon	shrp	X[10]=X[10],X[ 9],16	};;
410194206Ssimon{ .mii;	shrp	X[ 9]=X[ 9],X[ 8],16
411194206Ssimon	shrp	X[ 8]=X[ 8],X[ 7],16	}
412194206Ssimon{ .mii;	shrp	X[ 7]=X[ 7],X[ 6],16
413194206Ssimon	shrp	X[ 6]=X[ 6],X[ 5],16	}
414194206Ssimon{ .mii;	shrp	X[ 5]=X[ 5],X[ 4],16
415194206Ssimon	shrp	X[ 4]=X[ 4],X[ 3],16	}
416194206Ssimon{ .mii;	shrp	X[ 3]=X[ 3],X[ 2],16
417194206Ssimon	shrp	X[ 2]=X[ 2],X[ 1],16	}
418194206Ssimon{ .mii;	shrp	X[ 1]=X[ 1],X[ 0],16
419194206Ssimon	shrp	X[ 0]=X[ 0],T1,16	}
420194206Ssimon{ .mfb;	br.many	.L_first16		};;
421194206Ssimon.L7byte:
422194206Ssimon{ .mmi;	$LDW	X[ 1]=[r9],4*$SZ
423194206Ssimon	$LDW	X[ 0]=[r10],4*$SZ
424194206Ssimon	shrp	X[15]=X[15],X[14],8	};;
425194206Ssimon{ .mii;	$LDW	T1=[input]
426194206Ssimon	shrp	X[14]=X[14],X[13],8
427194206Ssimon	shrp	X[13]=X[13],X[12],8	}
428194206Ssimon{ .mii;	shrp	X[12]=X[12],X[11],8
429194206Ssimon	shrp	X[11]=X[11],X[10],8	};;
430194206Ssimon{ .mii;	shrp	X[10]=X[10],X[ 9],8
431194206Ssimon	shrp	X[ 9]=X[ 9],X[ 8],8	}
432194206Ssimon{ .mii;	shrp	X[ 8]=X[ 8],X[ 7],8
433194206Ssimon	shrp	X[ 7]=X[ 7],X[ 6],8	}
434194206Ssimon{ .mii;	shrp	X[ 6]=X[ 6],X[ 5],8
435194206Ssimon	shrp	X[ 5]=X[ 5],X[ 4],8	}
436194206Ssimon{ .mii;	shrp	X[ 4]=X[ 4],X[ 3],8
437194206Ssimon	shrp	X[ 3]=X[ 3],X[ 2],8	}
438194206Ssimon{ .mii;	shrp	X[ 2]=X[ 2],X[ 1],8
439194206Ssimon	shrp	X[ 1]=X[ 1],X[ 0],8	}
440194206Ssimon{ .mib;	shrp	X[ 0]=X[ 0],T1,8
441194206Ssimon	br.many	.L_first16		};;
442194206Ssimon
443194206Ssimon.align	32
444194206Ssimon.L_first16:
445194206Ssimon{ .mmi;		$LDW	K=[Ktbl],$SZ
446160814Ssimon		and	T1=F,E
447160814Ssimon		and	T2=A,B		}
448194206Ssimon{ .mmi;		//$LDW	X[15]=[input],$SZ	// X[i]=*input++
449160814Ssimon		andcm	r8=G,E
450160814Ssimon		and	r9=A,C		};;
451160814Ssimon{ .mmi;		xor	T1=T1,r8		//T1=((e & f) ^ (~e & g))
452160814Ssimon		and	r10=B,C
453160814Ssimon		_rotr	r11=$t1,$Sigma1[0] }	// ROTR(e,14)
454160814Ssimon{ .mmi;		xor	T2=T2,r9
455160814Ssimon		mux1	X[15]=X[15],\@rev };;	// eliminated in big-endian
456160814Ssimon___
457160814Ssimon$code.=<<___;
458160814Ssimon{ .mib;		add	T1=T1,H			// T1=Ch(e,f,g)+h
459160814Ssimon		_rotr	r8=$t1,$Sigma1[1] }	// ROTR(e,18)
460160814Ssimon{ .mib;		xor	T2=T2,r10		// T2=((a & b) ^ (a & c) ^ (b & c))
461160814Ssimon		mov	H=G		};;
462160814Ssimon{ .mib;		xor	r11=r8,r11
463160814Ssimon		_rotr	r9=$t1,$Sigma1[2] }	// ROTR(e,41)
464160814Ssimon{ .mib;		mov	G=F
465160814Ssimon		mov	F=E		};;
466160814Ssimon{ .mib;		xor	r9=r9,r11		// r9=Sigma1(e)
467160814Ssimon		_rotr	r10=$t0,$Sigma0[0] }	// ROTR(a,28)
468160814Ssimon{ .mib;		add	T1=T1,K			// T1=Ch(e,f,g)+h+K512[i]
469160814Ssimon		mov	E=D		};;
470160814Ssimon{ .mib;		add	T1=T1,r9		// T1+=Sigma1(e)
471160814Ssimon		_rotr	r11=$t0,$Sigma0[1] }	// ROTR(a,34)
472160814Ssimon{ .mib;		mov	D=C
473160814Ssimon		mov	C=B		};;
474160814Ssimon{ .mib;		add	T1=T1,X[15]		// T1+=X[i]
475160814Ssimon		_rotr	r8=$t0,$Sigma0[2] }	// ROTR(a,39)
476160814Ssimon{ .mib;		xor	r10=r10,r11
477160814Ssimon		mux2	X[15]=X[15],0x44 };;	// eliminated in 64-bit
478160814Ssimon{ .mmi;		xor	r10=r8,r10		// r10=Sigma0(a)
479160814Ssimon		mov	B=A
480160814Ssimon		add	A=T1,T2		};;
481160814Ssimon{ .mib;		add	E=E,T1
482160814Ssimon		add	A=A,r10			// T2=Maj(a,b,c)+Sigma0(a)
483160814Ssimon	br.ctop.sptk	.L_first16	};;
484194206Ssimon.L_first16_end:
485160814Ssimon
486194206Ssimon{ .mii;	mov	ar.lc=$rounds-17
487194206Ssimon	mov	ar.ec=1			};;
488194206Ssimon
489160814Ssimon.align	32
490160814Ssimon.L_rest:
491160814Ssimon.rotr	X[16]
492160814Ssimon{ .mib;		$LDW	K=[Ktbl],$SZ
493160814Ssimon		_rotr	r8=X[15-1],$sigma0[0] }	// ROTR(s0,1)
494160814Ssimon{ .mib; 	$ADD	X[15]=X[15],X[15-9]	// X[i&0xF]+=X[(i+9)&0xF]
495160814Ssimon		$SHRU	s0=X[15-1],sgm0	};;	// s0=X[(i+1)&0xF]>>7
496160814Ssimon{ .mib;		and	T1=F,E
497160814Ssimon		_rotr	r9=X[15-1],$sigma0[1] }	// ROTR(s0,8)
498160814Ssimon{ .mib;		andcm	r10=G,E
499160814Ssimon		$SHRU	s1=X[15-14],sgm1 };;	// s1=X[(i+14)&0xF]>>6
500160814Ssimon{ .mmi;		xor	T1=T1,r10		// T1=((e & f) ^ (~e & g))
501160814Ssimon		xor	r9=r8,r9
502160814Ssimon		_rotr	r10=X[15-14],$sigma1[0] };;// ROTR(s1,19)
503160814Ssimon{ .mib;		and	T2=A,B
504160814Ssimon		_rotr	r11=X[15-14],$sigma1[1] }// ROTR(s1,61)
505160814Ssimon{ .mib;		and	r8=A,C		};;
506160814Ssimon___
507160814Ssimon$t0="t0", $t1="t1", $code.=<<___ if ($BITS==32);
508160814Ssimon// I adhere to mmi; in order to hold Itanium 1 back and avoid 6 cycle
509160814Ssimon// pipeline flush in last bundle. Note that even on Itanium2 the
510160814Ssimon// latter stalls for one clock cycle...
511160814Ssimon{ .mmi;		xor	s0=s0,r9		// s0=sigma0(X[(i+1)&0xF])
512160814Ssimon		dep.z	$t1=E,32,32	}
513160814Ssimon{ .mmi;		xor	r10=r11,r10
514160814Ssimon		zxt4	E=E		};;
515160814Ssimon{ .mmi;		or	$t1=$t1,E
516160814Ssimon		xor	s1=s1,r10		// s1=sigma1(X[(i+14)&0xF])
517160814Ssimon		mux2	$t0=A,0x44	};;	// copy lower half to upper
518160814Ssimon{ .mmi;		xor	T2=T2,r8
519160814Ssimon		_rotr	r9=$t1,$Sigma1[0] }	// ROTR(e,14)
520160814Ssimon{ .mmi;		and	r10=B,C
521160814Ssimon		add	T1=T1,H			// T1=Ch(e,f,g)+h
522160814Ssimon		$ADD	X[15]=X[15],s0	};;	// X[i&0xF]+=sigma0(X[(i+1)&0xF])
523160814Ssimon___
524160814Ssimon$t0="A", $t1="E", $code.=<<___ if ($BITS==64);
525160814Ssimon{ .mib;		xor	s0=s0,r9		// s0=sigma0(X[(i+1)&0xF])
526160814Ssimon		_rotr	r9=$t1,$Sigma1[0] }	// ROTR(e,14)
527160814Ssimon{ .mib;		xor	r10=r11,r10
528160814Ssimon		xor	T2=T2,r8	};;
529160814Ssimon{ .mib;		xor	s1=s1,r10		// s1=sigma1(X[(i+14)&0xF])
530160814Ssimon		add	T1=T1,H		}
531160814Ssimon{ .mib;		and	r10=B,C
532160814Ssimon		$ADD	X[15]=X[15],s0	};;	// X[i&0xF]+=sigma0(X[(i+1)&0xF])
533160814Ssimon___
534160814Ssimon$code.=<<___;
535160814Ssimon{ .mmi;		xor	T2=T2,r10		// T2=((a & b) ^ (a & c) ^ (b & c))
536160814Ssimon		mov	H=G
537160814Ssimon		_rotr	r8=$t1,$Sigma1[1] };;	// ROTR(e,18)
538160814Ssimon{ .mmi;		xor	r11=r8,r9
539160814Ssimon		$ADD	X[15]=X[15],s1		// X[i&0xF]+=sigma1(X[(i+14)&0xF])
540160814Ssimon		_rotr	r9=$t1,$Sigma1[2] }	// ROTR(e,41)
541160814Ssimon{ .mmi;		mov	G=F
542160814Ssimon		mov	F=E		};;
543160814Ssimon{ .mib;		xor	r9=r9,r11		// r9=Sigma1(e)
544160814Ssimon		_rotr	r10=$t0,$Sigma0[0] }	// ROTR(a,28)
545160814Ssimon{ .mib;		add	T1=T1,K			// T1=Ch(e,f,g)+h+K512[i]
546160814Ssimon		mov	E=D		};;
547160814Ssimon{ .mib;		add	T1=T1,r9		// T1+=Sigma1(e)
548160814Ssimon		_rotr	r11=$t0,$Sigma0[1] }	// ROTR(a,34)
549160814Ssimon{ .mib;		mov	D=C
550160814Ssimon		mov	C=B		};;
551160814Ssimon{ .mmi;		add	T1=T1,X[15]		// T1+=X[i]
552160814Ssimon		xor	r10=r10,r11
553160814Ssimon		_rotr	r8=$t0,$Sigma0[2] };;	// ROTR(a,39)
554160814Ssimon{ .mmi;		xor	r10=r8,r10		// r10=Sigma0(a)
555160814Ssimon		mov	B=A
556160814Ssimon		add	A=T1,T2		};;
557160814Ssimon{ .mib;		add	E=E,T1
558160814Ssimon		add	A=A,r10			// T2=Maj(a,b,c)+Sigma0(a)
559160814Ssimon	br.ctop.sptk	.L_rest	};;
560194206Ssimon.L_rest_end:
561160814Ssimon
562194206Ssimon{ .mmi;	add	A_=A_,A
563194206Ssimon	add	B_=B_,B
564194206Ssimon	add	C_=C_,C			}
565194206Ssimon{ .mmi;	add	D_=D_,D
566194206Ssimon	add	E_=E_,E
567194206Ssimon	cmp.ltu	p16,p0=1,num		};;
568194206Ssimon{ .mmi;	add	F_=F_,F
569194206Ssimon	add	G_=G_,G
570194206Ssimon	add	H_=H_,H			}
571194206Ssimon{ .mmb;	add	Ktbl=-$SZ*$rounds,Ktbl
572194206Ssimon(p16)	add	num=-1,num
573194206Ssimon(p16)	br.dptk.many	.L_outer	};;
574194206Ssimon
575160814Ssimon{ .mib;	add	r8=0*$SZ,ctx
576160814Ssimon	add	r9=1*$SZ,ctx		}
577160814Ssimon{ .mib;	add	r10=2*$SZ,ctx
578160814Ssimon	add	r11=3*$SZ,ctx		};;
579194206Ssimon{ .mmi;	$STW	[r8]=A_,4*$SZ
580194206Ssimon	$STW	[r9]=B_,4*$SZ
581194206Ssimon	mov	ar.lc=lcsave		}
582194206Ssimon{ .mmi;	$STW	[r10]=C_,4*$SZ
583194206Ssimon	$STW	[r11]=D_,4*$SZ
584194206Ssimon	mov	pr=prsave,0x1ffff	};;
585194206Ssimon{ .mmb;	$STW	[r8]=E_
586194206Ssimon	$STW	[r9]=F_			}
587194206Ssimon{ .mmb;	$STW	[r10]=G_
588194206Ssimon	$STW	[r11]=H_
589160814Ssimon	br.ret.sptk.many	b0	};;
590160814Ssimon.endp	$func#
591160814Ssimon___
592160814Ssimon
593160814Ssimon$code =~ s/\`([^\`]*)\`/eval $1/gem;
594160814Ssimon$code =~ s/_rotr(\s+)([^=]+)=([^,]+),([0-9]+)/shrp$1$2=$3,$3,$4/gm;
595160814Ssimonif ($BITS==64) {
596160814Ssimon    $code =~ s/mux2(\s+)\S+/nop.i$1 0x0/gm;
597194206Ssimon    $code =~ s/mux1(\s+)\S+/nop.i$1 0x0/gm	if ($big_endian);
598194206Ssimon    $code =~ s/(shrp\s+X\[[^=]+)=([^,]+),([^,]+),([1-9]+)/$1=$3,$2,64-$4/gm
599194206Ssimon    						if (!$big_endian);
600194206Ssimon    $code =~ s/ld1(\s+)X\[\S+/nop.m$1 0x0/gm;
601160814Ssimon}
602160814Ssimon
603160814Ssimonprint $code;
604160814Ssimon
605160814Ssimonprint<<___ if ($BITS==32);
606160814Ssimon.align	64
607160814Ssimon.type	K256#,\@object
608160814SsimonK256:	data4	0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5
609160814Ssimon	data4	0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5
610160814Ssimon	data4	0xd807aa98,0x12835b01,0x243185be,0x550c7dc3
611160814Ssimon	data4	0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174
612160814Ssimon	data4	0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc
613160814Ssimon	data4	0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da
614160814Ssimon	data4	0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7
615160814Ssimon	data4	0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967
616160814Ssimon	data4	0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13
617160814Ssimon	data4	0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85
618160814Ssimon	data4	0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3
619160814Ssimon	data4	0xd192e819,0xd6990624,0xf40e3585,0x106aa070
620160814Ssimon	data4	0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5
621160814Ssimon	data4	0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3
622160814Ssimon	data4	0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208
623160814Ssimon	data4	0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
624160814Ssimon.size	K256#,$SZ*$rounds
625194206Ssimonstringz	"SHA256 block transform for IA64, CRYPTOGAMS by <appro\@openssl.org>"
626160814Ssimon___
627160814Ssimonprint<<___ if ($BITS==64);
628160814Ssimon.align	64
629160814Ssimon.type	K512#,\@object
630160814SsimonK512:	data8	0x428a2f98d728ae22,0x7137449123ef65cd
631160814Ssimon	data8	0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc
632160814Ssimon	data8	0x3956c25bf348b538,0x59f111f1b605d019
633160814Ssimon	data8	0x923f82a4af194f9b,0xab1c5ed5da6d8118
634160814Ssimon	data8	0xd807aa98a3030242,0x12835b0145706fbe
635160814Ssimon	data8	0x243185be4ee4b28c,0x550c7dc3d5ffb4e2
636160814Ssimon	data8	0x72be5d74f27b896f,0x80deb1fe3b1696b1
637160814Ssimon	data8	0x9bdc06a725c71235,0xc19bf174cf692694
638160814Ssimon	data8	0xe49b69c19ef14ad2,0xefbe4786384f25e3
639160814Ssimon	data8	0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65
640160814Ssimon	data8	0x2de92c6f592b0275,0x4a7484aa6ea6e483
641160814Ssimon	data8	0x5cb0a9dcbd41fbd4,0x76f988da831153b5
642160814Ssimon	data8	0x983e5152ee66dfab,0xa831c66d2db43210
643160814Ssimon	data8	0xb00327c898fb213f,0xbf597fc7beef0ee4
644160814Ssimon	data8	0xc6e00bf33da88fc2,0xd5a79147930aa725
645160814Ssimon	data8	0x06ca6351e003826f,0x142929670a0e6e70
646160814Ssimon	data8	0x27b70a8546d22ffc,0x2e1b21385c26c926
647160814Ssimon	data8	0x4d2c6dfc5ac42aed,0x53380d139d95b3df
648160814Ssimon	data8	0x650a73548baf63de,0x766a0abb3c77b2a8
649160814Ssimon	data8	0x81c2c92e47edaee6,0x92722c851482353b
650160814Ssimon	data8	0xa2bfe8a14cf10364,0xa81a664bbc423001
651160814Ssimon	data8	0xc24b8b70d0f89791,0xc76c51a30654be30
652160814Ssimon	data8	0xd192e819d6ef5218,0xd69906245565a910
653160814Ssimon	data8	0xf40e35855771202a,0x106aa07032bbd1b8
654160814Ssimon	data8	0x19a4c116b8d2d0c8,0x1e376c085141ab53
655160814Ssimon	data8	0x2748774cdf8eeb99,0x34b0bcb5e19b48a8
656160814Ssimon	data8	0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb
657160814Ssimon	data8	0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3
658160814Ssimon	data8	0x748f82ee5defb2fc,0x78a5636f43172f60
659160814Ssimon	data8	0x84c87814a1f0ab72,0x8cc702081a6439ec
660160814Ssimon	data8	0x90befffa23631e28,0xa4506cebde82bde9
661160814Ssimon	data8	0xbef9a3f7b2c67915,0xc67178f2e372532b
662160814Ssimon	data8	0xca273eceea26619c,0xd186b8c721c0c207
663160814Ssimon	data8	0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178
664160814Ssimon	data8	0x06f067aa72176fba,0x0a637dc5a2c898a6
665160814Ssimon	data8	0x113f9804bef90dae,0x1b710b35131c471b
666160814Ssimon	data8	0x28db77f523047d84,0x32caab7b40c72493
667160814Ssimon	data8	0x3c9ebe0a15c9bebc,0x431d67c49c100d4c
668160814Ssimon	data8	0x4cc5d4becb3e42b6,0x597f299cfc657e2a
669160814Ssimon	data8	0x5fcb6fab3ad6faec,0x6c44198c4a475817
670160814Ssimon.size	K512#,$SZ*$rounds
671194206Ssimonstringz	"SHA512 block transform for IA64, CRYPTOGAMS by <appro\@openssl.org>"
672160814Ssimon___
673