⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xbench.cpp

📁 eC++编译器源码
💻 CPP
字号:
//
// Echo Choi
// ENGR 653 - Computer Architecture, Spring 1996
// Term Project - Benchmark program
// 5/7/1996
//

#include <stdio.h>
// #define UNIX					// Compile in UNIX
// #define VC40                                    // Visual C++ 4.0
#define EC                                           // EC
// #define SAMPLING

// Timer for EC compiler
#if defined(EC)
#include <realsys.h>
#include <cmalloc.h>
#include <cstring.h>
#include <SYSTEM.h>
double dtime()
{
	double q;
	double h, m, u;
	unsigned int hour, min, sec, msec;
	Time(hour, min, sec, msec);
	h = FLOAT(hour) * 3.6e+3;
	m = FLOAT(min) * 6.0e+1;
	u = FLOAT(msec) * 1.0e-3;
	q = h + m + FLOAT(sec) + u;
	return q;
};
#endif

// Timer in UNIX system
#if defined(UNIX)
#include <stdlib.h>
#include <sys/time.h>
#include <sys/resource.h>
#define	FLOAT(x)	(double)x
#define TRUNC(x)	(long)x
#define	LONG(x)		x
#define	HALT		exit(0)
#define ADR(x)		&(x)
#define ORD(x)		x
#define CHR(x)		x
double dtime()
{
	double q;
	struct rusage rusage;
	getrusage(RUSAGE_SELF, &rusage);
	q = (double)(rusage.ru_utime.tv_sec);
	q = q + (double)(rusage.ru_utime.tv_usec) * 1.0e-6;
	return q;
};
#endif

// Timer in Visual C++ 4.0
#if defined(VC40)
#include <sys/types.h>
#include <sys/timeb.h>
#include <stdlib.h>
#include <malloc.h>
#define	FLOAT(x)	(double)x
#define TRUNC(x)	(long)x
#define	LONG(x)		(x)
#define	HALT		exit(0)
#define ADR(x)		&(x)
#define ORD(x)		(x)
#define CHR(x)		(x)
double dtime()
{
	struct _timeb t;
	double q;
	_ftime(&t);
	q = (double)t.time + (double)t.millitm * 1.0e-3;
	return q;
}
#endif

// Global Variables
class C1 {
public:
	double d;
	char c;
	void cChange(const char x);
	void dChange(const double x);
};

void C1 :: cChange (const char x) { c = x; };
void C1 :: dChange (const double x) { d = x; };

typedef C1 *ClassPtr;

#if defined(EC)
typedef Links *pLinks;
typedef struct {
	char c;
	pLinks next;
} Links;
#else
typedef struct _Links {
	char c;
	struct _Links *next;
} Links;
typedef Links *pLinks;
#endif

typedef struct {
	double EndTime, DeltaTime, index;
} Result;

pLinks head, tail;
C1 kG, kGA[256];
ClassPtr kGptr;
int iG, iGA[256];
long lG, lGA[256];
float fG, fGA[256];
double dG, dGA[256];
char cG, cGA[256], *cGP;

//////////////////////////////////////////////////
// Integer
//////////////////////////////////////////////////
int Proc_0(int iP1, int &iP2)
{
	int iL;
	iL = iP1;
	iP2 = iG;
	iGA[0] = iP1;
	iGA[1] = iP2;
	for (iG=2;iG<256;iG++) {
		iGA[iG] = (iGA[iG-1] + iGA[iG-2]) / 3;
	};
	return (iGA[255] % 7);
};

//////////////////////////////////////////////////
// FLoating Point data type
//////////////////////////////////////////////////
float Proc_1(float fP1, float &fP2)
{
	int i;
	float fL;
	fL = fP1;
	fP2 = fG;
	fGA[0] = fP1;
	fGA[1] = fP2;
	for (i=2;i<256;i++) {
		fGA[i] = (fGA[i-1] + fGA[i-2]) * 0.3;
	};
	return (fGA[255] / 7.0);
};

//////////////////////////////////////////////////
// Double data type
//////////////////////////////////////////////////
double Proc_2(double dP1, double &dP2)
{
	int i;
	double dL;
	dL = dP1;
	dP2 = dG;
	dGA[0] = dP1;
	dGA[1] = dP2;
	for (i=2;i<256;i++) {
		dGA[i] = (dGA[i-1] + dGA[i-2]) * 0.3;
	};
	return (dGA[255] / 7.0);
};

//////////////////////////////////////////////////
// Characters
//////////////////////////////////////////////////
char Proc_3(char cP1, char &cP2)
{
	char cL, *cptr;
	int i;
	cGA[0] = cP1;
	for (i=1;i<256;i++) {
		cGA[i] = CHR((ORD(cGA[i-1]) & 0x3c) % 26 + 65);
	};
	cL = cGA[255];
	return cL;
};

//////////////////////////////////////////////////
// Class
//////////////////////////////////////////////////
ClassPtr Proc_4(ClassPtr &kPptr)
{
	ClassPtr kLptr;
	int i;
	kPptr = kGptr;
	kLptr = kPptr;
	for (i=0;i<256;i++) {
		kGA[i].cChange(cGA[i]);
		kGA[i].dChange(dGA[i]);
	}
	return kLptr;
};

//////////////////////////////////////////////////
// Flow Control
//////////////////////////////////////////////////
void Proc_5()
{
	int i;
	for (i=0;i<256;i++);
	i=0;
	while (i < 256) { i++; };
	do { i--; } while (i > 0);
	if (i==0) i = 10;
	if (i < 100) i = 100;
	if (i < 50) i = 50;
	else i = 300;
	if (i != 300) i = 400;
};

//////////////////////////////////////////////////
// Pointers - Link list
//////////////////////////////////////////////////
void Proc_6()
{
	pLinks current;
	int i;
	current = head;
	for (i=0;i<256;i++) {
		current->c = 'q';
		current = current->next;
	};
};

//////////////////////////////////////////////////
// Long Integer
//////////////////////////////////////////////////
long Proc_7(long lP1, long &lP2)
{
	int i;
	long lL;
	lL = lP1;
	lP2 = lG;
	lGA[0] = lP1;
	lGA[1] = lP2;
	for (i=2;i<256;i++) {
		lGA[i] = (lGA[i-1] + lGA[i-2]) / 2L;
	};
	return (lGA[255] % 8L);
};

//////////////////////////////////////////////////
// Bit Operations
//////////////////////////////////////////////////
void Proc_8()
{
	unsigned int i, j;
	j = 3;
	for (i=0;i<256;i++) {
		j = j * 2;
		j = j | 3;
		j = j / 2;
		j = j & 3;
	};
};

//////////////////////////////////////////////////
// Clean Up Global Variables
//////////////////////////////////////////////////
void CleanGlobals()
{
	pLinks current;
	current = head;
	tail->next = NULL;
	while (current != NULL) {
		current = head->next;
		free(head);
		head = current;
	};
};


//////////////////////////////////////////////////
// INIT GLOBAL VARIABLES
//////////////////////////////////////////////////
void InitGlobals()
{
	// Basic Types
	pLinks current, back;
	fG = 0.777;
	lG = 1977L;
	dG = 1.33333;
	cG = 'z';
	for (iG=0;iG<256;iG++) {
		iGA[iG] = iG;
		lGA[iG] = lG * LONG(iG);
		fGA[iG] = fG * FLOAT(iG);
		dGA[iG] = dG * FLOAT(iG);
		cGA[iG] = CHR(32 + (iG % 26));
	};

	// Link List
	#if defined(EC)
	head = malloc(sizeof(Links));
	#else
	head = (Links *)malloc(sizeof(Links));
	#endif

	current = head;
	back = current;
	for (iG=0;iG<128;iG++) {
		#if defined(EC)
		current->next = malloc(sizeof(Links));
		#else
		current->next = (Links *)malloc(sizeof(Links));
		#endif
		current->c = 'k';
		back = current;
		current = current->next;
	};
	back->next = head;
	tail = back;
	iG = iG * 10;

	// Class
	kG.d = 177.9;
	kG.c = 'K';
	kGptr = ADR(kG);
};

const double LONG_ENOUGH = 3.0;		// Minimum elapse time
const double MIN_ADJUST = 0.01;		// Adust factor

#if defined(SAMPLING)
const double IDX_MSPEED = 66.0;
const double IDX_rps = IDX_MSPEED;
const double IDX_integer = IDX_MSPEED;
const double IDX_float = IDX_MSPEED;
const double IDX_double = IDX_MSPEED;
const double IDX_char = IDX_MSPEED;
const double IDX_class = IDX_MSPEED;
const double IDX_flow = IDX_MSPEED;
const double IDX_pointer = IDX_MSPEED;
const double IDX_long = IDX_MSPEED;
const double IDX_bits = IDX_MSPEED;
/*
const double IDX_rps = ;
const double IDX_integer = ;
const double IDX_float = ;
const double IDX_double = ;
const double IDX_char = ;
const double IDX_class = ;
const double IDX_flow = ;
const double IDX_pointer = ;
const double IDX_long = ;
const double IDX_bits = ;
*/
#else
//////////////////////////////////////////////////
// Index for different operations
//////////////////////////////////////////////////
const double IDX_rps = 8.081374E+00;
const double IDX_integer = 4.514941E+01;
const double IDX_float = 7.056582E+01;
const double IDX_double = 6.629641E+01;
const double IDX_char = 5.411616E+01;
const double IDX_class = 4.454022E+01;
const double IDX_flow = 1.036117E+02;
const double IDX_pointer = 2.029356E+02;
const double IDX_long = 1.094839E+02;
const double IDX_bits = 1.285586E+02;
#endif

void main (void)
{
	// Variables Declarations
	double StartTime, EndTime, DeltaTime, Rps, Spr, ova;	
	Result tm[16];
	int iL, attemps, j;
	long LoopCount, OldCount, i, lL;
	float fL;
	double dL;
	char cL;
	ClassPtr kLptr;

	// Initialization
	InitGlobals();
	tm[0].index = IDX_integer;
	tm[1].index = IDX_float;
	tm[2].index = IDX_double;
	tm[3].index = IDX_char;
	tm[4].index = IDX_class;
	tm[5].index = IDX_flow;
	tm[6].index = IDX_pointer;
	tm[7].index = IDX_long;
	tm[8].index = IDX_bits;

	iL = 1;
	lL = 10000L;
	fL = 1.0;
	dL = 1.0;
	cL = '1';
	kLptr = NULL;
	LoopCount = 1L;
	StartTime = 0.0;
	EndTime = 0.1;
	DeltaTime = EndTime - StartTime;
	attemps = 0;
	
	// main loop
	while (DeltaTime < LONG_ENOUGH) { 
		OldCount = LoopCount;
		// Auto adjusting function
		LoopCount = TRUNC((FLOAT(LoopCount) * LONG_ENOUGH/DeltaTime));
		if (LoopCount < 2L) {
			printf("\nError, loop count < 2\n");
			HALT;
		};
		OldCount = LoopCount - OldCount;
		if (OldCount < 10L) LoopCount = LoopCount + 10L;
		attemps++;
		printf("Proceeding cycle #%d, loops %d\n",attemps, LoopCount);
		// Timer starts
		StartTime = dtime();
		for (i=0L; i<LoopCount; i++) iL = Proc_0(iL,iL);
		tm[0].EndTime = dtime();
		for (i=0L; i<LoopCount; i++) fL = Proc_1(fL, fL);
		tm[1].EndTime = dtime();
		for (i=0L; i<LoopCount; i++) dL = Proc_2(dL, dL);
		tm[2].EndTime = dtime();
		for (i=0L; i<LoopCount; i++) cL = Proc_3(cL, cL);
		tm[3].EndTime = dtime();
		for (i=0L; i<LoopCount; i++) kLptr = Proc_4(kLptr);
		tm[4].EndTime = dtime();
		for (i=0L; i<LoopCount; i++) Proc_5();
		tm[5].EndTime = dtime();
		for (i=0L; i<LoopCount; i++) Proc_6();
		tm[6].EndTime = dtime();
		for (i=0L; i<LoopCount; i++) lL = Proc_7(lL, lL);
		tm[7].EndTime = dtime();
		for (i=0L; i<LoopCount; i++) Proc_8();
		tm[8].EndTime = dtime();
		EndTime = tm[8].EndTime;
		DeltaTime = EndTime - StartTime;
		// Timer Stops
		if (DeltaTime < MIN_ADJUST) DeltaTime = MIN_ADJUST;
	};
	CleanGlobals();
	Rps = FLOAT(LoopCount) / DeltaTime;
	Spr = DeltaTime / FLOAT(LoopCount);
	ova = Rps / IDX_rps;
	// Output
	printf("\nStarts at %e\n", StartTime);
	printf("Ends at %e\n", EndTime);
	printf("User time %e\n",DeltaTime);
	printf("Adjusted %d times\n", attemps);
	printf("Loops: %d\n", LoopCount);
	printf("Runs Per Second = %e\n", Rps);
	printf("Seconds per run = %e\n", Spr);
	#if defined(SAMPLING)
	printf("Overall Speed : %e Mhz\n", ova);
	#else
	printf("Overall Speed : %6.2f Mhz\n", ova);
	#endif
	j = 0;
	tm[j].DeltaTime = FLOAT(LoopCount)/(tm[j].EndTime - StartTime);
	tm[j].index = tm[j].DeltaTime / tm[j].index;
	#if defined(SAMPLING)
	printf("\tInteger      : %e Mhz\n", tm[j].index);
	#else
	printf("\tInteger      : %6.2f Mhz\n", tm[j].index);
	#endif
	for (j=1;j<9;j++) {
		tm[j].DeltaTime = FLOAT(LoopCount)/(tm[j].EndTime - tm[j-1].EndTime);
		tm[j].index = tm[j].DeltaTime / tm[j].index;
		switch (j) {
		case 1 : printf("\tFloat        "); break;
		case 2 : printf("\tDouble       "); break;
		case 3 : printf("\tCharacter    "); break;
		case 4 : printf("\tClass        "); break;
		case 5 : printf("\tControl Flow "); break;
		case 6 : printf("\tPointers     "); break;
		case 7 : printf("\tLong Integer "); break;
		case 8 : printf("\tBits         "); break;
		};
		#if defined(SAMPLING)
		printf(": %e Mhz\n",tm[j].index);
		#else
		printf(": %6.2f Mhz\n",tm[j].index);
		#endif
	};
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -