From: adash Date: Fri, 18 Apr 2008 00:14:53 +0000 (+0000) Subject: fully compiled version of Crypt benchmark X-Git-Tag: preEdgeChange~145 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=b74ad014d0b224688f8d6699b317cc5486868a6f;p=IRC.git fully compiled version of Crypt benchmark --- diff --git a/Robust/src/Benchmarks/Prefetch/Crypt/dsm/JGFCryptBenchSizeA.java b/Robust/src/Benchmarks/Prefetch/Crypt/dsm/JGFCryptBenchSizeA.java index 63a6af3c..f27d675c 100644 --- a/Robust/src/Benchmarks/Prefetch/Crypt/dsm/JGFCryptBenchSizeA.java +++ b/Robust/src/Benchmarks/Prefetch/Crypt/dsm/JGFCryptBenchSizeA.java @@ -1,20 +1,281 @@ -public class JGFCryptBenchSizeA{ - public static void main(String argv[]){ - int nthreads; - if(argv.length != 0 ) { - nthreads = Integer.parseInt(argv[0]); - } else { - System.printString("The no of threads has not been specified, defaulting to 1"); - System.printString(" "); - nthreads = 1; +public class JGFCryptBenchSizeA extends Thread{ + JGFCryptBench cb; + int id,key[]; + byte text1[],text2[]; + int nthreads; + + public JGFCryptBenchSizeA(JGFCryptBench cb, int id, byte [] text1, byte [] text2, int [] key, int nthreads) { + this.cb = cb; + this.id = id; + this.text1=text1; + this.text2=text2; + this.key=key; + this.nthreads = nthreads; + } + + // run() + // + // IDEA encryption/decryption algorithm. It processes plaintext in + // 64-bit blocks, one at a time, breaking the block into four 16-bit + // unsigned subblocks. It goes through eight rounds of processing + // using 6 new subkeys each time, plus four for last step. The source + // text is in array text1, the destination text goes into array text2 + // The routine represents 16-bit subblocks and subkeys as type int so + // that they can be treated more easily as unsigned. Multiplication + // modulo 0x10001 interprets a zero sub-block as 0x10000; it must to + // fit in 16 bits. + // + + public void run() { + int ilow, iupper, slice, tslice, ttslice; + + atomic { + tslice = text1.length / 8; + ttslice = (tslice + nthreads-1) / nthreads; + slice = ttslice*8; + + ilow = id*slice; + iupper = (id+1)*slice; + if(iupper > text1.length) iupper = text1.length; + } + + int i1 = ilow; // Index into first text array. + int i2 = ilow; // Index into second text array. + int ik; // Index into key array. + int x1, x2, x3, x4, t1, t2; // Four "16-bit" blocks, two temps. + int r; // Eight rounds of processing. + + for (int i =ilow ; i >> 8); + text2[i2++] = (byte) x3; // x3 and x2 are switched + text2[i2++] = (byte) (x3 >>> 8); // only in name. + text2[i2++] = (byte) x2; + text2[i2++] = (byte) (x2 >>> 8); + text2[i2++] = (byte) x4; + text2[i2++] = (byte) (x4 >>> 8); + } //End of atomic + + } // End for loop. + + } // End routine. + + public static void main(String argv[]){ + int nthreads; + if(argv.length != 0 ) { + nthreads = Integer.parseInt(argv[0]); + } else { + System.printString("The no of threads has not been specified, defaulting to 1"); + System.printString(" "); + nthreads = 1; + } + + /* Instruments output messages */ + JGFInstrumentor instr = new JGFInstrumentor(); + instr.printHeader(2,0,nthreads); + + JGFCryptBench cb; + int size = 0; + instr.addTimer("Section2:Crypt:Kernel", "Kbyte",size); + atomic { + cb = global new JGFCryptBench(); + cb.JGFsetsize(size); + cb.JGFinitialise(); + } + + + /* Start computation */ + int mid = (128<<24)|(195<<16)|(175<<8)|73; + + JGFCryptBenchSizeA[] th; + atomic { + th = global new JGFCryptBenchSizeA [nthreads]; + } + + // Start the stopwatch. + instr.startTimer("Section2:Crypt:Kernel"); + + // Encrypt plain1. + JGFCryptBenchSizeA tmp; + for(int i=1;i>>9) | (Z[i-6]<<7)) // Shift and combine. + & 0xFFFF; // Just 16 bits. + //continue; // Next iteration. + flag1 = 1; + } + + if(flag1 == 0) { + int flag2 = 0; + + if (j == 6) // Wrap to beginning for second chunk. + { + Z[i] = ((Z[i -7]>>>9) | (Z[i-14]<<7)) + & 0xFFFF; + //continue; + flag2 = 1; + } + + if(flag2 == 0) { + // j == 7 so wrap to beginning for both chunks. + Z[i] = ((Z[i -15]>>>9) | (Z[i-14]<<7)) + & 0xFFFF; + } + } + } + } + + // + //calcDecryptKey + // + //Builds the 52 16-bit encryption subkeys DK[] from the encryption- + //subkeys Z[]. DK[] is a 32-bit int array holding 16-bit values as + //unsigned. + // + + private void calcDecryptKey() + { + int j, k; // Index counters. + int t1, t2, t3; // Temps to hold decrypt subkeys. + + t1 = inv(Z[0]); // Multiplicative inverse (mod x10001). + t2 = - Z[1] & 0xffff; // Additive inverse, 2nd encrypt subkey. + t3 = - Z[2] & 0xffff; // Additive inverse, 3rd encrypt subkey. + + DK[51] = inv(Z[3]); // Multiplicative inverse (mod x10001). + DK[50] = t3; + DK[49] = t2; + DK[48] = t1; + + j = 47; // Indices into temp and encrypt arrays. + k = 4; + for (int i = 0; i < 7; i++) + { + t1 = Z[k++]; + DK[j--] = Z[k++]; + DK[j--] = t1; + t1 = inv(Z[k++]); + t2 = -Z[k++] & 0xffff; + t3 = -Z[k++] & 0xffff; + DK[j--] = inv(Z[k++]); + DK[j--] = t2; + DK[j--] = t3; + DK[j--] = t1; + } + + t1 = Z[k++]; + DK[j--] = Z[k++]; + DK[j--] = t1; + t1 = inv(Z[k++]); + t2 = -Z[k++] & 0xffff; + t3 = -Z[k++] & 0xffff; + DK[j--] = inv(Z[k++]); + DK[j--] = t3; + DK[j--] = t2; + DK[j--] = t1; + } + + + // + //mul + // + // Performs multiplication, modulo (2**16)+1. This code is structured + // on the assumption that untaken branches are cheaper than taken + // branches, and that the compiler doesn't schedule branches. + // Java: Must work with 32-bit int and one 64-bit long to keep + // 16-bit values and their products "unsigned." The routine assumes + // that both a and b could fit in 16 bits even though they come in + // as 32-bit ints. Lots of "& 0xFFFF" masks here to keep things 16-bit. + // Also, because the routine stores mod (2**16)+1 results in a 2**16 + // space, the result is truncated to zero whenever the result would + // zero, be 2**16. And if one of the multiplicands is 0, the result + // is not zero, but (2**16) + 1 minus the other multiplicand (sort + // of an additive inverse mod 0x10001). + + // NOTE: The java conversion of this routine works correctly, but + // is half the speed of using Java's modulus division function (%) + // on the multiplication with a 16-bit masking of the result--running + // in the Symantec Caje IDE. So it's not called for now; the test + // uses Java % instead. + // + + private int mul(int a, int b) + { + int ret; + long p; // Large enough to catch 16-bit multiply + // without hitting sign bit. + if (a != 0) + { + if(b != 0) + { + p = (long) a * b; + b = (int) p & 0xFFFF; // Lower 16 bits. + a = (int) p >>> 16; // Upper 16 bits. + if (b < a) + return (b - a + 1) & 0xFFFF; + else + return (b - a) & 0xFFFF; + } + else + return ((1 - a) & 0xFFFF); // If b = 0, then same as + // 0x10001 - a. + } + else // If a = 0, then return + return((1 - b) & 0xFFFF); // same as 0x10001 - b. + } + + // + // inv + // + // Compute multiplicative inverse of x, modulo (2**16)+1 using + // extended Euclid's GCD (greatest common divisor) algorithm. + // It is unrolled twice to avoid swapping the meaning of + // the registers. And some subtracts are changed to adds. + // Java: Though it uses signed 32-bit ints, the interpretation + // of the bits within is strictly unsigned 16-bit. + // + + private int inv(int x) + { + int t0, t1; + int q, y; + + if (x <= 1) // Assumes positive x. + return(x); // 0 and 1 are self-inverse. + + t1 = 0x10001 / x; // (2**16+1)/x; x is >= 2, so fits 16 bits. + y = 0x10001 % x; + if (y == 1) + return((1 - t1) & 0xFFFF); + + t0 = 1; + do { + q = x / y; + x = x % y; + t0 += q * t1; + if (x == 1) return(t0); + q = y / x; + y = y % x; + t1 += q * t0; + } while (y != 1); + + return((1 - t1) & 0xFFFF); + } + + // + // freeTestData + // + // Nulls arrays and forces garbage collection to free up memory. + // + + void freeTestData(int array_rows) + { + for(int i = 0; i