fix packman sort col, and make sort case-insensitive
[minix3.git] / commands / simple / dhrystone.c
blob5eceb8f6831702b68f79febd768132b668e30df5
1 /* dhrystone - benchmark program */
3 #define REGISTER
4 /*
6 * "DHRYSTONE" Benchmark Program
8 * Version: C/1.1, 12/01/84
10 * Date: PROGRAM updated 01/06/86, COMMENTS changed 01/31/87
12 * Author: Reinhold P. Weicker, CACM Vol 27, No 10, 10/84 pg.1013
13 * Translated from ADA by Rick Richardson
14 * Every method to preserve ADA-likeness has been used,
15 * at the expense of C-ness.
17 * Compile: cc -O dry.c -o drynr : No registers
18 * cc -O -DREG=register dry.c -o dryr : Registers
20 * Defines: Defines are provided for old C compiler's
21 * which don't have enums, and can't assign structures.
22 * The time(2) function is library dependant; Most
23 * return the time in seconds, but beware of some, like
24 * Aztec C, which return other units.
25 * The LOOPS define is initially set for 50000 loops.
26 * If you have a machine with large integers and is
27 * very fast, please change this number to 500000 to
28 * get better accuracy. Please select the way to
29 * measure the execution time using the TIME define.
30 * For single user machines, time(2) is adequate. For
31 * multi-user machines where you cannot get single-user
32 * access, use the times(2) function. Be careful to
33 * adjust the HZ parameter below for the units which
34 * are returned by your times(2) function. You can
35 * sometimes find this in <sys/param.h>. If you have
36 * neither time(2) nor times(2), use a stopwatch in
37 * the dead of the night.
38 * Use a "printf" at the point marked "start timer"
39 * to begin your timings. DO NOT use the UNIX "time(1)"
40 * command, as this will measure the total time to
41 * run this program, which will (erroneously) include
42 * the time to malloc(3) storage and to compute the
43 * time it takes to do nothing.
45 * Run: drynr; dryr
47 * Results: If you get any new machine/OS results, please send to:
49 * ihnp4!castor!pcrat!rick
51 * and thanks to all that do.
53 * Note: I order the list in increasing performance of the
54 * "with registers" benchmark. If the compiler doesn't
55 * provide register variables, then the benchmark
56 * is the same for both REG and NOREG.
58 * PLEASE: Send complete information about the machine type,
59 * clock speed, OS and C manufacturer/version. If
60 * the machine is modified, tell me what was done.
61 * On UNIX, execute uname -a and cc -V to get this info.
63 * 80x8x NOTE: 80x8x benchers: please try to do all memory models
64 * for a particular compiler.
67 * The following program contains statements of a high-level programming
68 * language (C) in a distribution considered representative:
70 * assignments 53%
71 * control statements 32%
72 * procedure, function calls 15%
74 * 100 statements are dynamically executed. The program is balanced with
75 * respect to the three aspects:
76 * - statement type
77 * - operand type (for simple data types)
78 * - operand access
79 * operand global, local, parameter, or constant.
81 * The combination of these three aspects is balanced only approximately.
83 * The program does not compute anything meaningfull, but it is
84 * syntactically and semantically correct.
89 #include <sys/types.h>
90 #include <stdlib.h>
91 #include <string.h>
92 #include <time.h>
93 #include <stdio.h>
94 #include <signal.h>
95 #include <unistd.h>
97 /* Accuracy of timings and human fatigue controlled by next two lines */
98 /*#define LOOPS 50000 */ /* Use this for slow or 16 bit machines */
99 /*#define LOOPS 500000 */ /* Use this for faster machines */
100 /*#define LOOPS (sizeof(int) == 2 ? 50000 : 1000000)*/
102 /* Seconds to run */
103 #define SECONDS 15
105 /* Compiler dependent options */
106 #define NOENUM /* Define if compiler has no enum's */
107 /* #define NOSTRUCTASSIGN */ /* Define if compiler can't assign structures*/
110 /* Define only one of the next two defines */
111 #define TIMES /* Use times(2) time function */
112 /*#define TIME */ /* Use time(2) time function */
115 #ifdef TIME
116 /* Ganularity of time(2) is of course 1 second */
117 #define HZ 1
118 #endif
120 #ifdef TIMES
121 /* Define the granularity of your times(2) function */
122 /*#define HZ 50 */ /* times(2) returns 1/50 second (europe?) */
123 /*#define HZ 60 */ /* times(2) returns 1/60 second (most) */
124 /*#define HZ 100 */ /* times(2) returns 1/100 second (WECo) */
125 #endif
127 /* For compatibility with goofed up version */
128 /*#undef GOOF */ /* Define if you want the goofed up version */
131 #ifdef GOOF
132 char Version[] = "1.0";
133 #else
134 char Version[] = "1.1";
135 #endif
138 #ifdef NOSTRUCTASSIGN
139 #define structassign(d, s) memcpy(&(d), &(s), sizeof(d))
140 #else
141 #define structassign(d, s) d = s
142 #endif
145 #ifdef NOENUM
146 #define Ident1 1
147 #define Ident2 2
148 #define Ident3 3
149 #define Ident4 4
150 #define Ident5 5
151 typedef int Enumeration;
152 #else
153 typedef enum {
154 Ident1, Ident2, Ident3, Ident4, Ident5
155 } Enumeration;
156 #endif
158 typedef int OneToThirty;
159 typedef int OneToFifty;
160 typedef char CapitalLetter;
161 typedef char String30[31];
162 typedef int Array1Dim[51];
163 typedef int Array2Dim[51][51];
165 struct Record {
166 struct Record *PtrComp;
167 Enumeration Discr;
168 Enumeration EnumComp;
169 OneToFifty IntComp;
170 String30 StringComp;
173 typedef struct Record RecordType;
174 typedef RecordType *RecordPtr;
175 typedef int boolean;
177 #ifdef NULL
178 #undef NULL
179 #endif
181 #define NULL 0
182 #define TRUE 1
183 #define FALSE 0
185 #ifndef REG
186 #define REG
187 #endif
190 #ifdef TIMES
191 #include <sys/times.h>
192 #endif
194 #ifndef _PROTOTYPE
195 #define _PROTOTYPE(fun, args) fun args
196 #endif
198 _PROTOTYPE(int main, (void));
199 _PROTOTYPE(void prep_timer, (void));
200 _PROTOTYPE(void timeout, (int sig));
201 _PROTOTYPE(void Proc0, (void));
202 _PROTOTYPE(void Proc1, (RecordPtr PtrParIn));
203 _PROTOTYPE(void Proc2, (OneToFifty *IntParIO));
204 _PROTOTYPE(void Proc3, (RecordPtr *PtrParOut));
205 _PROTOTYPE(void Proc4, (void));
206 _PROTOTYPE(void Proc5, (void));
207 _PROTOTYPE(void Proc6, (Enumeration EnumParIn, Enumeration *EnumParOut));
208 _PROTOTYPE(void Proc7, (OneToFifty IntParI1, OneToFifty IntParI2,
209 OneToFifty *IntParOut));
210 _PROTOTYPE(void Proc8, (Array1Dim Array1Par, Array2Dim Array2Par,
211 OneToFifty IntParI1, OneToFifty IntParI2));
212 /*_PROTOTYPE(Enumeration Func1,(CapitalLetter CharPar1, CapitalLetter CharPar2));*/
213 _PROTOTYPE(boolean Func2, (String30 StrParI1, String30 StrParI2));
214 _PROTOTYPE(boolean Func3, (Enumeration EnumParIn));
216 _PROTOTYPE(Enumeration Func1, (int CharPar1, int CharPar2));
219 int main()
221 Proc0();
222 return(0);
226 #if __STDC__
227 volatile int done;
228 #else
229 int done;
230 #endif
232 void prep_timer()
234 signal(SIGALRM, timeout);
235 done = 0;
238 void timeout(sig)
239 int sig;
241 done = 1;
244 /* Package 1 */
245 int IntGlob;
246 boolean BoolGlob;
247 char Char1Glob;
248 char Char2Glob;
249 Array1Dim Array1Glob;
250 Array2Dim Array2Glob;
251 RecordPtr PtrGlb;
252 RecordPtr PtrGlbNext;
255 void Proc0()
257 OneToFifty IntLoc1;
258 REG OneToFifty IntLoc2;
259 OneToFifty IntLoc3;
260 REG char CharIndex;
261 Enumeration EnumLoc;
262 String30 String1Loc;
263 String30 String2Loc;
264 register unsigned long i;
265 unsigned long starttime;
266 unsigned long benchtime;
267 unsigned long nulltime;
268 unsigned long nullloops;
269 unsigned long benchloops;
270 unsigned long ticks_per_sec;
271 #ifdef TIMES
272 struct tms tms;
273 #endif
275 #ifdef HZ
276 #define ticks_per_sec HZ
277 #else
278 ticks_per_sec = sysconf(_SC_CLK_TCK);
279 #endif
281 i = 0;
282 prep_timer();
284 #ifdef TIME
285 starttime = time((long *) 0);
286 #endif
288 #ifdef TIMES
289 times(&tms);
290 starttime = tms.tms_utime;
291 #endif
293 alarm(1);
294 while (!done) i++;
296 #ifdef TIME
297 nulltime = time((long *) 0) - starttime; /* Computes o'head of loop */
298 #endif
300 #ifdef TIMES
301 times(&tms);
302 nulltime = tms.tms_utime - starttime; /* Computes overhead of looping */
303 #endif
305 nullloops = i;
308 PtrGlbNext = (RecordPtr) malloc(sizeof(RecordType));
309 PtrGlb = (RecordPtr) malloc(sizeof(RecordType));
310 PtrGlb->PtrComp = PtrGlbNext;
311 PtrGlb->Discr = Ident1;
312 PtrGlb->EnumComp = Ident3;
313 PtrGlb->IntComp = 40;
314 strcpy(PtrGlb->StringComp, "DHRYSTONE PROGRAM, SOME STRING");
315 #ifndef GOOF
316 strcpy(String1Loc, "DHRYSTONE PROGRAM, 1'ST STRING"); /* GOOF */
317 #endif
319 Array2Glob[8][7] = 10; /* Was missing in published program */
322 /*****************
323 -- Start Timer --
324 *****************/
325 i = 0;
326 prep_timer();
328 #ifdef TIME
329 starttime = time((long *) 0);
330 #endif
332 #ifdef TIMES
333 times(&tms);
334 starttime = tms.tms_utime;
335 #endif
337 alarm(SECONDS);
338 while (!done) {
339 i++;
340 Proc5();
341 Proc4();
342 IntLoc1 = 2;
343 IntLoc2 = 3;
344 strcpy(String2Loc, "DHRYSTONE PROGRAM, 2'ND STRING");
345 EnumLoc = Ident2;
346 BoolGlob = !Func2(String1Loc, String2Loc);
347 while (IntLoc1 < IntLoc2) {
348 IntLoc3 = 5 * IntLoc1 - IntLoc2;
349 Proc7(IntLoc1, IntLoc2, &IntLoc3);
350 ++IntLoc1;
352 Proc8(Array1Glob, Array2Glob, IntLoc1, IntLoc3);
353 Proc1(PtrGlb);
354 for (CharIndex = 'A'; CharIndex <= Char2Glob; ++CharIndex)
355 if (EnumLoc == Func1(CharIndex, 'C'))
356 Proc6(Ident1, &EnumLoc);
357 IntLoc3 = IntLoc2 * IntLoc1;
358 IntLoc2 = IntLoc3 / IntLoc1;
359 IntLoc2 = 7 * (IntLoc3 - IntLoc2) - IntLoc1;
360 Proc2(&IntLoc1);
364 /*****************
365 -- Stop Timer --
366 *****************/
369 #ifdef TIME
370 benchtime = time((long *) 0) - starttime;
371 #endif
373 #ifdef TIMES
374 times(&tms);
375 benchtime = tms.tms_utime - starttime;
376 #endif
377 benchloops = i;
379 /* Approximately correct benchtime to the nulltime. */
380 benchtime -= nulltime / (nullloops / benchloops);
382 printf("Dhrystone(%s) time for %lu passes = %lu.%02lu\n",
383 Version,
384 benchloops, benchtime / ticks_per_sec,
385 benchtime % ticks_per_sec * 100 / ticks_per_sec);
386 printf("This machine benchmarks at %lu dhrystones/second\n",
387 benchloops * ticks_per_sec / benchtime);
391 void Proc1(PtrParIn)
392 REG RecordPtr PtrParIn;
394 #define NextRecord (*(PtrParIn->PtrComp))
397 structassign(NextRecord, *PtrGlb);
398 PtrParIn->IntComp = 5;
399 NextRecord.IntComp = PtrParIn->IntComp;
400 NextRecord.PtrComp = PtrParIn->PtrComp;
401 Proc3((RecordPtr *)NextRecord.PtrComp);
402 if (NextRecord.Discr == Ident1) {
403 NextRecord.IntComp = 6;
404 Proc6(PtrParIn->EnumComp, &NextRecord.EnumComp);
405 NextRecord.PtrComp = PtrGlb->PtrComp;
406 Proc7(NextRecord.IntComp, 10, &NextRecord.IntComp);
407 } else
408 structassign(*PtrParIn, NextRecord);
411 #undef NextRecord
415 void Proc2(IntParIO)
416 OneToFifty *IntParIO;
418 REG OneToFifty IntLoc;
419 REG Enumeration EnumLoc;
422 IntLoc = *IntParIO + 10;
423 for (;;) {
424 if (Char1Glob == 'A') {
425 --IntLoc;
426 *IntParIO = IntLoc - IntGlob;
427 EnumLoc = Ident1;
429 if (EnumLoc == Ident1) break;
434 void Proc3(PtrParOut)
435 RecordPtr *PtrParOut;
437 if (PtrGlb != NULL)
438 *PtrParOut = PtrGlb->PtrComp;
439 else
440 IntGlob = 100;
441 Proc7(10, IntGlob, &PtrGlb->IntComp);
445 void Proc4()
447 REG boolean BoolLoc;
450 BoolLoc = Char1Glob == 'A';
451 BoolLoc |= BoolGlob;
452 Char2Glob = 'B';
456 void Proc5()
458 Char1Glob = 'A';
459 BoolGlob = FALSE;
463 void Proc6(EnumParIn, EnumParOut)
464 REG Enumeration EnumParIn;
465 REG Enumeration *EnumParOut;
467 *EnumParOut = EnumParIn;
468 if (!Func3(EnumParIn)) *EnumParOut = Ident4;
469 switch (EnumParIn) {
470 case Ident1: *EnumParOut = Ident1; break;
471 case Ident2:
472 if (IntGlob > 100)
473 *EnumParOut = Ident1;
474 else
475 *EnumParOut = Ident4;
476 break;
477 case Ident3: *EnumParOut = Ident2; break;
478 case Ident4:
479 break;
480 case Ident5: *EnumParOut = Ident3;
485 void Proc7(IntParI1, IntParI2, IntParOut)
486 OneToFifty IntParI1;
487 OneToFifty IntParI2;
488 OneToFifty *IntParOut;
490 REG OneToFifty IntLoc;
493 IntLoc = IntParI1 + 2;
494 *IntParOut = IntParI2 + IntLoc;
498 void Proc8(Array1Par, Array2Par, IntParI1, IntParI2)
499 Array1Dim Array1Par;
500 Array2Dim Array2Par;
501 OneToFifty IntParI1;
502 OneToFifty IntParI2;
504 REG OneToFifty IntLoc;
505 REG OneToFifty IntIndex;
508 IntLoc = IntParI1 + 5;
509 Array1Par[IntLoc] = IntParI2;
510 Array1Par[IntLoc + 1] = Array1Par[IntLoc];
511 Array1Par[IntLoc + 30] = IntLoc;
512 for (IntIndex = IntLoc; IntIndex <= (IntLoc + 1); ++IntIndex)
513 Array2Par[IntLoc][IntIndex] = IntLoc;
514 ++Array2Par[IntLoc][IntLoc - 1];
515 Array2Par[IntLoc + 20][IntLoc] = Array1Par[IntLoc];
516 IntGlob = 5;
520 Enumeration Func1(CharPar1, CharPar2)
521 CapitalLetter CharPar1;
522 CapitalLetter CharPar2;
524 REG CapitalLetter CharLoc1;
525 REG CapitalLetter CharLoc2;
528 CharLoc1 = CharPar1;
529 CharLoc2 = CharLoc1;
530 if (CharLoc2 != CharPar2)
531 return(Ident1);
532 else
533 return(Ident2);
537 boolean Func2(StrParI1, StrParI2)
538 String30 StrParI1;
539 String30 StrParI2;
541 REG OneToThirty IntLoc;
542 REG CapitalLetter CharLoc;
545 IntLoc = 1;
546 while (IntLoc <= 1)
547 if (Func1(StrParI1[IntLoc], StrParI2[IntLoc + 1]) == Ident1) {
548 CharLoc = 'A';
549 ++IntLoc;
551 if (CharLoc >= 'W' && CharLoc <= 'Z') IntLoc = 7;
552 if (CharLoc == 'X')
553 return(TRUE);
554 else {
555 if (strcmp(StrParI1, StrParI2) > 0) {
556 IntLoc += 7;
557 return(TRUE);
558 } else
559 return(FALSE);
564 boolean Func3(EnumParIn)
565 REG Enumeration EnumParIn;
567 REG Enumeration EnumLoc;
570 EnumLoc = EnumParIn;
571 if (EnumLoc == Ident3) return(TRUE);
572 return(FALSE);
576 #ifdef NOSTRUCTASSIGN
577 memcpy(d, s, l)
578 register char *d;
579 register char *s;
580 register int l;
582 while (l--) *d++ = *s++;
585 #endif