File: new_test_x.c

package info (click to toggle)
gxemul 0.7.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,152 kB
  • sloc: ansic: 111,065; sh: 972; exp: 354; makefile: 118
file content (185 lines) | stat: -rw-r--r-- 3,385 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>

/*  #define	N_CALLS_PER_PAGE	1024  */
#define	N_CALLS_PER_PAGE	128

#define MHZ	533

struct cpu;

struct instr_call {
	void	(*f)(struct cpu *cpu, struct instr_call *ic);
/*	int	instr_len;  */
	void	*arg[3];
};

struct cpu {
	void	*curpage;
	int	orig_nloops;
	int	nloops;

	struct instr_call *next_instr_call;
};

void r(struct cpu *cpu)
{
	struct instr_call *ic;

	for (;;) {
		ic = cpu->next_instr_call++;
		ic->f(cpu, ic);

		ic = cpu->next_instr_call++;
		ic->f(cpu, ic);

		ic = cpu->next_instr_call++;
		ic->f(cpu, ic);

		ic = cpu->next_instr_call++;
		ic->f(cpu, ic);

		ic = cpu->next_instr_call++;
		ic->f(cpu, ic);

		ic = cpu->next_instr_call++;
		ic->f(cpu, ic);

		ic = cpu->next_instr_call++;
		ic->f(cpu, ic);

		ic = cpu->next_instr_call++;
		ic->f(cpu, ic);
	}
}


void f_add(struct cpu *cpu, struct instr_call *ic)
{
#if 0
#if 0
	int *a = (int *) ic->arg[0];
	int *b = (int *) ic->arg[1];
	int *c = (int *) ic->arg[2];

	*a = (*b) + (*c);
#else
	int *a = (int *) ic->arg[0];
	int *b = (int *) ic->arg[1];
	int *c = (int *) ic->arg[2];
	int *d, *e, *f;

	ic = cpu->next_instr_call++;
	d = (int *) ic->arg[0];
	e = (int *) ic->arg[1];
	f = (int *) ic->arg[2];

	*a = (*b) + (*c);
	*d = (*e) + (*f);
#endif
#endif
}


void f_sub(struct cpu *cpu, struct instr_call *ic)
{
	int *a = (int *) ic->arg[0];
	int *b = (int *) ic->arg[1];
	int *c = (int *) ic->arg[2];
	*a = (*b) - (*c);
}


void f_and(struct cpu *cpu, struct instr_call *ic)
{
	int *a = (int *) ic->arg[0];
	int *b = (int *) ic->arg[1];
	int *c = (int *) ic->arg[2];
	*a = (*b) & (*c);
}


void f_or(struct cpu *cpu, struct instr_call *ic)
{
	int *a = (int *) ic->arg[0];
	int *b = (int *) ic->arg[1];
	int *c = (int *) ic->arg[2];
	*a = (*b) | (*c);
}


void f_end(struct cpu *cpu, struct instr_call *ic)
{
	struct rusage rusage;
	double t, mips;

	cpu->nloops--;
	if (cpu->nloops > 0) {
		cpu->next_instr_call = cpu->curpage;
		return;
	}

	getrusage(RUSAGE_SELF, &rusage);
	t = rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec / 1000000.0;
	mips = (float)(N_CALLS_PER_PAGE * cpu->orig_nloops) / t
	    / 1000000.0;

	printf("%.2f user seconds = %.1f MIPS.  %.1f cycles/emulated "
	    "instruction on a %i MHz machine\n", (float)t, (float)mips,
	    (float)MHZ/mips, MHZ);
	exit(1);
}


int main(int argc, char *argv[])
{
	int32_t tmp_a, tmp_b, tmp_c;
	struct instr_call *call_array;
	int i, ncalls;
	struct cpu *cpu = malloc(sizeof(struct cpu));

	if (argc <= 1) {
		fprintf(stderr, "usage: %s n\n", argv[0]);
		exit(1);
	}

	cpu->orig_nloops = cpu->nloops = atoi(argv[1]);
	ncalls = N_CALLS_PER_PAGE + 1;

	/*  Fill a range of nonsense calls:  */
	call_array = malloc(sizeof(struct instr_call) * ncalls);
	cpu->curpage = call_array;

	printf("ncalls = %i\n", ncalls);
	for (i=0; i<ncalls; i++) {
		if (i == ncalls-1) {
			call_array[i].f = f_end;
		} else {
#if 0
			switch (i & 3) {
			case 0:	call_array[i].f = f_add;
			case 1:	call_array[i].f = f_sub;
			case 2:	call_array[i].f = f_and;
			case 3:	call_array[i].f = f_or;
			}
#else
			call_array[i].f = f_add;
#endif

			call_array[i].arg[0] = &tmp_a;
			call_array[i].arg[1] = &tmp_b;
			call_array[i].arg[2] = &tmp_c;
		}
	}

	printf("running...\n");
	cpu->next_instr_call = &call_array[0];
	r(cpu);

	printf("ERROR!\n");
	return 0;
}