shift 64 bit
Posted: 23 Aug 2006, 16:24
On 32 bit processor the right shift operator (>>) of 64 bit integer (u64) is very slow.
My code below seems faster.
I tried it with visual c++ on a Pentium 4 processor.
shift 7 bit...
u64 shr7(const u64 bits){ //bits >> 7
unsigned s2 = (((unsigned*)&bits)[1]);
u64 x=(((unsigned) bits)>>7)|(s2<<25); // 25 = 32 - 7
((unsigned*)(&x)+1)[0]=(s2>>7);
return x;
}
shift N bit...
u64 shrN(const u64 bits,const int N){ //bits >> N
assert(N);
if (N<32){
unsigned s2 = (((unsigned*)&bits)[1]);
u64 x=(((unsigned) bits)>>N)|(s2<<(32-N));
((unsigned*)(&x)+1)[0]=(s2>>N);
return x;
}
u64 x=shift32(bits)>>(N-32);
return x;
}
bye giuseppe
My code below seems faster.
I tried it with visual c++ on a Pentium 4 processor.
shift 7 bit...
u64 shr7(const u64 bits){ //bits >> 7
unsigned s2 = (((unsigned*)&bits)[1]);
u64 x=(((unsigned) bits)>>7)|(s2<<25); // 25 = 32 - 7
((unsigned*)(&x)+1)[0]=(s2>>7);
return x;
}
shift N bit...
u64 shrN(const u64 bits,const int N){ //bits >> N
assert(N);
if (N<32){
unsigned s2 = (((unsigned*)&bits)[1]);
u64 x=(((unsigned) bits)>>N)|(s2<<(32-N));
((unsigned*)(&x)+1)[0]=(s2>>N);
return x;
}
u64 x=shift32(bits)>>(N-32);
return x;
}
bye giuseppe