1*ebfedea0SLionel Sambuc#!/usr/bin/env perl 2*ebfedea0SLionel Sambuc# 3*ebfedea0SLionel Sambuc# ==================================================================== 4*ebfedea0SLionel Sambuc# Written by David Mosberger <David.Mosberger@acm.org> based on the 5*ebfedea0SLionel Sambuc# Itanium optimized Crypto code which was released by HP Labs at 6*ebfedea0SLionel Sambuc# http://www.hpl.hp.com/research/linux/crypto/. 7*ebfedea0SLionel Sambuc# 8*ebfedea0SLionel Sambuc# Copyright (c) 2005 Hewlett-Packard Development Company, L.P. 9*ebfedea0SLionel Sambuc# 10*ebfedea0SLionel Sambuc# Permission is hereby granted, free of charge, to any person obtaining 11*ebfedea0SLionel Sambuc# a copy of this software and associated documentation files (the 12*ebfedea0SLionel Sambuc# "Software"), to deal in the Software without restriction, including 13*ebfedea0SLionel Sambuc# without limitation the rights to use, copy, modify, merge, publish, 14*ebfedea0SLionel Sambuc# distribute, sublicense, and/or sell copies of the Software, and to 15*ebfedea0SLionel Sambuc# permit persons to whom the Software is furnished to do so, subject to 16*ebfedea0SLionel Sambuc# the following conditions: 17*ebfedea0SLionel Sambuc# 18*ebfedea0SLionel Sambuc# The above copyright notice and this permission notice shall be 19*ebfedea0SLionel Sambuc# included in all copies or substantial portions of the Software. 20*ebfedea0SLionel Sambuc 21*ebfedea0SLionel Sambuc# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22*ebfedea0SLionel Sambuc# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23*ebfedea0SLionel Sambuc# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24*ebfedea0SLionel Sambuc# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 25*ebfedea0SLionel Sambuc# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26*ebfedea0SLionel Sambuc# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 27*ebfedea0SLionel Sambuc# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ 28*ebfedea0SLionel Sambuc 29*ebfedea0SLionel Sambuc 30*ebfedea0SLionel Sambuc 31*ebfedea0SLionel Sambuc# This is a little helper program which generates a software-pipelined 32*ebfedea0SLionel Sambuc# for RC4 encryption. The basic algorithm looks like this: 33*ebfedea0SLionel Sambuc# 34*ebfedea0SLionel Sambuc# for (counter = 0; counter < len; ++counter) 35*ebfedea0SLionel Sambuc# { 36*ebfedea0SLionel Sambuc# in = inp[counter]; 37*ebfedea0SLionel Sambuc# SI = S[I]; 38*ebfedea0SLionel Sambuc# J = (SI + J) & 0xff; 39*ebfedea0SLionel Sambuc# SJ = S[J]; 40*ebfedea0SLionel Sambuc# T = (SI + SJ) & 0xff; 41*ebfedea0SLionel Sambuc# S[I] = SJ, S[J] = SI; 42*ebfedea0SLionel Sambuc# ST = S[T]; 43*ebfedea0SLionel Sambuc# outp[counter] = in ^ ST; 44*ebfedea0SLionel Sambuc# I = (I + 1) & 0xff; 45*ebfedea0SLionel Sambuc# } 46*ebfedea0SLionel Sambuc# 47*ebfedea0SLionel Sambuc# Pipelining this loop isn't easy, because the stores to the S[] array 48*ebfedea0SLionel Sambuc# need to be observed in the right order. The loop generated by the 49*ebfedea0SLionel Sambuc# code below has the following pipeline diagram: 50*ebfedea0SLionel Sambuc# 51*ebfedea0SLionel Sambuc# cycle 52*ebfedea0SLionel Sambuc# | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |10 |11 |12 |13 |14 |15 |16 |17 | 53*ebfedea0SLionel Sambuc# iter 54*ebfedea0SLionel Sambuc# 1: xxx LDI xxx xxx xxx LDJ xxx SWP xxx LDT xxx xxx 55*ebfedea0SLionel Sambuc# 2: xxx LDI xxx xxx xxx LDJ xxx SWP xxx LDT xxx xxx 56*ebfedea0SLionel Sambuc# 3: xxx LDI xxx xxx xxx LDJ xxx SWP xxx LDT xxx xxx 57*ebfedea0SLionel Sambuc# 58*ebfedea0SLionel Sambuc# where: 59*ebfedea0SLionel Sambuc# LDI = load of S[I] 60*ebfedea0SLionel Sambuc# LDJ = load of S[J] 61*ebfedea0SLionel Sambuc# SWP = swap of S[I] and S[J] 62*ebfedea0SLionel Sambuc# LDT = load of S[T] 63*ebfedea0SLionel Sambuc# 64*ebfedea0SLionel Sambuc# Note that in the above diagram, the major trouble-spot is that LDI 65*ebfedea0SLionel Sambuc# of the 2nd iteration is performed BEFORE the SWP of the first 66*ebfedea0SLionel Sambuc# iteration. Fortunately, this is easy to detect (I of the 1st 67*ebfedea0SLionel Sambuc# iteration will be equal to J of the 2nd iteration) and when this 68*ebfedea0SLionel Sambuc# happens, we simply forward the proper value from the 1st iteration 69*ebfedea0SLionel Sambuc# to the 2nd one. The proper value in this case is simply the value 70*ebfedea0SLionel Sambuc# of S[I] from the first iteration (thanks to the fact that SWP 71*ebfedea0SLionel Sambuc# simply swaps the contents of S[I] and S[J]). 72*ebfedea0SLionel Sambuc# 73*ebfedea0SLionel Sambuc# Another potential trouble-spot is in cycle 7, where SWP of the 1st 74*ebfedea0SLionel Sambuc# iteration issues at the same time as the LDI of the 3rd iteration. 75*ebfedea0SLionel Sambuc# However, thanks to IA-64 execution semantics, this can be taken 76*ebfedea0SLionel Sambuc# care of simply by placing LDI later in the instruction-group than 77*ebfedea0SLionel Sambuc# SWP. IA-64 CPUs will automatically forward the value if they 78*ebfedea0SLionel Sambuc# detect that the SWP and LDI are accessing the same memory-location. 79*ebfedea0SLionel Sambuc 80*ebfedea0SLionel Sambuc# The core-loop that can be pipelined then looks like this (annotated 81*ebfedea0SLionel Sambuc# with McKinley/Madison issue port & latency numbers, assuming L1 82*ebfedea0SLionel Sambuc# cache hits for the most part): 83*ebfedea0SLionel Sambuc 84*ebfedea0SLionel Sambuc# operation: instruction: issue-ports: latency 85*ebfedea0SLionel Sambuc# ------------------ ----------------------------- ------------- ------- 86*ebfedea0SLionel Sambuc 87*ebfedea0SLionel Sambuc# Data = *inp++ ld1 data = [inp], 1 M0-M1 1 cyc c0 88*ebfedea0SLionel Sambuc# shladd Iptr = I, KeyTable, 3 M0-M3, I0, I1 1 cyc 89*ebfedea0SLionel Sambuc# I = (I + 1) & 0xff padd1 nextI = I, one M0-M3, I0, I1 3 cyc 90*ebfedea0SLionel Sambuc# ;; 91*ebfedea0SLionel Sambuc# SI = S[I] ld8 SI = [Iptr] M0-M1 1 cyc c1 * after SWAP! 92*ebfedea0SLionel Sambuc# ;; 93*ebfedea0SLionel Sambuc# cmp.eq.unc pBypass = I, J * after J is valid! 94*ebfedea0SLionel Sambuc# J = SI + J add J = J, SI M0-M3, I0, I1 1 cyc c2 95*ebfedea0SLionel Sambuc# (pBypass) br.cond.spnt Bypass 96*ebfedea0SLionel Sambuc# ;; 97*ebfedea0SLionel Sambuc# --------------------------------------------------------------------------------------- 98*ebfedea0SLionel Sambuc# J = J & 0xff zxt1 J = J I0, I1, 1 cyc c3 99*ebfedea0SLionel Sambuc# ;; 100*ebfedea0SLionel Sambuc# shladd Jptr = J, KeyTable, 3 M0-M3, I0, I1 1 cyc c4 101*ebfedea0SLionel Sambuc# ;; 102*ebfedea0SLionel Sambuc# SJ = S[J] ld8 SJ = [Jptr] M0-M1 1 cyc c5 103*ebfedea0SLionel Sambuc# ;; 104*ebfedea0SLionel Sambuc# --------------------------------------------------------------------------------------- 105*ebfedea0SLionel Sambuc# T = (SI + SJ) add T = SI, SJ M0-M3, I0, I1 1 cyc c6 106*ebfedea0SLionel Sambuc# ;; 107*ebfedea0SLionel Sambuc# T = T & 0xff zxt1 T = T I0, I1 1 cyc 108*ebfedea0SLionel Sambuc# S[I] = SJ st8 [Iptr] = SJ M2-M3 c7 109*ebfedea0SLionel Sambuc# S[J] = SI st8 [Jptr] = SI M2-M3 110*ebfedea0SLionel Sambuc# ;; 111*ebfedea0SLionel Sambuc# shladd Tptr = T, KeyTable, 3 M0-M3, I0, I1 1 cyc c8 112*ebfedea0SLionel Sambuc# ;; 113*ebfedea0SLionel Sambuc# --------------------------------------------------------------------------------------- 114*ebfedea0SLionel Sambuc# T = S[T] ld8 T = [Tptr] M0-M1 1 cyc c9 115*ebfedea0SLionel Sambuc# ;; 116*ebfedea0SLionel Sambuc# data ^= T xor data = data, T M0-M3, I0, I1 1 cyc c10 117*ebfedea0SLionel Sambuc# ;; 118*ebfedea0SLionel Sambuc# *out++ = Data ^ T dep word = word, data, 8, POS I0, I1 1 cyc c11 119*ebfedea0SLionel Sambuc# ;; 120*ebfedea0SLionel Sambuc# --------------------------------------------------------------------------------------- 121*ebfedea0SLionel Sambuc 122*ebfedea0SLionel Sambuc# There are several points worth making here: 123*ebfedea0SLionel Sambuc 124*ebfedea0SLionel Sambuc# - Note that due to the bypass/forwarding-path, the first two 125*ebfedea0SLionel Sambuc# phases of the loop are strangly mingled together. In 126*ebfedea0SLionel Sambuc# particular, note that the first stage of the pipeline is 127*ebfedea0SLionel Sambuc# using the value of "J", as calculated by the second stage. 128*ebfedea0SLionel Sambuc# - Each bundle-pair will have exactly 6 instructions. 129*ebfedea0SLionel Sambuc# - Pipelined, the loop can execute in 3 cycles/iteration and 130*ebfedea0SLionel Sambuc# 4 stages. However, McKinley/Madison can issue "st1" to 131*ebfedea0SLionel Sambuc# the same bank at a rate of at most one per 4 cycles. Thus, 132*ebfedea0SLionel Sambuc# instead of storing each byte, we accumulate them in a word 133*ebfedea0SLionel Sambuc# and then write them back at once with a single "st8" (this 134*ebfedea0SLionel Sambuc# implies that the setup code needs to ensure that the output 135*ebfedea0SLionel Sambuc# buffer is properly aligned, if need be, by encoding the 136*ebfedea0SLionel Sambuc# first few bytes separately). 137*ebfedea0SLionel Sambuc# - There is no space for a "br.ctop" instruction. For this 138*ebfedea0SLionel Sambuc# reason we can't use module-loop support in IA-64 and have 139*ebfedea0SLionel Sambuc# to do a traditional, purely software-pipelined loop. 140*ebfedea0SLionel Sambuc# - We can't replace any of the remaining "add/zxt1" pairs with 141*ebfedea0SLionel Sambuc# "padd1" because the latency for that instruction is too high 142*ebfedea0SLionel Sambuc# and would push the loop to the point where more bypasses 143*ebfedea0SLionel Sambuc# would be needed, which we don't have space for. 144*ebfedea0SLionel Sambuc# - The above loop runs at around 3.26 cycles/byte, or roughly 145*ebfedea0SLionel Sambuc# 440 MByte/sec on a 1.5GHz Madison. This is well below the 146*ebfedea0SLionel Sambuc# system bus bandwidth and hence with judicious use of 147*ebfedea0SLionel Sambuc# "lfetch" this loop can run at (almost) peak speed even when 148*ebfedea0SLionel Sambuc# the input and output data reside in memory. The 149*ebfedea0SLionel Sambuc# max. latency that can be tolerated is (PREFETCH_DISTANCE * 150*ebfedea0SLionel Sambuc# L2_LINE_SIZE * 3 cyc), or about 384 cycles assuming (at 151*ebfedea0SLionel Sambuc# least) 1-ahead prefetching of 128 byte cache-lines. Note 152*ebfedea0SLionel Sambuc# that we do NOT prefetch into L1, since that would only 153*ebfedea0SLionel Sambuc# interfere with the S[] table values stored there. This is 154*ebfedea0SLionel Sambuc# acceptable because there is a 10 cycle latency between 155*ebfedea0SLionel Sambuc# load and first use of the input data. 156*ebfedea0SLionel Sambuc# - We use a branch to out-of-line bypass-code of cycle-pressure: 157*ebfedea0SLionel Sambuc# we calculate the next J, check for the need to activate the 158*ebfedea0SLionel Sambuc# bypass path, and activate the bypass path ALL IN THE SAME 159*ebfedea0SLionel Sambuc# CYCLE. If we didn't have these constraints, we could do 160*ebfedea0SLionel Sambuc# the bypass with a simple conditional move instruction. 161*ebfedea0SLionel Sambuc# Fortunately, the bypass paths get activated relatively 162*ebfedea0SLionel Sambuc# infrequently, so the extra branches don't cost all that much 163*ebfedea0SLionel Sambuc# (about 0.04 cycles/byte, measured on a 16396 byte file with 164*ebfedea0SLionel Sambuc# random input data). 165*ebfedea0SLionel Sambuc# 166*ebfedea0SLionel Sambuc 167*ebfedea0SLionel Sambuc$phases = 4; # number of stages/phases in the pipelined-loop 168*ebfedea0SLionel Sambuc$unroll_count = 6; # number of times we unrolled it 169*ebfedea0SLionel Sambuc$pComI = (1 << 0); 170*ebfedea0SLionel Sambuc$pComJ = (1 << 1); 171*ebfedea0SLionel Sambuc$pComT = (1 << 2); 172*ebfedea0SLionel Sambuc$pOut = (1 << 3); 173*ebfedea0SLionel Sambuc 174*ebfedea0SLionel Sambuc$NData = 4; 175*ebfedea0SLionel Sambuc$NIP = 3; 176*ebfedea0SLionel Sambuc$NJP = 2; 177*ebfedea0SLionel Sambuc$NI = 2; 178*ebfedea0SLionel Sambuc$NSI = 3; 179*ebfedea0SLionel Sambuc$NSJ = 2; 180*ebfedea0SLionel Sambuc$NT = 2; 181*ebfedea0SLionel Sambuc$NOutWord = 2; 182*ebfedea0SLionel Sambuc 183*ebfedea0SLionel Sambuc# 184*ebfedea0SLionel Sambuc# $threshold is the minimum length before we attempt to use the 185*ebfedea0SLionel Sambuc# big software-pipelined loop. It MUST be greater-or-equal 186*ebfedea0SLionel Sambuc# to: 187*ebfedea0SLionel Sambuc# PHASES * (UNROLL_COUNT + 1) + 7 188*ebfedea0SLionel Sambuc# 189*ebfedea0SLionel Sambuc# The "+ 7" comes from the fact we may have to encode up to 190*ebfedea0SLionel Sambuc# 7 bytes separately before the output pointer is aligned. 191*ebfedea0SLionel Sambuc# 192*ebfedea0SLionel Sambuc$threshold = (3 * ($phases * ($unroll_count + 1)) + 7); 193*ebfedea0SLionel Sambuc 194*ebfedea0SLionel Sambucsub I { 195*ebfedea0SLionel Sambuc local *code = shift; 196*ebfedea0SLionel Sambuc local $format = shift; 197*ebfedea0SLionel Sambuc $code .= sprintf ("\t\t".$format."\n", @_); 198*ebfedea0SLionel Sambuc} 199*ebfedea0SLionel Sambuc 200*ebfedea0SLionel Sambucsub P { 201*ebfedea0SLionel Sambuc local *code = shift; 202*ebfedea0SLionel Sambuc local $format = shift; 203*ebfedea0SLionel Sambuc $code .= sprintf ($format."\n", @_); 204*ebfedea0SLionel Sambuc} 205*ebfedea0SLionel Sambuc 206*ebfedea0SLionel Sambucsub STOP { 207*ebfedea0SLionel Sambuc local *code = shift; 208*ebfedea0SLionel Sambuc $code .=<<___; 209*ebfedea0SLionel Sambuc ;; 210*ebfedea0SLionel Sambuc___ 211*ebfedea0SLionel Sambuc} 212*ebfedea0SLionel Sambuc 213*ebfedea0SLionel Sambucsub emit_body { 214*ebfedea0SLionel Sambuc local *c = shift; 215*ebfedea0SLionel Sambuc local *bypass = shift; 216*ebfedea0SLionel Sambuc local ($iteration, $p) = @_; 217*ebfedea0SLionel Sambuc 218*ebfedea0SLionel Sambuc local $i0 = $iteration; 219*ebfedea0SLionel Sambuc local $i1 = $iteration - 1; 220*ebfedea0SLionel Sambuc local $i2 = $iteration - 2; 221*ebfedea0SLionel Sambuc local $i3 = $iteration - 3; 222*ebfedea0SLionel Sambuc local $iw0 = ($iteration - 3) / 8; 223*ebfedea0SLionel Sambuc local $iw1 = ($iteration > 3) ? ($iteration - 4) / 8 : 1; 224*ebfedea0SLionel Sambuc local $byte_num = ($iteration - 3) % 8; 225*ebfedea0SLionel Sambuc local $label = $iteration + 1; 226*ebfedea0SLionel Sambuc local $pAny = ($p & 0xf) == 0xf; 227*ebfedea0SLionel Sambuc local $pByp = (($p & $pComI) && ($iteration > 0)); 228*ebfedea0SLionel Sambuc 229*ebfedea0SLionel Sambuc $c.=<<___; 230*ebfedea0SLionel Sambuc////////////////////////////////////////////////// 231*ebfedea0SLionel Sambuc___ 232*ebfedea0SLionel Sambuc 233*ebfedea0SLionel Sambuc if (($p & 0xf) == 0) { 234*ebfedea0SLionel Sambuc $c.="#ifdef HOST_IS_BIG_ENDIAN\n"; 235*ebfedea0SLionel Sambuc &I(\$c,"shr.u OutWord[%u] = OutWord[%u], 32;;", 236*ebfedea0SLionel Sambuc $iw1 % $NOutWord, $iw1 % $NOutWord); 237*ebfedea0SLionel Sambuc $c.="#endif\n"; 238*ebfedea0SLionel Sambuc &I(\$c, "st4 [OutPtr] = OutWord[%u], 4", $iw1 % $NOutWord); 239*ebfedea0SLionel Sambuc return; 240*ebfedea0SLionel Sambuc } 241*ebfedea0SLionel Sambuc 242*ebfedea0SLionel Sambuc # Cycle 0 243*ebfedea0SLionel Sambuc &I(\$c, "{ .mmi") if ($pAny); 244*ebfedea0SLionel Sambuc &I(\$c, "ld1 Data[%u] = [InPtr], 1", $i0 % $NData) if ($p & $pComI); 245*ebfedea0SLionel Sambuc &I(\$c, "padd1 I[%u] = One, I[%u]", $i0 % $NI, $i1 % $NI)if ($p & $pComI); 246*ebfedea0SLionel Sambuc &I(\$c, "zxt1 J = J") if ($p & $pComJ); 247*ebfedea0SLionel Sambuc &I(\$c, "}") if ($pAny); 248*ebfedea0SLionel Sambuc &I(\$c, "{ .mmi") if ($pAny); 249*ebfedea0SLionel Sambuc &I(\$c, "LKEY T[%u] = [T[%u]]", $i1 % $NT, $i1 % $NT) if ($p & $pOut); 250*ebfedea0SLionel Sambuc &I(\$c, "add T[%u] = SI[%u], SJ[%u]", 251*ebfedea0SLionel Sambuc $i0 % $NT, $i2 % $NSI, $i1 % $NSJ) if ($p & $pComT); 252*ebfedea0SLionel Sambuc &I(\$c, "KEYADDR(IPr[%u], I[%u])", $i0 % $NIP, $i1 % $NI) if ($p & $pComI); 253*ebfedea0SLionel Sambuc &I(\$c, "}") if ($pAny); 254*ebfedea0SLionel Sambuc &STOP(\$c); 255*ebfedea0SLionel Sambuc 256*ebfedea0SLionel Sambuc # Cycle 1 257*ebfedea0SLionel Sambuc &I(\$c, "{ .mmi") if ($pAny); 258*ebfedea0SLionel Sambuc &I(\$c, "SKEY [IPr[%u]] = SJ[%u]", $i2 % $NIP, $i1%$NSJ)if ($p & $pComT); 259*ebfedea0SLionel Sambuc &I(\$c, "SKEY [JP[%u]] = SI[%u]", $i1 % $NJP, $i2%$NSI) if ($p & $pComT); 260*ebfedea0SLionel Sambuc &I(\$c, "zxt1 T[%u] = T[%u]", $i0 % $NT, $i0 % $NT) if ($p & $pComT); 261*ebfedea0SLionel Sambuc &I(\$c, "}") if ($pAny); 262*ebfedea0SLionel Sambuc &I(\$c, "{ .mmi") if ($pAny); 263*ebfedea0SLionel Sambuc &I(\$c, "LKEY SI[%u] = [IPr[%u]]", $i0 % $NSI, $i0%$NIP)if ($p & $pComI); 264*ebfedea0SLionel Sambuc &I(\$c, "KEYADDR(JP[%u], J)", $i0 % $NJP) if ($p & $pComJ); 265*ebfedea0SLionel Sambuc &I(\$c, "xor Data[%u] = Data[%u], T[%u]", 266*ebfedea0SLionel Sambuc $i3 % $NData, $i3 % $NData, $i1 % $NT) if ($p & $pOut); 267*ebfedea0SLionel Sambuc &I(\$c, "}") if ($pAny); 268*ebfedea0SLionel Sambuc &STOP(\$c); 269*ebfedea0SLionel Sambuc 270*ebfedea0SLionel Sambuc # Cycle 2 271*ebfedea0SLionel Sambuc &I(\$c, "{ .mmi") if ($pAny); 272*ebfedea0SLionel Sambuc &I(\$c, "LKEY SJ[%u] = [JP[%u]]", $i0 % $NSJ, $i0%$NJP) if ($p & $pComJ); 273*ebfedea0SLionel Sambuc &I(\$c, "cmp.eq pBypass, p0 = I[%u], J", $i1 % $NI) if ($pByp); 274*ebfedea0SLionel Sambuc &I(\$c, "dep OutWord[%u] = Data[%u], OutWord[%u], BYTE_POS(%u), 8", 275*ebfedea0SLionel Sambuc $iw0%$NOutWord, $i3%$NData, $iw1%$NOutWord, $byte_num) if ($p & $pOut); 276*ebfedea0SLionel Sambuc &I(\$c, "}") if ($pAny); 277*ebfedea0SLionel Sambuc &I(\$c, "{ .mmb") if ($pAny); 278*ebfedea0SLionel Sambuc &I(\$c, "add J = J, SI[%u]", $i0 % $NSI) if ($p & $pComI); 279*ebfedea0SLionel Sambuc &I(\$c, "KEYADDR(T[%u], T[%u])", $i0 % $NT, $i0 % $NT) if ($p & $pComT); 280*ebfedea0SLionel Sambuc &P(\$c, "(pBypass)\tbr.cond.spnt.many .rc4Bypass%u",$label)if ($pByp); 281*ebfedea0SLionel Sambuc &I(\$c, "}") if ($pAny); 282*ebfedea0SLionel Sambuc &STOP(\$c); 283*ebfedea0SLionel Sambuc 284*ebfedea0SLionel Sambuc &P(\$c, ".rc4Resume%u:", $label) if ($pByp); 285*ebfedea0SLionel Sambuc if ($byte_num == 0 && $iteration >= $phases) { 286*ebfedea0SLionel Sambuc &I(\$c, "st8 [OutPtr] = OutWord[%u], 8", 287*ebfedea0SLionel Sambuc $iw1 % $NOutWord) if ($p & $pOut); 288*ebfedea0SLionel Sambuc if ($iteration == (1 + $unroll_count) * $phases - 1) { 289*ebfedea0SLionel Sambuc if ($unroll_count == 6) { 290*ebfedea0SLionel Sambuc &I(\$c, "mov OutWord[%u] = OutWord[%u]", 291*ebfedea0SLionel Sambuc $iw1 % $NOutWord, $iw0 % $NOutWord); 292*ebfedea0SLionel Sambuc } 293*ebfedea0SLionel Sambuc &I(\$c, "lfetch.nt1 [InPrefetch], %u", 294*ebfedea0SLionel Sambuc $unroll_count * $phases); 295*ebfedea0SLionel Sambuc &I(\$c, "lfetch.excl.nt1 [OutPrefetch], %u", 296*ebfedea0SLionel Sambuc $unroll_count * $phases); 297*ebfedea0SLionel Sambuc &I(\$c, "br.cloop.sptk.few .rc4Loop"); 298*ebfedea0SLionel Sambuc } 299*ebfedea0SLionel Sambuc } 300*ebfedea0SLionel Sambuc 301*ebfedea0SLionel Sambuc if ($pByp) { 302*ebfedea0SLionel Sambuc &P(\$bypass, ".rc4Bypass%u:", $label); 303*ebfedea0SLionel Sambuc &I(\$bypass, "sub J = J, SI[%u]", $i0 % $NSI); 304*ebfedea0SLionel Sambuc &I(\$bypass, "nop 0"); 305*ebfedea0SLionel Sambuc &I(\$bypass, "nop 0"); 306*ebfedea0SLionel Sambuc &I(\$bypass, ";;"); 307*ebfedea0SLionel Sambuc &I(\$bypass, "add J = J, SI[%u]", $i1 % $NSI); 308*ebfedea0SLionel Sambuc &I(\$bypass, "mov SI[%u] = SI[%u]", $i0 % $NSI, $i1 % $NSI); 309*ebfedea0SLionel Sambuc &I(\$bypass, "br.sptk.many .rc4Resume%u\n", $label); 310*ebfedea0SLionel Sambuc &I(\$bypass, ";;"); 311*ebfedea0SLionel Sambuc } 312*ebfedea0SLionel Sambuc} 313*ebfedea0SLionel Sambuc 314*ebfedea0SLionel Sambuc$code=<<___; 315*ebfedea0SLionel Sambuc.ident \"rc4-ia64.s, version 3.0\" 316*ebfedea0SLionel Sambuc.ident \"Copyright (c) 2005 Hewlett-Packard Development Company, L.P.\" 317*ebfedea0SLionel Sambuc 318*ebfedea0SLionel Sambuc#define LCSave r8 319*ebfedea0SLionel Sambuc#define PRSave r9 320*ebfedea0SLionel Sambuc 321*ebfedea0SLionel Sambuc/* Inputs become invalid once rotation begins! */ 322*ebfedea0SLionel Sambuc 323*ebfedea0SLionel Sambuc#define StateTable in0 324*ebfedea0SLionel Sambuc#define DataLen in1 325*ebfedea0SLionel Sambuc#define InputBuffer in2 326*ebfedea0SLionel Sambuc#define OutputBuffer in3 327*ebfedea0SLionel Sambuc 328*ebfedea0SLionel Sambuc#define KTable r14 329*ebfedea0SLionel Sambuc#define J r15 330*ebfedea0SLionel Sambuc#define InPtr r16 331*ebfedea0SLionel Sambuc#define OutPtr r17 332*ebfedea0SLionel Sambuc#define InPrefetch r18 333*ebfedea0SLionel Sambuc#define OutPrefetch r19 334*ebfedea0SLionel Sambuc#define One r20 335*ebfedea0SLionel Sambuc#define LoopCount r21 336*ebfedea0SLionel Sambuc#define Remainder r22 337*ebfedea0SLionel Sambuc#define IFinal r23 338*ebfedea0SLionel Sambuc#define EndPtr r24 339*ebfedea0SLionel Sambuc 340*ebfedea0SLionel Sambuc#define tmp0 r25 341*ebfedea0SLionel Sambuc#define tmp1 r26 342*ebfedea0SLionel Sambuc 343*ebfedea0SLionel Sambuc#define pBypass p6 344*ebfedea0SLionel Sambuc#define pDone p7 345*ebfedea0SLionel Sambuc#define pSmall p8 346*ebfedea0SLionel Sambuc#define pAligned p9 347*ebfedea0SLionel Sambuc#define pUnaligned p10 348*ebfedea0SLionel Sambuc 349*ebfedea0SLionel Sambuc#define pComputeI pPhase[0] 350*ebfedea0SLionel Sambuc#define pComputeJ pPhase[1] 351*ebfedea0SLionel Sambuc#define pComputeT pPhase[2] 352*ebfedea0SLionel Sambuc#define pOutput pPhase[3] 353*ebfedea0SLionel Sambuc 354*ebfedea0SLionel Sambuc#define RetVal r8 355*ebfedea0SLionel Sambuc#define L_OK p7 356*ebfedea0SLionel Sambuc#define L_NOK p8 357*ebfedea0SLionel Sambuc 358*ebfedea0SLionel Sambuc#define _NINPUTS 4 359*ebfedea0SLionel Sambuc#define _NOUTPUT 0 360*ebfedea0SLionel Sambuc 361*ebfedea0SLionel Sambuc#define _NROTATE 24 362*ebfedea0SLionel Sambuc#define _NLOCALS (_NROTATE - _NINPUTS - _NOUTPUT) 363*ebfedea0SLionel Sambuc 364*ebfedea0SLionel Sambuc#ifndef SZ 365*ebfedea0SLionel Sambuc# define SZ 4 // this must be set to sizeof(RC4_INT) 366*ebfedea0SLionel Sambuc#endif 367*ebfedea0SLionel Sambuc 368*ebfedea0SLionel Sambuc#if SZ == 1 369*ebfedea0SLionel Sambuc# define LKEY ld1 370*ebfedea0SLionel Sambuc# define SKEY st1 371*ebfedea0SLionel Sambuc# define KEYADDR(dst, i) add dst = i, KTable 372*ebfedea0SLionel Sambuc#elif SZ == 2 373*ebfedea0SLionel Sambuc# define LKEY ld2 374*ebfedea0SLionel Sambuc# define SKEY st2 375*ebfedea0SLionel Sambuc# define KEYADDR(dst, i) shladd dst = i, 1, KTable 376*ebfedea0SLionel Sambuc#elif SZ == 4 377*ebfedea0SLionel Sambuc# define LKEY ld4 378*ebfedea0SLionel Sambuc# define SKEY st4 379*ebfedea0SLionel Sambuc# define KEYADDR(dst, i) shladd dst = i, 2, KTable 380*ebfedea0SLionel Sambuc#else 381*ebfedea0SLionel Sambuc# define LKEY ld8 382*ebfedea0SLionel Sambuc# define SKEY st8 383*ebfedea0SLionel Sambuc# define KEYADDR(dst, i) shladd dst = i, 3, KTable 384*ebfedea0SLionel Sambuc#endif 385*ebfedea0SLionel Sambuc 386*ebfedea0SLionel Sambuc#if defined(_HPUX_SOURCE) && !defined(_LP64) 387*ebfedea0SLionel Sambuc# define ADDP addp4 388*ebfedea0SLionel Sambuc#else 389*ebfedea0SLionel Sambuc# define ADDP add 390*ebfedea0SLionel Sambuc#endif 391*ebfedea0SLionel Sambuc 392*ebfedea0SLionel Sambuc/* Define a macro for the bit number of the n-th byte: */ 393*ebfedea0SLionel Sambuc 394*ebfedea0SLionel Sambuc#if defined(_HPUX_SOURCE) || defined(B_ENDIAN) 395*ebfedea0SLionel Sambuc# define HOST_IS_BIG_ENDIAN 396*ebfedea0SLionel Sambuc# define BYTE_POS(n) (56 - (8 * (n))) 397*ebfedea0SLionel Sambuc#else 398*ebfedea0SLionel Sambuc# define BYTE_POS(n) (8 * (n)) 399*ebfedea0SLionel Sambuc#endif 400*ebfedea0SLionel Sambuc 401*ebfedea0SLionel Sambuc/* 402*ebfedea0SLionel Sambuc We must perform the first phase of the pipeline explicitly since 403*ebfedea0SLionel Sambuc we will always load from the stable the first time. The br.cexit 404*ebfedea0SLionel Sambuc will never be taken since regardless of the number of bytes because 405*ebfedea0SLionel Sambuc the epilogue count is 4. 406*ebfedea0SLionel Sambuc*/ 407*ebfedea0SLionel Sambuc/* MODSCHED_RC4 macro was split to _PROLOGUE and _LOOP, because HP-UX 408*ebfedea0SLionel Sambuc assembler failed on original macro with syntax error. <appro> */ 409*ebfedea0SLionel Sambuc#define MODSCHED_RC4_PROLOGUE \\ 410*ebfedea0SLionel Sambuc { \\ 411*ebfedea0SLionel Sambuc ld1 Data[0] = [InPtr], 1; \\ 412*ebfedea0SLionel Sambuc add IFinal = 1, I[1]; \\ 413*ebfedea0SLionel Sambuc KEYADDR(IPr[0], I[1]); \\ 414*ebfedea0SLionel Sambuc } ;; \\ 415*ebfedea0SLionel Sambuc { \\ 416*ebfedea0SLionel Sambuc LKEY SI[0] = [IPr[0]]; \\ 417*ebfedea0SLionel Sambuc mov pr.rot = 0x10000; \\ 418*ebfedea0SLionel Sambuc mov ar.ec = 4; \\ 419*ebfedea0SLionel Sambuc } ;; \\ 420*ebfedea0SLionel Sambuc { \\ 421*ebfedea0SLionel Sambuc add J = J, SI[0]; \\ 422*ebfedea0SLionel Sambuc zxt1 I[0] = IFinal; \\ 423*ebfedea0SLionel Sambuc br.cexit.spnt.few .+16; /* never taken */ \\ 424*ebfedea0SLionel Sambuc } ;; 425*ebfedea0SLionel Sambuc#define MODSCHED_RC4_LOOP(label) \\ 426*ebfedea0SLionel Sambuclabel: \\ 427*ebfedea0SLionel Sambuc { .mmi; \\ 428*ebfedea0SLionel Sambuc (pComputeI) ld1 Data[0] = [InPtr], 1; \\ 429*ebfedea0SLionel Sambuc (pComputeI) add IFinal = 1, I[1]; \\ 430*ebfedea0SLionel Sambuc (pComputeJ) zxt1 J = J; \\ 431*ebfedea0SLionel Sambuc }{ .mmi; \\ 432*ebfedea0SLionel Sambuc (pOutput) LKEY T[1] = [T[1]]; \\ 433*ebfedea0SLionel Sambuc (pComputeT) add T[0] = SI[2], SJ[1]; \\ 434*ebfedea0SLionel Sambuc (pComputeI) KEYADDR(IPr[0], I[1]); \\ 435*ebfedea0SLionel Sambuc } ;; \\ 436*ebfedea0SLionel Sambuc { .mmi; \\ 437*ebfedea0SLionel Sambuc (pComputeT) SKEY [IPr[2]] = SJ[1]; \\ 438*ebfedea0SLionel Sambuc (pComputeT) SKEY [JP[1]] = SI[2]; \\ 439*ebfedea0SLionel Sambuc (pComputeT) zxt1 T[0] = T[0]; \\ 440*ebfedea0SLionel Sambuc }{ .mmi; \\ 441*ebfedea0SLionel Sambuc (pComputeI) LKEY SI[0] = [IPr[0]]; \\ 442*ebfedea0SLionel Sambuc (pComputeJ) KEYADDR(JP[0], J); \\ 443*ebfedea0SLionel Sambuc (pComputeI) cmp.eq.unc pBypass, p0 = I[1], J; \\ 444*ebfedea0SLionel Sambuc } ;; \\ 445*ebfedea0SLionel Sambuc { .mmi; \\ 446*ebfedea0SLionel Sambuc (pComputeJ) LKEY SJ[0] = [JP[0]]; \\ 447*ebfedea0SLionel Sambuc (pOutput) xor Data[3] = Data[3], T[1]; \\ 448*ebfedea0SLionel Sambuc nop 0x0; \\ 449*ebfedea0SLionel Sambuc }{ .mmi; \\ 450*ebfedea0SLionel Sambuc (pComputeT) KEYADDR(T[0], T[0]); \\ 451*ebfedea0SLionel Sambuc (pBypass) mov SI[0] = SI[1]; \\ 452*ebfedea0SLionel Sambuc (pComputeI) zxt1 I[0] = IFinal; \\ 453*ebfedea0SLionel Sambuc } ;; \\ 454*ebfedea0SLionel Sambuc { .mmb; \\ 455*ebfedea0SLionel Sambuc (pOutput) st1 [OutPtr] = Data[3], 1; \\ 456*ebfedea0SLionel Sambuc (pComputeI) add J = J, SI[0]; \\ 457*ebfedea0SLionel Sambuc br.ctop.sptk.few label; \\ 458*ebfedea0SLionel Sambuc } ;; 459*ebfedea0SLionel Sambuc 460*ebfedea0SLionel Sambuc .text 461*ebfedea0SLionel Sambuc 462*ebfedea0SLionel Sambuc .align 32 463*ebfedea0SLionel Sambuc 464*ebfedea0SLionel Sambuc .type RC4, \@function 465*ebfedea0SLionel Sambuc .global RC4 466*ebfedea0SLionel Sambuc 467*ebfedea0SLionel Sambuc .proc RC4 468*ebfedea0SLionel Sambuc .prologue 469*ebfedea0SLionel Sambuc 470*ebfedea0SLionel SambucRC4: 471*ebfedea0SLionel Sambuc { 472*ebfedea0SLionel Sambuc .mmi 473*ebfedea0SLionel Sambuc alloc r2 = ar.pfs, _NINPUTS, _NLOCALS, _NOUTPUT, _NROTATE 474*ebfedea0SLionel Sambuc 475*ebfedea0SLionel Sambuc .rotr Data[4], I[2], IPr[3], SI[3], JP[2], SJ[2], T[2], \\ 476*ebfedea0SLionel Sambuc OutWord[2] 477*ebfedea0SLionel Sambuc .rotp pPhase[4] 478*ebfedea0SLionel Sambuc 479*ebfedea0SLionel Sambuc ADDP InPrefetch = 0, InputBuffer 480*ebfedea0SLionel Sambuc ADDP KTable = 0, StateTable 481*ebfedea0SLionel Sambuc } 482*ebfedea0SLionel Sambuc { 483*ebfedea0SLionel Sambuc .mmi 484*ebfedea0SLionel Sambuc ADDP InPtr = 0, InputBuffer 485*ebfedea0SLionel Sambuc ADDP OutPtr = 0, OutputBuffer 486*ebfedea0SLionel Sambuc mov RetVal = r0 487*ebfedea0SLionel Sambuc } 488*ebfedea0SLionel Sambuc ;; 489*ebfedea0SLionel Sambuc { 490*ebfedea0SLionel Sambuc .mmi 491*ebfedea0SLionel Sambuc lfetch.nt1 [InPrefetch], 0x80 492*ebfedea0SLionel Sambuc ADDP OutPrefetch = 0, OutputBuffer 493*ebfedea0SLionel Sambuc } 494*ebfedea0SLionel Sambuc { // Return 0 if the input length is nonsensical 495*ebfedea0SLionel Sambuc .mib 496*ebfedea0SLionel Sambuc ADDP StateTable = 0, StateTable 497*ebfedea0SLionel Sambuc cmp.ge.unc L_NOK, L_OK = r0, DataLen 498*ebfedea0SLionel Sambuc (L_NOK) br.ret.sptk.few rp 499*ebfedea0SLionel Sambuc } 500*ebfedea0SLionel Sambuc ;; 501*ebfedea0SLionel Sambuc { 502*ebfedea0SLionel Sambuc .mib 503*ebfedea0SLionel Sambuc cmp.eq.or L_NOK, L_OK = r0, InPtr 504*ebfedea0SLionel Sambuc cmp.eq.or L_NOK, L_OK = r0, OutPtr 505*ebfedea0SLionel Sambuc nop 0x0 506*ebfedea0SLionel Sambuc } 507*ebfedea0SLionel Sambuc { 508*ebfedea0SLionel Sambuc .mib 509*ebfedea0SLionel Sambuc cmp.eq.or L_NOK, L_OK = r0, StateTable 510*ebfedea0SLionel Sambuc nop 0x0 511*ebfedea0SLionel Sambuc (L_NOK) br.ret.sptk.few rp 512*ebfedea0SLionel Sambuc } 513*ebfedea0SLionel Sambuc ;; 514*ebfedea0SLionel Sambuc LKEY I[1] = [KTable], SZ 515*ebfedea0SLionel Sambuc/* Prefetch the state-table. It contains 256 elements of size SZ */ 516*ebfedea0SLionel Sambuc 517*ebfedea0SLionel Sambuc#if SZ == 1 518*ebfedea0SLionel Sambuc ADDP tmp0 = 1*128, StateTable 519*ebfedea0SLionel Sambuc#elif SZ == 2 520*ebfedea0SLionel Sambuc ADDP tmp0 = 3*128, StateTable 521*ebfedea0SLionel Sambuc ADDP tmp1 = 2*128, StateTable 522*ebfedea0SLionel Sambuc#elif SZ == 4 523*ebfedea0SLionel Sambuc ADDP tmp0 = 7*128, StateTable 524*ebfedea0SLionel Sambuc ADDP tmp1 = 6*128, StateTable 525*ebfedea0SLionel Sambuc#elif SZ == 8 526*ebfedea0SLionel Sambuc ADDP tmp0 = 15*128, StateTable 527*ebfedea0SLionel Sambuc ADDP tmp1 = 14*128, StateTable 528*ebfedea0SLionel Sambuc#endif 529*ebfedea0SLionel Sambuc ;; 530*ebfedea0SLionel Sambuc#if SZ >= 8 531*ebfedea0SLionel Sambuc lfetch.fault.nt1 [tmp0], -256 // 15 532*ebfedea0SLionel Sambuc lfetch.fault.nt1 [tmp1], -256;; 533*ebfedea0SLionel Sambuc lfetch.fault.nt1 [tmp0], -256 // 13 534*ebfedea0SLionel Sambuc lfetch.fault.nt1 [tmp1], -256;; 535*ebfedea0SLionel Sambuc lfetch.fault.nt1 [tmp0], -256 // 11 536*ebfedea0SLionel Sambuc lfetch.fault.nt1 [tmp1], -256;; 537*ebfedea0SLionel Sambuc lfetch.fault.nt1 [tmp0], -256 // 9 538*ebfedea0SLionel Sambuc lfetch.fault.nt1 [tmp1], -256;; 539*ebfedea0SLionel Sambuc#endif 540*ebfedea0SLionel Sambuc#if SZ >= 4 541*ebfedea0SLionel Sambuc lfetch.fault.nt1 [tmp0], -256 // 7 542*ebfedea0SLionel Sambuc lfetch.fault.nt1 [tmp1], -256;; 543*ebfedea0SLionel Sambuc lfetch.fault.nt1 [tmp0], -256 // 5 544*ebfedea0SLionel Sambuc lfetch.fault.nt1 [tmp1], -256;; 545*ebfedea0SLionel Sambuc#endif 546*ebfedea0SLionel Sambuc#if SZ >= 2 547*ebfedea0SLionel Sambuc lfetch.fault.nt1 [tmp0], -256 // 3 548*ebfedea0SLionel Sambuc lfetch.fault.nt1 [tmp1], -256;; 549*ebfedea0SLionel Sambuc#endif 550*ebfedea0SLionel Sambuc { 551*ebfedea0SLionel Sambuc .mii 552*ebfedea0SLionel Sambuc lfetch.fault.nt1 [tmp0] // 1 553*ebfedea0SLionel Sambuc add I[1]=1,I[1];; 554*ebfedea0SLionel Sambuc zxt1 I[1]=I[1] 555*ebfedea0SLionel Sambuc } 556*ebfedea0SLionel Sambuc { 557*ebfedea0SLionel Sambuc .mmi 558*ebfedea0SLionel Sambuc lfetch.nt1 [InPrefetch], 0x80 559*ebfedea0SLionel Sambuc lfetch.excl.nt1 [OutPrefetch], 0x80 560*ebfedea0SLionel Sambuc .save pr, PRSave 561*ebfedea0SLionel Sambuc mov PRSave = pr 562*ebfedea0SLionel Sambuc } ;; 563*ebfedea0SLionel Sambuc { 564*ebfedea0SLionel Sambuc .mmi 565*ebfedea0SLionel Sambuc lfetch.excl.nt1 [OutPrefetch], 0x80 566*ebfedea0SLionel Sambuc LKEY J = [KTable], SZ 567*ebfedea0SLionel Sambuc ADDP EndPtr = DataLen, InPtr 568*ebfedea0SLionel Sambuc } ;; 569*ebfedea0SLionel Sambuc { 570*ebfedea0SLionel Sambuc .mmi 571*ebfedea0SLionel Sambuc ADDP EndPtr = -1, EndPtr // Make it point to 572*ebfedea0SLionel Sambuc // last data byte. 573*ebfedea0SLionel Sambuc mov One = 1 574*ebfedea0SLionel Sambuc .save ar.lc, LCSave 575*ebfedea0SLionel Sambuc mov LCSave = ar.lc 576*ebfedea0SLionel Sambuc .body 577*ebfedea0SLionel Sambuc } ;; 578*ebfedea0SLionel Sambuc { 579*ebfedea0SLionel Sambuc .mmb 580*ebfedea0SLionel Sambuc sub Remainder = 0, OutPtr 581*ebfedea0SLionel Sambuc cmp.gtu pSmall, p0 = $threshold, DataLen 582*ebfedea0SLionel Sambuc(pSmall) br.cond.dpnt .rc4Remainder // Data too small for 583*ebfedea0SLionel Sambuc // big loop. 584*ebfedea0SLionel Sambuc } ;; 585*ebfedea0SLionel Sambuc { 586*ebfedea0SLionel Sambuc .mmi 587*ebfedea0SLionel Sambuc and Remainder = 0x7, Remainder 588*ebfedea0SLionel Sambuc ;; 589*ebfedea0SLionel Sambuc cmp.eq pAligned, pUnaligned = Remainder, r0 590*ebfedea0SLionel Sambuc nop 0x0 591*ebfedea0SLionel Sambuc } ;; 592*ebfedea0SLionel Sambuc { 593*ebfedea0SLionel Sambuc .mmb 594*ebfedea0SLionel Sambuc.pred.rel "mutex",pUnaligned,pAligned 595*ebfedea0SLionel Sambuc(pUnaligned) add Remainder = -1, Remainder 596*ebfedea0SLionel Sambuc(pAligned) sub Remainder = EndPtr, InPtr 597*ebfedea0SLionel Sambuc(pAligned) br.cond.dptk.many .rc4Aligned 598*ebfedea0SLionel Sambuc } ;; 599*ebfedea0SLionel Sambuc { 600*ebfedea0SLionel Sambuc .mmi 601*ebfedea0SLionel Sambuc nop 0x0 602*ebfedea0SLionel Sambuc nop 0x0 603*ebfedea0SLionel Sambuc mov.i ar.lc = Remainder 604*ebfedea0SLionel Sambuc } 605*ebfedea0SLionel Sambuc 606*ebfedea0SLionel Sambuc/* Do the initial few bytes via the compact, modulo-scheduled loop 607*ebfedea0SLionel Sambuc until the output pointer is 8-byte-aligned. */ 608*ebfedea0SLionel Sambuc 609*ebfedea0SLionel Sambuc MODSCHED_RC4_PROLOGUE 610*ebfedea0SLionel Sambuc MODSCHED_RC4_LOOP(.RC4AlignLoop) 611*ebfedea0SLionel Sambuc 612*ebfedea0SLionel Sambuc { 613*ebfedea0SLionel Sambuc .mib 614*ebfedea0SLionel Sambuc sub Remainder = EndPtr, InPtr 615*ebfedea0SLionel Sambuc zxt1 IFinal = IFinal 616*ebfedea0SLionel Sambuc clrrrb // Clear CFM.rrb.pr so 617*ebfedea0SLionel Sambuc ;; // next "mov pr.rot = N" 618*ebfedea0SLionel Sambuc // does the right thing. 619*ebfedea0SLionel Sambuc } 620*ebfedea0SLionel Sambuc { 621*ebfedea0SLionel Sambuc .mmi 622*ebfedea0SLionel Sambuc mov I[1] = IFinal 623*ebfedea0SLionel Sambuc nop 0x0 624*ebfedea0SLionel Sambuc nop 0x0 625*ebfedea0SLionel Sambuc } ;; 626*ebfedea0SLionel Sambuc 627*ebfedea0SLionel Sambuc 628*ebfedea0SLionel Sambuc.rc4Aligned: 629*ebfedea0SLionel Sambuc 630*ebfedea0SLionel Sambuc/* 631*ebfedea0SLionel Sambuc Unrolled loop count = (Remainder - ($unroll_count+1)*$phases)/($unroll_count*$phases) 632*ebfedea0SLionel Sambuc */ 633*ebfedea0SLionel Sambuc 634*ebfedea0SLionel Sambuc { 635*ebfedea0SLionel Sambuc .mlx 636*ebfedea0SLionel Sambuc add LoopCount = 1 - ($unroll_count + 1)*$phases, Remainder 637*ebfedea0SLionel Sambuc movl Remainder = 0xaaaaaaaaaaaaaaab 638*ebfedea0SLionel Sambuc } ;; 639*ebfedea0SLionel Sambuc { 640*ebfedea0SLionel Sambuc .mmi 641*ebfedea0SLionel Sambuc setf.sig f6 = LoopCount // M2, M3 6 cyc 642*ebfedea0SLionel Sambuc setf.sig f7 = Remainder // M2, M3 6 cyc 643*ebfedea0SLionel Sambuc nop 0x0 644*ebfedea0SLionel Sambuc } ;; 645*ebfedea0SLionel Sambuc { 646*ebfedea0SLionel Sambuc .mfb 647*ebfedea0SLionel Sambuc nop 0x0 648*ebfedea0SLionel Sambuc xmpy.hu f6 = f6, f7 649*ebfedea0SLionel Sambuc nop 0x0 650*ebfedea0SLionel Sambuc } ;; 651*ebfedea0SLionel Sambuc { 652*ebfedea0SLionel Sambuc .mmi 653*ebfedea0SLionel Sambuc getf.sig LoopCount = f6;; // M2 5 cyc 654*ebfedea0SLionel Sambuc nop 0x0 655*ebfedea0SLionel Sambuc shr.u LoopCount = LoopCount, 4 656*ebfedea0SLionel Sambuc } ;; 657*ebfedea0SLionel Sambuc { 658*ebfedea0SLionel Sambuc .mmi 659*ebfedea0SLionel Sambuc nop 0x0 660*ebfedea0SLionel Sambuc nop 0x0 661*ebfedea0SLionel Sambuc mov.i ar.lc = LoopCount 662*ebfedea0SLionel Sambuc } ;; 663*ebfedea0SLionel Sambuc 664*ebfedea0SLionel Sambuc/* Now comes the unrolled loop: */ 665*ebfedea0SLionel Sambuc 666*ebfedea0SLionel Sambuc.rc4Prologue: 667*ebfedea0SLionel Sambuc___ 668*ebfedea0SLionel Sambuc 669*ebfedea0SLionel Sambuc$iteration = 0; 670*ebfedea0SLionel Sambuc 671*ebfedea0SLionel Sambuc# Generate the prologue: 672*ebfedea0SLionel Sambuc$predicates = 1; 673*ebfedea0SLionel Sambucfor ($i = 0; $i < $phases; ++$i) { 674*ebfedea0SLionel Sambuc &emit_body (\$code, \$bypass, $iteration++, $predicates); 675*ebfedea0SLionel Sambuc $predicates = ($predicates << 1) | 1; 676*ebfedea0SLionel Sambuc} 677*ebfedea0SLionel Sambuc 678*ebfedea0SLionel Sambuc$code.=<<___; 679*ebfedea0SLionel Sambuc.rc4Loop: 680*ebfedea0SLionel Sambuc___ 681*ebfedea0SLionel Sambuc 682*ebfedea0SLionel Sambuc# Generate the body: 683*ebfedea0SLionel Sambucfor ($i = 0; $i < $unroll_count*$phases; ++$i) { 684*ebfedea0SLionel Sambuc &emit_body (\$code, \$bypass, $iteration++, $predicates); 685*ebfedea0SLionel Sambuc} 686*ebfedea0SLionel Sambuc 687*ebfedea0SLionel Sambuc$code.=<<___; 688*ebfedea0SLionel Sambuc.rc4Epilogue: 689*ebfedea0SLionel Sambuc___ 690*ebfedea0SLionel Sambuc 691*ebfedea0SLionel Sambuc# Generate the epilogue: 692*ebfedea0SLionel Sambucfor ($i = 0; $i < $phases; ++$i) { 693*ebfedea0SLionel Sambuc $predicates <<= 1; 694*ebfedea0SLionel Sambuc &emit_body (\$code, \$bypass, $iteration++, $predicates); 695*ebfedea0SLionel Sambuc} 696*ebfedea0SLionel Sambuc 697*ebfedea0SLionel Sambuc$code.=<<___; 698*ebfedea0SLionel Sambuc { 699*ebfedea0SLionel Sambuc .mmi 700*ebfedea0SLionel Sambuc lfetch.nt1 [EndPtr] // fetch line with last byte 701*ebfedea0SLionel Sambuc mov IFinal = I[1] 702*ebfedea0SLionel Sambuc nop 0x0 703*ebfedea0SLionel Sambuc } 704*ebfedea0SLionel Sambuc 705*ebfedea0SLionel Sambuc.rc4Remainder: 706*ebfedea0SLionel Sambuc { 707*ebfedea0SLionel Sambuc .mmi 708*ebfedea0SLionel Sambuc sub Remainder = EndPtr, InPtr // Calculate 709*ebfedea0SLionel Sambuc // # of bytes 710*ebfedea0SLionel Sambuc // left - 1 711*ebfedea0SLionel Sambuc nop 0x0 712*ebfedea0SLionel Sambuc nop 0x0 713*ebfedea0SLionel Sambuc } ;; 714*ebfedea0SLionel Sambuc { 715*ebfedea0SLionel Sambuc .mib 716*ebfedea0SLionel Sambuc cmp.eq pDone, p0 = -1, Remainder // done already? 717*ebfedea0SLionel Sambuc mov.i ar.lc = Remainder 718*ebfedea0SLionel Sambuc(pDone) br.cond.dptk.few .rc4Complete 719*ebfedea0SLionel Sambuc } 720*ebfedea0SLionel Sambuc 721*ebfedea0SLionel Sambuc/* Do the remaining bytes via the compact, modulo-scheduled loop */ 722*ebfedea0SLionel Sambuc 723*ebfedea0SLionel Sambuc MODSCHED_RC4_PROLOGUE 724*ebfedea0SLionel Sambuc MODSCHED_RC4_LOOP(.RC4RestLoop) 725*ebfedea0SLionel Sambuc 726*ebfedea0SLionel Sambuc.rc4Complete: 727*ebfedea0SLionel Sambuc { 728*ebfedea0SLionel Sambuc .mmi 729*ebfedea0SLionel Sambuc add KTable = -SZ, KTable 730*ebfedea0SLionel Sambuc add IFinal = -1, IFinal 731*ebfedea0SLionel Sambuc mov ar.lc = LCSave 732*ebfedea0SLionel Sambuc } ;; 733*ebfedea0SLionel Sambuc { 734*ebfedea0SLionel Sambuc .mii 735*ebfedea0SLionel Sambuc SKEY [KTable] = J,-SZ 736*ebfedea0SLionel Sambuc zxt1 IFinal = IFinal 737*ebfedea0SLionel Sambuc mov pr = PRSave, 0x1FFFF 738*ebfedea0SLionel Sambuc } ;; 739*ebfedea0SLionel Sambuc { 740*ebfedea0SLionel Sambuc .mib 741*ebfedea0SLionel Sambuc SKEY [KTable] = IFinal 742*ebfedea0SLionel Sambuc add RetVal = 1, r0 743*ebfedea0SLionel Sambuc br.ret.sptk.few rp 744*ebfedea0SLionel Sambuc } ;; 745*ebfedea0SLionel Sambuc___ 746*ebfedea0SLionel Sambuc 747*ebfedea0SLionel Sambuc# Last but not least, emit the code for the bypass-code of the unrolled loop: 748*ebfedea0SLionel Sambuc 749*ebfedea0SLionel Sambuc$code.=$bypass; 750*ebfedea0SLionel Sambuc 751*ebfedea0SLionel Sambuc$code.=<<___; 752*ebfedea0SLionel Sambuc .endp RC4 753*ebfedea0SLionel Sambuc___ 754*ebfedea0SLionel Sambuc 755*ebfedea0SLionel Sambucprint $code; 756