Lazarus

Free Pascal => FPC development => Topic started by: fcu on April 23, 2019, 03:49:54 pm

Title: optimization gcc vs fpc
Post by: fcu on April 23, 2019, 03:49:54 pm
Hi
just looking how fpc optimize this function compared with how gcc does
gcc seems smarter, it reduce it to one instruction while fpc didn't noticed it require only one instruction
both codes compiled with -O3

Title: Re: optimization gcc vs fpc
Post by: 440bx on April 23, 2019, 04:01:01 pm
just looking how fpc optimize this function compared with how gcc does
gcc seems smarter, it reduce it to one instruction while fpc didn't noticed it require only one instruction
both codes compiled with -O3
You'll find many such cases. 

A lot of C/C++ programmers are addicted to optimizing code.  That can be a good thing sometimes but, sometimes it isn't (no shortage of times when one looks at a piece of C/C++ code and thinks... what the hell is this supposed to do !?)
Title: Re: optimization gcc vs fpc
Post by: marcov on April 23, 2019, 04:11:39 pm
This is a bit a specific case for C. Because there are so many C compilers, and umpteen codebases all have such constructs for endianswaps, they added a peephole.

A more logical FPC way would be to simply declare an intrinsic.
Title: Re: optimization gcc vs fpc
Post by: ASerge on April 23, 2019, 08:13:08 pm
By the way, this is assembler code for System.SwapEndian(X)
Code: ASM  [Select]
  1. ; SYSTEM_$$_SWAPENDIAN$LONGWORD$$LONGWORD
  2. mov %ecx,%eax
  3. bswap  %eax
  4. retq
FPC 3.0.4, x64, Windows
Title: Re: optimization gcc vs fpc
Post by: fcu on April 23, 2019, 11:03:57 pm
By the way, this is assembler code for System.SwapEndian(X)
Code: ASM  [Select]
  1. ; SYSTEM_$$_SWAPENDIAN$LONGWORD$$LONGWORD
  2. mov %ecx,%eax
  3. bswap  %eax
  4. retq

what version do you have ?
i am using 3.0.4 32bits  ,and this is the asm of this function
Title: Re: optimization gcc vs fpc
Post by: ASerge on April 24, 2019, 12:04:20 am
what version do you have ?
i am using 3.0.4 32bits  ,and this is the asm of this function
As I wrote 64-bit. Can be 32 bit code such because the BSWAP instruction is not supported on IA-32 processors earlier than the Intel 486 processor family?