[fpc-pascal] Optimisation : X+X or 2*X or X*2 ?

Nikolay Nikolov nickysn at users.sourceforge.net
Sun Jan 19 21:54:09 CET 2014


On 01/19/2014 10:45 PM, Sven Barth wrote:
> On 19.01.2014 20:47, Ched wrote:
>> Dear Freepascalers,
>>
>> The multiplication in floating arthmetic units is usualy know as being
>> slower than the addition. So, have I interest to code X+X, 2*X or X*2
>> when using reals (mainly double and extended) ?And with integers ?
>
> Can't really comment on floats, but for integers the fasted to 
> multiply by 2 should be a shift left.
>
>> Can the compiler optimise this operation choosing the fastest one -
>> probably depending on the architecture x86, arm, aso.
>
> It can if someone codes support for it (maybe some code generators 
> already have such simplifications). I thought I've at least seen such 
> code for integers (that exactly converts *2 to a shl), but I can't 
> find it currently...
Integer multiplication by a power of 2 constant is converted to a SHL in 
pass 1, see nadd.pas:2869.

Nikolay



More information about the fpc-pascal mailing list