Sign in Join

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

Mailing lists / fpc-devel / 2006-09

Bit manipulation as optimization in FPC

8 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

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

ik

Hello,

I wonder if FPC optimize a code such as:

if a < b then
  c := d
else
  c := b;

as :

r := b + ((d - b) and -(a < b));

If so, do you check the type of CPU (because as I understand, some CPU will not execute as fast as other CPU's.

If not, then why, and how would you optimize such code ?

Thanks,

Ido

Reply by email

Daniël Mantione
to ik

Op Wed, 6 Sep 2006, schreef ik:

Show quoted text 18 lines
Hello, I wonder if FPC optimize a code such as: if a < b then c := d else c := b; as : r := b + ((d - b) and -(a < b)); If so, do you check the type of CPU (because as I understand, some CPU will not execute as fast as other CPU's. If not, then why, and how would you optimize such code ?

The construction above would be fastest with conditional moves; in the above construction the optimal code generation would be:

mov c,d
cmp a,b
movge c,b

This would be much faster than any code generated for the construction you propose.

Free Pascal won't generate the optimal code, but it can already use conditional moves and I would expect that the compiler would use it in this situation.

Daniël

Reply by email

Florian Klaempfl
to Daniël Mantione
Show quoted text 35 lines
Op Wed, 6 Sep 2006, schreef ik: > Hello, > > I wonder if FPC optimize a code such as: > > if a < b then > c := d > else > c := b; > > as : > > r := b + ((d - b) and -(a < b)); > > If so, do you check the type of CPU (because as I understand, some CPU > will not execute as fast as other CPU's. > > > If not, then why, and how would you optimize such code ? The construction above would be fastest with conditional moves; in the above construction the optimal code generation would be: mov c,d cmp a,b movge c,b This would be much faster than any code generated for the construction you propose. Free Pascal won't generate the optimal code, but it can already use conditional moves and I would expect that the compiler would use it in this situation.

It does, if the reg. allocator starts correctly :)

Show quoted text 9 lines
Daniël ------------------------------------------------------------------------ _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply by email

Peter Vreman
to ik: I wonder if FPC optimize a code such as: if a < b then c := d else c := b; as : r := b +…
Show quoted text 17 lines
Hello, I wonder if FPC optimize a code such as: if a < b then c := d else c := b; as : r := b + ((d - b) and -(a < b)); If so, do you check the type of CPU (because as I understand, some CPU will not execute as fast as other CPU's. If not, then why, and how would you optimize such code ?

This is not a generic optimization that will give real benefits. Your code also relies on the fact that (a<b) always returns 0 or 1. But this is not garantueed. A boolean is defined as 0=false and everything else is true.

Reply by email

Jonas Maebe
to Peter Vreman
Peter Vreman wrote 6 lines
This is not a generic optimization that will give real benefits. Your code also relies on the fact that (a<b) always returns 0 or 1. But this is not garantueed. A boolean is defined as 0=false and everything else is true.

Afaik only in case of reading a boolean everything 0 is true (for compatibility with other languages). In case of writing, Pascal (and FPC in particular) always stores a 1 for true and 0 for false, since boolean is defined as an enum with values (false, true)

Jonas

Reply by email

Daniël Mantione
to Jonas Maebe

Op Thu, 7 Sep 2006, schreef Jonas Maebe:

Show quoted text 12 lines
On 7 sep 2006, at 07:52, Peter Vreman wrote: > This is not a generic optimization that will give real benefits. Your > code > also relies on the fact that (a<b) always returns 0 or 1. But this is not > garantueed. A boolean is defined as 0=false and everything else is true. Afaik only in case of reading a boolean everything 0 is true (for compatibility with other languages). In case of writing, Pascal (and FPC in particular) always stores a 1 for true and 0 for false, since boolean is defined as an enum with values (false, true)

Well, the last minute change in 2.0.4 by Florian was needed because constructors return non-1 true cases, i.e.:

if constructor_call then

... results actuall a pointer to the object, or nil.

Daniël

Reply by email

Jonas Maebe
to Daniël Mantione
Daniël Mantione wrote 13 lines
> Afaik only in case of reading a boolean everything 0 is true (for > compatibility with other languages). In case of writing, Pascal > (and FPC in > particular) always stores a 1 for true and 0 for false, since > boolean is > defined as an enum with values (false, true) Well, the last minute change in 2.0.4 by Florian was needed because constructors return non-1 true cases, i.e.: if constructor_call then ... results actuall a pointer to the object, or nil.

Even though it's a dirty construct by Borland, it does not contradict what I wrote.

Jonas

Reply by email

Florian Klaempfl
to Jonas Maebe
Jonas Maebe wrote 18 lines
On 7 sep 2006, at 08:59, Daniël Mantione wrote: >> Afaik only in case of reading a boolean everything 0 is true (for >> compatibility with other languages). In case of writing, Pascal (and >> FPC in >> particular) always stores a 1 for true and 0 for false, since boolean is >> defined as an enum with values (false, true) > > Well, the last minute change in 2.0.4 by Florian was needed because > constructors return non-1 true cases, i.e.: > > if constructor_call then > > ... results actuall a pointer to the object, or nil. Even though it's a dirty construct by Borland, it does not contradict what I wrote.

A constructor returns a qword/longbool which isn't defined as enumeration.

Reply by email