http://bbs.csdn.net/topics/390966142
//有两个相同元素个数的数组,把这两个数组的元素按位轮流插入,得到新的数组。
//例如:数组a第一个元素为10,数组b第一个元素为5。数组为int32型。那么换算成二进制
// 10为0000 0000 0000 0000 0000 0000 0000 1010
// 5为0000 0000 0000 0000 0000 0000 0000 0101
//按位轮流插入后为
//0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1001 1001
//变成了一个64位。前32位存新数组1,后32位存新数组2.
//算法要求,时间复杂度低,不要新增数组
#include <stdio.h>
#include <stdlib.h>
#define N 2
int a[N],b[N],one[N],two[N];
char bs[33];
char bb[33];
int i,v;
void process() {
int k=0;
__asm {
push esi
push edi
push ebx
push ecx
Lk:
lea esi,a
add esi,k
mov eax,[esi]
lea edi,b
add edi,k
mov ebx,[edi]
mov ecx,16
L16_1:
rcl eax,1
rcl edx,1
rcl ebx,1
rcl edx,1
loop L16_1
lea ecx,one
add ecx,k
mov [ecx],edx
mov ecx,16
L16_2:
rcl eax,1
rcl edx,1
rcl ebx,1
rcl edx,1
loop L16_2
lea ecx,two
add ecx,k
mov [ecx],edx
add k,4
cmp k,N*4
jb Lk
pop ecx
pop ebx
pop edi
pop esi
}
}
int main() {
a[0]=10;a[1]=-1;
b[0]= 5;b[1]= 0;
for (i=0;i<N;i++) {
v=a[i];
ltoa(v,bb,2);
sprintf(bs,"%032s",bb);
printf("a[%d]==%s\n",i,bs);
}
for (i=0;i<N;i++) {
v=b[i];
ltoa(v,bb,2);
sprintf(bs,"%032s",bb);
printf("b[%d]==%s\n",i,bs);
}
process();
for (i=0;i<N;i++) {
v=one[i];
ltoa(v,bb,2);
sprintf(bs,"%032s",bb);
printf("one[%d]==%s\n",i,bs);
}
for (i=0;i<N;i++) {
v=two[i];
ltoa(v,bb,2);
sprintf(bs,"%032s",bb);
printf("two[%d]==%s\n",i,bs);
}
return 0;
}
//a[0]==00000000000000000000000000001010
//a[1]==11111111111111111111111111111111
//b[0]==00000000000000000000000000000101
//b[1]==00000000000000000000000000000000
//one[0]==00000000000000000000000000000000
//one[1]==10101010101010101010101010101010
//two[0]==00000000000000000000000010011001
//two[1]==10101010101010101010101010101010
//
------解决思路----------------------
我是来混分的,愿大神光芒照耀我身,助我修得正果,燃尽码中BUG,语言归一,浑然无缺。
------解决思路----------------------
看炫技 + 接分
------解决思路----------------------
这是在一台农企A10核显电脑上的测试结果(使用VC++ 2010编译,/Ox优化):
D:\TEMP>test64
--- Test data ---
Buffer 1 =
0000000a 000000ff 12345678 89abcdef
Buffer 2 =
00000005 00000000 56780000 90a0b0c0
--- Validate functionality ---
Result of BitShuffle32() =
0000000000000099 000000000000aaaa 131c1f6022282a80 c182cc8ae5a2f8aa
Result of BitShuffle32_asm_ssse3() =
0000000000000099 000000000000aaaa 131c1f6022282a80 c182cc8ae5a2f8aa
Result of BitShuffle32_z4z() =
0000000000000099 000000000000aaaa 131c1f6022282a80 c182cc8ae5a2f8aa
--- Test 20000000 loops for each function ---
BitShuffle32() ... 0.280 seconds
BitShuffle32_asm_ssse3() ... 0.250 seconds
BitShuffle32_z4z() ... 3.635 seconds