summaryrefslogtreecommitdiff
path: root/idea.c
diff options
context:
space:
mode:
authorMatt Turner <mattst88@gmail.com>2008-11-14 04:01:47 +0000
committerMatt Turner <mattst88@gmail.com>2008-11-14 04:01:47 +0000
commit70e35dac3ac20a4554bce8b30f236d220e22487e (patch)
treedc2cb38fd574a25672993acc6d78a3e2dedc78a8 /idea.c
parentfac9b754c5c93f49c951fa157a3cb30b6fb05214 (diff)
-- Remove stop watch functions from idea.c, linear.c, and huffman.c
git-svn-id: svn://mattst88.com/svn/cleanbench/trunk@26 0d43b9a7-5ab2-4d7b-af9d-f64450cef757
Diffstat (limited to 'idea.c')
-rw-r--r--idea.c271
1 files changed, 112 insertions, 159 deletions
diff --git a/idea.c b/idea.c
index 6198446..16cebfd 100644
--- a/idea.c
+++ b/idea.c
@@ -3,6 +3,7 @@
#include <stdint.h>
#include <math.h>
#include <limits.h>
+#include <time.h>
#include "nmglobal.h"
#include "nbench1.h"
@@ -38,7 +39,7 @@ typedef uint16_t IDEAkey[KEYLEN];
/*
** PROTOTYPES
*/
-static unsigned long DoIDEAIteration(unsigned char *plain1,
+static clock_t DoIDEAIteration(unsigned char *plain1,
unsigned char *crypt1, unsigned char *plain2,
unsigned long arraysize, unsigned long nloops,
IDEAkey Z, IDEAkey DK);
@@ -54,131 +55,107 @@ static void cipher_idea(uint16_t in[4], uint16_t out[4], IDEAkey Z);
** Perform IDEA encryption. Note that we time encryption & decryption
** time as being a single loop.
*/
-void DoIDEA(void)
+void
+DoIDEA(void)
{
-IDEAStruct *locideastruct; /* Loc pointer to global structure */
-int i;
-IDEAkey Z,DK;
-uint16_t userkey[8];
-unsigned long accumtime;
-double iterations;
-char *context;
-unsigned char *plain1 = NULL; /* First plaintext buffer */
-unsigned char *crypt1 = NULL; /* Encryption buffer */
-unsigned char *plain2 = NULL; /* Second plaintext buffer */
-
-/*
-** Link to global data
-*/
-locideastruct=&global_ideastruct;
-
-/*
-** Set error context
-*/
-context="CPU:IDEA";
-
-/*
-** Re-init random-number generator.
-*/
-/* randnum(3L); */
-randnum(3);
-
-/*
-** Build an encryption/decryption key
-*/
-for (i=0;i<8;i++)
- /* userkey[i]=(uint16_t)(abs_randwc(60000L) & 0xFFFF); */
- userkey[i]=(uint16_t)(abs_randwc((int32_t)60000) & 0xFFFF);
-for(i=0;i<KEYLEN;i++)
- Z[i]=0;
-
-/*
-** Compute encryption/decryption subkeys
-*/
-en_key_idea(userkey,Z);
-de_key_idea(Z,DK);
+ const char *context = "CPU:IDEA";
+ IDEAStruct *locideastruct=&global_ideastruct; /* Loc pointer to global structure */
+ clock_t total_time = 0;
+ int iterations = 0;
+ unsigned char *plain1 = NULL; /* First plaintext buffer */
+ unsigned char *crypt1 = NULL; /* Encryption buffer */
+ unsigned char *plain2 = NULL; /* Second plaintext buffer */
+ IDEAkey Z,DK;
+ uint16_t userkey[8];
+ int i;
+
+ /*
+ ** Re-init random-number generator.
+ */
+ randnum(3);
+
+ /*
+ ** Build an encryption/decryption key
+ */
+ for ( i = 0; i < 8; i++) {
+ userkey[i] = (uint16_t)(abs_randwc((int32_t)60000) & 0xFFFF);
+ }
+ for(i = 0; i < KEYLEN ; i++) {
+ Z[i] = 0;
+ }
+
+ /*
+ ** Compute encryption/decryption subkeys
+ */
+ en_key_idea(userkey,Z);
+ de_key_idea(Z,DK);
+
+ /*
+ ** Allocate memory for buffers. We'll make 3, called plain1,
+ ** crypt1, and plain2. It works like this:
+ ** plain1 >>encrypt>> crypt1 >>decrypt>> plain2.
+ ** So, plain1 and plain2 should match.
+ ** Also, fill up plain1 with sample text.
+ */
+ plain1 = malloc(locideastruct->arraysize);
+ if (!plain1) {
+ fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context);
+ exit(1);
+ }
+
+ crypt1 = malloc(locideastruct->arraysize);
+ if (!crypt1) {
+ fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context);
+ free(plain1);
+ exit(1);
+ }
+
+ plain2 = malloc(locideastruct->arraysize);
+ if (!plain2) {
+ fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context);
+ free(plain1);
+ free(plain2);
+ exit(1);
+ }
+ /*
+ ** Note that we build the "plaintext" by simply loading
+ ** the array up with random numbers.
+ */
+ for (i = 0;i < locideastruct->arraysize; i++) {
+ plain1[i] = (unsigned char)(abs_randwc(255) & 0xFF);
+ }
+
+ /*
+ ** See if we need to perform self adjustment loop.
+ */
+ if (locideastruct->adjust == FALSE) {
+ locideastruct->adjust = TRUE;
+ /*
+ ** Do self-adjustment. This involves initializing the
+ ** # of loops and increasing the loop count until we
+ ** get a number of loops that we can use.
+ */
+ for (locideastruct->loops = 100; locideastruct->loops < MAXIDEALOOPS; locideastruct->loops += 10) {
+ if (DoIDEAIteration(plain1, crypt1, plain2, locideastruct->arraysize, locideastruct->loops, Z, DK) > global_min_ticks) {
+ break;
+ }
+ }
+ }
-/*
-** Allocate memory for buffers. We'll make 3, called plain1,
-** crypt1, and plain2. It works like this:
-** plain1 >>encrypt>> crypt1 >>decrypt>> plain2.
-** So, plain1 and plain2 should match.
-** Also, fill up plain1 with sample text.
-*/
-plain1 = malloc(locideastruct->arraysize);
-if (!plain1) {
- fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context);
- exit(1);
-}
+ /*
+ ** All's well if we get here. Do the test.
+ */
-crypt1 = malloc(locideastruct->arraysize);
-if (!crypt1) {
- fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context);
- free(plain1);
- exit(1);
-}
+ do {
+ total_time += DoIDEAIteration(plain1, crypt1, plain2, locideastruct->arraysize, locideastruct->loops, Z, DK);
+ iterations += locideastruct->loops;
+ } while (total_time < locideastruct->request_secs * CLOCKS_PER_SEC);
-plain2 = malloc(locideastruct->arraysize);
-if (!plain2) {
- fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context);
free(plain1);
+ free(crypt1);
free(plain2);
- exit(1);
-}
-/*
-** Note that we build the "plaintext" by simply loading
-** the array up with random numbers.
-*/
-for(i=0;i<locideastruct->arraysize;i++)
- plain1[i]=(unsigned char)(abs_randwc(255) & 0xFF);
-
-/*
-** See if we need to perform self adjustment loop.
-*/
-if(locideastruct->adjust==0)
-{
- /*
- ** Do self-adjustment. This involves initializing the
- ** # of loops and increasing the loop count until we
- ** get a number of loops that we can use.
- */
- for(locideastruct->loops=100L;
- locideastruct->loops<MAXIDEALOOPS;
- locideastruct->loops+=10L)
- if(DoIDEAIteration(plain1,crypt1,plain2,
- locideastruct->arraysize,
- locideastruct->loops,
- Z,DK)>global_min_ticks) break;
-}
-
-/*
-** All's well if we get here. Do the test.
-*/
-accumtime=0L;
-iterations=(double)0.0;
-
-do {
- accumtime+=DoIDEAIteration(plain1,crypt1,plain2,
- locideastruct->arraysize,
- locideastruct->loops,Z,DK);
- iterations+=(double)locideastruct->loops;
-} while(TicksToSecs(accumtime)<locideastruct->request_secs);
-
-/*
-** Clean up, calculate results, and go home. Be sure to
-** show that we don't have to rerun adjustment code.
-*/
-free(plain1);
-free(crypt1);
-free(plain2);
-
-locideastruct->iterspersec=iterations / TicksToFracSecs(accumtime);
-
-if(locideastruct->adjust==0)
- locideastruct->adjust=1;
-
-return;
+ locideastruct->iterspersec = (double)(iterations * CLOCKS_PER_SEC) / (double)total_time;
}
/********************
@@ -188,51 +165,28 @@ return;
** Actually, a single iteration is one encryption and one
** decryption.
*/
-static unsigned long DoIDEAIteration(unsigned char *plain1,
- unsigned char *crypt1,
- unsigned char *plain2,
- unsigned long arraysize,
- unsigned long nloops,
- IDEAkey Z,
- IDEAkey DK)
+static clock_t
+DoIDEAIteration(unsigned char *plain1, unsigned char *crypt1, unsigned char *plain2, unsigned long arraysize, unsigned long nloops, IDEAkey Z, IDEAkey DK)
{
-register unsigned long i;
-register unsigned long j;
-unsigned long elapsed;
-#ifdef DEBUG
-int status=0;
-#endif
+ clock_t start, stop;
+ register unsigned long i;
+ register unsigned long j;
-/*
-** Start the stopwatch.
-*/
-elapsed=StartStopwatch();
+ start = clock();
-/*
-** Do everything for nloops.
-*/
-for(i=0;i<nloops;i++)
-{
- for(j=0;j<arraysize;j+=(sizeof(uint16_t)*4))
- cipher_idea((uint16_t *)(plain1+j),(uint16_t *)(crypt1+j),Z); /* Encrypt */
+ for (i = 0; i < nloops; i++) {
+ for (j = 0; j < arraysize; j += sizeof(uint16_t) * 4) {
+ cipher_idea((uint16_t *)(plain1 + j), (uint16_t *)(crypt1 + j), Z); /* Encrypt */
+ }
+ }
- for(j=0;j<arraysize;j+=(sizeof(uint16_t)*4))
- cipher_idea((uint16_t *)(crypt1+j),(uint16_t *)(plain2+j),DK); /* Decrypt */
-}
+ for (j = 0; j < arraysize; j += sizeof(uint16_t) * 4) {
+ cipher_idea((uint16_t *)(crypt1 + j), (uint16_t *)(plain2 + j), DK); /* Decrypt */
+ }
-#ifdef DEBUG
-for(j=0;j<arraysize;j++)
- if(*(plain1+j)!=*(plain2+j)){
- printf("IDEA Error! \n");
- status=1;
- }
-if (status==0) printf("IDEA: OK\n");
-#endif
+ stop = clock();
-/*
-** Get elapsed time.
-*/
-return(StopStopwatch(elapsed));
+ return stop - start;
}
/********
@@ -376,7 +330,6 @@ return;
** bits. Here, I am using the simplest form. May not be the
** fastest. -- RG
*/
-/* #define MUL(x,y) (x=mul(low16(x),y)) */
/****************
** cipher_idea **