Recent

Author Topic: Assembler - logN and many more ..  (Read 12263 times)

PeterX

  • Sr. Member
  • ****
  • Posts: 326
Re: Assembler - logN and many more ..
« Reply #15 on: July 19, 2016, 10:55:11 pm »
Thanks Jonas, I'll file it soon.
usually using latest Lazarus release version
with Windows 10 at home
and Windows 7 on the job

PeterX

  • Sr. Member
  • ****
  • Posts: 326
Re: Assembler - logN and many more ..
« Reply #16 on: July 19, 2016, 10:58:28 pm »
Submitted as 0030389: assembler code compiles different under Delphi 5 and Lazarus 1.6
usually using latest Lazarus release version
with Windows 10 at home
and Windows 7 on the job

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 674
Re: Assembler - logN and many more ..
« Reply #17 on: July 19, 2016, 11:09:26 pm »
I just noticed you added begin/end between {$ifdef fpc}: that is wrong. It will probably also crash under Delphi if you would do that, because then it is no longer a pure assembly routine and the compiler will insert extra code around your assembler code. Remove the begin/end and use {$mode delphi} instead.

PeterX

  • Sr. Member
  • ****
  • Posts: 326
Re: Assembler - logN and many more ..
« Reply #18 on: July 19, 2016, 11:17:50 pm »
The whole project contains ~750.000 lines of code.
It still compiles under Delphi 5 (yes, it was a lot of work .. the past years ..).

And it still compiles under both Lazarus 1.6 AND Delphi 5 !
usually using latest Lazarus release version
with Windows 10 at home
and Windows 7 on the job

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: Assembler - logN and many more ..
« Reply #19 on: July 19, 2016, 11:31:54 pm »
I just tried Jonas' suggestion:
Code: Pascal  [Select]
  1. {$mode delphi}
  2.  
  3. function LogN( Base, X: Extended): Extended;
  4. {$ifdef FPC}
  5. {$ASMMODE intel}
  6. {$endif}
  7. asm
  8.         FLD1
  9.         FLD     X
  10.         FYL2X
  11.         FLD1
  12.         FLD     Base
  13.         FYL2X
  14.         FDIV           // Lazarus Warning: fdivrp without operand translated into fdivrpP
  15.         FWAIT
  16. end;
  17.  
  18. begin
  19.   WriteLn('LogN(10,10000): ', LogN(10,10000));
  20.   ReadLn;
  21. end.
  22.  

The result for LogN(10,10000) is 4.

PeterX

  • Sr. Member
  • ****
  • Posts: 326
Re: Assembler - logN and many more ..
« Reply #20 on: July 19, 2016, 11:46:43 pm »
I have a Test Project for this unit.

I can confirm now that it compiles under both Lazarus 1.6 and Dephi 5 with {$mode delphi} !
« Last Edit: July 19, 2016, 11:51:16 pm by PeterX »
usually using latest Lazarus release version
with Windows 10 at home
and Windows 7 on the job

PeterX

  • Sr. Member
  • ****
  • Posts: 326
Re: Assembler - logN and many more ..
« Reply #21 on: July 19, 2016, 11:50:37 pm »
Switching back to  {$mode objfpc}{$H+} I get ..

.. Fatal: Syntax error, "BEGIN" expected but "ASM" found


Is there no way to make it compile with {$mode objfpc}{$H+}  ?

I would like to leave behind the Delphi 5 version ..
« Last Edit: July 19, 2016, 11:52:22 pm by PeterX »
usually using latest Lazarus release version
with Windows 10 at home
and Windows 7 on the job

PeterX

  • Sr. Member
  • ****
  • Posts: 326
Re: Assembler - logN and many more ..
« Reply #22 on: July 20, 2016, 12:13:07 am »
Thanks Jonas and engkin !

Maybe it guides me to make a failing project work under Lazarus - tomorrow !
In my TimeZone it's time to go to bed now .. ;-)
usually using latest Lazarus release version
with Windows 10 at home
and Windows 7 on the job

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: Assembler - logN and many more ..
« Reply #23 on: July 20, 2016, 01:14:29 am »
Switching back to  {$mode objfpc}{$H+} I get ..

.. Fatal: Syntax error, "BEGIN" expected but "ASM" found


Is there no way to make it compile with {$mode objfpc}{$H+}  ?

I would like to leave behind the Delphi 5 version ..
In objfpc mode you need to add the keyword assembler:
Code: Pascal  [Select]
  1. {$mode objfpc}
  2.  
  3. function LogN( Base, X: Extended): Extended; assembler;

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 674
Re: Assembler - logN and many more ..
« Reply #24 on: July 20, 2016, 10:49:06 am »
Is there no way to make it compile with {$mode objfpc}{$H+}  ?

I would like to leave behind the Delphi 5 version ..

See engkin's answer for that, but you can perfectly keep compiling your code using {$mode delphi} in FPC. We don't plan on removing this mode, nor on changing it so that it will break your code. All breaking changes added by Delphi 2009 and later will be added to {$mode delphiunicode}

PeterX

  • Sr. Member
  • ****
  • Posts: 326
Re: Assembler - logN and many more ..
« Reply #25 on: July 20, 2016, 11:50:15 am »
.. you can perfectly keep compiling your code using {$mode delphi} in FPC. We don't plan on removing this mode, nor on changing it so that it will break your code.

I try to avoid the DELPHI mode as far as possible.
In FPC mode the rules are stricter.
So - from my point of view - it helps cleaning the (foreign) code.

In my project folder there are ~2.500 files
(lots of overhead due to backups and TestProjects)
But only 597 files still in {mode Delphi}

Most of the differences between DELPHI 5 and LAZARUS can be solved.
I just checked this - currently there are 2751 {$ifdef FPC} in my code ... hahaha !
OMG ..
« Last Edit: July 20, 2016, 11:58:39 am by PeterX »
usually using latest Lazarus release version
with Windows 10 at home
and Windows 7 on the job

PeterX

  • Sr. Member
  • ****
  • Posts: 326
Re: Assembler - logN and many more ..
« Reply #26 on: July 20, 2016, 11:52:44 am »
In objfpc mode you need to add the keyword assembler
Thanks for this hint !

I'll check this in both D' & L' version of the project.
usually using latest Lazarus release version
with Windows 10 at home
and Windows 7 on the job

PeterX

  • Sr. Member
  • ****
  • Posts: 326
Re: Assembler - logN and many more ..
« Reply #27 on: July 20, 2016, 11:53:56 am »
Feedback from FPC bug tracker:

"That is a known Delphi 5 bug:   Delphi compiles FDIV indeed as FDIVP."
usually using latest Lazarus release version
with Windows 10 at home
and Windows 7 on the job

Thaddy

  • Hero Member
  • *****
  • Posts: 9273
Re: Assembler - logN and many more ..
« Reply #28 on: July 20, 2016, 12:06:32 pm »
Indeed. Delphi users can verify the fact that FDIV is compiled as FDIVP through the CPU/FPU window.
It is not an FPC bug and the bug is still (since 2004 at least) open in Delphi. I reported it at the time....
The solution for compatibility is to write FDIVP and that will be accepted by both Delphi and FPC.
But it is a Delphi bug in BASM.

Note this guidance also means that the other way around is hardly possible: compile fpc code that contains proper FDIV into Delphi ....would lead to floating point stack corruption.
But in this case, changing Delphi sourcecode FDIV's to FDIVP's will solve it.
« Last Edit: July 20, 2016, 12:17:44 pm by Thaddy »
also related to equus asinus.

Thaddy

  • Hero Member
  • *****
  • Posts: 9273
Re: Assembler - logN and many more ..
« Reply #29 on: July 20, 2016, 12:31:09 pm »
Jonas, although you resolved that on Mantis with "The problem had nothing to do with fdiv/fdivp, but with the fact that the code contains an extra "begin .. end" around the assembler block when compiled with FPC. That means it was no longer a pure assembler routine, and the compiler inserted entry and exit code that did not expect a value to be left on the FPU stack at the end of the assembler block. "

that is not really the case. I don't know if you have access to a Delphi compiler of that era D2,3,4,5,6,2005,2006)  but it is still a Delphi bug as you would be able to see if you open up the CPU/FPU window in delphi.
It can of course be the case that there where actually 2 bugs. If you solved it both ways? Cudo's.... but I think there is more to it than just the begin/end.
also related to equus asinus.