-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmul_div_ryota-iso.c
121 lines (98 loc) · 2.99 KB
/
mul_div_ryota-iso.c
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
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#define LOGIN "ryota-iso"
long long count;
int loop;
#define LOOP_SEC (2)
static __inline int mul(int x, int y) {
int result = 0;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x == 0) { return result; }
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x == 0) { return result; }
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x == 0) { return result; }
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x == 0) { return result; }
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x == 0) { return result; }
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x == 0) { return result; }
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x == 0) { return result; }
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
if (x & 1) { result += y; } x >>= 1; y <<= 1;
return result;
}
static __inline int div(int x,int y) {
//
// 以下を削除してここに除算のロジックを入れてください。x=32bit , y=32bit, result=32bit
//
return x/y;
}
void alarm_mul(int signum){
printf("MUL: %lld K instructions/sec\n", count/LOOP_SEC/1000);
loop = 0;
}
void alarm_div(int signum){
printf("DIV: %lld K instructions/sec\n", count/LOOP_SEC/1000);
loop = 0;
}
int main() {
int x,y;
printf("Start (%s program)\n", LOGIN);
// Benchmark MUL
count = 0;
loop = 1;
signal(SIGALRM, alarm_mul);
alarm(LOOP_SEC);
for (x=1; loop ; ++x) {
for (y=1; y<0xffff; ++y) {
if (mul(x,y) != x*y) {
printf("MUL: Calculation results are incorrect. results=%d, expected=%d\n", mul(x,y), x*y);
return(-1);
}
++count;
}
}
// Benchmark DIV
count = 0;
loop = 1;
signal(SIGALRM, alarm_div);
alarm(LOOP_SEC);
for (x=0x0fffffff; loop ; --x) {
for (y=1; y<0xffff; ++y) {
if (div(x,y) != x/y) {
printf("DIV: Calculation results are incorrect. results=%d, expected=%d\n", div(x,y), x/y);
return(-1);
}
++count;
}
}
return 0;
}