### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

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

#### dieselnutjob

• Full Member
• Posts: 203
##### Bugfixing mparith (Wolfgang Ehrhardt's code) & Extended record enumerators
« on: July 31, 2020, 01:40:27 pm »
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

• Global Moderator
• Hero Member
• Posts: 9217
• FPC developer.
##### Re: Bugfixing mparith (Wolfgang Ehrhardt's code) & Extended record enumerators
« Reply #1 on: July 31, 2020, 01:50:18 pm »
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: 203
##### Re: Bugfixing mparith (Wolfgang Ehrhardt's code) & Extended record enumerators
« Reply #2 on: July 31, 2020, 01:55:21 pm »
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: 536
##### Re: Bugfixing mparith (Wolfgang Ehrhardt's code) & Extended record enumerators
« Reply #3 on: July 31, 2020, 02:01:30 pm »
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: 203
##### Re: Bugfixing mparith (Wolfgang Ehrhardt's code) & Extended record enumerators
« Reply #4 on: July 31, 2020, 02:18:36 pm »
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: 536
##### Re: Bugfixing mparith (Wolfgang Ehrhardt's code) & Extended record enumerators
« Reply #5 on: July 31, 2020, 02:39:33 pm »
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 201297194177735908175207981982079326473737797879155345685082728081084772518818444815269080619149045968297679578305403209347401163036907660573971740862463751801641201490284097309096322681531675707666695323797578127=311759807762174781605301007201736860141952393239819073913168769888623683854510476118474315229371415703126^2 + 24970374757386993674494899978552505624409975485893125^2 + 2388281972686783802745888945^2 + 127348960691262133779212551^2Time [s]: 0.057`

#### PascalDragon

• Hero Member
• Posts: 2890
• Compiler Developer
##### Re: Bugfixing mparith (Wolfgang Ehrhardt's code) & Extended record enumerators
« Reply #6 on: July 31, 2020, 03:32:31 pm »
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: 536
##### Re: Bugfixing mparith (Wolfgang Ehrhardt's code) & Extended record enumerators
« Reply #7 on: July 31, 2020, 03:39:32 pm »
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: 2890
• Compiler Developer
##### Re: Bugfixing mparith (Wolfgang Ehrhardt's code) & Extended record enumerators
« Reply #8 on: July 31, 2020, 04:32:00 pm »
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: 203
##### Re: Bugfixing mparith (Wolfgang Ehrhardt's code) & Extended record enumerators
« Reply #9 on: July 31, 2020, 07:49:12 pm »
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 201297194177735908175207981982079326473737797879155345685082728081084772518818444815269080619149045968297679578305403209347401163036907660573971740862463751801641201490284097309096322681531675707666695323797578127=311759807762174781605301007201736860141952393239819073913168769888623683854510476118474315229371415703126^2 + 24970374757386993674494899978552505624409975485893125^2 + 2388281972686783802745888945^2 + 127348960691262133779212551^2Time [s]: 0.057`

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

#### dieselnutjob

• Full Member
• Posts: 203
##### Re: Bugfixing mparith (Wolfgang Ehrhardt's code) & Extended record enumerators
« Reply #10 on: July 31, 2020, 07:59:32 pm »
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: 536
##### Re: Bugfixing mparith (Wolfgang Ehrhardt's code) & Extended record enumerators
« Reply #11 on: July 31, 2020, 08:14:52 pm »
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: 203
##### Re: Bugfixing mparith (Wolfgang Ehrhardt's code) & Extended record enumerators
« Reply #12 on: July 31, 2020, 08:21:22 pm »
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: 1270
##### Re: Bugfixing mparith (Wolfgang Ehrhardt's code) & Extended record enumerators
« Reply #13 on: July 31, 2020, 08:40:50 pm »
not using "if defined()" to be compatible with other pascals.

#### PascalDragon

• Hero Member
• Posts: 2890
• Compiler Developer
##### Re: Bugfixing mparith (Wolfgang Ehrhardt's code) & Extended record enumerators
« Reply #14 on: July 31, 2020, 09:02:35 pm »
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.