一般的代码: (当然,输出数组也可以是另外一个数组,下同)
uint8 a[10000];
for (int i=0;i<10000;++i)
a[i]=a[i]>>1;
uint8 a[10000];
uint32* a32=(uint32*)a; //实际代码可能需要考虑内存访问对齐和边界处理问题,下同
for (int i=0;i<2500;++i){
uint32 c=a32[i]&0xFEFEFEFE;
a32[i]=c>>1;
}
uint8 a[10000];
uint32* c=(uint32*)a;
for (int i=0;i<2500;++i){
a32[i]=~a32[i];
}
uint8 a[10000];
uint8 b[10000];
for (int i=0;i<10000;++i)
a[i]=(a[i]+b[i])>>1;//我见过的一个处理图像颜色50%混合的代码
uint8 a[10000];
uint8 b[10000];
uint32* a32=(uint32*)a;
uint32* b32=(uint32*)b;
for (int i=0;i<2500;++i){
uint32 c=a32[i];
uint32 d=b32[i];
uint32 e_1_3 =(c & 0xFF00FF00)>>1;
uint32 e_0_2 =(c & 0x00FF00FF);
e_1_3+=(d & 0xFF00FF00)>>1;
e_0_2+=(d & 0x00FF00FF);
a32[i]=((e_1_3 & 0xFF00FF00)) | ((e_0_2>>1) & 0x00FF00FF);
}
uint8 a[10000];
uint8 b[10000];
uint32* a32=(uint32*)a;
uint32* b32=(uint32*)b;
for (int i=0;i<2500;++i){
a32[i]=(a32[i]&0xFEFEFEFE>>1)+(b32[i]&0xFEFEFEFE>>1);
}
uint8 a[10000];
uint8 b[10000];
uint32* a32=(uint32*)a;
uint32* b32=(uint32*)b;
for (int i=0;i<2500;++i){
uint32 c=a32[i];
uint32 d=b32[i];
a32[i]=(c&d) + (((c^d) & 0xFEFEFEFE) >> 1);
}
//(还可以试试,注意最后一个bit位 (c|d)- (((c^d)&0xFEFEFEFE)>>1); )
问题四
: 按指定比例混合两个字节流 (alphaBlend混合,线性插值缩放等常用的算法)
uint8 a[10000];
uint8 b[10000];
int s=13; //s 可能属于[0..255];
for (int i=0;i<10000;++i){
int c=a[i];
a[i]=((c<<8)+(b[i]-c)*s)>>8;
}
//如果不能有误差,这里可以用公式(x/255)==(x*32897>>23)==(x+(x>>8)+1)>>8;
使用SIMD思路的代码(2路数据流同时计算):
uint8 a[10000];
uint8 b[10000];
int s=13; //s 可能属于[0..255];
uint32* a32=(uint32*)a;
uint32* b32=(uint32*)b;
int rs=256-s;
for (int i=0;i<2500;++i){
uint32 c=a32[i];
uint32 d=b32[i];
uint32 e_0_2=(c & 0x00FF00FF)*rs + (d & 0x00FF00FF)*s;
uint32 e_1_3=((c & 0xFF00FF00)>>8)*rs + ((d & 0xFF00FF00)>>8)*s;
a32[i]=((e_0_2 & 0xFF00FF00)>>8) | (e_1_3 & 0xFF00FF00);
}
uint8 a[10000];
for (int i=0;i<10000;++i){
if (a[i]==0)
return i;
}
return -1;
uint8 a[10000];
uint32* a32=(uint32*)a;
uint32 test=0;
int i=0;
for (;i<2500;++i){
test=(a32[i]-0x01010101)&0x80808080;
if (test!=0)
break;
}
if (test==0)
return -1;
i*=4;
while ((test&0x80)==0){
++i;
test>>=8;
}
return i;
uint8 a[10000];
uint32* a32=(uint32*)a;
uint32 test=0;
int i=0;
for (;i<2500;++i){
uint32 c=a32[i];
c=((c&0xF0F0F0F0)>>4)|(c&0x0F0F0F0F);
test=(c-0x01010101)&0x80808080;
if (test!=0)
break;
}
if (test==0)
return -1;
i*=4;
while ((test&0x80)==0){
++i;
test>>=8;
}
return i;