Subj : Re: Multiplications faster than bit shifts? To : comp.programming From : websnarf Date : Sat Jul 09 2005 04:08 pm Willem wrote: > Eric wrote: > ) I read a post by someone recently, and they asserted that multiplications > ) by two were faster on recent x86 cpus than simple bit shifts. > ) > ) I have a very hard time believing a simple shift would take longer than a > ) multiplication and wanted to know what you people thought about this. > > A multiplication by two is (probably) slower than a shift by one bit, but > a multiplication by sixteen is probably faster than a shift by four bits. > > This is because recent x86 cpus don't have barrel shifters anymore, > so they have to do X consecutive shifts to get a shift by X bits. You better define what you mean by "recent x86 CPUs". The Athlon and Athlon 64s have 3 parallel barrel shifters. And Intel has all but abandoned the Pentium 4 (where they had no barrel shifter) and gone back to the Pentium-Pro architecture (now dubbed Pentium-M) which has one barrel shifter. > I'm not sure where the boundary is between a shift or a multiply > being faster. For powers of two, even on the Pentium 4, shifting is faster than multiplication. BTW, shift by one is optimally replaced by adding the value to itself. -- Paul Hsieh http://www.pobox.com/~qed/ http://bstring.sf.net/ .