-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmul_div_macchan.c
169 lines (147 loc) · 3.18 KB
/
mul_div_macchan.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
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
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#define LOGIN "macchan"
long long count;
int loop;
#define LOOP_SEC (2)
static __inline int mul(int x, int y) {
//
// 以下を削除してここに乗算のロジックを入れてください。x=32bit , y=32bit, result=64bit
//
#ifdef NO
return x*y;
#endif
#ifdef NO
int ans, i;
ans = 0;
for (i=0; i< 16; ++i) {
if (y & 1)
ans += x;
y = y >> 1;
x = x << 1;
}
return ans;
#endif
#ifndef NO
// return ((x & 32768) ? (y << 15) : 0) +
// return ((x & 16384) ? (y << 14) : 0) +
// return ((x & 8192) ? (y << 13) : 0) +
// return ((x & 4096) ? (y << 12) : 0) +
return ((x & 2048) ? (y << 11) : 0) +
((x & 1024) ? (y << 10) : 0) +
((x & 512) ? (y << 9) : 0) +
((x & 256) ? (y << 8) : 0) +
((x & 128) ? (y << 7) : 0) +
((x & 64) ? (y << 6) : 0) +
((x & 32) ? (y << 5) : 0) +
((x & 16) ? (y << 4) : 0) +
((x & 8) ? (y << 3) : 0) +
((x & 4) ? (y << 2) : 0) +
((x & 2) ? (y << 1) : 0) +
((x & 1) ? y : 0);
#endif
}
static __inline int msb(int a)
{
#ifdef NO
int c = 0;
while (a != 0) {
a >>= 1;
++c;
}
return c;
#endif
#ifndef NO
return (a & 0x8000000) ? 27 :
(a & 0x4000000) ? 26 :
(a & 0x2000000) ? 25 :
(a & 0x1000000) ? 24 :
(a & 0x0800000) ? 23 :
(a & 0x0400000) ? 22 :
(a & 0x0200000) ? 21 :
(a & 0x0100000) ? 20 :
(a & 0x0080000) ? 19 :
(a & 0x0040000) ? 18 :
(a & 0x0020000) ? 17 :
(a & 0x0010000) ? 16 :
(a & 0x0008000) ? 15 :
(a & 0x0004000) ? 14 :
(a & 0x0002000) ? 13 :
(a & 0x0001000) ? 12 :
(a & 0x0000800) ? 11 :
(a & 0x0000400) ? 10 :
(a & 0x0000200) ? 9 :
(a & 0x0000100) ? 8 :
(a & 0x0000080) ? 7 :
(a & 0x0000040) ? 6 :
(a & 0x0000020) ? 5 :
(a & 0x0000010) ? 4 :
(a & 0x0000008) ? 3 :
(a & 0x0000004) ? 2 :
(a & 0x0000002) ? 1 :
(a & 0x0000001) ? 0 : 0;
#endif
}
static __inline int div(int x,int y) {
//
// 以下を削除してここに除算のロジックを入れてください。x=32bit , y=32bit, result=32bit
//
#ifdef NO
return x/y;
#endif
int ans = 0, d;
int c = msb(x) - msb(y);
while ( c >= 0 ) {
d = x - (y << c);
if (d >= 0) {
x = d;
ans |= (1 << c);
}
--c;
}
return ans;
}
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 (x=0x0ffff; loop ; --x) {
for (y=1; y<0xffff; ++y) {
if (div(x,y) != x/y) {
printf("DIV: %X/%X=Calculation results are incorrect. results=%d, expected=%d\n", x, y, div(x,y), x/y);
// return(-1);
}
++count;
}
}
return 0;
}