-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_78.c
41 lines (38 loc) · 1.02 KB
/
2_78.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
#include <stdio.h>
#include <limits.h>
/*
* 5*x/8其实就是((x << 2) + x) >> 3,但是5*x有可能会溢出,或者x < 0的时候,使用移位操作会导致不是向0舍入
* 所以需要向divide_power2那么处理
*/
int mul5div8(int x) {
int m = (x << 2) + x;
int sign = !!(m & INT_MIN);
sign && (m = (m + 8 - 1));
return m >> 3;
}
int expected(int x) {
return 5 * x / 8;
}
int main(void)
{
printf(" expected\t mul5div8\n");
int x = 0;
printf("%10d\t%10d\n", expected(x), mul5div8(x));
x = INT_MAX;
printf("%10d\t%10d\n", expected(x), mul5div8(x));
x = INT_MIN;
printf("%10d\t%10d\n", expected(x), mul5div8(x));
x = 10;
printf("%10d\t%10d\n", expected(x), mul5div8(x));
x = -10;
printf("%10d\t%10d\n", expected(x), mul5div8(x));
x = 1;
printf("%10d\t%10d\n", expected(x), mul5div8(x));
x = -1;
printf("%10d\t%10d\n", expected(x), mul5div8(x));
x = 12340;
printf("%10d\t%10d\n", expected(x), mul5div8(x));
x = -12340;
printf("%10d\t%10d\n", expected(x), mul5div8(x));
return 0;
}