PAPI  5.7.0.0
instructions_testcode.c
Go to the documentation of this file.
1 #include "testcode.h"
2 
3  /* Test a simple loop of 1 million instructions */
4  /* Most implementations should count be correct within 1% */
5  /* This loop in in assembly language, as compiler generated */
6  /* code varies too much. */
7 
9 
10 #if defined(__i386__) || (defined __x86_64__)
11  asm( " xor %%ecx,%%ecx\n"
12  " mov $499999,%%ecx\n"
13  "test_loop:\n"
14  " dec %%ecx\n"
15  " jnz test_loop\n"
16  : /* no output registers */
17  : /* no inputs */
18  : "cc", "%ecx" /* clobbered */
19  );
20  return 0;
21 #elif defined(__PPC__)
22  asm( " nop # to give us an even million\n"
23  " lis 15,499997@ha # load high 16-bits of counter\n"
24  " addi 15,15,499997@l # load low 16-bits of counter\n"
25  "55:\n"
26  " addic. 15,15,-1 # decrement counter\n"
27  " bne 0,55b # loop until zero\n"
28  : /* no output registers */
29  : /* no inputs */
30  : "cc", "15" /* clobbered */
31  );
32  return 0;
33 #elif defined(__ia64__)
34 
35  asm( " mov loc6=166666 // below is 6 instr.\n"
36  " ;; // because of that we count 4 too few\n"
37  "55:\n"
38  " add loc6=-1,loc6 // decrement count\n"
39  " ;;\n"
40  " cmp.ne p2,p3=0,loc6\n"
41  "(p2) br.cond.dptk 55b // if not zero, loop\n"
42  : /* no output registers */
43  : /* no inputs */
44  : "p2", "loc6" /* clobbered */
45  );
46  return 0;
47 #elif defined(__sparc__)
48  asm( " sethi %%hi(333333), %%l0\n"
49  " or %%l0,%%lo(333333),%%l0\n"
50  "test_loop:\n"
51  " deccc %%l0 ! decrement count\n"
52  " bnz test_loop ! repeat until zero\n"
53  " nop ! branch delay slot\n"
54  : /* no output registers */
55  : /* no inputs */
56  : "cc", "l0" /* clobbered */
57  );
58  return 0;
59 #elif defined(__arm__)
60  asm( " ldr r2,count @ set count\n"
61  " b test_loop\n"
62  "count: .word 333332\n"
63  "test_loop:\n"
64  " add r2,r2,#-1\n"
65  " cmp r2,#0\n"
66  " bne test_loop @ repeat till zero\n"
67  : /* no output registers */
68  : /* no inputs */
69  : "cc", "r2" /* clobbered */
70  );
71  return 0;
72 #elif defined(__aarch64__)
73  asm( " ldr x2,=333332 // set count\n"
74  "test_loop:\n"
75  " add x2,x2,#-1\n"
76  " cmp x2,#0\n"
77  " bne test_loop // repeat till zero\n"
78  : /* no output registers */
79  : /* no inputs */
80  : "cc", "r2" /* clobbered */
81  );
82  return 0;
83 #endif
84 
85  return CODE_UNIMPLEMENTED;
86 
87 }
88 
89 
90 /* fldcw instructions are counted oddly on Pentium 4 machines */
91 
92 int instructions_fldcw(void) {
93 
94 #if defined(__i386__) || (defined __x86_64__)
95 
96  int saved_cw,result,cw;
97  double three=3.0;
98 
99  asm( " mov $100000,%%ecx\n"
100  "big_loop:\n"
101  " fldl %1 # load value onto fp stack\n"
102  " fnstcw %0 # store control word to mem\n"
103  " movzwl %0, %%eax # load cw from mem, zero extending\n"
104  " movb $12, %%ah # set cw for \"round to zero\"\n"
105  " movw %%ax, %3 # store back to memory\n"
106  " fldcw %3 # save new rounding mode\n"
107  " fistpl %2 # save stack value as integer to mem\n"
108  " fldcw %0 # restore old cw\n"
109  " loop big_loop # loop to make the count more obvious\n"
110  : /* no output registers */
111  : "m"(saved_cw), "m"(three), "m"(result), "m"(cw) /* inputs */
112  : "cc", "%ecx","%eax" /* clobbered */
113  );
114  return 0;
115 #endif
116 
117  return CODE_UNIMPLEMENTED;
118 }
119 
120 
121 /* rep instructions are counted a bit non-intuitively */
122 /* some tools like Valgrind and Pin may count differently than real hardware */
123 
124 int instructions_rep(void) {
125 
126 #if defined(__i386__) || defined(__ILP32__)
127 
128  char buffer_out[16384];
129 
130  asm( " mov $1000,%%edx\n"
131  " cld\n"
132  "loadstore: # test 8-bit store\n"
133  " mov $0xd, %%al # set eax to d\n"
134  " mov $16384, %%ecx\n"
135  " mov %0, %%edi # set destination\n"
136  " rep stosb # store d 16384 times, auto-increment\n"
137  " dec %%edx\n"
138  " jnz loadstore\n"
139  : /* outputs */
140  : "rm" (buffer_out) /* inputs */
141  : "cc", "%esi","%edi","%edx","%ecx","%eax","memory" /* clobbered */
142  );
143  return 0;
144 #elif defined (__x86_64__)
145 
146  char buffer_out[16384];
147 
148  asm( " mov $1000,%%edx\n"
149  " cld\n"
150  "loadstore: # test 8-bit store\n"
151  " mov $0xd, %%al # set eax to d\n"
152  " mov $16384, %%ecx\n"
153  " mov %0, %%rdi # set destination\n"
154  " rep stosb # store d 16384 times, auto-increment\n"
155  " dec %%edx\n"
156  " jnz loadstore\n"
157  : /* outputs */
158  : "rm" (buffer_out) /* inputs */
159  : "cc", "%esi","%edi","%edx","%ecx","%eax","memory" /* clobbered */
160  );
161  return 0;
162 #endif
163 
164  return CODE_UNIMPLEMENTED;
165 
166 }
#define CODE_UNIMPLEMENTED
Definition: testcode.h:2
int instructions_million(void)
int instructions_rep(void)
int instructions_fldcw(void)