Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-01

Optimisation : X+X or 2*X or X*2 ?

9 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ched

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 the compiler optimise this operation choosing the fastest one - probably depending on the architecture x86, arm, aso.

Cheers, Raoul

Philippe
to Ched

forgot

X shl 1

normaly the fastest ...

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 the

compiler optimise this operation choosing the fastest one - probably depending on the

architecture x86, arm, aso. Cheers, Raoul
Sven Barth
to Ched: The multiplication in floating arthmetic units is usualy know as being slower than the…
Ched wrote 5 lines
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...

Regards,
Sven

Charles Edouard Des Vastes-Vignes
to Philippe: forgot X shl 1 normaly the fastest ... floating arthmetic units is usualy know as being…

That is right for positive integers. But for floating point numbers ?

Cheers, Raoul

"Philippe" wrote 28 lines
forgot X shl 1 normaly the fastest ... Em 19.01.2014 17:47, Ched escreveu: 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 the compiler optimise this operation choosing the fastest one - probably depending on the architecture x86, arm, aso. Cheers, Raoul _______________________________________________ fpc-pascal maillist - _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Nikolay Nikolov
to Sven Barth: Can't really comment on floats, but for integers the fasted to multiply by 2 should be a…
Sven Barth wrote 17 lines
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

Sven Barth
to Nikolay Nikolov

Am 19.01.2014 22:50 schrieb "Nikolay Nikolov" <

Show quoted text 8 lines
: On 01/19/2014 10:45 PM, Sven Barth wrote: >> 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.

Ah, there it is. Thanks. I must have been too tired already when I looked through the code ^^

Regards,
Sven

Mark Morgan Lloyd
to Sven Barth: Can't really comment on floats, but for integers the fasted to multiply by 2 should be a…
Sven Barth wrote 7 lines
> 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...

With the caveat that at least some Pentiums have a limited amount of shifting hardware (specifically, of the 2x ALUs in a processor only one has a barrel shifter on the output). That might have the effect that converting a long run of *2 to <<1 would have the effect of preventing work being routed to half of the ALUs.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]