]>
git.saurik.com Git - apple/security.git/blob - libsecurity_cryptkit/ckutils/giantBench/giantBench.c
1 /* Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
3 * giantBench.c - Benchmark of NSGiantInteger primitives.
7 * 13 Apr 98 Doug Mitchell at Apple
11 #include "giantIntegers.h"
12 #include "ckutilities.h"
13 #include "feeFunctions.h"
16 #include "ckutilsPlatform.h"
21 #define MIN_SIZE_DEF 4 /* min giant size in bytes */
22 #define MAX_SIZE_DEF 32 /* max in bytes */
23 #define LOOP_NOTIFY 100
26 static void usage(char **argv
)
28 printf("usage: %s [options]\n", argv
[0]);
29 printf(" Options:\n");
30 printf(" l=loops (default = %d)\n", LOOPS_DEF
);
31 printf(" n=maxBytes (default = %d\n", MIN_SIZE_DEF
);
32 printf(" x=maxBytes (default = %d\n", MAX_SIZE_DEF
);
33 printf(" o (use old 16-bit CryptKit\n");
40 * Fill buffer with random data.
42 static void fillData(unsigned bufSize
,
51 intCount
= bufSize
>> 2;
53 for(i
=0; i
<intCount
; i
++) {
57 residue
= bufSize
& 0x3;
58 cp
= (unsigned char *)ip
;
59 for(i
=0; i
<residue
; i
++) {
60 *cp
++ = (unsigned char)RAND();
65 * fill a pre-allocd giant with specified number of bytes of random
66 * data. *Buf is mallocd and uninitialized and will change here.
68 static void genGiant(giant g
,
74 fillData(numBytes
, buf
);
75 deserializeGiant(buf
, g
, numBytes
);
77 /* set random sign; deserializeGiant() is always positive */
83 /* avoid zero data - too many pitfalls with mod and div */
91 * Init giant arrays with random data.
93 static void initRandGiants(unsigned numBytes
,
101 for(i
=0; i
<numGiants
; i
++) {
102 genGiant(g1
[i
], numBytes
, buf
);
103 genGiant(g2
[i
], numBytes
, buf
);
108 * Individual tests. API is identical for all tests.
110 * loops : number of ops to perform.
111 * g1, g2 : arrays of giants with random data and sign. Tests may modify
112 * these. Size of array = 'loops'. Capacity big enough for all
114 * Return : total microseconds to do 'loops' ops.
117 static int mulgTest(unsigned loops
,
125 PLAT_GET_TIME(startTime
);
126 for(loop
=0; loop
<loops
; loop
++) {
129 PLAT_GET_TIME(endTime
);
130 return PLAT_GET_NS(startTime
, endTime
);
133 static int squareTest(unsigned loops
,
141 PLAT_GET_TIME(startTime
);
142 for(loop
=0; loop
<loops
; loop
++) {
145 PLAT_GET_TIME(endTime
);
146 return PLAT_GET_NS(startTime
, endTime
);
150 int main(int argc
, char **argv
)
156 unsigned char *buf
; // random data
160 unsigned mulgElapsed
;
163 int loops
= LOOPS_DEF
;
166 unsigned maxSize
= MAX_SIZE_DEF
;
167 unsigned minSize
= MIN_SIZE_DEF
;
173 argc
= ccommand(&argv
);
176 for(arg
=1; arg
<argc
; arg
++) {
180 maxSize
= atoi(&argp
[2]);
183 minSize
= atoi(&argp
[2]);
186 loops
= atoi(&argp
[2]);
192 seed
= atoi(&argp
[2]);
200 buf
= malloc(maxSize
);
205 seed
= (unsigned)tim
;
210 * Scratch giants, big enough for anything. Malloc here, init with
211 * random data before each test
212 * note these mallocs will be too big in the useOld case...
214 g1
= malloc(sizeof(giant
) * loops
);
215 g2
= malloc(sizeof(giant
) * loops
);
216 if((g1
== NULL
) || (g2
== NULL
)) {
217 printf("malloc error\n");
221 numDigits
= ((2 * maxSize
) + 1) / 2;
224 numDigits
= BYTES_TO_GIANT_DIGITS(2 * maxSize
);
226 for(i
=0; i
<loops
; i
++) {
227 g1
[i
] = newGiant(numDigits
);
228 g2
[i
] = newGiant(numDigits
);
229 if((g1
[i
] == NULL
) || (g2
[i
] == NULL
)) {
230 printf("malloc error\n");
235 printf("Starting giants test: seed %d\n", seed
);
236 for(numBytes
=minSize
; numBytes
<=maxSize
; numBytes
*=2) {
238 initRandGiants(numBytes
,
244 mulgElapsed
= mulgTest(loops
, g1
, g2
);
245 initRandGiants(numBytes
,
251 sqrElapsed
= squareTest(loops
, g1
, g2
);
252 printf(" bits : %4d mulg : %3d ns gsquare : %3d ns\n",