Re: [Algorithms] Shifting or Multiplication....
Brought to you by:
vexxed72
From: Ville M. <wi...@hy...> - 2001-01-05 20:28:13
|
Modern compilers will turn your constant integer multiplications into sequences of shifts, additions and similar instructions (such as 'lea' on the x86), if the strength-reduced sequence is faster than the single multiplication. Back in the bad old days, _anything_ was faster than a mul op, nowadays two adds for a mul is probably a fair deal (x86 again); however, there are tons of other things that must be taken into account (code size, register dependencies etc.), so no exact rules can be given even for a specific processor... Let the compiler worry about such things as it has more knowledge of the situation... Similar strength reduction happens for constant divisions of powers-of-two (such as foo/4 --> foo>>2). You just have to keep in mind that dividing a negative integer by a power-of-two is not the same as right-shifting (though it is for positive values), so the compiler needs to generate some extra code. Even with the extra code, the sequence is faster than performing the division. However, you can optimize such a sequence by either hand-writing the equation with right-shifts or by casting the integer to unsigned (assuming you know it is positive in the first place). A similar thing happens with modulas, i.e. foo%8 --> foo&7 reduction cannot be done (without extra code) if 'foo' may be negative. As a rule of thumb, I use multiplications, divisions and modulas for all arithmetics (which are always done with signed integers). I use shifts and logical ands/ors/xors for all bitmask manipulation (unsigned ints). I think that this separation makes the code more readable. So, to answer the original question: I would definitely use shifts etc. for pure integer pixel manipulation (as this falls into the category of bitmasks), just to improve the readability of the code. cheers, -wili Ville Miettinen http://surrender3d.com/umbra On Wed, 27 Dec 2000, Corrinne Yu wrote: > Floating point multiplication can be fast on PC based systems. Some > compilers can choose to turn all your * 2 into << 1 automatically for you > for integers. > > IMO nothing to get too stressed about. Whatever is readable and debuggable. > :) > > Corrinne > > ----- Original Message ----- > From: Alex Morano > To: gda...@li... > Sent: Saturday, December 23, 2000 2:38 PM > Subject: [Algorithms] Shifting or Multiplication.... > > > I was wondering if anyone could clear up something. Algorithmically, is > bitshifting or using multiplication faster?? Now, I know this sounds like a > subjective question, as some will undoubtadly say, it depends on the > compiler and what it translates the code into. Lets take it down to the ASM > level. > > Shifting, to me, looks like a 1 cycle operation. > > In this age, if the CPU can handle most MUL's almost as rapidly, is > there a place for bitshifts? If so, then for what kinds of algo > applications? Do we still need them to mask off bits in pixels colors? > > > _______________________________________________ > GDAlgorithms-list mailing list > GDA...@li... > http://lists.sourceforge.net/mailman/listinfo/gdalgorithms-list > |