PAPI  5.7.0.0
branches_testcode.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 #include "testcode.h"
5 
6 
7 /* This code has 1,500,000 total branches */
8 /* 500,000 not-taken conditional branches */
9 /* 500,000 taken conditional branches */
10 /* 500,000 unconditional branches */
11 int branches_testcode(void) {
12 
13 #if defined(__i386__) || (defined __x86_64__)
14  asm( "\txor %%ecx,%%ecx\n"
15  "\tmov $500000,%%ecx\n"
16  "test_loop:\n"
17  "\tjmp test_jmp\n"
18  "\tnop\n"
19  "test_jmp:\n"
20  "\txor %%eax,%%eax\n"
21  "\tjnz test_jmp2\n"
22  "\tinc %%eax\n"
23  "test_jmp2:\n"
24  "\tdec %%ecx\n"
25  "\tjnz test_loop\n"
26  : /* no output registers */
27  : /* no inputs */
28  : "cc", "%ecx", "%eax" /* clobbered */
29  );
30  return 0;
31 
32 #elif defined(__arm__)
33  /* Initial code contributed by sam wang linux.swang _at_ gmail.com */
34  asm( "\teor r3,r3,r3\n"
35  "\tldr r3,=500000\n"
36  "test_loop:\n"
37  "\tB test_jmp\n"
38  "\tnop\n"
39  "test_jmp:\n"
40  "\teor r2,r2,r2\n"
41  "\tcmp r2,#1\n"
42  "\tbge test_jmp2\n"
43  "\tnop\n"
44  "\tadd r2,r2,#1\n"
45  "test_jmp2:\n"
46  "\tsub r3,r3,#1\n"
47  "\tcmp r3,#1\n"
48  "\tbgt test_loop\n"
49  : /* no output registers */
50  : /* no inputs */
51  : "cc", "r2", "r3" /* clobbered */
52  );
53 
54  return 0;
55 #elif defined(__aarch64__)
56  asm( "\teor x3,x3,x3\n"
57  "\tldr x3,=500000\n"
58  "test_loop:\n"
59  "\tB test_jmp\n"
60  "\tnop\n"
61  "test_jmp:\n"
62  "\teor x2,x2,x2\n"
63  "\tcmp x2,#1\n"
64  "\tbge test_jmp2\n"
65  "\tnop\n"
66  "\tadd x2,x2,#1\n"
67  "test_jmp2:\n"
68  "\tsub x3,x3,#1\n"
69  "\tcmp x3,#1\n"
70  "\tbgt test_loop\n"
71  : /* no output registers */
72  : /* no inputs */
73  : "cc", "x2", "x3" /* clobbered */
74  );
75 
76  return 0;
77 #elif defined(__powerpc__)
78  /* Not really optimized */
79 
80  asm( "\txor 3,3,3\n"
81  "\tlis 3,500000@ha\n"
82  "\taddi 3,3,500000@l\n"
83  "test_loop:\n"
84  "\tb test_jmp\n"
85  "\tnop\n"
86  "test_jmp:\n"
87  "\txor 4,4,4\n"
88  "\tcmpwi cr0,4,1\n"
89  "\tbge test_jmp2\n"
90  "\tnop\n"
91  "\taddi 4,4,1\n"
92  "test_jmp2:\n"
93  "\taddi 3,3,-1\n"
94  "\tcmpwi cr0,3,1\n"
95  "\tbgt test_loop\n"
96  : /* no output registers */
97  : /* no inputs */
98  : "cr0", "r3", "r4" /* clobbered */
99  );
100 
101  return 0;
102 #endif
103 
104  return -1;
105 
106 }
107 
108 
109 int random_branches_testcode(int number, int quiet) {
110 
111  int j,junk=0;
112  double junk2=5.0;
113 
114  for(j=0;j<number;j++) {
115 
116  if (( ((random()>>2)^(random()>>4)) %1000)>500) goto label_false;
117 
118  junk++; /* can't just add, the optimizer is way too clever */
119 
120  junk2*=junk;
121 
122  //printf("T");
123  label_false:
124  //printf("F");
125  ;
126  }
127  if (!quiet) printf("%lf\n",junk2);
128 
129  return junk;
130 }
131 
int random_branches_testcode(int number, int quiet)
int junk
Definition: fileop.c:77
int quiet
Definition: rapl_overflow.c:18
int branches_testcode(void)