Recent

Author Topic: Bugfixing mparith (Wolfgang Ehrhardt's code) & Extended record enumerators  (Read 4409 times)

dieselnutjob

  • Full Member
  • ***
  • Posts: 217
Hi
I am trying to compile mparith on a number of different computers.
It appears that mparith is still available here
https://web.archive.org/web/20190729022124/http://www.wolfgang-ehrhardt.de:80/misc_en.html#mparith
(Wolfgang passed away June 2019)

I'm on fpc3.20.
On 64 bit Intel Linux and Windows it compiles fine.
On 64 bit ARM I am having some trouble.

The trouble starts on line 513 of std.inc
Code: Pascal  [Select][+][-]
  1.  {$ifdef CPUARM}
  2.     {$define EXT64}       {No extended for ARM}
  3.     {$define PurePascal}
  4.   {$endif}    

There is also this comment
Code: Pascal  [Select][+][-]
  1. 1.59     06.05.14  we          FPC/CPUARM: $define EXT64, i.e. no FP 80-bit extended

Then line 1408 of mp_base.pas
Code: Pascal  [Select][+][-]
  1. {$ifndef EXT64}
  2. {---------------------------------------------------------------------------}
  3. procedure frexpx(x: extended; var m: extended; var e: longint);
  4.   {-Return the mantissa m and exponent e of x with x = m*2^e, 0.5 <= abs(m) < 1;}
  5.   { if x is 0, +-INF, NaN or denormal, return m=x, e=0}
  6. begin
  7.   e  := TMPHexExtW(x)[4] and $7FFF;
  8.   {First check is INF or NAN, then if x is zero/denormal}
  9.   if (e=$7FFF) or (e=0) or (x=0.0) then e := 0
  10.   else begin
  11.     dec(e,$3FFE);
  12.     TMPHexExtW(x)[4] := (TMPHexExtW(x)[4] and $8000) or $3FFE;
  13.   end;
  14.   m := x;
  15. end;      

On Intel this function isn't used, but on ARM 64 bit it won't compile line 1414
Code: Pascal  [Select][+][-]
  1. e  := TMPHexExtW(x)[4] and $7FFF;  

The error is Illegal type conversion: "Double" to "TMPHexExtW"

I have previously compiled this code with an older version of FPC on 32 bit ARM (Raspbian) so I'm guessing it's a problem with stricter type checking in 3.20 ?

The problem is that I don't understand the "extended" part of TMPHexExtW(x)[4]

This is the definition
Code: Pascal  [Select][+][-]
  1. type
  2.   TMPHexExtW = packed array[0..4] of word;  {Extended as array of word}    

What does the "extended" and the x mean or do?  I don't understand.

Is "extended" in the context of an array related to "extended" in the context of an ARM processor? or is it the same word for two different things?

thanks, DNJ
« Last Edit: July 31, 2020, 09:47:36 pm by dieselnutjob »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11447
  • FPC developer.
If EXT64 is true, code under {$ifndef EXT64} should not be compiled. There is your problem.

Somehow that EXT64 is not in the same unit, or is otherwise not executed. maybe because cpuARM is only 32-bit, and you need a similar block for cpuARM64 or so.


dieselnutjob

  • Full Member
  • ***
  • Posts: 217
another question..

it says here https://wiki.freepascal.org/Variables_and_Data_Types
Code: Pascal  [Select][+][-]
  1. Type    Range   Significant digits      Bytes
  2. Double  5.0E-324 .. 1.7E308     15-16   8
  3.  

so is he just storing an 8 byte variable in an array of 4 words?

why is this necessary on a 64 bit ARM processor?

is it possible that the problem is just that he defined ARM because 64 bit ARM didn't exist back then?

TRon

  • Hero Member
  • *****
  • Posts: 2506
it says here https://wiki.freepascal.org/Variables_and_Data_Types
It says here https://freepascal.org/docs-html/ref/refsu5.html

extended 1.9E-4932 .. 1.1E4932   19–20   10

why is this necessary on a 64 bit ARM processor?
Because arm processor does not have the same fpu as i386 did, e.g. incompatible. That is what is keeping cross-compilation happening from arm to any i386 platform.

Quote
is it possible that the problem is just that he defined ARM because 64 bit ARM didn't exist back then?
Most probably these part were not added before author had a chance to do so.
« Last Edit: July 31, 2020, 02:11:26 pm by TRon »

dieselnutjob

  • Full Member
  • ***
  • Posts: 217
I guess that I need to put FPC 3.20 on a 32 bit ARM system and check that as well.
Changing the defs from ARM to ARM AND NOT AARCH64 might (or might not) fix it for 64 bit ARM but I think that 32 bit will still be broken.

TRon

  • Hero Member
  • *****
  • Posts: 2506
I guess that I need to put FPC 3.20 on a 32 bit ARM system and check that as well.
It compiles without any issues for me.

Quote
... but I think that 32 bit will still be broken.
Could you be a bit more specific on what you expect to be broken ?

for instance
Code: [Select]
./t_4sq
Test of MP library version 1.39.12 (31/32 bit)   (c) W.Ehrhardt 2012
97194177735908175207981982079326473737797879155345685082728081084772518818444815269080619149045968297679578305403209347401163036907660573971740862463751801641201490284097309096322681531675707666695323797578127=311759807762174781605301007201736860141952393239819073913168769888623683854510476118474315229371415703126^2 + 24970374757386993674494899978552505624409975485893125^2 + 2388281972686783802745888945^2 + 127348960691262133779212551^2
Time [s]: 0.057

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
I'm on fpc3.20.
On 64 bit Intel Linux and Windows it compiles fine.
On 64 bit ARM I am having some trouble.

The trouble starts on line 513 of std.inc
Code: Pascal  [Select][+][-]
  1.  {$ifdef CPUARM}
  2.     {$define EXT64}       {No extended for ARM}
  3.     {$define PurePascal}
  4.   {$endif}    

If you are compiling with Aarch64 then the code needs to check for CPUAARCH64. The define CPUARM is for 32-bit ARM only.

TRon

  • Hero Member
  • *****
  • Posts: 2506
Indeed PascalDragon.

@dieselnutjob:
Code: Pascal  [Select][+][-]
  1. {$if defined(CPUARM) or defined(CPUAARCH64)}
  2.     {$define EXT64}       {No extended for ARM}
  3.     {$define PurePascal}
  4.   {$endif}    
  5.  

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
Though the definition of EXT64 could also be improved like this:

Code: Pascal  [Select][+][-]
  1. {$ifndef FPC_HAS_TYPE_EXTENDED}
  2.   {$define EXT64}
  3. {$endif}

After all Extended isn't available for most platforms and also not for Win64.

dieselnutjob

  • Full Member
  • ***
  • Posts: 217
I guess that I need to put FPC 3.20 on a 32 bit ARM system and check that as well.
It compiles without any issues for me.

Quote
... but I think that 32 bit will still be broken.
Could you be a bit more specific on what you expect to be broken ?

for instance
Code: [Select]
./t_4sq
Test of MP library version 1.39.12 (31/32 bit)   (c) W.Ehrhardt 2012
97194177735908175207981982079326473737797879155345685082728081084772518818444815269080619149045968297679578305403209347401163036907660573971740862463751801641201490284097309096322681531675707666695323797578127=311759807762174781605301007201736860141952393239819073913168769888623683854510476118474315229371415703126^2 + 24970374757386993674494899978552505624409975485893125^2 + 2388281972686783802745888945^2 + 127348960691262133779212551^2
Time [s]: 0.057

you are right. It's fine on 32 bit ARM

dieselnutjob

  • Full Member
  • ***
  • Posts: 217
Though the definition of EXT64 could also be improved like this:

Code: Pascal  [Select][+][-]
  1. {$ifndef FPC_HAS_TYPE_EXTENDED}
  2.   {$define EXT64}
  3. {$endif}

After all Extended isn't available for most platforms and also not for Win64.

what's the significance to leaving out the define PurePascal ?

TRon

  • Hero Member
  • *****
  • Posts: 2506
what's the significance to leaving out the define PurePascal ?
You are right in that it needs to be defined, at least for arm.

The purepascal define indicates the routines to be written in (pure) Pascal. Some of them are using intel assembler. Purepascal is the define that determines which version to compile.

dieselnutjob

  • Full Member
  • ***
  • Posts: 217
tested and working so far on 32 bit and 64 bit ARM

thanks!

Someone made a backup of Wolfgang's code here https://forum.lazarus.freepascal.org/index.php/topic,50821.0.html

AlexTP

  • Hero Member
  • *****
  • Posts: 2401
    • UVviewsoft
I made fix in GH
https://github.com/Alexey-T/Wolfgang_Ehrhardt_codes/commit/a8055fc03ddd0c3da8aad6a2b309407052aa842d
not using "if defined()" to be compatible with other pascals.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
what's the significance to leaving out the define PurePascal ?

The PurePascal define isn't depending on the existance of Extended instead it's dependent on what CPU architectures the assembly code supports. I'd wager that the following should be used for that:

Code: Pascal  [Select][+][-]
  1. {$if not defined(CPUI386) and not defined(CPUX86_64)}
  2.   {$define PurePascal}
  3. {$endif}

Depending on the code it might even be available only for i386.

 

TinyPortal © 2005-2018