33,311
社区成员
发帖
与我相关
我的任务
分享
inline int min( int a, int b)
{
int mask = (a - b) >> 31;
return (a & mask) | (b & ~mask);
}
int main(void)
{
int i = -1500000000;
int j = 1500000000;
printf("min=%d", min(i, j));
return 0;
}
00000000 55 push ebp
00000001 89E5 mov ebp,esp
00000003 8B550C mov edx,[ebp+0xc]
00000006 8B4508 mov eax,[ebp+0x8]
00000009 39D0 cmp eax,edx
0000000B 7E02 jng 0xf
0000000D 89D0 mov eax,edx
0000000F 5D pop ebp
00000010 C3 ret
00000000 55 push ebp
00000001 89E5 mov ebp,esp
00000003 83EC10 sub esp,byte +0x10
00000006 8B450C mov eax,[ebp+0xc]
00000009 8B5508 mov edx,[ebp+0x8]
0000000C 89D1 mov ecx,edx
0000000E 29C1 sub ecx,eax
00000010 89C8 mov eax,ecx
00000012 C1F81F sar eax,0x1f
00000015 8945FC mov [ebp-0x4],eax
00000018 8B45FC mov eax,[ebp-0x4]
0000001B 8B5508 mov edx,[ebp+0x8]
0000001E 21C2 and edx,eax
00000020 8B45FC mov eax,[ebp-0x4]
00000023 F7D0 not eax
00000025 23450C and eax,[ebp+0xc]
00000028 09D0 or eax,edx
0000002A C9 leave
0000002B C3 ret
#include <sys/time.h>
#include <stdio.h>
int min1(int a,int b)
{
return a>b?b:a;
}
int min2(int a,int b)
{
int mask = (a - b) >> 31;
return (a & mask) | (b & ~mask);
}
int main()
{
struct timeval t,end;
gettimeofday(&t,NULL);
int i=0;
while(i++<10000){
min1(1,2);
}
gettimeofday(&end,NULL);
printf("%lfsec,%lfusec\n",(double)end.tv_sec-t.tv_sec,(double)end.tv_usec-t.tv_usec);
gettimeofday(&t,NULL);
i=0;
while(i++<10000){
min2(1,2);
}
gettimeofday(&end,NULL);
printf("%lfsec,%lfusec\n",(double)end.tv_sec-t.tv_sec,(double)end.tv_usec-t.tv_usec);
return 0;
}