Recent

Author Topic: optimization gcc vs fpc  (Read 2781 times)

fcu

  • Jr. Member
  • **
  • Posts: 89
optimization gcc vs fpc
« 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

« Last Edit: April 23, 2019, 03:52:02 pm by fcu »

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: optimization gcc vs fpc
« Reply #1 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 !?)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: optimization gcc vs fpc
« Reply #2 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.

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: optimization gcc vs fpc
« Reply #3 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

fcu

  • Jr. Member
  • **
  • Posts: 89
Re: optimization gcc vs fpc
« Reply #4 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

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: optimization gcc vs fpc
« Reply #5 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?

 

TinyPortal © 2005-2018